timepicker-ui
4.6.0GitHub
timepicker-ui
Overview / Quick Start

Quick Start

Turn a text input into a date picker with @pglejzer/datepicker-ui, the companion of timepicker-ui.

Your first picker

#

Add a text input to the page:

index.html
<input id="date" />

Import the class and the stylesheet, create the picker and call create():

index.ts
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
const picker = new DatepickerUI("#date", {
ui: { theme: "m3-green" },
callbacks: {
onConfirm: ({ value }) => console.log(value),
},
});
picker.create();

The options argument is optional. The first argument is a selector, an <input> or an element that contains an <input>. A bare <input> is wrapped in a div.dp-ui host. For any other element, the parent of the input becomes the host. The constructor throws when no input is found.

Option groups

#

Options come in five groups. Every option is optional. A value of the wrong type or an unknown choice falls back to the default and emits an error event with the code invalid-option.

calendar
Which days exist and what the user selects: mode, minDate, maxDate, disabled dates, locale, weekStart, months and precision.
ui
Presentation: displayMode, inline, format, theme, colorScheme and the footer buttons.
labels
Every button text and accessible name. Month and weekday names come from Intl.
behavior
Interaction rules: closeOnSelect, commitOnChange, returnFocus and the instance id.
callbacks
One on* function per event, such as onConfirm and onOpen.

All of them are listed on the Options page. This demo sets one option from four of the groups:

Option groups
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
new DatepickerUI("#date", {
calendar: { weekStart: "monday" },
ui: { todayButton: true, clearButton: true },
labels: { today: "Go to today" },
behavior: { closeOnSelect: true },
}).create();

Frameworks

#

Create the picker on the client after the input is mounted, and call destroy() when the component unmounts:

index.tsx
"use client";
import { useEffect, useRef } from "react";
import DatepickerUI from "@pglejzer/datepicker-ui";
import "@pglejzer/datepicker-ui/index.css";
export function DateField() {
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (!inputRef.current) return;
const picker = new DatepickerUI(inputRef.current);
picker.create();
return () => picker.destroy();
}, []);
return <input ref={inputRef} />;
}

Angular styles

Add node_modules/@pglejzer/datepicker-ui/dist/css/index.css to the styles array of angular.json, or import @pglejzer/datepicker-ui/index.css from your global stylesheet.

More on server rendering and each framework in Frameworks and SSR.

Next steps

Display Modes
Docked popover, modal dialog and inline calendar
Selection Modes
Single date, date range and multiple dates, and the values they return
Examples
Live demos with code
Esc