Date Picker Display Modes
@pglejzer/datepicker-ui, the companion of timepicker-ui, opens as a docked popover, a modal dialog or an inline calendar. ui.displayMode picks the surface.
Display modes
#ui.displayMode picks the surface. The default is "auto".
| Value | Surface |
|---|---|
"auto" | Modal dialog when the viewport is below 600 px wide or 480 px tall, docked otherwise. A docked picker that fits neither below nor above the field, at its current position on the page, opens as a dialog instead. |
"docked" | Popover next to the input. |
"modal" | Dialog with a backdrop. |
Docked
#A docked picker is a popover placed next to the input. It uses CSS anchor positioning where the browser supports it, and a JavaScript fallback otherwise.
Modal
#A modal picker is a native <dialog> with a backdrop. It is the surface "auto" picks on small screens.
Inline
#For an inline picker, set ui.inline.enabled and give ui.inline.container a selector or an element. create() mounts it. It has no open triggers.
<input id="date" /><div id="calendar"></div>
An inline picker hides the footer unless ui.footer is true. Without a footer there is no confirm button, so set behavior.commitOnChange to write each pick to the input. The same applies to behavior.closeOnSelect.
Inline footer
ui.footer: true instead of commitOnChange to keep the confirm and cancel buttons under an inline calendar.Layouts
#ui.layout sets the look of the calendar on any surface. The default is "auto", which gives each surface its own look. The table shows the look per surface.
| ui.layout | Docked | Modal | Inline |
|---|---|---|---|
"auto" | Docked. | Modal. A range picker below ui.fullscreen opens full screen. | Docked. |
"docked" | Docked. | Docked, with the month and year menus in the dialog. | Docked. |
"modal" | Modal. | Modal. A range picker below ui.fullscreen opens full screen. | Modal. |
"fullscreen" | Modal. | A range picker opens full screen at every width. Other modes are modal. | A range picker gets the full-screen layout sized to the container. Other modes are modal. |
The docked look has the month and year menus and is the only one that shows several months or a presets column. The modal look has the headline, a month row with arrows on every page, and month and year grids instead of menus. An inline calendar with the modal look follows ui.fixedWeeks. A single picker with the modal look has the input view at day precision.
To open a dialog with the docked month and year menus, set layout next to displayMode:
Three more options change what an inline calendar shows and announces:
ui.inline.headline: falseleaves out the headline, for a page that shows its own: the modal title, the selected value and the view switch, or the range header. The full-screen top bar keeps its close and save buttons.ui.inline.navigation: "caption"shows the month and year as text next to the arrows, with no month or year menus. It works at day precision only. The calendar then opens on the day grid, and Shift+M and Shift+Y do nothing.ui.inline.announceSelection: falsestops selection announcements, for a page that announces them itself. Month changes and errors are still announced.
new DatepickerUI("#date", {ui: {layout: "modal",inline: {enabled: true,container: "#calendar",headline: false,navigation: "caption",},},behavior: { commitOnChange: true },}).create();
Full screen range picker
#A range picker in a modal opens full screen below ui.fullscreen. true means below 600 px, a number means below that width in pixels and false means never. The width is read when the picker opens.
new DatepickerUI("#range", {calendar: { mode: "range" },ui: { displayMode: "modal", fullscreen: 768 },}).create();
Where the modal is appended
#ui.appendTo takes a selector or an element for the modal dialog. By default the dialog is appended to the host.
new DatepickerUI("#date", {ui: { displayMode: "modal", appendTo: "#modal-root" },}).create();
Toggle button
#ui.toggleButton adds an extra element that opens the picker, such as a calendar icon button. When it is not a <button>, Enter and Space open the picker. The picker sets aria-expanded on it and gives it an accessible name from labels.openCalendar. Clicking it while a docked picker is open closes the picker without committing and emits cancel with reason: "toggle".
<input id="date" /><button id="open" type="button">Choose date</button>
Page scroll in a modal
#A modal picker locks page scroll while it is open. Set ui.allowPageScroll to true to keep the page scrollable.