Giter Club home page Giter Club logo

ember-power-calendar-date-fns's Introduction

@makepanic/ember-power-calendar-date-fns

Build Status npm version

Date manipulation, formatting and parsin is too complex for ember-power-calendar to reinvent, so it has to rely on other well tested libraries for that.

This is the addon that exposes the utils used internally by ember-power-calendar, using date-fns underneath.

You will want to install this addon if you already use date-fns in your application already, or if you don't want to use moment.js or Luxon.

Compatibility

  • Ember.js v3.11 or above
  • Node.js v10 or above
ember date-fns ember-power-calendar-date-fns
? 1.x < 0.3
? 2.x ≥ 0.3
≥ 3.11 2.x ≥ 0.4

Installation

ember install @makepanic/ember-power-calendar-date-fns

Usage

Don't use it.

This library is meant to be used internally by ember-power-calendar only.

The API can change in breaking ways based on the needs of Ember Power Calendar.

I18n

This addon provides multiple ways to localize the calendar.

Using date-fns/locale

By default, all of date-fns/locale is imported and used if you configured the power-calendar locale. Note: this will include ~360 KB of Javascript.


If you only want to support only a subset of all date-fns/locale locales, you can pass them as a list to the addon options:

const app = new EmberApp(defaults, {
    'ember-power-calendar-date-fns': {
      // include only 'en-US', 'en-GB', 'zh-CN' and 'de' locale
      includeLocales: ['en-US', 'en-GB', 'zh-CN', 'de']
    }
});

This will cause the addon to build imports that directly load the specified locales.

Note:

  • you can only include locales that date-fns exports. See locale for locales that this addon can import. (See also Supported Languages).

DIY

Alternatively, if you don't want to ship all of date-fns/locale, you can localize the calendar yourself. To do this, configure the addon by setting the 'ember-power-calendar-date-fns' field as ember option:

const app = new EmberApp(defaults, {
    'ember-power-calendar-date-fns': {
      includeLocales: false
    }
});

Setting includeLocales to false excludes the date-fns localized modules.

By default, this adapter implements any localization feature as a fallback, using the en locale.

If you want to localize the dates yourself, extend the DaysComponent and implement localized methods, like startOfWeek, weekdaysShort:

import DaysComponent from 'ember-power-calendar/components/power-calendar/days';
import layout from 'ember-power-calendar/templates/components/power-calendar/days';

export default class PowerCalendarDays extends DaysComponent {
  layout = layout;

  startOfWeek = 1;

  @service intl!: IntlService;

  @computed('calendar.locale')
  get weekdaysShort(): string[] {
    const { intl } = this;
    const prefix = 'calendar.days.short';

    return ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
      .map(k => intl.t(`${prefix}.${k}`));
  }

  @alias('weekdaysShort') weekdaysMin!: string[];

  @computed('calendar.locale')
  get weekdays(): string[] {
    const { intl } = this;
    const prefix = 'calendar.days.long';

    return ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
      .map(k => intl.t(`${prefix}.${k}`));
  }
}

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

ember-power-calendar-date-fns's People

Contributors

makepanic avatar veelci avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

ember-power-calendar-date-fns's Issues

Date id isn't set correctly

power-calendar calls formatDate with YYYY-MM-DD to generate the day id.

We should probably transform that to yyyy-MM-dd as it's currently generating something like data-date="2019-09-254" in tests.

Let user configure locale so we don't include all of date-fns/locale

Probably by not including it by default and letting user overwrite its own somewhat like this:

import allLocales from 'date-fns/locale';
import deLocale from 'date-fns/locale/de';

// just load all, will bloat size
const locales = allLocales;

// just load one locale
const locales = {
  de: Locale
}

export default class PowerCalendarDays extends DaysComponent {
  locales;
}

This would also allow users to not use date-fns locales but also custom implementations, based on already used intl frameworks.

I think that there's also an easier solution.
We could extract the date-fns/locale methods into a separate module and depending on the addon config, filter that module and use the unlocalized base instead.

Error when specifying 'en' in the list of included locales.

When specifying 'en' in the list of included locales, I'm getting an error:

Can't resolve 'date-fns/locale/en'

I believe this is because 'en' is used as the default locale for date-fns. It isn't in the docs for ^2.0.0, but can be found in the Supported Languages section of the Internationalization docs for v1.30.1.

date-fns currently supports:

  • English (en; it's the built-in language and doesn't require any setup)

Possible solutions:

  • Remove "en" from the example list of supported languages in the docs.
  • Skip the import for "en" in index.js

0.4.2 bad release?

I'm getting now the following js error in our app:

Uncaught Error: Could not find module `date-fns` imported from...

Not sure why, as I didn't change anything but updating ember-power-calendar-date-fns from 0.4.1 to 0.4.2.

When I force the version to 0.4.1 it works normally.
Could also be a Issue with the implementation inside our app, but we are not doing anything special.

Do you have a clue why this happens?

Embroider compatibility

It seems that the addon cannot be run with Embroider.
Indeed, I have the following error in my test
Screenshot 2021-10-07 at 17 48 05

I would say that it's caused by the fact that we're renaming the import path of the addon from
ember-power-calendar-date-fns to ember-power-calendar-util but I'm not sure how we could fix that

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.