timepicker-ui
4.7.0GitHub
timepicker-ui
Guides / Kit

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.

index.ts
import {
PopoverHost,
resolveSurfaceMode,
parseDate,
} from "@pglejzer/datepicker-ui/kit";

Load the CSS

The hosts render dp-ui__container elements, so load main.css or index.css next to them.

Surfaces

#
ExportSignaturePurpose
PopoverHostnew PopoverHost(options)Docked surface: a popover placed next to an anchor element.
DialogHostnew DialogHost(options)Modal surface: a native dialog with a backdrop.
InlineHostnew InlineHost({ container, label, labelledBy? })Inline surface inside a container element.
waitForExitwaitForExit(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.

index.ts
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 again
host.setLabel("Choose a return date");

Surface choice

#
ExportSignaturePurpose
resolveSurfaceModeresolveSurfaceMode(option: "auto" | "docked" | "modal", inlineEnabled: boolean, viewportWidth: number, viewportHeight: number): SurfaceModeReturns "docked", "modal" or "inline". "auto" means modal below 600 px wide or 480 px tall.
isFullscreenRangeisFullscreenRange(mode: CalendarMode, surface: SurfaceMode, viewportWidth: number, fullscreenBelow: number): booleanSays whether a range picker goes full screen.
fitsViewportfitsViewport(root: HTMLElement, anchor: HTMLElement): booleanSays 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

#
ExportSignaturePurpose
detectColorSchemedetectColorScheme(from: HTMLElement): SchemeReturns "light" or "dark" for an element.
watchColorSchemewatchColorScheme(onChange: () => void): () => voidReports changes and returns a function that stops watching.
applyThemeapplyTheme(root: HTMLElement, theme: SurfaceTheme): voidWrites 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

#
ExportSignaturePurpose
Pagernew 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.
createTextButtoncreateTextButton(modifier: string, label: string): HTMLButtonElementBuilds a text button with the classes dp-ui__text-button and dp-ui__text-button--<modifier>.

Date text

#

A date is { year, month, day }.

ExportSignaturePurpose
resolvePatternresolvePattern(format: FormatOption, locale: string, precision?: Precision): FormatPatternTurns "auto" or a pattern string into a pattern for a locale.
formatDateformatDate(date: PlainDate, pattern: FormatPattern): stringFormats a date as text.
parseDateparseDate(text: string, pattern: FormatPattern): PlainDate | nullParses typed text, or returns null.
maskDateTextmaskDateText(text: string, pattern: FormatPattern): stringFormats typed digits to the pattern.
formatHintformatHint(pattern: FormatPattern, letters?: HintLetters): stringReturns a hint for the pattern, such as a placeholder. letters sets the day, month and year letters, dd, mm and yyyy by default.
leadingDateLengthleadingDateLength(text: string, pattern: FormatPattern): numberLength 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.
normalizeDigitsnormalizeDigits(text: string): stringMaps Arabic-Indic and Persian digits to Latin digits, drops bidi marks and trims.
RANGE_SEPARATORRANGE_SEPARATOR: " – "What the picker writes between the two dates of a range: a space, an en dash and a space.
splitRangeTextsplitRangeText(text: string): readonly [string, string] | nullSplits 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.
index.ts
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.

ExportSignaturePurpose
resolveWeekStartresolveWeekStart(locale: string, option: WeekStartOption): WeekdayReturns the first day of the week, 0 for Sunday to 6 for Saturday. "auto" follows the locale.
weekdayNamesweekdayNames(locale: string, width: "long" | "short" | "narrow"): readonly string[]The seven weekday names from Intl, starting with Sunday.
formatMonthYearformatMonthYear(view: YearMonth, locale: string): stringThe month and year as the picker writes them, such as March 2025.
formatFullDateformatFullDate(date: PlainDate, locale: string): stringWeekday, day, month and year, as in the accessible name of a day.

Types

#
TypeMeaning
SurfaceHostCommon shape of the three hosts.
SurfaceMode"docked", "modal" or "inline".
Scheme"light" or "dark".
SurfaceTheme{ theme, colorScheme, className?, rtl? }, the settings applyTheme writes.
PagerOptionsOptions of Pager: right-to-left, swipe, animation and how to build and step pages.
FormatPatternA resolved date pattern.
FormatOptionThe value of ui.format.
HintLetters{ day, month, year }, the letters formatHint uses.
DatePartOne part of a pattern: day, month or year.
WeekStartOption"auto", "sunday", "monday" or "saturday".
Weekday0 to 6, Sunday to Saturday.
PlainDate{ year, month, day }.
YearMonth{ year, month }.
ThemeNameA built-in theme name.

From a CDN

#

dist/umd/kit.js exposes the exports as the global DatepickerUIKit.

index.html
<script src="https://unpkg.com/@pglejzer/datepicker-ui/dist/umd/kit.js"></script>
<script>
const { resolvePattern, parseDate } = DatepickerUIKit;
</script>
#
Esc