# Date Picker

A date field that opens a calendar, for one date, several dates or a range.

A date field with a calendar in a popup. The field reads as words in the locale: a date ("Mar 10, 2025") or a range as one phrase ("Mar 10 – 28, 2025", "Mar 28 – Apr 2, 2025", "Dec 30, 2025 – Jan 2, 2026"), with the calendar icon at its start. A press anywhere on the field opens the calendar and leaves the caret in the text, so a date can still be typed: the shown text, the locale's numeric date, ISO, month names, a range with a dash or "to"; it is read on Enter and when focus leaves. The calendar is the shared popup with the calendar's views (days, months, years); it grows out of the field from the pointer and is instant from the keyboard. Choosing a date writes it and closes the popup. Single dates, several dates or a range, min / max, unavailable days, week start and locale. Behaviour, focus and ARIA come from the Zag date-picker machine, through Ark UI; the text is the component's own (helper `date-text.ts`).

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `date-picker` machine, through Ark UI

## Add

```bash
npx fndn add date-picker
```

It writes the folder `date-picker/` into the project's ui folder (`index.ts`, `date-picker.tsx`, `DatePicker.vue`, `DatePicker.svelte`, `date-picker.recipe.ts`), and what it is built from: [Calendar](/components/calendar.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { DatePicker } from "@/components/ui/date-picker";
```

```vue
import { DatePicker } from "@/components/ui/date-picker";
```

```svelte
import { DatePicker } from "$lib/components/ui/date-picker";
```

## Parts

In the DOM the root carries `data-slot="date-picker"` and each part `data-slot="date-picker-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `date-picker` |  | The engine's root; data-state=open\|closed. Never narrower than its dates (min-width: min-content), so a caller's width widens the field but never cuts a date. |
| `label` | `label` | `date-picker-label` | optional |  |
| `control` | `div` | `date-picker-control` |  | The shared control (`lib/control.ts`), field kind: its sizes (xs 24 · sm 28 · md 32 · lg 36) and looks (outline, subtle, ghost) holding the input and the icon. A text field: the accent ring shows for every kind of focus and while the calendar is open. data-invalid, data-disabled from the engine. |
| `input` | `input` | `date-picker-input` |  | One text input over the whole control, as wide as its text (field-sizing: content) and growing into a wider field. Shows the dates in words in the locale; a range is one phrase (Intl formatRange). role=combobox, aria-haspopup=dialog, aria-expanded; it is the engine's trigger (focus comes back to it). Placeholder "Select date" / "Select dates". dir=auto; 16px on coarse pointers. With a name, one hidden input per date carries the ISO date. |
| `icon` | `span` | `date-picker-icon` |  | The calendar icon (Phosphor calendar-blank, bold) at the field's start, 14px ink-3 like an input group's icon, drawn over the input's start padding (control padding + 14px + 8px gap); takes no pointer, aria-hidden. Mirrors to the right end right-to-left. |
| `positioner` | `div` | `date-picker-positioner` |  | Placed by the engine under the control; rendered in a portal. Exposes --transform-origin, --available-height. |
| `content` | `div` | `date-picker-content` |  | role=application, named by triggerLabel; the shared popup surface (`lib/popup.ts`) with 8px padding, holding the calendar's views (the Calendar adapter's CalendarViews: header, grid, dates). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string[]` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). Selected dates as ISO strings ("2025-03-12"). single: one; range: the start and the end. |
| `selectionMode` | `"single" \| "multiple" \| "range"` | `"single"` | range: a start and an end, shown as one phrase in the one input; the calendar stays open until the end is chosen. multiple: the dates joined by a middle dot. |
| `label` | `string` |  | Visible label; otherwise pass aria-label. |
| `placeholder` | `string` |  | Shown while no date is chosen. Without it: "Select date" ("Select dates" for a range). |
| `triggerLabel` | `string` | `"Choose date"` | The calendar popup's accessible name (the field itself is the trigger). |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The shared control's sizes (`lib/control.ts`), dimensions only: xs 24px, sm 28px, md 32px, lg 36px. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `min` | `string` |  | Earliest date that can be chosen (ISO). |
| `max` | `string` |  | Latest date that can be chosen (ISO). |
| `unavailable` | `string[]` |  | Dates that cannot be chosen (ISO). Also takes isDateUnavailable(date). |
| `defaultFocusedValue` | `string` |  | The date the calendar opens on when no value is set. Without it: today. |
| `startOfWeek` | `number` |  | 0 Sunday … 6 Saturday. Without it: the locale's. |
| `locale` | `string` |  | BCP 47 tag for the field's words and digits, the calendar's names and the typed forms it reads. Without it: the LocaleProvider's. |
| `timeZone` | `string` |  | Where "today" is taken. Without it: the browser's. |
| `fixedWeeks` | `boolean` | `true` |  |
| `disabled` | `boolean` | `false` |  |
| `readOnly` | `boolean` | `false` |  |
| `invalid` | `boolean` | `false` |  |
| `required` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name: one hidden input per date carries it, as an ISO date ("2025-03-12"). |
| `placement` | `string` | `"bottom-start"` | Preferred side; flips when there is no room. |
| `defaultOpen` | `boolean` | `false` | The calendar is open at mount: it lands with no entrance (nothing plays on first paint) and takes no focus; until it first closes it stays on its placement's side (no flip), in the room its host reserved. For an example or a page whose subject is the calendar; it closes as usual (Escape, a press outside, a choice). |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string[]) ISO dates |
| `openChange` | (open: boolean) |

## States

`hover`, `focus`, `open`, `selected`, `disabled`, `invalid`, `read-only`

## Keyboard and accessibility

Role: combobox (the text input, aria-haspopup=dialog) + dialog-like grid (role=application).

Keyboard:

- Tab reaches the input (one tab stop; Chrome selects its text, so typing replaces it)
- Typing writes a date or a range; the calendar follows what is read so far; Enter or leaving the field takes it (text that is not a date, or an unavailable date, gives the field its value back). Escape while typing restores the value
- Arrow Down opens the calendar at once (no animation) with focus on the selected date, or the focused one
- In the calendar: the calendar's keys (arrows, Page Up / Down, Home / End, Enter / Space), all instant
- Choosing a date closes the calendar (in range mode, the end date does) and focus returns to the input
- Escape closes the calendar and returns focus to the input; Tab moves through the calendar's header and grid

Notes:

- Give it a visible label or an aria-label; the input carries the name.
- A press anywhere on the field (the icon included) opens the calendar and keeps the caret in the text; a tap on a touch screen opens it without the on-screen keyboard (focus goes to the calendar), and a tap while it is open is for typing.
- Opening and closing from the keyboard are instant; the pointer gets the popup's scale-in out of the field.

## Motion

- popup: the shared popup surface (`lib/popup.ts`): opacity + scale from motion.popup-scale out of the field, duration.popup, easing.out-strong; closes at duration.fast; instant when opened or closed from the keyboard
- control: fill over duration.fast, ring over duration.base; nothing else in the field moves (the text changes at once: it is a value)
- calendar: the calendar's own motion (colour on hover and selection, press scale from the pointer; paging instant)

## Tokens

`color.field`, `color.inset`, `color.surface`, `color.hover`, `color.ink`, `color.ink-3`, `color.accent`, `color.accent-tint`, `color.red`, `color.red-tint`, `shadow.hairline`, `shadow.raised`, `radius.control`, `radius.chip`, `radius.card`, `text.sm`, `text.xs`, `duration.fast`, `duration.base`, `duration.popup`, `easing.out-strong`, `motion.popup-scale`

## Dependencies

- [Calendar](/components/calendar.md)

## Examples

### Due date

React · `due-date.tsx`

```tsx
import { DatePicker } from "@/components/ui/date-picker";

export default function DueDate() {
  return <DatePicker label="Due date" name="dueDate" defaultValue={["2026-12-12"]} style={{ width: 220 }} />;
}
```

Vue · `due-date.vue`

```vue
<script setup lang="ts">
import { DatePicker } from "@/components/ui/date-picker";
</script>

<template>
  <DatePicker label="Due date" name="dueDate" :defaultValue="['2026-12-12']" style="width: 220px" />
</template>
```

Svelte · `due-date.svelte`

```svelte
<script lang="ts">
  import { DatePicker } from "$lib/components/ui/date-picker";
</script>

<DatePicker label="Due date" name="dueDate" defaultValue={["2026-12-12"]} style="width: 220px" />
```

### Booking: a stay

React · `booking.tsx`

```tsx
import { DatePicker } from "@/components/ui/date-picker";

/* nights already booked cannot be chosen */
const booked = ["2026-12-21", "2026-12-22", "2026-12-23"];

export default function Booking() {
  return (
    <DatePicker
      label="Stay"
      selectionMode="range"
      defaultValue={["2026-12-10", "2026-12-14"]}
      min="2026-12-03"
      unavailable={booked}
      style={{ width: 260 }}
    />
  );
}
```

Vue · `booking.vue`

```vue
<script setup lang="ts">
import { DatePicker } from "@/components/ui/date-picker";

/* nights already booked cannot be chosen */
const booked = ["2026-12-21", "2026-12-22", "2026-12-23"];
</script>

<template>
  <DatePicker
    label="Stay"
    selectionMode="range"
    :defaultValue="['2026-12-10', '2026-12-14']"
    min="2026-12-03"
    :unavailable="booked"
    style="width: 260px"
  />
</template>
```

Svelte · `booking.svelte`

```svelte
<script lang="ts">
  import { DatePicker } from "$lib/components/ui/date-picker";

  /* nights already booked cannot be chosen */
  const booked = ["2026-12-21", "2026-12-22", "2026-12-23"];
</script>

<DatePicker
  label="Stay"
  selectionMode="range"
  defaultValue={["2026-12-10", "2026-12-14"]}
  min="2026-12-03"
  unavailable={booked}
  style="width: 260px"
/>
```

### Report period in a toolbar

React · `report-period.tsx`

```tsx
import { DatePicker } from "@/components/ui/date-picker";

export default function ReportPeriod() {
  return (
    <div className="flex items-center justify-between gap-4" style={{ width: 360 }}>
      <h3 className="text-sm font-medium text-ink">Revenue</h3>
      <DatePicker size="sm" variant="ghost" aria-label="Report period" selectionMode="range" defaultValue={["2026-12-01", "2026-12-31"]} placement="bottom-end" />
    </div>
  );
}
```

Vue · `report-period.vue`

```vue
<script setup lang="ts">
import { DatePicker } from "@/components/ui/date-picker";
</script>

<template>
  <div class="flex items-center justify-between gap-4" style="width: 360px">
    <h3 class="text-sm font-medium text-ink">Revenue</h3>
    <DatePicker size="sm" variant="ghost" aria-label="Report period" selectionMode="range" :defaultValue="['2026-12-01', '2026-12-31']" placement="bottom-end" />
  </div>
</template>
```

Svelte · `report-period.svelte`

```svelte
<script lang="ts">
  import { DatePicker } from "$lib/components/ui/date-picker";
</script>

<div class="flex items-center justify-between gap-4" style="width: 360px">
  <h3 class="text-sm font-medium text-ink">Revenue</h3>
  <DatePicker size="sm" variant="ghost" aria-label="Report period" selectionMode="range" defaultValue={["2026-12-01", "2026-12-31"]} placement="bottom-end" />
</div>
```

### Appointment: min, max and booked days

React · `appointment.tsx`

```tsx
import { DatePicker } from "@/components/ui/date-picker";

const weekend = (date: string) => [0, 6].includes(new Date(`${date}T12:00:00Z`).getUTCDay());

/* the calendar is the point here, so it starts open: weekends, booked days and the dates outside
 * Dec 1–23 cannot be chosen */
export default function Appointment() {
  return (
    <div style={{ width: 300, height: 340 }}>
      <DatePicker
        label="Appointment"
        placeholder="Pick a weekday"
        defaultOpen
        defaultFocusedValue="2026-12-01"
        min="2026-12-01"
        max="2026-12-23"
        unavailable={["2026-12-09", "2026-12-16"]}
        isDateUnavailable={weekend}
        style={{ width: 220 }}
      />
    </div>
  );
}
```

Vue · `appointment.vue`

```vue
<script setup lang="ts">
import { DatePicker } from "@/components/ui/date-picker";

const weekend = (date: string) => [0, 6].includes(new Date(`${date}T12:00:00Z`).getUTCDay());
</script>

<!-- the calendar is the point here, so it starts open: weekends, booked days and the dates outside
     Dec 1–23 cannot be chosen -->
<template>
  <div style="width: 300px; height: 340px">
    <DatePicker
      label="Appointment"
      placeholder="Pick a weekday"
      defaultOpen
      defaultFocusedValue="2026-12-01"
      min="2026-12-01"
      max="2026-12-23"
      :unavailable="['2026-12-09', '2026-12-16']"
      :isDateUnavailable="weekend"
      style="width: 220px"
    />
  </div>
</template>
```

Svelte · `appointment.svelte`

```svelte
<script lang="ts">
  import { DatePicker } from "$lib/components/ui/date-picker";

  const weekend = (date: string) => [0, 6].includes(new Date(`${date}T12:00:00Z`).getUTCDay());
</script>

<!-- the calendar is the point here, so it starts open: weekends, booked days and the dates outside
     Dec 1–23 cannot be chosen -->
<div style="width: 300px; height: 340px">
  <DatePicker
    label="Appointment"
    placeholder="Pick a weekday"
    defaultOpen
    defaultFocusedValue="2026-12-01"
    min="2026-12-01"
    max="2026-12-23"
    unavailable={["2026-12-09", "2026-12-16"]}
    isDateUnavailable={weekend}
    style="width: 220px"
  />
</div>
```

### Schedule a release

React · `release-date.tsx`

```tsx
import { useState } from "react";
import { DatePicker } from "@/components/ui/date-picker";

export default function ReleaseDate() {
  const [dates, setDates] = useState<string[]>([]);
  return (
    <div className="flex flex-col gap-2" style={{ width: 220 }}>
      <DatePicker label="Release date" value={dates} onValueChange={setDates} defaultFocusedValue="2026-12-01" />
      <span className="text-sm text-ink-2">{dates[0] ? `Saved as ${dates[0]}` : "Not scheduled yet"}</span>
    </div>
  );
}
```

Vue · `release-date.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { DatePicker } from "@/components/ui/date-picker";

const dates = ref<string[]>([]);
</script>

<template>
  <div class="flex flex-col gap-2" style="width: 220px">
    <DatePicker label="Release date" v-model:value="dates" defaultFocusedValue="2026-12-01" />
    <span class="text-sm text-ink-2">{{ dates[0] ? `Saved as ${dates[0]}` : "Not scheduled yet" }}</span>
  </div>
</template>
```

Svelte · `release-date.svelte`

```svelte
<script lang="ts">
  import { DatePicker } from "$lib/components/ui/date-picker";

  let dates = $state<string[]>([]);
</script>

<div class="flex flex-col gap-2" style="width: 220px">
  <DatePicker label="Release date" bind:value={dates} defaultFocusedValue="2026-12-01" />
  <span class="text-sm text-ink-2">{dates[0] ? `Saved as ${dates[0]}` : "Not scheduled yet"}</span>
</div>
```

## Recipe

`date-picker.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { popup } from "../lib/popup";
import { control, controlSize, controlVariant } from "../lib/control";

/* date-picker — a date field with a calendar in a popup, on the Zag date-picker machine.
 *
 * The field is one text input in the shared control recipe (lib/control.ts, field kind: xs 24 · sm 28 ·
 * md 32 · lg 36; outline, subtle, ghost), with the calendar icon at its start (14px, ink-3, as an
 * input group's icon) and the dates in words: "Mar 10, 2025", a range as one phrase, "Mar 10 – 28,
 * 2025" (helper `date-text.ts`). The input covers the whole control and the icon is drawn over
 * its start padding without taking the pointer, so a press anywhere on the field lands in the
 * input: it opens the calendar and leaves the caret there, the dates can still be typed. The field
 * is as wide as its text unless the caller gives it a width, and never narrower: a date is never
 * cut. A text field: the accent ring shows for every kind of focus, and stays while the calendar
 * is open (focus may be in the grid, so the ring follows the root's data-state).
 *
 * The popup is the shared popup surface (it grows out of the field from the pointer, instant from
 * the keyboard) holding the calendar's views (calendar.recipe.ts, rendered by its adapter); 8px of
 * padding instead of the list's 4px, since it holds a grid, not rows. It lives in a portal: nothing
 * below depends on the root being an ancestor. defaultOpen shows it open at mount, landed (no
 * entrance, no focus taken): for a page whose subject is the calendar. */

const ctl = control({ kind: "field" });

/* the input's padding, physical like the control's input (its dir is auto): the icon's side takes
 * the control's padding, the 14px icon and an 8px gap */
const ICON_SIDE = "[:dir(ltr)>&]:pl-[calc(var(--control-px)+22px)] [:dir(rtl)>&]:pr-[calc(var(--control-px)+22px)]";
const END_SIDE = "[:dir(ltr)>&]:pr-(--control-px) [:dir(rtl)>&]:pl-(--control-px)";

export const datePicker = tv({
  slots: {
    /* never narrower than its dates (min-w-min: the input's text is its least width), so a caller's
     * width can widen the field but never cut a date */
    base: "inline-flex flex-col gap-1.5 min-w-min max-w-full",
    label: "text-sm font-medium text-ink",
    /* the shared control recipe (lib/control.ts), field kind; open, it keeps the ring while focus is in
     * the calendar (the root's data-state). The input inside draws the padding. */
    control: [
      ctl.base(), "relative flex items-center px-0 cursor-text",
      "[[data-slot=date-picker][data-state=open]_&]:bg-surface [[data-slot=date-picker][data-state=open]_&]:[box-shadow:0_0_0_1px_var(--fndn-accent),0_0_0_4px_var(--fndn-accent-tint)]",
    ],
    /* the calendar icon: drawn over the input's start padding, it never takes the pointer */
    icon: "pointer-events-none absolute inset-y-0 start-(--control-px) flex items-center text-ink-3 [&>svg]:size-[14px] [&>svg]:shrink-0",
    /* the control's bare input, over the whole control; as wide as its text (or placeholder), and
     * it grows into a wider field */
    input: [ctl.input(), "flex-[1_0_auto] w-auto min-w-auto [field-sizing:content] text-clip", ICON_SIDE, END_SIDE],
    positioner: "",
    content: [popup(), "w-max p-2 [--popup-max-height:480px]"],
  },
  variants: {
    /* the shared control's sizes and looks */
    size: {
      xs: { control: controlSize.xs },
      sm: { control: controlSize.sm },
      md: { control: controlSize.md },
      lg: { control: controlSize.lg },
    },
    variant: {
      outline: { control: controlVariant.outline },
      subtle: { control: controlVariant.subtle },
      ghost: { control: controlVariant.ghost },
    },
    /* open at mount (defaultOpen): the calendar is simply there, with no entrance, until it has been
     * painted once (the adapter clears it after the first paint, lib/paint.ts) */
    landing: { true: { content: "transition-none!" } },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

export type DatePickerRecipeProps = VariantProps<typeof datePicker>;
```
