Date picker
A date field with a calendar in a popover: type the date, or open the calendar and pick it. Both set the same value. For a range of days use the date range picker; for a field with no calendar use the date input.
Install
npx shadcn@latest add https://thrumui.dev/r/date-picker.jsonAdds components/ui/date-picker.tsx. Nothing else in your project changes.
import { DatePicker } from "@/components/ui/date-picker";Automatically installs icons calendar and x from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | Date | null | – | The date, when the parent holds it. Use it with onValueChange. |
defaultValue | Date | null | Default null | The date at first, when the picker holds it itself. |
onValueChange | (value: Date | null) => void | – | Called when the date is typed in full, picked, chosen from a preset or cleared. |
min | Date | null | – | The earliest valid date. Earlier days cannot be picked, and a typed one marks the field invalid. |
max | Date | null | – | The latest valid date. |
disabledDays | Date[] | { dayOfWeek: number[] } | – | Days the calendar will not pick, and presets that land on them are disabled. A typed one is accepted. |
weekStartsOn | 0 | 1 | 2 | 3 | 4 | 5 | 6 | – | The calendar's first column, 0 for Sunday. Defaults to the locale's week. |
locale | string | Default "en-US" | Sets the segment order and the month and weekday names. |
today | Date | – | Marks today in the calendar, opens it there when the field is empty, and anchors the presets. Without it the calendar opens on the visitor's clock. |
presets | boolean | Default false | Show Today, Tomorrow and In a week beside the calendar, above it on a phone. Needs today. |
clearable | boolean | Default false | Show a button that empties the field. It is disabled while the field is empty. |
captionLayout | "label" | "dropdown" | Default "label" | dropdown gives the calendar a month and a year select, for dates years away. |
name | string | – | The field name in a form. A hidden input submits the date as 2026-12-03. |
size | "sm" | "md" | "lg" | Default "md" | sm is 28 pixels tall, md 32, lg 40. The buttons beside the field match it. |
disabled | boolean | Default false | The field and both buttons are dimmed and cannot be used. |
required | boolean | Default false | Sets aria-required on every segment of the field. Check for a null value before you submit. |
invalid | boolean | Default false | Marks the field invalid for a reason of your own. A date outside min and max marks it on its own. |
sound | boolean | Default true | Play the popover's open and close sounds, and a click when a day or preset is picked, if the visitor has sound on. |
className | string | – | Extra classes for the row that holds the field and its buttons. It fills its container. |
Motion and sound
Motion
- The calendar fades in and grows from 96% to full size over 150 ms, from the edge that touches the button.
- Closing plays the same change backwards.
- Reduced motion: it appears and leaves at once.
Sound
- A soft rising tone when the calendar opens and a falling one when it closes by Escape, a press outside or the button.
- A click when a day or a preset is picked, and a soft falling sound when the date is cleared.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The field is the date input: a group of spin buttons, named by your aria-labelledby or aria-label.
- The calendar button is named by the date it holds (Choose date, Thursday, December 10, 2026), so a screen reader hears the value from the button too.
- The panel is a non-modal dialog named Choose date. Inside it the calendar follows the WAI-ARIA date grid pattern.
- The preset that matches the date is marked with aria-pressed and a filled background.
- Under a glass material the panel is translucent. Its tint keeps the text at 4.5 to 1 over any page.
| Keys | Action |
|---|---|
| 0 to 9, ↑ / ↓, ← / → | In the field: type and step the segments, as in the date input. |
| Enter / Space | On the calendar button: open the calendar, with the focus on the date, or on today when the field is empty. |
| Arrows, Page Up / Page Down, Home / End | In the calendar: move by day, week, month and week edge, as in the calendar. |
| Enter / Space | In the calendar: pick the focused day. The calendar closes and the focus returns to its button. |
| Tab | In the panel: move between the presets, the month buttons and the grid. |
| Esc | Close the calendar and keep the date. The focus returns to the button. |
Source
"use client";
import { useRef, useState, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { CalendarIcon } from "@/components/icons/calendar";
import { XIcon } from "@/components/icons/x";
import { play } from "@/lib/thrum-sound";
import { buttonClass } from "@/components/ui/button";
import { addDays, Calendar, isDayDisabled, isSameDay, type DisabledDays, type Weekday } from "@/components/ui/calendar";
import { DateInput, type SegmentFieldSize } from "@/components/ui/date-input";
import { IconButton } from "@/components/ui/icon-button";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
export type DatePickerProps = Omit<ComponentProps<"div">, "defaultValue" | "onChange"> & {
/** The date, when the parent holds it. Use it with onValueChange. */
value?: Date | null;
/** The date at first, when the picker holds it itself. */
defaultValue?: Date | null;
/** Called with the date when it is typed in full, picked, chosen from a preset or cleared. */
onValueChange?: (value: Date | null) => void;
/** The earliest valid date. Earlier days cannot be picked; a typed one marks the field invalid. */
min?: Date | null;
/** The latest valid date. */
max?: Date | null;
/** Days that cannot be picked in the calendar: a list of dates, or `{ dayOfWeek: [0, 6] }`. */
disabledDays?: DisabledDays;
/** The calendar's first column: 0 is Sunday, 1 Monday. Defaults to the locale's week. */
weekStartsOn?: Weekday;
/** Sets the segment order and the month and day names. */
locale?: string;
/** Today, for the calendar's mark and the presets. The picker never reads the clock itself. */
today?: Date;
/** Show Today, Tomorrow and In a week beside the calendar. Needs `today`. */
presets?: boolean;
/** Show a button that empties the field. */
clearable?: boolean;
/** `dropdown` gives the calendar a month and a year select, for dates far away such as a birth date. */
captionLayout?: "label" | "dropdown";
/** The field name in a form. A hidden input submits the date as 2026-12-03. */
name?: string;
/** `sm` is 28 pixels tall, `md` 32, `lg` 40. */
size?: SegmentFieldSize;
disabled?: boolean;
required?: boolean;
/** Mark the field invalid for a reason of your own. Out-of-range dates mark it on their own. */
invalid?: boolean;
/** Play the popover's open and close sounds and a click on a pick, if the visitor has sound on. */
sound?: boolean;
};
/**
* A date field with a calendar in a popover. Typing and picking set the same value. Name it with `aria-label` or
* `aria-labelledby`, which go to the field; the calendar button is named by the date it holds.
*/
export function DatePicker({
value,
defaultValue = null,
onValueChange,
min,
max,
disabledDays,
weekStartsOn,
locale = "en-US",
today,
presets = false,
clearable = false,
captionLayout,
name,
size = "md",
disabled = false,
required = false,
invalid = false,
sound = true,
className,
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
"aria-describedby": ariaDescribedby,
...props
}: DatePickerProps) {
const [own, setOwn] = useState<Date | null>(defaultValue);
const date = value !== undefined ? value : own;
const [open, setOpen] = useState(false);
// The month the calendar opens on: the date, else today, else the clock read at the moment of opening.
const [openMonth, setOpenMonth] = useState<Date | undefined>(undefined);
const panel = useRef<HTMLDivElement>(null);
const change = (next: Date | null) => {
if (isSameDay(next, date) || (next === null && date === null)) return;
if (value === undefined) setOwn(next);
onValueChange?.(next);
};
const choose = (next: Date) => {
change(next);
setOpen(false); // a pick closes the panel without the closing sound: the pick made its own
};
const long = new Intl.DateTimeFormat(locale, { weekday: "long", day: "numeric", month: "long", year: "numeric" });
const shortcuts = today
? [
{ label: "Today", date: today },
{ label: "Tomorrow", date: addDays(today, 1) },
{ label: "In a week", date: addDays(today, 7) },
]
: [];
return (
<div data-slot="date-picker" className={cn("flex w-full min-w-0 items-center gap-1", className)} {...props}>
<DateInput
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
aria-describedby={ariaDescribedby}
value={date}
onValueChange={change}
min={min}
max={max}
locale={locale}
name={name}
size={size}
disabled={disabled}
required={required}
invalid={invalid}
className="flex-1"
/>
{clearable && (
<IconButton
aria-label="Clear date"
variant="ghost"
size={size}
disabled={disabled || !date}
onClick={() => {
if (sound) play("remove");
change(null);
}}
>
<XIcon />
</IconButton>
)}
<Popover
open={open}
onOpenChange={(next) => {
if (next) setOpenMonth(date ?? today ?? new Date());
setOpen(next);
}}
sound={sound}
>
<PopoverTrigger
data-slot="date-picker-trigger"
data-surface="control"
aria-label={date ? `Choose date, ${long.format(date)}` : "Choose date"}
disabled={disabled}
className={cn(buttonClass({ variant: "outline", size: "icon" }), size === "sm" ? "size-7" : size === "lg" ? "size-10" : "size-8", "[&_svg]:size-4")}
>
<CalendarIcon />
</PopoverTrigger>
<PopoverContent
aria-label="Choose date"
align="end"
className="w-auto max-w-(--available-width) p-3"
// Open onto the picked day (or today), the calendar's one tab stop, so the arrow keys work at once.
initialFocus={() => panel.current?.querySelector<HTMLElement>('[data-slot="calendar-day"][tabindex="0"]') ?? true}
>
<div ref={panel} data-slot="date-picker-panel" className="flex flex-col gap-3 sm:flex-row">
{presets && shortcuts.length > 0 && (
<div data-slot="date-picker-presets" className="flex flex-wrap gap-1 sm:w-28 sm:flex-col sm:border-e sm:pe-3">
{shortcuts.map((shortcut) => (
<button
key={shortcut.label}
type="button"
aria-pressed={isSameDay(shortcut.date, date)}
disabled={isDayDisabled(shortcut.date, { min, max, disabledDays })}
className={cn(buttonClass({ variant: "ghost", size: "sm" }), "justify-start aria-pressed:bg-accent aria-pressed:text-accent-foreground")}
onClick={() => {
if (sound) play("select");
choose(shortcut.date);
}}
>
{shortcut.label}
</button>
))}
</div>
)}
<Calendar
value={date}
defaultMonth={openMonth}
onValueChange={(next) => next && choose(next)}
min={min}
max={max}
disabledDays={disabledDays}
weekStartsOn={weekStartsOn}
locale={locale}
today={today}
captionLayout={captionLayout}
sound={sound}
surface={false}
className="border-0 bg-transparent p-0"
/>
</div>
</PopoverContent>
</Popover>
</div>
);
}Files and dependencies
- Adds
components/ui/date-picker.tsx- Packages
- None
- Brings along
- ButtonCalendarDate inputIcon buttonPopoverSound engine
- Icons
calendarx
Changelog
- 1.0.0 · 2026-12-03First release.