1.0.6 • Published 9 months ago

calunia-stimulus-datepicker v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Stimulus Datepicker

This is a Stimulus-powered datepicker which:

  • localises and parses dates in the input field according to the strftime directives you configure;
  • presents a calendar closely adhering to the WAI-ARIA date picker dialog design pattern;
  • sends the date back to the server in ISO8601 format (YYYY-MM-DD).

Please see the demo page for a few simple examples.

Your server produces an ISO8601 date; your user interacts with it in the format you configured and/or via the popup calendar; the form sends the date back to the server in ISO format.

Month and day names are generated in the user's locale. Button titles and ARIA labels are customisable, e.g. for localisation.

Dates are local; no timezones or daylight savings are involved.

You can easily adjust the calendar's appearance via CSS custom properties or just overriding the CSS classes.

Size: JS 3.6kB minified and gzipped, CSS 2.7kB gzipped.

Installation

yarn add stimulus-datepicker

Usage

Register the datepicker controller with your Stimulus application:

  import { Application } from '@hotwired/stimulus'
+ import { Datepicker } from 'stimulus-datepicker'

  const application = Application.start()
+ application.register('datepicker', Datepicker)

To use the datepicker, wrap your input field with a controller div.

<div data-controller="datepicker">
  <input data-datepicker-target="input" type="text" name="foobar" value="2022-03-23"/>
  <span data-datepicker-target="toggle">calendar</span>
</div>

Your calendar icon or equivalent must have the data attribute data-datepicker-target="toggle".

The input field:

  • must have the data attribute data-datepicker-target="input";
  • should have type="text", not type="date", to avoid conflicting with built-in browser functionality;
  • must have a name;
  • its value, if given, must be a YYYY-MM-DD date string.

Configuration

You can configure your datepicker with the following attributes. Declare them on your controller as data-datepicker-[name]-value.

NameDefaultDescription
format"%Y-%m-%d"Format for the date in the input field (see below).
first-day-of-week1First day of the week in the calendar (Sunday is 0).
day-name-length2Length of the abbreviated day names in the calendar, e.g. "Mo".
allow-weekends"true"Whether weekends are selectable.
month-jump"dayOfMonth"When jumping to the previous/next month, whether to go to the same day of the month ("dayOfMonth") or the same day of the week ("dayOfWeek").
min""The earliest choosable date (YYYY-MM-DD).
max""The latest choosable date (YYYY-MM-DD).
underflow-message""Client-side form validation message when the selected date is earlier than the min date. %s is replaced with the formatted min date. E.g. "Date must be %s or later."
overflow-message""Client-side form validation message when the selected date is later than the max date. %s is replaced with the formatted max date. E.g. "Date must be %s or earlier."
disallow[]Dates which cannot be selected. E.g. ["2022-12-25", "2023-01-01"]
text{ underflow: "", overflow: "", previousMonth: "Previous month", nextMonth: "Next month", today: "Today", chooseDate: "Choose Date", changeDate: "Change Date"}User-facing text. The value object is merged into the defaults.underflow: client-side form validation message when the selected date is earlier than the min date. %s is replaced with the formatted min date. E.g. "Date must be %s or later."overflow: client-side form validation message when the selected date is later than the max date. %s is replaced with the formatted max date. E.g. "Date must be %s or earlier."previousMonth, nextMonth, today: button titles and ARIA labelschooseDate, changeDate: ARIA labels

You can use the following strftime directives in data-datepicker-format-value:

DirectiveMeaning
%dDay of the month, zero-padded (01..31)
%-dDay of the month, no padding (1..31)
%mMonth of the year, zero-padded (01..12)
%-mMonth of the year, no padding (1..12)
%BThe full month name in the browser's locale (January)
%bThe abbreviated month name in the browser's locale (Jan)
%YFull year, four digits (2022)
%yYear in 21st Century, two digits (22)

Keyboard support

The datepicker is fully navigable by keyboard. In addition to the WAI-ARIA keys, you can also use Vim-style movement keys.

Toggle target (your calendar icon)

KeyResult
Space, EnterOpen the datepicker dialog.Move focus to selected date, i.e. the date displayed in the input text field. If no date has been selected, focus on the current date.

Datepicker dialog (the calendar)

KeyResult
EscapeClose the dialog and return focus to the toggle target.
TabMove focus to next element in the dialog's tab sequence.The dialog "traps" focus so if focus is on the last item, move focus to the first.
Shift + TabMove focus to previous element in the dialog's tab sequence.The dialog "traps" focus so if focus is on the first item, move focus to the last.

Previous-month / today / next-month Buttons

KeyResult
Space, EnterChange the month displayed in the calendar grid.

Date grid

KeyResult
Space, EnterSelect the date, close the dialog, move focus to the toggle target.
Up Arrow, kMove focus to the same day of the previous week.
Down Arrow, jMove focus to the same day of the next week.
Right Arrow, lMove focus to the next day.
Left Arrow, hMove focus to the previous day.
Home, ^, 0Move focus to the first day of the current week.
End, $Move focus to the last day of the current week.
Page Up, bChange the grid of dates to the previous month and focus on the corresponding* date one month earlier.
Shift + Page Up, BChange the grid of dates to the previous year and focus on the same date one year earlier.
Page Down, wChange the grid of dates to the next month and focus on the corresponding* date one month later.
Shift + Page Down, WChange the grid of dates to the next year and focus on the same date one year later.

* The corresponding date in the adjacent month depends on data-datepicker-month-jump-value:

  • "dayOfMonth": the corresponding date is the same date, e.g. 7th;
  • "dayOfWeek": the corresponding date is the same day of the week, four weeks earlier/later.

CSS

You can alter the appearance of the calendar by setting these custom properties, or of course by just overriding the CSS classes.

PropertyDefaultDescription
--sdp-selected#005fccBackground colour of selected date; colour of date focus ring.
--sdp-selected-invert#ffffffColour of selected date.
--sdp-prev-month#888888Colour of dates in the previous month.
--sdp-next-month#888888Colour of dates in the next month.
--sdp-disabled#1010104dColour of disabled dates; background color of selected date when disabled.
--sdp-disabled-invert#ffffffColour of selected date when disabled.
--sdp-background#ffffffBackground colour.
--sdp-border#ddddddBorder colour.
--sdp-shadow0deg 0% 50%Box shadow colour (hsl).
--sdp-icon#4a4a4accColour of the icons, e.g. previous-month button.
--sdp-nav-button-background#f5f5f5Background colour of the navigation buttons.
--sdp-nav-button-background-hover#eeeeeeBackground colour on hover of the navigation buttons.
--sdp-days-of-week#4a4a4aColour of the days of the week.

Intellectual Property

This package is copyright Andrew Stewart.

This package is available as open source under the terms of the MIT licence.