Date Range Picker
Two dates, a start and an end, typed part by part or picked from two months.
Installation
pnpm dlx @human-kit/design add date-range-pickernpx @human-kit/design add date-range-pickeryarn dlx @human-kit/design add date-range-pickerbunx @human-kit/design add date-range-pickerThe command copies the files to src/lib/components/ui/date-range-picker. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as DateRangePicker from '$lib/components/ui/date-range-picker';
let dates = $state<{ start: string; end: string } | null>(null);
</script>
<DateRangePicker.Root aria-label="Treatment dates" bind:value={dates} /> Examples
Locale
Wrap the picker in a LocaleProvider: both ends are written as the locale writes a date, and the calendars follow it. The value stays YYYY-MM-DD in every one.
Guidelines
- Use the date range picker for a stretch with a start and an end: a treatment, a leave, the period of a report. For one day, use the date picker.
- Keep two months side by side where there is room: a stretch that crosses the end of a month is then picked in one view. Show one with
visibleMonths={1}in a narrow column. - Use the
defaultvariant in a form,ghostin a toolbar andunderlinein a form drawn with lines. - Rename
startLabelandendLabelto what the dates mean, "Check-in" and "Check-out", when the general words read poorly. - In a form, give
startNameandendName: each date travels in a hidden input of its own.
Accessibility
- The range is a group named by
aria-label. Each date in it is a group of its own, named bystartLabelandendLabel. - Each part of a date is a
role="spinbutton": digits fill it and move on, Up and Down step it, Left and Right move between the parts. - In the calendar, the first press starts the range, the arrows stretch it, and Enter or Space ends it.
- An unavailable day still takes the focus and is read as disabled, so the grid has no holes.
- The calendar button and the month arrows are named by
triggerLabel,previousLabelandnextLabel, so each can be translated.
API reference
| Prop | Type | Default |
|---|---|---|
value | { start: string; end: string } | null The two dates, each as | — |
defaultValue | { start: string; end: string } | null The range at the start, when no | — |
onChange | (value: { start: string; end: string } | null) => void Runs when the range 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' |
visibleMonths | number How many months the calendar shows side by side. | 2 |
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: DateRangePickerOpenChangeDetails) => void Runs when the calendar opens or closes. | — |
closeOnSelect | boolean Closes the calendar once the range is complete. | true |
invalid | boolean Marks both dates as refused, for a | false |
disabled | boolean Turns the picker off. | false |
readonly | boolean Shows the range and lets it be read, but not changed. | false |
aria-label | string The name of the range as a whole. | — |
aria-describedby | string The id of the hint or the error under it, read with each date. | — |
startLabel | string The name of the first date, read aloud. | 'Start date' |
endLabel | string The name of the second date, read aloud. | 'End date' |
startName | string The name of the hidden input that carries the first date in a form. | — |
endName | string The name of the hidden input that carries the second date. | — |
triggerLabel | string The name of the calendar button, read aloud. | 'Choose dates' |
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.