# Date Range

A composable date range picker with synchronized presets, two-month calendar selection, and ISO text input.

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown variants are available by appending `.md` to any URL or sending an `Accept: text/markdown` header. An agent skill is available at [/.well-known/agent-skills/site-skill.md](/.well-known/agent-skills/site-skill.md).

<ComponentPreview name="date-range" align="start">
  <DateRangeDemo />
</ComponentPreview>

## Installation [#installation]

```bash
npx shadcn@latest add https://motif.sweetretry.com/r/date-range.json
```

Installs shadcn [Calendar](https://ui.shadcn.com/docs/components/calendar) (`react-day-picker`) without modifying `components/ui/calendar.tsx`, and passes a custom root to avoid remounting the grid on hover.

## Usage [#usage]

```tsx
import {
  DateRange,
  DateRangeCalendar,
  DateRangeConfirm,
  DateRangeContent,
  DateRangeFooter,
  DateRangeShortcuts,
  DateRangeTrigger,
} from "@/components/ui/motif/date-range";

export function ReportRange() {
  const [range, setRange] = useState<DateRangeValue>();

  return (
    <DateRange max={new Date()} onValueChange={setRange} value={range}>
      <DateRangeTrigger />
      <DateRangeContent>
        <DateRangeCalendar />
        <DateRangeFooter>
          <DateRangeShortcuts />
          <DateRangeConfirm />
        </DateRangeFooter>
      </DateRangeContent>
    </DateRange>
  );
}
```

`value`, `defaultValue`, and `onValueChange` only emit complete `{ from, to }` ranges; partial selections stay internal to the draft.

## Interaction & input [#interaction--input]

- **Commit rules**: Shortcuts commit and close immediately; calendar selection and typed input update a shared draft and require <kbd>Enter</kbd> or `DateRangeConfirm` to commit. <kbd>Escape</kbd> or clicking outside discards the draft.
- **Text input**: Accepts `YYYY-MM-DD ~ YYYY-MM-DD` (ISO order for locale-neutral SSR consistency). Invalid syntax, nonexistent dates, reversed ranges, or out-of-bounds dates set `aria-invalid` on blur or <kbd>Enter</kbd>.
- **Trigger props**: `DateRangeTrigger` defaults to `w-64` and forwards input props; call `e.preventDefault()` in `onBlur` to bypass the picker's built-in blur handler.

## Shortcuts [#shortcuts]

```tsx
import { DateRange, lastDays } from "@/components/ui/motif/date-range";

const PRESETS = [
  lastDays("today", "Today", 1),
  lastDays("7d", "Last 7 days", 7),
  lastDays("30d", "Last 30 days", 30),
  {
    id: "this-month",
    label: "This month",
    range: (today) => ({
      from: new Date(today.getFullYear(), today.getMonth(), 1),
      to: today,
    }),
  },
];

<DateRange presets={PRESETS} />;
```

- `range(today)` receives the current date on each interaction; `lastDays(id, label, days)` includes today in the count.
- Presets outside `min`/`max` are automatically disabled. Omit `DateRangeShortcuts` or pass `presets={[]}` for a bare calendar:

<ComponentPreview name="date-range" align="start">
  <DateRangeCalendarDemo />
</ComponentPreview>

## Bounds & locale [#bounds--locale]

```tsx
<DateRange max={new Date()} min={new Date("2025-01-01")} />
```

- `min` and `max` disable out-of-bounds days and stop month navigation; each month always renders 6 weeks to prevent height jumps.
- To avoid SSR/client midnight mismatches, set relative initial values (like "last 7 days") after mount.
- `locale` accepts a `react-day-picker` locale for month/weekday names and week start; trigger, shortcut, and confirm labels are passed as props or children:

<ComponentPreview name="date-range" align="start">
  <DateRangeChineseDemo />
</ComponentPreview>

```tsx
import { zhCN } from "react-day-picker/locale";

<DateRange locale={zhCN} presets={PRESETS}>
  <DateRangeTrigger calendarLabel="打开日历" />
  <DateRangeContent>
    <DateRangeCalendar />
    <DateRangeFooter>
      <DateRangeShortcuts aria-label="快捷选择" />
      <DateRangeConfirm>确定</DateRangeConfirm>
    </DateRangeFooter>
  </DateRangeContent>
</DateRange>;
```

## Parts [#parts]

| Part                 | Draws                                                             |
| -------------------- | ----------------------------------------------------------------- |
| `DateRange`          | Nothing but the popover; owns the draft and the pending first day |
| `DateRangeTrigger`   | The field and the icon button                                     |
| `DateRangeContent`   | The panel, as wide as its calendar                                |
| `DateRangeCalendar`  | The shadcn calendar as a range, two months                        |
| `DateRangeFooter`    | The row under the calendar, and the rule above it                 |
| `DateRangeShortcuts` | The presets; nothing when there are none                          |
| `DateRangeConfirm`   | OK; disabled until the draft is a whole range                     |

`useDateRange` exposes the same context state for custom parts.

## Responsive & accessibility [#responsive--accessibility]

- Below `sm`, renders only the second month so keyboard focus stays within visible days; outside-month padding days are non-interactive.
- Built on `react-day-picker` grid navigation (arrows, <kbd>Home</kbd>/<kbd>End</kbd>, <kbd>PageUp</kbd>/<kbd>PageDown</kbd>); days and active presets use `aria-pressed`.

## Component source [#component-source]

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/index.tsx"
  title="index.tsx"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/parts.tsx"
  title="parts.tsx"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/range-calendar.tsx"
  title="range-calendar.tsx"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/context.ts"
  title="context.ts"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/format.ts"
  title="format.ts"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/presets.ts"
  title="presets.ts"
/>

<ComponentSource
  name="date-range"
  src="registry/new-york/date-range/types.ts"
  title="types.ts"
/>
