How it works
For choosing a single day. The trigger is the same filled field as the text input: calendar button, a real <input>, and a clear button that only hides when empty; the panel is a floating sheet with round day cells and shortcut pills. Values are 'YYYY-MM-DD' strings computed in local time, never through UTC, so min and max are string comparisons. Typed dates in several formats are normalized on Enter or blur; invalid ones restore the old value with a short hint. The panel is absolutely positioned inside the root (no portal) and flips above when there is no room below.
Use it for
- Filling in a specific day in a form: departure, delivery or birthday.
- Filtering by a single day in a filter bar (use size="sm").
- Dates with an allowed range or blocked days: only the next 30 days, no delivery on holidays.
Look elsewhere when
- Choosing a span of dates: use a date range picker.
- Only a month or year: the value here is always a full day.
- Date and time together: this component has no time.
- Dates far in the past that people already know, such as a birth year: typing or three dropdowns is faster on phones.
Usage
import { useState } from 'react'
import { DatePicker } from './DatePicker.jsx'
// Compute dates from the local year, month and day; toISOString() is UTC and can be a day off
const pad = (n) => String(n).padStart(2, '0')
const ymd = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
const daysLater = (n) => {
const d = new Date()
d.setDate(d.getDate() + n)
return ymd(d)
}
export function DepartureField() {
const [date, setDate] = useState('')
return (
<div>
<label htmlFor="departure">Departure date</label>
<DatePicker
value={date}
onChange={setDate}
min={daysLater(0)}
max={daysLater(30)}
shortcuts={[{ label: 'Tomorrow', value: () => daysLater(1) }]}
inputProps={{ id: 'departure', name: 'departure', required: true }}
/>
</div>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
value | string | — | Controlled value, 'YYYY-MM-DD'. An empty string means none. An invalid date is treated as empty. |
defaultValue | string | '' | Initial value when uncontrolled. |
onChange | (value: string) => void | — | Called after a pick, a confirmed typed date or a clear. Clearing gives an empty string. |
open | boolean | — | Controlled: whether the panel is open. |
defaultOpen | boolean | false | Uncontrolled: whether it starts open. Starting open does not take focus. |
onOpenChange | (open: boolean) => void | — | Called when the panel opens or closes. |
size | 'sm' | 'md' | 'lg' | 'md' | Trigger height 36 / 44 / 52px, radius 10 / 12 / 14px, font 14 / 15 / 16px. The panel size does not change. |
disabled | boolean | false | Disabled: the field stays filled without its hairline, the text is muted, and it cannot open. |
invalid | boolean | false | Red hairline, a faint red fill, a red halo while focused, and aria-invalid on the input. The error text belongs to the surrounding form field. |
placeholder | string | 'YYYY-MM-DD' | Placeholder; use it for the format example only. |
clearable | boolean | true | Shows a clear button when there is a value. When false, emptying the input restores the previous value. |
editable | boolean | true | Whether the date can be typed. On touch screens, false avoids raising the keyboard. |
min | string | — | Earliest selectable date (inclusive), 'YYYY-MM-DD'. |
max | string | — | Latest selectable date (inclusive), 'YYYY-MM-DD'. |
disabledDate | (date: string) => boolean | — | Dates that return true are unavailable: faded, not clickable or focusable, skipped by arrow keys, and announced as unavailable. |
weekStartsOn | 0 | 1 | 1 | First day of the week: 1 Monday, 0 Sunday. |
showToday | boolean | true | Shows a Today pill in the footer. It is disabled when today is unavailable. |
shortcuts | { label: string, value: string | (() => string) }[] | — | Quick dates as pills after Today in the footer. Write relative dates as functions; they are computed when the panel opens. |
onInvalidInput | (text, reason: 'format' | 'unavailable') => void | — | Called when typed text is not a valid date (format) or that day is unavailable. The previous value is already restored. |
inputProps | InputHTMLAttributes | — | Passed to the real <input>: id (for <label htmlFor>), name, required, aria-label, aria-describedby and so on. |
labels | object | — | Replaces built-in text: weekdays, weekdayNames, monthNames, monthsShort, monthTitle, yearTitle, yearRangeTitle, dayLabel, todaySuffix, unavailableSuffix, chooseMonth, chooseYear, backToDates, monthGroup, previousMonth, nextMonth, previousYear, nextYear, previousYears, nextYears, today, dialog, openCalendar, openCalendarWith, clear, formatHint, minHint, maxHint, unavailableHint. Lists start on Sunday and January. Shallow-merged over the English defaults. |
...rest | HTMLAttributes<HTMLDivElement> | — | className, style and other attributes go to the root. Width fills the container by default. |
Accessibility
- The input is role="combobox" with aria-haspopup="dialog"; the panel is a dialog and the calendar a grid. Each cell is named with its full date, plus "today" or "unavailable" when it applies.
- Down arrow in the input opens the panel and moves focus into the calendar; arrows move by day or week, Home and End go to the start and end of the week, PageUp and PageDown change month (with Shift, year), Enter or Space selects, Esc closes and returns focus.
- Only one cell is in the Tab order; unavailable days cannot be focused and arrow keys skip them.
- Today is bold with a dot, so it does not rely on colour alone. Unavailable days are faded, cannot be focused, and their cell name ends with "unavailable".
- Typing errors are announced through role="alert"; the new month and year are announced after paging.
- Pair the input with <label htmlFor> and inputProps.id, or pass an aria-label.
Motion
The field's focus halo grows from 0 to 4px over 220ms. Panel enter 220ms ease-out (moves 6px and scales 0.96 to 1 from the corner touching the trigger, with a fade), exit 140ms ease-in. The chosen day pops in (scale 0.6 to 1, 380ms spring). Changing month slides the new page in 12px over 280ms. With reduced motion the panel only fades and nothing else moves.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
DatePicker.jsxdate-picker.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





