Skip to the content
human-kit design
24 — Pickers

Date Picker

A date typed part by part, or picked from a month that opens under it.

mmddyyyy

Installation

pnpm dlx @human-kit/design add date-picker
npx @human-kit/design add date-picker
yarn dlx @human-kit/design add date-picker
bunx @human-kit/design add date-picker

The 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.

11202026
20112026
20261120

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.

mmddyyyy

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 default variant in a form, ghost in a table cell and underline in a form drawn with lines.
  • Match the size to 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 by aria-label or by a Field.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 triggerLabel and the month arrows by previousLabel and nextLabel, 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

PropTypeDefault
valuestring | null

The date, as YYYY-MM-DD, or null while the parts spell no date. Bindable.

—
defaultValuestring | null

The date at the start, when no value is bound.

—
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'
minValuestring

The first day that can be picked, as YYYY-MM-DD.

—
maxValuestring

The last day that can be picked, as YYYY-MM-DD.

—
isDateUnavailable(date: string) => boolean

Turns off the days it returns true for.

—
openboolean

Whether the calendar shows. Bindable.

—
defaultOpenboolean

Whether the calendar starts open, when no open is bound.

false
onOpenChange(open: boolean, details: DatePickerOpenChangeDetails) => void

Runs when the calendar opens or closes. details.cancel() keeps it as it was.

—
closeOnSelectboolean

Closes the calendar once a day is picked.

true
invalidboolean

Marks the date as refused, for a Field.Root with an error.

false
disabledboolean

Turns the picker off.

false
readonlyboolean

Shows the date and lets it be read, but not changed.

false
idstring

The id of the hidden input that carries the date in a form.

—
namestring

The name of that hidden input.

—
aria-labelstring

The name of the date, when there is no Field.Root to name it.

—
aria-labelledbystring

The id of the label that names it, as a Field.Root gives.

—
aria-describedbystring

The id of the hint or the error under it.

—
triggerLabelstring

The name of the calendar button, read aloud.

'Choose a date'
previousLabelstring

The name of the arrow to the month before.

'Previous month'
nextLabelstring

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.