# DatePicker

A typed date input with a calendar popup, built on DateField and Calendar.

- Page: https://www.effectcn.com/components/date-picker
- Kind: Headless component with parts
- File: `src/ui/date-picker.ts`
- Built on: `DatePicker` from `@effect/ui-kit`

## Installation

1. Set up the project once: the packages, Tailwind CSS, the theme tokens and the `cn` helper. See [Installation](https://www.effectcn.com/docs/installation.md).
2. Copy the file below to `src/ui/date-picker.ts`. The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
3. Import it.

`src/ui/date-picker.ts`

```ts
import type { Component } from "@effect/ui"
import type { Html } from "@effect/ui/html"
import type { DatePicker } from "@effect/ui-kit"
import type { DateTime } from "effect"
import { cn } from "#lib/cn"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"
import { DateFieldInput, type DateFieldSize } from "#ui/date-field"

/** What the kit's `DatePicker` passes to its `view` closure. */
export type DatePickerParts = Component.Headless.Parts<typeof DatePicker>

/**
 * The box around the input and its trigger. It isn't a kit part, so inside a
 * `Field` it can be the `focusWithin` wrapper: `field.focusWithin(DatePickerControl(h))`.
 */
export const DatePickerControl = <Msg>(h: Html.H<Msg>, options: { readonly class?: string | undefined } = {}) =>
  h.div.class(cn("relative inline-flex w-44 items-center", options.class))

/** The typed date, with room for the trigger. Inside a `Field`, pass `element: field.member(h.input)`. */
export const DatePickerInput = <Msg>(
  h: Html.H<Msg>,
  datePicker: DatePickerParts,
  options: {
    readonly size?: DateFieldSize | undefined
    readonly class?: string | undefined
    readonly element?: Html.H<Msg>["input"] | undefined
  } = {}
) => DateFieldInput(h, datePicker, { ...options, class: cn("w-full pe-9", options.class) })

/** The calendar button at the end of the input. */
export const DatePickerTrigger = <Msg>(
  h: Html.H<Msg>,
  datePicker: DatePickerParts,
  options: { readonly class?: string | undefined } = {}
) =>
  datePicker.trigger(
    h.button.class(
      cn(
        "absolute end-1 inline-grid size-6.5 place-items-center rounded-sm text-muted-foreground " +
          "transition-colors duration-150 hover:bg-accent hover:text-foreground " +
          "data-[popup-open]:bg-accent data-[popup-open]:text-foreground " +
          "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 data-[disabled]:hover:bg-transparent " +
          "[&_svg]:size-4",
        options.class
      )
    )
  ).children(
    h.el("svg")
      .attrs({ viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2", "aria-hidden": "true" })
      .children(
        h.el("path").attr(
          "d",
          "M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z"
        )
      )
  )

/**
 * The popup, already inside its positioner. Children go in the popup:
 * `DatePickerPopup(h, datePicker).children(DatePickerCalendar(h, datePicker))`.
 * Name it with `.attr("aria-label", ...)`.
 */
export const DatePickerPopup = <Msg>(
  h: Html.H<Msg>,
  datePicker: DatePickerParts,
  options: { readonly class?: string | undefined } = {}
) =>
  datePicker.popup(
    h.div.class(
      cn(
        "flex origin-[var(--transform-origin)] gap-3 rounded-lg border bg-popover p-3 shadow-float outline-none " +
          "transition-[opacity,scale] duration-150 starting:scale-95 starting:opacity-0",
        options.class
      )
    )
  ).forward((popup) => datePicker.positioner(h.div.class("z-50")).children(popup))

/** The calendar of the popup: header and grid, as `#ui/calendar` draws them. */
export const DatePickerCalendar = <Msg>(
  h: Html.H<Msg>,
  datePicker: DatePickerParts,
  options: {
    readonly unavailable?: ((date: DateTime.Utc) => boolean) | undefined
    readonly class?: string | undefined
  } = {}
) =>
  CalendarRoot(h, datePicker, { variant: "ghost", class: options.class }).children(
    CalendarHeader(h, datePicker),
    CalendarGrid(h, datePicker, { unavailable: options.unavailable })
  )
```

```ts
import { DatePickerControl, DatePickerInput, DatePickerTrigger, DatePickerPopup, DatePickerCalendar } from "#ui/date-picker"
```

## Usage

Type a date, or open the calendar with the button. Picking a day closes the popup.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DatePicker } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import {
  DatePickerCalendar,
  DatePickerControl,
  DatePickerInput,
  DatePickerPopup,
  DatePickerTrigger
} from "#ui/date-picker"

const DatePickerBasicMessage = UiSchema.TaggedEnum({
  PickedDatePickerDate: { date: Schema.NullOr(Schema.DateTimeUtc) },
  RequestedOpenDatePicker: {},
  RequestedCloseDatePicker: {},
  RequestedDatePickerMonth: { month: Schema.DateTimeUtc }
})

const today = DateTime.makeUnsafe("2026-11-26T00:00:00Z")

export const DatePickerBasic = Html.component({
  name: "DatePickerBasic",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc,
    open: Schema.Boolean
  }),
  message: DatePickerBasicMessage
}).make({
  init: () => ({ date: today, month: today, open: false }),
  update: {
    PickedDatePickerDate: ($, { date }) => $.evolve({ date }),
    RequestedOpenDatePicker: ($) => $.evolve({ open: true }),
    RequestedCloseDatePicker: ($) => $.evolve({ open: false }),
    RequestedDatePickerMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    DatePicker(h, {
      props: { value: model.date, open: model.open, month: model.month, today, locale: "en-US", sideOffset: 6 },
      onEmit: {
        Picked: ({ date }) => DatePickerBasicMessage.PickedDatePickerDate({ date }),
        RequestedOpen: () => DatePickerBasicMessage.RequestedOpenDatePicker(),
        RequestedClose: () => DatePickerBasicMessage.RequestedCloseDatePicker(),
        RequestedMonth: ({ month }) => DatePickerBasicMessage.RequestedDatePickerMonth({ month })
      },
      view: (datePicker) =>
        DatePickerControl(h).children(
          DatePickerInput(h, datePicker).attr("aria-label", "Date"),
          DatePickerTrigger(h, datePicker),
          DatePickerPopup(h, datePicker).attr("aria-label", "Choose date").children(
            DatePickerCalendar(h, datePicker)
          )
        )
    })
})
```

## Examples

### In a field

A Field adds the label, a description and the consumer's error; focus may move into the popup.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DatePicker, Field } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import {
  DatePickerCalendar,
  DatePickerControl,
  DatePickerInput,
  DatePickerPopup,
  DatePickerTrigger
} from "#ui/date-picker"

const DatePickerLabelledMessage = UiSchema.TaggedEnum({
  PickedDatePickerDate: { date: Schema.NullOr(Schema.DateTimeUtc) },
  RequestedOpenDatePicker: {},
  RequestedCloseDatePicker: {},
  RequestedDatePickerMonth: { month: Schema.DateTimeUtc }
})

const today = DateTime.makeUnsafe("2026-11-26T00:00:00Z")

export const DatePickerLabelled = Html.component({
  name: "DatePickerLabelled",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc,
    open: Schema.Boolean
  }),
  message: DatePickerLabelledMessage
}).make({
  init: () => ({ date: null, month: today, open: false }),
  update: {
    PickedDatePickerDate: ($, { date }) => $.evolve({ date }),
    RequestedOpenDatePicker: ($) => $.evolve({ open: true }),
    RequestedCloseDatePicker: ($) => $.evolve({ open: false }),
    RequestedDatePickerMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    Field(h, {
      props: { invalid: model.date === null, described: true },
      view: (field) =>
        DatePicker(h, {
          props: {
            value: model.date,
            open: model.open,
            month: model.month,
            today,
            locale: "en-US",
            name: "departure",
            required: true,
            disabled: field.disabled,
            sideOffset: 6
          },
          onEmit: {
            Picked: ({ date }) => DatePickerLabelledMessage.PickedDatePickerDate({ date }),
            RequestedOpen: () => DatePickerLabelledMessage.RequestedOpenDatePicker(),
            RequestedClose: () => DatePickerLabelledMessage.RequestedCloseDatePicker(),
            RequestedMonth: ({ month }) => DatePickerLabelledMessage.RequestedDatePickerMonth({ month })
          },
          view: (datePicker) =>
            field.root(h.div.class("flex flex-col items-start gap-1.5")).children(
              // `focusWithin` writes no id, so the label points at the input itself.
              h.label.class("text-[13px] font-medium").attr("for", "date-picker-departure").children("Departure"),
              field.focusWithin(DatePickerControl(h, { class: "w-56" })).children(
                DatePickerInput(h, datePicker, {
                  element: field.member(h.input.attr("id", "date-picker-departure"))
                }),
                DatePickerTrigger(h, datePicker),
                DatePickerPopup(h, datePicker).attr("aria-label", "Departure").children(
                  DatePickerCalendar(h, datePicker)
                )
              ),
              field.description(h.p.class("m-0 text-xs text-muted-foreground")).children("Type a date or pick one from the calendar."),
              field.error(h.div.class("text-xs text-destructive"))?.children("Choose a departure date.")
            )
        })
    })
})
```

### Bounded

min and max apply to the calendar and to typed dates.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DatePicker } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import {
  DatePickerCalendar,
  DatePickerControl,
  DatePickerInput,
  DatePickerPopup,
  DatePickerTrigger
} from "#ui/date-picker"

const DatePickerBoundedMessage = UiSchema.TaggedEnum({
  PickedDatePickerDate: { date: Schema.NullOr(Schema.DateTimeUtc) },
  RequestedOpenDatePicker: {},
  RequestedCloseDatePicker: {},
  RequestedDatePickerMonth: { month: Schema.DateTimeUtc }
})

const today = DateTime.makeUnsafe("2026-11-26T00:00:00Z")
const lastDay = DateTime.add(today, { days: 14 })

export const DatePickerBounded = Html.component({
  name: "DatePickerBounded",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc,
    open: Schema.Boolean
  }),
  message: DatePickerBoundedMessage
}).make({
  init: () => ({ date: null, month: today, open: false }),
  update: {
    PickedDatePickerDate: ($, { date }) => $.evolve({ date }),
    RequestedOpenDatePicker: ($) => $.evolve({ open: true }),
    RequestedCloseDatePicker: ($) => $.evolve({ open: false }),
    RequestedDatePickerMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    DatePicker(h, {
      // A typed date outside the bounds commits nothing and the input reverts.
      props: {
        value: model.date,
        open: model.open,
        month: model.month,
        today,
        min: today,
        max: lastDay,
        locale: "en-US",
        sideOffset: 6
      },
      onEmit: {
        Picked: ({ date }) => DatePickerBoundedMessage.PickedDatePickerDate({ date }),
        RequestedOpen: () => DatePickerBoundedMessage.RequestedOpenDatePicker(),
        RequestedClose: () => DatePickerBoundedMessage.RequestedCloseDatePicker(),
        RequestedMonth: ({ month }) => DatePickerBoundedMessage.RequestedDatePickerMonth({ month })
      },
      view: (datePicker) =>
        h.div.class("flex flex-col items-center gap-3").children(
          DatePickerControl(h).children(
            DatePickerInput(h, datePicker).attr("aria-label", "Pickup date"),
            DatePickerTrigger(h, datePicker),
            DatePickerPopup(h, datePicker).attr("aria-label", "Pickup date").children(
              DatePickerCalendar(h, datePicker)
            )
          ),
          h.p.class("m-0 text-xs text-muted-foreground tabular-nums").children("Pickup is possible within the next 14 days.")
        )
    })
})
```

### With presets

Buttons in the popup write the date, the month and open in one update.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DatePicker } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { Button } from "#ui/button"
import {
  DatePickerCalendar,
  DatePickerControl,
  DatePickerInput,
  DatePickerPopup,
  DatePickerTrigger
} from "#ui/date-picker"

const DatePickerPresetsMessage = UiSchema.TaggedEnum({
  PickedDatePickerDate: { date: Schema.NullOr(Schema.DateTimeUtc) },
  RequestedOpenDatePicker: {},
  RequestedCloseDatePicker: {},
  RequestedDatePickerMonth: { month: Schema.DateTimeUtc },
  ClickedPreset: { days: Schema.Number }
})

const today = DateTime.makeUnsafe("2026-11-26T00:00:00Z")

const presets = [
  { label: "Today", days: 0 },
  { label: "Tomorrow", days: 1 },
  { label: "In 3 days", days: 3 },
  { label: "In a week", days: 7 },
  { label: "In a month", days: 30 }
]

export const DatePickerPresets = Html.component({
  name: "DatePickerPresets",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc,
    open: Schema.Boolean
  }),
  message: DatePickerPresetsMessage
}).make({
  init: () => ({ date: null, month: today, open: false }),
  update: {
    PickedDatePickerDate: ($, { date }) => $.evolve({ date }),
    RequestedOpenDatePicker: ($) => $.evolve({ open: true }),
    RequestedCloseDatePicker: ($) => $.evolve({ open: false }),
    RequestedDatePickerMonth: ($, { month }) => $.evolve({ month }),
    ClickedPreset: ($, { days }) => {
      const date = DateTime.add(today, { days })
      return $.evolve({ date, month: DateTime.startOf(date, "month"), open: false })
    }
  },
  view: (h, model) =>
    DatePicker(h, {
      props: {
        value: model.date,
        open: model.open,
        month: model.month,
        today,
        min: today,
        locale: "en-US",
        sideOffset: 6
      },
      onEmit: {
        Picked: ({ date }) => DatePickerPresetsMessage.PickedDatePickerDate({ date }),
        RequestedOpen: () => DatePickerPresetsMessage.RequestedOpenDatePicker(),
        RequestedClose: () => DatePickerPresetsMessage.RequestedCloseDatePicker(),
        RequestedMonth: ({ month }) => DatePickerPresetsMessage.RequestedDatePickerMonth({ month })
      },
      view: (datePicker) =>
        DatePickerControl(h).children(
          DatePickerInput(h, datePicker).attr("aria-label", "Remind me on"),
          DatePickerTrigger(h, datePicker),
          DatePickerPopup(h, datePicker).attr("aria-label", "Remind me on").children(
            h.div.class("flex min-w-28 flex-col gap-0.5 border-e pe-3").children(
              ...presets.map(({ days, label }) =>
                Button(h, {
                  variant: "ghost",
                  size: "sm",
                  class: "justify-start",
                  onPress: DatePickerPresetsMessage.ClickedPreset({ days })
                }).children(label)
              )
            ),
            DatePickerCalendar(h, datePicker)
          )
        )
    })
})
```

### Disabled and read-only

Disabled suppresses everything; read-only still opens the calendar for browsing.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DatePicker } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import {
  DatePickerCalendar,
  DatePickerControl,
  DatePickerInput,
  DatePickerPopup,
  DatePickerTrigger
} from "#ui/date-picker"

const DatePickerDisabledMessage = UiSchema.TaggedEnum({
  RequestedOpenDatePicker: {},
  RequestedCloseDatePicker: {},
  RequestedDatePickerMonth: { month: Schema.DateTimeUtc }
})

const today = DateTime.makeUnsafe("2026-11-26T00:00:00Z")

export const DatePickerDisabled = Html.component({
  name: "DatePickerDisabled",
  model: Schema.Struct({ month: Schema.DateTimeUtc, open: Schema.Boolean }),
  message: DatePickerDisabledMessage
}).make({
  init: () => ({ month: today, open: false }),
  update: {
    RequestedOpenDatePicker: ($) => $.evolve({ open: true }),
    RequestedCloseDatePicker: ($) => $.evolve({ open: false }),
    RequestedDatePickerMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    h.div.class("flex flex-wrap justify-center gap-4").children(
      DatePicker(h, {
        key: "disabled",
        props: { value: today, open: false, month: today, today, locale: "en-US", disabled: true },
        view: (datePicker) =>
          h.div.class("flex flex-col items-start gap-1.5").children(
            h.span.class("text-[13px] font-medium text-muted-foreground").children("Disabled"),
            DatePickerControl(h).children(
              DatePickerInput(h, datePicker).attr("aria-label", "Disabled date"),
              DatePickerTrigger(h, datePicker),
              DatePickerPopup(h, datePicker)
            )
          )
      }),
      // Read-only still opens, and the calendar can be browsed.
      DatePicker(h, {
        key: "read-only",
        props: {
          value: today,
          open: model.open,
          month: model.month,
          today,
          locale: "en-US",
          readOnly: true,
          sideOffset: 6
        },
        onEmit: {
          Picked: () => undefined,
          RequestedOpen: () => DatePickerDisabledMessage.RequestedOpenDatePicker(),
          RequestedClose: () => DatePickerDisabledMessage.RequestedCloseDatePicker(),
          RequestedMonth: ({ month }) => DatePickerDisabledMessage.RequestedDatePickerMonth({ month })
        },
        view: (datePicker) =>
          h.div.class("flex flex-col items-start gap-1.5").children(
            h.span.class("text-[13px] font-medium").children("Read-only"),
            DatePickerControl(h).children(
              DatePickerInput(h, datePicker).attr("aria-label", "Read-only date"),
              DatePickerTrigger(h, datePicker),
              DatePickerPopup(h, datePicker).attr("aria-label", "Read-only date").children(
                DatePickerCalendar(h, datePicker)
              )
            )
          )
      })
    )
})
```

## Anatomy

The parts the `view` closure receives. Apply each one to an element of your own.

| Part | Description |
| --- | --- |
| `input` | The <input>, the DateField's typed date. The popup anchors to it. |
| `trigger` | The <button> that opens and closes the popup. |
| `positioner` | A <div>, a manual popover placed next to the input. |
| `popup` | A <div> with role="dialog" inside the positioner. Give it an aria-label. |
| `root, heading, previous, next, grid, weekday, cell, day` | The Calendar's parts, drawn inside the popup. |
| `open, side, align` | Values: whether the popup shows and where it ended up. |
| `pattern, weeks, weekdays, month, title` | Values of DateField and Calendar. |

## API reference

### Props

| Prop | Type | Description |
| --- | --- | --- |
| `value` | `DateTime \| null` | The date, or null for none. |
| `open` | `boolean` | Whether the popup shows. Required and controlled. |
| `month` | `DateTime` | Any date in the month the calendar shows. |
| `today` | `DateTime` | The current day. |
| `min / max` | `DateTime` | Inclusive bounds for the calendar and for typed dates. |
| `locale` | `string \| string[]` | The locale of the input and the calendar. |
| `name / form / required` | `string / string / boolean` | As DateField takes them. |
| `disabled` | `boolean` | Disables the input, the trigger and the calendar. |
| `readOnly` | `boolean` | The popup opens for browsing, but nothing asks for a date. |
| `side / align / sideOffset` | `Positioning options` | Where the popup goes: bottom and start by default. |

### Events

| Event | Description |
| --- | --- |
| `Picked` | { date }: a typed commit or a day press within the bounds; null when cleared. |
| `RequestedOpen` | { reason }: the trigger was pressed. |
| `RequestedClose` | { reason }: a day press, Escape, the trigger, an outside press or focus leaving. |
| `RequestedMonth` | { month }: calendar navigation, or a pick outside the shown month. |

### Data attributes

| Attribute | Description |
| --- | --- |
| `data-popup-open` | On trigger: the popup is open. |
| `data-open / data-closed` | On positioner and popup. |
| `data-side / data-align` | On positioner and popup: the placement after collisions. |
| `data-disabled / data-readonly` | On trigger, input and the calendar's parts. |
| `data-selected, data-today, ...` | On day, as Calendar lists them. |

## Related components

- [Button](https://www.effectcn.com/components/button.md): A native button that can stay focusable while disabled.
- [Calendar](https://www.effectcn.com/components/calendar.md): A controlled month grid for picking one date, with full keyboard navigation.
- [DateField](https://www.effectcn.com/components/date-field.md): A text input that formats and parses a calendar date for a locale and commits it on blur or Enter.
