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:
<input id="date" />
Import the class and the stylesheet, create the picker and call create():
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,monthsandprecision. ui- Presentation:
displayMode,inline,format,theme,colorSchemeand the footer buttons. labels- Every button text and accessible name. Month and weekday names come from Intl.
behavior- Interaction rules:
closeOnSelect,commitOnChange,returnFocusand the instanceid. callbacks- One
on*function per event, such asonConfirmandonOpen.
All of them are listed on the Options page. This demo sets one option from four of the groups:
Frameworks
#Create the picker on the client after the input is mounted, and call destroy() when the component unmounts:
"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
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