# DatePicker

A field-looking trigger that opens a Calendar in a popover, for picking one date or a range of dates in a form. It takes the same value props as Calendar.

**Dependencies:** npm: 2 direct, 5 total; registry: 4 direct, 4 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Picking one date in a form, next to other fields, with the same field look as Input.
- Limiting the choice to real dates, with disabled days or a min and max date.

### When not to use

- A whole month on the page when the date is the point of the screen. Use Calendar.
- Entering a date as text, such as a birth date the user knows by heart. Use Input.
- Picking a time of day. Use TimePicker.

### Native

The trigger has the field look, and the Calendar opens in a bottom Sheet.

**In a form**

```text
WerewolfDatePicker(
    sheetTitle: "Start date",
    value: date,
    onValueChange: { date = $0 },
    placeholder: "When does it start?"
)
```

```text
WerewolfDatePicker(
    sheetTitle = "Start date",
    value = date,
    onValueChange = { date = it },
    placeholder = "When does it start?",
)
```

Range mode keeps a draft inside the Sheet with Clear and Done buttons.

**Booking dates**

```text
WerewolfDatePicker(
    sheetTitle: "Booking dates",
    mode: .range,
    value: range,
    onValueChange: { range = $0 },
    placeholder: "Check-in to check-out"
)
```

```text
WerewolfDatePicker(
    sheetTitle = "Booking dates",
    mode = WerewolfCalendarMode.Range,
    value = range,
    onValueChange = { range = it },
    placeholder = "Check-in to check-out",
)
```

## Variants

### mode

single picks one Date; range picks a start and an end ({ from, to }) and closes once both are picked.

Values: `single`, `range`. Default: `single`.

### weekStartsOn

The first day of the week in the Calendar: 0 Sunday or 1 Monday. Follows the locale when not set.

Values: `0`, `1`. Default: `undefined`.

### captionLayout

Passed to the Calendar. dropdown is a month select and a year select. dropdown-months and dropdown-years show one select and leave the other as text. label is the month and year as text. With a year select and no minDate or maxDate, the years run from 100 years before today to 100 years after. On native v1, all captionLayout values (label, dropdown, dropdown-months, dropdown-years) render as label until v2.

Values: `label`, `dropdown`, `dropdown-months`, `dropdown-years`. Default: `dropdown`.

### size

Trigger height from size-element-*, the same steps as Input and Select: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Match the Input or Button next to it.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

## Examples

### In a form

The trigger has the field look, and the grid opens in a popover.

```tsx
<DatePicker placeholder="When does it start?" />
```

### Booking dates

A range over two months. The popover stays open after check-in and closes after check-out.

```tsx
<DatePicker mode="range" numberOfMonths={2} placeholder="Check-in to check-out" />
```

## Do and don't

### Do

- Use DatePicker in forms, next to Input fields of the same size.
- Name it with a Label pointing at its id, and describe it with aria-describedby, like any field.
- Disable days that can never be picked, such as past days or closed days, over a minDate and maxDate.

### Don't

- Don't use it when the date is the whole screen; put a Calendar on the page instead.
- Don't put long text in the trigger; it shows one formatted date or one range.
- Don't use it for dates the user knows by heart, such as a birth date; an Input is faster.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `mode` | `enum` | `single`, `range` | `single` | single picks one Date; range picks a start and an end ({ from, to }) and closes once both are picked. |
| `value` | `Date \| DateRange` |  |  | The picked Date, or in range mode the picked range ({ from, to }), when you control it. An explicit undefined clears the selection; omitting value uses internal state. Pair it with onValueChange. |
| `defaultValue` | `Date \| DateRange` |  |  | The Date, or in range mode the range, picked on first render, when you don't control it. Not generated for iOS and Android. |
| `onValueChange` | `(value: Date \| DateRange \| undefined) => void` |  |  | Called with the picked Date, or undefined when the user unpicks it. In range mode only with a complete range, or undefined when a one-day range is cleared. |
| `disabled` | `boolean` |  | `false` | true disables the trigger; a function receives a Date and disables single days in the Calendar, such as weekends or past dates. |
| `invalid` | `boolean` |  | `false` | Marks the value as wrong: sets aria-invalid, which draws the danger border. |
| `minDate` | `string` |  |  | No day before this Date can be picked or navigated to. |
| `maxDate` | `string` |  |  | No day after this Date can be picked or navigated to. |
| `locale` | `string` |  |  | A date-fns Locale object for the trigger text and the Calendar's month and weekday names. The default is enUS. Not generated for iOS and Android. |
| `weekStartsOn` | `enum` | `0`, `1` |  | The first day of the week in the Calendar: 0 Sunday or 1 Monday. Follows the locale when not set. |
| `numberOfMonths` | `number` |  | `1` | How many months the Calendar shows, side by side from sm and stacked below it. Use two for ranges that often cross a month. Not generated for iOS and Android. |
| `captionLayout` | `enum` | `label`, `dropdown`, `dropdown-months`, `dropdown-years` | `dropdown` | Passed to the Calendar. dropdown is a month select and a year select. dropdown-months and dropdown-years show one select and leave the other as text. label is the month and year as text. With a year select and no minDate or maxDate, the years run from 100 years before today to 100 years after. On native v1, all captionLayout values (label, dropdown, dropdown-months, dropdown-years) render as label until v2. Deferred on iOS and Android: dropdown, dropdown-months, dropdown-years. |
| `rangeStartLabel` | `string` |  | `start of range` | Range mode. Added to the start day's accessible name in the Calendar; translate it with the locale. |
| `rangeEndLabel` | `string` |  | `end of range` | Range mode. Added to the end day's accessible name in the Calendar; translate it with the locale. |
| `placeholder` | `string` |  | `Pick a date` | Text shown in the trigger while no date is picked. It also names the dialog when there is no aria-label. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Trigger height from size-element-*, the same steps as Input and Select: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Match the Input or Button next to it. |
| `modal` | `boolean` |  | `false` | Whether the open popover blocks interaction with the rest of the page. Not generated for iOS and Android. |
| `sheetTitle` (required) | `string` |  |  | The name of the Sheet that holds the Calendar. Shown as its title and read by assistive technology. Native only. Only on iOS, Android. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The trigger button: the same field look as Input, in the size you set. |
| `icon` | A decorative Tabler Calendar icon at the start, 16px (20px in xl), secondary. |
| `label` | The picked date, formatted long; in range mode both ends in the medium format joined by an en dash (one date for a one-day range); or the placeholder in secondary text. |
| `hitArea` | Transparent ::before centered on the trigger, never smaller than size-target-min (24px on mouse, 44px on touch), so sm, md and lg stay easy to tap. |
| `content` | The Popover that holds a Calendar. |

## Accessibility

Role: `button`

| Keys | Action |
| --- | --- |
| `Space, Enter` | On the trigger, opens the popover and focuses the selected day, or today. |
| `Escape` | Closes the popover and returns focus to the trigger. In range mode a start without an end is dropped and the value is unchanged. |

- The trigger opens a dialog named by aria-label or the placeholder; inside it, the Calendar grid keeps its own keyboard model.
- A picked date closes the dialog and focus returns to the trigger.
- In range mode the popover keeps a draft: it stays open after the start and sends onValueChange only with a complete range (then closes), or with undefined when the user clears a one-day range. A form value is never start-only. Closing with only a start drops it.
- The trigger accepts id, aria-label and aria-describedby, so a Label and a hint can name and describe it like any field.

## Install

```bash
npx werewolf-ui add date-picker
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add date-picker --platform ios
npx werewolf-ui add date-picker --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
