datepicker-ui Kit for Companion Packages
The kit entry of @pglejzer/datepicker-ui, the companion of timepicker-ui, exports the building blocks the picker uses, for packages built on top of it.
What the kit is
#@pglejzer/datepicker-ui/kit exports the building blocks the picker itself uses, for packages built on top of datepicker-ui. It has named exports only.
import {PopoverHost,resolveSurfaceMode,parseDate,} from "@pglejzer/datepicker-ui/kit";
Load the CSS
dp-ui__container elements, so load main.css or index.css next to them.Surfaces
#| Export | Signature | Purpose |
|---|---|---|
| PopoverHost | new PopoverHost(options) | Docked surface: a popover placed next to an anchor element. |
| DialogHost | new DialogHost(options) | Modal surface: a native dialog with a backdrop. |
| InlineHost | new InlineHost({ container, label, labelledBy? }) | Inline surface inside a container element. |
| waitForExit | waitForExit(element: HTMLElement): Promise<void> | Waits for an exit animation to finish. |
Each host has root and body elements and open(), close(), destroy() and setLabel(label, labelledBy?) methods.
Every host constructor takes a label and an optional labelledBy: the id, or space-separated ids, of a visible title. A non-empty labelledBy sets aria-labelledby and removes aria-label. Otherwise the label becomes the aria-label and aria-labelledby is removed. setLabel() renames a shown surface by the same rule.
import { InlineHost } from "@pglejzer/datepicker-ui/kit";// <h2 id="trip-title">Trip dates</h2>const host = new InlineHost({container: document.querySelector<HTMLElement>("#calendar")!,label: "Choose dates",labelledBy: "trip-title",});// No labelledBy: the label becomes aria-label againhost.setLabel("Choose a return date");
Surface choice
#| Export | Signature | Purpose |
|---|---|---|
| resolveSurfaceMode | resolveSurfaceMode(option: "auto" | "docked" | "modal", inlineEnabled: boolean, viewportWidth: number, viewportHeight: number): SurfaceMode | Returns "docked", "modal" or "inline". "auto" means modal below 600 px wide or 480 px tall. |
| isFullscreenRange | isFullscreenRange(mode: CalendarMode, surface: SurfaceMode, viewportWidth: number, fullscreenBelow: number): boolean | Says whether a range picker goes full screen. |
| fitsViewport | fitsViewport(root: HTMLElement, anchor: HTMLElement): boolean | Says whether a docked surface fits the window height: its own height, the anchor's height and an 8 px margin above and below. Call it while the surface is shown. The picker uses it to open a dialog instead with "auto". |
Theme and color scheme
#| Export | Signature | Purpose |
|---|---|---|
| detectColorScheme | detectColorScheme(from: HTMLElement): Scheme | Returns "light" or "dark" for an element. |
| watchColorScheme | watchColorScheme(onChange: () => void): () => void | Reports changes and returns a function that stops watching. |
| applyTheme | applyTheme(root: HTMLElement, theme: SurfaceTheme): void | Writes data-theme, data-color-scheme, the extra classes and the right-to-left class onto a surface root. Classes from the previous call are removed, so it is safe to call again. |
Paging and buttons
#| Export | Signature | Purpose |
|---|---|---|
| Pager | new Pager<K>(viewport: HTMLElement, options: PagerOptions<K>) | The month paging of the picker, with its drag and wheel gestures. show(key) shows a page, slide(key, direction) animates to it, finish() ends a running slide and destroy() removes it. onCommit reports a finished gesture, and you call slide() to show the new page. |
| createTextButton | createTextButton(modifier: string, label: string): HTMLButtonElement | Builds a text button with the classes dp-ui__text-button and dp-ui__text-button--<modifier>. |
Date text
#A date is { year, month, day }.
| Export | Signature | Purpose |
|---|---|---|
| resolvePattern | resolvePattern(format: FormatOption, locale: string, precision?: Precision): FormatPattern | Turns "auto" or a pattern string into a pattern for a locale. |
| formatDate | formatDate(date: PlainDate, pattern: FormatPattern): string | Formats a date as text. |
| parseDate | parseDate(text: string, pattern: FormatPattern): PlainDate | null | Parses typed text, or returns null. |
| maskDateText | maskDateText(text: string, pattern: FormatPattern): string | Formats typed digits to the pattern. |
| formatHint | formatHint(pattern: FormatPattern, letters?: HintLetters): string | Returns a hint for the pattern, such as a placeholder. letters sets the day, month and year letters, dd, mm and yyyy by default. |
| leadingDateLength | leadingDateLength(text: string, pattern: FormatPattern): number | Length of the date at the start of the text, or 0 when it holds no digit. A separator after the last group is not part of the date. |
| normalizeDigits | normalizeDigits(text: string): string | Maps Arabic-Indic and Persian digits to Latin digits, drops bidi marks and trims. |
| RANGE_SEPARATOR | RANGE_SEPARATOR: " – " | What the picker writes between the two dates of a range: a space, an en dash and a space. |
| splitRangeText | splitRangeText(text: string): readonly [string, string] | null | Splits range text the way the picker does: at an en or em dash, or at a hyphen with spaces around it. Returns null unless there are exactly two parts. |
import { resolvePattern, parseDate, formatDate } from "@pglejzer/datepicker-ui/kit";const pattern = resolvePattern("dd.MM.yyyy", "de-DE");const date = parseDate("14.03.2025", pattern);// { year: 2025, month: 3, day: 14 }if (date) formatDate(date, pattern);// "14.03.2025"
Week and name helpers
#The helpers the calendar uses for its week layout and its month and day names.
| Export | Signature | Purpose |
|---|---|---|
| resolveWeekStart | resolveWeekStart(locale: string, option: WeekStartOption): Weekday | Returns the first day of the week, 0 for Sunday to 6 for Saturday. "auto" follows the locale. |
| weekdayNames | weekdayNames(locale: string, width: "long" | "short" | "narrow"): readonly string[] | The seven weekday names from Intl, starting with Sunday. |
| formatMonthYear | formatMonthYear(view: YearMonth, locale: string): string | The month and year as the picker writes them, such as March 2025. |
| formatFullDate | formatFullDate(date: PlainDate, locale: string): string | Weekday, day, month and year, as in the accessible name of a day. |
Types
#| Type | Meaning |
|---|---|
| SurfaceHost | Common shape of the three hosts. |
| SurfaceMode | "docked", "modal" or "inline". |
| Scheme | "light" or "dark". |
| SurfaceTheme | { theme, colorScheme, className?, rtl? }, the settings applyTheme writes. |
| PagerOptions | Options of Pager: right-to-left, swipe, animation and how to build and step pages. |
| FormatPattern | A resolved date pattern. |
| FormatOption | The value of ui.format. |
| HintLetters | { day, month, year }, the letters formatHint uses. |
| DatePart | One part of a pattern: day, month or year. |
| WeekStartOption | "auto", "sunday", "monday" or "saturday". |
| Weekday | 0 to 6, Sunday to Saturday. |
| PlainDate | { year, month, day }. |
| YearMonth | { year, month }. |
| ThemeName | A built-in theme name. |
From a CDN
#dist/umd/kit.js exposes the exports as the global DatepickerUIKit.
<script src="https://unpkg.com/@pglejzer/datepicker-ui/dist/umd/kit.js"></script><script>const { resolvePattern, parseDate } = DatepickerUIKit;</script>