Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(date-picker): replace moment with vanilla JS #1013

Merged
merged 4 commits into from
Oct 4, 2023
Merged

Conversation

gcornut
Copy link
Member

@gcornut gcornut commented Aug 29, 2023

General summary

Replacing moment with vanilla JS.

All the logic used to generate the month calendar is re-written in vanilla JS. The date utils from @lumx/core that used moment have been deprecated (will have to remove them in the next major version as it is a breaking change). The DatePicker/DatePickerField components API do not change at all since we already do not reference moment.

The only challenging part for the implementation of the date picker was to get the first day of the week for a given locale (Monday in most of europe, Sunday in the US, etc.). The browser API Intl.Local.getWeekInfo() is still experimental (although partially implemented in Chrome & Safari via a weekInfo accessor property). I've choose to try this browser API (most trustworthy source) and fallback to a lookup in a constant (built from locale date of moment) for older browsers.

Pros:

  • No need for external lib
    • @lumx/react and @lumx/core won't require moment anymore
    • No need to setup a dynamic import of the date locale (ex: moment/locale/fr)

Cons:

  • Slightly larger code on the date picker components

Impact on bundle size

Before with @lumx/react v3.5.3
=> 44.5kb (gzipped) see on bundlephobia
=> 129kb (gzipped, including moment, lodash & react) see on bundlejs

After with @lumx/react v3.5.4-alpha-remove-moment.0 (with moment fully removed)
=> 45.1kB (gzipped) see on bundlephobia
=> 106kb (gzipped, including lodash & react) see on bundlejs

Non regression test

  • Calendar display (start of month, end of month, changing month) ✔️
  • Calendar localization
    • First day of week adapted to locale ✔️
    • Formatted month + year to locale ✔️
    • Formatted week day short name to locale ✔️
    • Formatted day number to locale ✔️
  • Browser compatibility
    • Latest Chrome & Safari use Intl.Locale week info ✔️
    • Alternative locale metadata for older browser ✔️
    • Latest browser version: Chrome v118 ✔️ Safari v16 ✔️ Firefox v117 ✔️
    • Older browser version: Chrome v75 ✔️ Safari v13 ✔️ Firefox v70 ✔️

Capture d’écran 2023-09-08 à 11 50 14
See in StoryBook

Alpha version: v3.5.4-alpha-remove-moment.0 https://github.com/lumapps/design-system/actions/runs/6120085722

StoryBook: https://b8cc29a9--5fbfb1d508c0520021560f10.chromatic.com/ (Chromatic build) ⚠️ Outdated commit

Copy link
Contributor

@Mcourt Mcourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge work!
Well done 👏🏻

@gcornut
Copy link
Member Author

gcornut commented Sep 29, 2023

StoryBook Test OK ✔️
Testing on lumapps-web... (v3.5.4-alpha-remove-moment.1)

@gcornut
Copy link
Member Author

gcornut commented Oct 2, 2023

Fixing a bug when providing an invalid to the date picker

@gcornut
Copy link
Member Author

gcornut commented Oct 4, 2023

All good, merging this. I'm not planning on releasing it right away

@gcornut gcornut merged commit 67e7235 into master Oct 4, 2023
10 checks passed
@gcornut gcornut deleted the feat/remove-moment branch October 4, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants