Set up the token layer and the StyleX compiler, if you have not already. See
Installation, and paste state-styles.ts and
field-state.ts from the same page. The date picker also needs the
Calendar, the Popover
it opens in, and the Select, whose trigger style it
shares.
Paste this source into the file named in its title.
DatePicker popover root├── DatePickerTrigger the field; shows children, or placeholder when empty└── DatePickerContent popup, sized to what it holds └── Calendar
The trigger shows whatever you pass as children, so you choose the date format.
With no children it shows placeholder. The popup has no padding of its own;
the calendar brings its own.
To close the popup once a date is picked, control open and set it to false
in onSelect, as the first example does.
Opening the popup moves focus to the selected day, or to today when nothing is
selected, so the arrow keys move through dates straight away.
Range
Pass a range calendar. Intl.DateTimeFormat.formatRange writes both ends
without repeating the month or year they share.
"use client"import { useState } from "react"import { Calendar, type DateRange } from "@/components/ui/calendar"import { DatePicker, DatePickerContent, DatePickerTrigger } from "@/components/ui/date-picker"const format = new Intl.DateTimeFormat("en-US", { dateStyle: "medium" })export default function DatePickerRange() { const [range, setRange] = useState<DateRange>() return ( <DatePicker> <DatePickerTrigger aria-label="Dates" placeholder="Pick a range"> {range?.from && range.to && format.formatRange(range.from, range.to)} </DatePickerTrigger> <DatePickerContent> <Calendar mode="range" numberOfMonths={2} defaultMonth={range?.from} selected={range} onSelect={setRange} /> </DatePickerContent> </DatePicker> )}
Date of birth
Month and year dropdowns get to a date decades back without paging through
every month. endMonth stops the years at today.
"use client"import { useState } from "react"import { Calendar } from "@/components/ui/calendar"import { DatePicker, DatePickerContent, DatePickerTrigger } from "@/components/ui/date-picker"const format = new Intl.DateTimeFormat("en-US", { dateStyle: "long" })export default function DatePickerDropdown() { const [open, setOpen] = useState(false) const [date, setDate] = useState<Date>() return ( <DatePicker open={open} onOpenChange={setOpen}> <DatePickerTrigger aria-label="Date of birth" placeholder="Date of birth"> {date && format.format(date)} </DatePickerTrigger> <DatePickerContent> <Calendar mode="single" captionLayout="dropdown" startMonth={new Date(1920, 0)} endMonth={new Date()} defaultMonth={date ?? new Date(1995, 0)} selected={date} onSelect={(next) => { setDate(next) setOpen(false) }} /> </DatePickerContent> </DatePicker> )}
API reference
DatePicker is the Base UI Popover root and takes its props. See the
Base UI reference.
DatePickerTrigger
Wraps Popover.Trigger and accepts its props, plus:
Prop
Type
Default
placeholder
ReactNode
"Pick a date"
icon
ReactNode
calendar icon
size
"default" | "sm"
"default"
aria-invalid
boolean
—
xstyle
StyleXStyles
—
className
string
—
style
CSSProperties
—
Inside a Field, the trigger shows the invalid ring when the field is invalid.
DatePickerContent
Takes every PopoverContent prop. align defaults to "start", and
initialFocus defaults to the calendar's focusable day.