Date range picker
Pick a start and end date together, with presets for the ranges people ask for most.
Purpose
One field for a span: the trigger opens the Calendar in range mode — two months side by side by default — with a preset column for the ranges people actually ask for ("Last 30 days", "Last month"). For a single date use Date picker; two single pickers for a span force users to keep the start in their head while finding the end, and let them pick an end before the start.
Example
The picker is controlled — hold a DateRange ({ from, to }) in state:
import { DateRangePicker, type DateRange } from "@healthmetrics/ui/date-range-picker";
const [range, setRange] = React.useState<DateRange | undefined>();
<div className="grid w-80 gap-1.5">
<Label htmlFor="claim-period">Claim period</Label>
<DateRangePicker id="claim-period" value={range} onChange={setRange} />
</div>Presets
Four presets ship as DEFAULT_RANGE_PRESETS — Last 7 days, Last 30 days, This month, Last month.
Supply your own DateRangePreset[] when the domain has better ones, or pass presets={[]} to
hide the column entirely (pair that with numberOfMonths={1} for a compact picker):
import { DateRangePicker, type DateRangePreset } from "@healthmetrics/ui/date-range-picker";
import { startOfYear } from "date-fns";
const presets: DateRangePreset[] = [
{ label: "Year to date", range: () => ({ from: startOfYear(new Date()), to: new Date() }) },
];
<DateRangePicker value={range} onChange={setRange} presets={presets} />
// Compact: one month, no preset column.
<DateRangePicker value={range} onChange={setRange} presets={[]} numberOfMonths={1} />Each preset's range is a function, so "Last 30 days" is computed when clicked, not when the
page rendered. Choosing a preset calls onChange with the computed range; the popover stays
open so the user can confirm or adjust it on the calendar.
States
Empty, selected and disabled. The empty trigger shows the placeholder in --muted-foreground;
a selection renders as "1 Jul 2026 – 28 Jul 2026" (a start with no end yet shows the start
alone). disabled disables the trigger, so the popover cannot open.
Structure
| Property | Value |
|---|---|
| Trigger | outline Button, 40px (h-10), full width, left-aligned |
| Trigger icon | 18px date_range glyph, --muted-foreground |
| Placeholder | --muted-foreground |
| Value format | date-fns d MMM yyyy — "1 Jul 2026 – 28 Jul 2026" |
| Popover | --popover surface, zero padding, aligned to the trigger start |
| Preset column | left of the calendar, 1px border-right, ghost sm buttons |
| Calendar | range mode, 2 months by default (numberOfMonths: 1 or 2) |
| Range band | --accent fill; --primary endpoints |
Best practices
Do
- Pair the trigger with a
Labelvia theidprop — the field needs a name of its own. - Keep the presets that match the questions your screen answers — reporting periods, claim windows — and drop the rest.
- Use
numberOfMonths={1}withpresets={[]}when space is tight and spans are short. - Validate the span length in your form logic — the picker guarantees order (end after start) but not duration.
Don't
- Use two single Date pickers for a span — order mistakes and double the clicks.
- Reach for this when only one date matters — an end-date-only deadline is a single Date picker.
- Invent presets nobody asked for — every extra row pushes the calendar further away.
Accessibility & keyboard
- The trigger is a real button announcing the current range (or placeholder); pair it with a
Labelviaidso the field itself is named. - When the popover opens, focus deliberately moves into the calendar grid (the source
autoFocusis required — without it keyboard users are stranded on the trigger). - The preset column is a
role="group"labelled "Quick ranges"; each preset is a real button. - The month and year dropdowns are native
<select>elements with their own accessible names. - Selecting dates or a preset does not close the popover — the user confirms with Esc or by clicking away, which is deliberate for a two-click selection.
- Keyboard: Enter or Space on the trigger opens the calendar with focus in the grid; arrow keys move a day (up/down a week) at a time; Enter or Space picks the start, then the end; Tab and Shift+Tab move between the preset buttons, the month/year dropdowns and the grid; Esc closes and returns focus to the trigger.
Props
DateRangePicker is controlled: pass value and handle onChange, which fires with the new
DateRange or undefined when the selection is cleared. DateRange and DateRangePreset are
re-exported from the same subpath.
| Prop | Type | Default | Description |
|---|---|---|---|
value | DateRange | undefined | — | The currently selected range (controlled). |
onChange | ((range: DateRange | undefined) => void) | undefined | — | — |
presets | DateRangePreset[] | undefined | DEFAULT_RANGE_PRESETS | Quick-pick presets shown beside the calendar; pass an empty array to hide the column. |
numberOfMonths | 1 | 2 | undefined | 2 | Number of months displayed. |
placeholder | string | undefined | "Pick a date range" | — |
disabled | boolean | undefined | — | Disables the trigger; the popover cannot open while set. |
id | string | undefined | — | — |
className | string | undefined | — | — |