Date Picker
A date typed part by part, or picked from a month that opens under it.
Installation
pnpm dlx @human-kit/design add date-pickernpx @human-kit/design add date-pickeryarn dlx @human-kit/design add date-pickerbunx @human-kit/design add date-pickerThe command copies the files to src/lib/components/ui/date-picker. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as DatePicker from '$lib/components/ui/date-picker';
let day = $state<string | null>(null);
</script>
<DatePicker.Root aria-label="Appointment day" bind:value={day} /> Examples
Locale
Wrap the picker in a LocaleProvider: the order of the day, the month and the year follows it, and so does the calendar. The value stays YYYY-MM-DD in every one.
Limits and unavailable days
minValue and maxValue bound the month, and isDateUnavailable turns off days such as weekends. The calendar never lets one be picked, but a typed date is kept as typed and marked invalid, so the form can say why.
A weekday in December.
Guidelines
- Use the date picker for a single day that may be typed or picked: an appointment, a birth date, a due date. For a stretch of days, use the date range picker.
- Typing is often quicker than the calendar for a date far away, such as a birth date. Both stay available: do not make the field read-only to force the calendar.
- Use the
defaultvariant in a form,ghostin a table cell andunderlinein a form drawn with lines. - Match the
sizeto the buttons beside it, so the row lines up. - Put the rule that refuses a date in the form, and the error text in a
Field.Root. The picker marks the date invalid; it does not correct it.
Accessibility
- Each part of the date, the day, the month and the year, is a
role="spinbutton"named in the language of the locale, in a group named byaria-labelor by aField.Root. - Digits fill the focused part and move on by themselves. Up and Down step it, Left and Right move between the parts, Backspace clears it.
- The calendar button is named by
triggerLabeland the month arrows bypreviousLabelandnextLabel, so each can be translated. - In the calendar, the arrows move by day and by week. An unavailable day still takes the focus and is read as disabled, so the grid has no holes.
- When the calendar closes, the focus goes back to the button that opened it.
API reference
| Prop | Type | Default |
|---|---|---|
value | string | null The date, as | — |
defaultValue | string | null The date at the start, when no | — |
onChange | (value: string | null) => void Runs when the date changes, by typing or in the calendar. | — |
variant | 'default' | 'filled' | 'ghost' | 'underline' The look of the box, as the input has. | 'default' |
size | 'sm' | 'default' | 'lg' The height, matched to the buttons of each size. | 'default' |
minValue | string The first day that can be picked, as | — |
maxValue | string The last day that can be picked, as | — |
isDateUnavailable | (date: string) => boolean Turns off the days it returns | — |
open | boolean Whether the calendar shows. Bindable. | — |
defaultOpen | boolean Whether the calendar starts open, when no | false |
onOpenChange | (open: boolean, details: DatePickerOpenChangeDetails) => void Runs when the calendar opens or closes. | — |
closeOnSelect | boolean Closes the calendar once a day is picked. | true |
invalid | boolean Marks the date as refused, for a | false |
disabled | boolean Turns the picker off. | false |
readonly | boolean Shows the date and lets it be read, but not changed. | false |
id | string The id of the hidden input that carries the date in a form. | — |
name | string The name of that hidden input. | — |
aria-label | string The name of the date, when there is no | — |
aria-labelledby | string The id of the label that names it, as a | — |
aria-describedby | string The id of the hint or the error under it. | — |
triggerLabel | string The name of the calendar button, read aloud. | 'Choose a date' |
previousLabel | string The name of the arrow to the month before. | 'Previous month' |
nextLabel | string The name of the arrow to the month after. | 'Next month' |
class styles the box. The picker takes only these props: there is no passthrough of other attributes.