Date Range Picker
A single-trigger date-range picker with a preset rail and a calendar.
A single-trigger date-range picker with a preset rail down the left and a calendar on the right. Selecting a preset highlights its range on the calendar; picking days directly clears the preset and becomes a custom range.
Built from design-system primitives: Menu (popover + radio rail), Calendar, Button, and an icon.
Usage
import { useState } from "react";
import {
DateRangePicker,
DEFAULT_PRESETS,
type DateRangeSelection,
} from "@smartacteam/ambient-web/date-range-picker";
const [value, setValue] = useState<DateRangeSelection>({
preset: "15d",
range: DEFAULT_PRESETS.find((preset) => preset.key === "15d")!.getRange(
new Date(),
),
});
<DateRangePicker value={value} onChange={setValue} />;The component is controlled — it never holds its own selection. onChange fires with { preset, range }:
- Choosing a preset →
{ preset: "<key>", range: { from, to } } - Picking days on the calendar →
{ preset: null, range: { from, to } }
Custom presets
Pass your own presets. Each resolves to a concrete range relative to now, so it stays correct regardless of when the picker opens. Supply translated labels here to localize the rail. Use the now prop to anchor presets to a fixed clock (handy when working against historical data). Build ranges with date-fns helpers.
import { addDays, endOfDay, startOfDay } from "date-fns";
const presets: DateRangePreset[] = [
{
key: "24h",
label: "Last 24 hours",
getRange: (now) => ({ from: addDays(now, -1), to: now }),
},
{
key: "7d",
label: "Last 7 days",
getRange: (now) => ({
from: startOfDay(addDays(now, -6)),
to: endOfDay(now),
}),
},
{
key: "30d",
label: "Last 30 days",
getRange: (now) => ({
from: startOfDay(addDays(now, -29)),
to: endOfDay(now),
}),
},
];
<DateRangePicker value={value} onChange={setValue} presets={presets} />;Props
Exports
Build custom preset ranges with date-fns (startOfDay, endOfDay, addDays).