# DateRangePicker

> Two date fields with a separator, a popup with presets, two months side by side, the range summary and Apply / Cancel; minDays, maxDays, bounds and range form values.

Canonical: https://salmonui.dev/docs/calendars/date-range-picker

```tsx
import { DateRangePicker, getRangeDays } from '@salmonui/calendars/date-range-picker';
import '@salmonui/calendars/date-range-picker/styles.css';

<DateRangePicker
  label="Stay"
  minDays={2}
  maxDays={14}
  presets={[{ label: 'Next 7 days', value: () => ({ start: today(), end: addDays(today(), 6) }) }]}
  onValueChange={(range) => setRange(range)}
/>;
```

## Value

`value` / `defaultValue` are `DateRange` objects (`{ start, end }`, each a
date input) or `null`; `onValueChange(range, details)` receives
`{ start: PlainDate, end: PlainDate }` and `details.iso`, the ISO 8601
interval `start/end`. `getRangeDays(range)` returns the inclusive
day count.

## The popup

- Two months side by side (`visibleMonths`, default 2); under a narrow
  container one month shows, by a CSS container query, without measuring.
- The first click sets the start, hovering or focusing previews the range
  (`data-in-range`, `data-range-end` on the day cells), the second
  click sets the end and fires `onRangeSelect(range)`. The summary shows the range and its day count with the
  `localeText.selectedDays` plural forms.
- `confirm` (default `true`) commits on Apply; Cancel
  restores. `confirm={false}` commits on the second click.
- `presets: { label, value }[]` render as a listbox; a `value` function is
  evaluated when the popup opens, so "last 7 days" never freezes at server
  render time. Choosing a preset commits and closes.
- `minDays` / `maxDays` disable the days that would make the span too short
  or too long once a start is chosen.
- `isDateDisabled`, `renderDay` and `getDayProps` work as in `DatePicker`;
  `rangeOverDisabled: 'block' | 'allow'` (default `'block'`) decides whether a
  range may span disabled days; with `'block'` the days past the next
  disabled day cannot end the range.
- Alt+Right / Alt+Left move between the presets, the calendars and the
  buttons (mirrored in RTL); Tab follows the same order.

## Fields

The two `DateField`s sit in one labelled group with `separator` between them
(a `ReactNode`, default an en dash). Paste on either field splits
the text on the separator (or an en dash, `/`, ` - `, ` to `) and parses both
ends. `format` applies to both fields. `granularity="month"
| "year"` picks month or year ranges from the year or decade view.
`min`, `max` and `strict` work as in `DatePicker`: with `strict`, an
out-of-range end moves to the nearest bound on blur.

### Live demo: Range options and paste

Live demo: https://salmonui.dev/demos/calendars/date-range-picker-options

A custom separator that also splits pasted text, onRangeSelect before Apply, getRangeDays, a month range picker, strict quarter bounds and weekends and holidays that block or allow a range.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { DateRangePicker, getRangeDays, type DateRange, type PlainDate } from '@salmonui/calendars/date-range-picker';
import { Checkbox } from '@salmonui/inputs/checkbox';

const HOLIDAYS = new Set(['2026-12-24', '2026-12-25', '2026-12-26', '2026-12-31', '2027-01-01']);
const isWeekendOrHoliday = (d: PlainDate): boolean => {
  const day = new Date(Date.UTC(d.year, d.month - 1, d.day)).getUTCDay();
  return day === 0 || day === 6 || HOLIDAYS.has(d.toString());
};

/** Separator and paste, onRangeSelect before Apply, the day count, month ranges, strict bounds and ranges over disabled days. */
export default function DateRangePickerOptionsDemo() {
  const [allowOverDisabled, setAllowOverDisabled] = useState(false);
  const [log, setLog] = useState<string[]>([]);
  const add = (line: string) => setLog((lines) => [line, ...lines].slice(0, 6));
  const describe = (range: DateRange | null) => (range ? `${String(range.start)} to ${String(range.end)} (${getRangeDays(range)} days)` : 'null');
  return (
    <div style={{ display: 'grid', gap: 16 }}>
      <div className="demo-toolbar">
        <Checkbox checked={allowOverDisabled} onCheckedChange={(checked) => setAllowOverDisabled(checked)}>
          Allow ranges over weekends and holidays (<code>rangeOverDisabled=&quot;allow&quot;</code>)
        </Checkbox>
      </div>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'flex-start' }}>
        <DateRangePicker
          label="Sprint"
          description="Paste 2026-12-01 → 2026-12-11 into either field."
          separator=" → "
          defaultValue={{ start: '2026-11-23', end: '2026-12-04' }}
          onRangeSelect={(range) => add(`onRangeSelect: ${describe(range)} (not applied yet)`)}
          onValueChange={(range) => add(`onValueChange: ${describe(range)}`)}
        />
        <DateRangePicker
          label="Working days off"
          description="Weekends and holidays are disabled."
          isDateDisabled={isWeekendOrHoliday}
          rangeOverDisabled={allowOverDisabled ? 'allow' : 'block'}
          getDayProps={(ctx) => (HOLIDAYS.has(ctx.date.toString()) ? { title: 'Public holiday', 'data-holiday': true } : undefined)}
          defaultValue={{ start: '2026-12-21', end: '2026-12-23' }}
          onValueChange={(range) => add(`days off: ${describe(range)}`)}
        />
        <DateRangePicker label="Reporting months" granularity="month" defaultValue={{ start: '2026-01-01', end: '2026-06-01' }} onValueChange={(range) => add(`months: ${range ? `${String(range.start)} to ${String(range.end)}` : 'null'}`)} />
        <DateRangePicker
          label="Within Q1 2027 (strict)"
          description="Typed dates outside Q1 move to the nearest bound on blur."
          min="2027-01-01"
          max="2027-03-31"
          strict
          defaultValue={{ start: '2027-02-01', end: '2027-02-14' }}
          onValueChange={(range) => add(`Q1: ${describe(range)}`)}
        />
      </div>
      <div className="demo-log" aria-live="polite">
        <ol>{log.length ? log.map((line, i) => <li key={`${i}-${line}`}>{line}</li>) : <li>Choose or paste a range</li>}</ol>
      </div>
    </div>
  );
}
```

## Forms

`name` posts one ISO interval (`2026-11-02/2026-11-06`); `startName` and
`endName` post two ISO dates instead.

## API reference

The API reference for
[`@salmonui/calendars/date-range-picker`](https://salmonui.dev/docs/reference/calendars/date-range-picker)
lists every prop, method and type. Open state, focus, forms and events
shared by the pickers are in [Picker behavior and
forms](https://salmonui.dev/docs/calendars/picker-behavior); server rendering, accessibility
coverage and budgets in [SSR, accessibility and
performance](https://salmonui.dev/docs/calendars/platform).

### Live demo: DateRangePicker

Live demo: https://salmonui.dev/demos/calendars/calendar-date-range-picker

Two months side by side, presets, a 2 to 14 day span, the day count and Apply / Cancel.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { DateRangePicker, getRangeDays, toPlainDate, type DateRange, type PlainDate } from '@salmonui/calendars/date-range-picker';

// The local calendar day (toISOString would give the UTC day).
const iso = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
const nextDays = (n: number) => () => {
  const start = new Date();
  return { start: iso(start), end: iso(new Date(start.getFullYear(), start.getMonth(), start.getDate() + n - 1)) };
};
const STAY: DateRange = { start: toPlainDate('2026-11-09') as PlainDate, end: toPlainDate('2026-11-13') as PlainDate };

export default function CalendarDateRangePickerDemo() {
  const [range, setRange] = useState<DateRange | null>(STAY);
  return (
    <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'flex-start' }}>
      <DateRangePicker
        label="Stay"
        defaultValue={STAY}
        minDays={2}
        maxDays={14}
        clearable
        presets={[
          { label: 'Next 7 days', value: nextDays(7) },
          { label: 'Christmas week', value: { start: '2026-12-21', end: '2026-12-27' } },
        ]}
        onValueChange={setRange}
      />
      <DateRangePicker label="Quick range" confirm={false} separator=" to " onValueChange={setRange} />
      <output style={{ flexBasis: '100%' }}>{range ? `${String(range.start)} / ${String(range.end)}: ${getRangeDays(range)} days` : 'No range yet'}</output>
    </div>
  );
}
```
