# Calendar

A controlled month grid for picking one date, with full keyboard navigation.

- Page: https://www.effectcn.com/components/calendar
- Kind: Headless component with parts
- File: `src/ui/calendar.ts`
- Built on: `Calendar` 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/calendar.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/calendar.ts`

```ts
import type { Component } from "@effect/ui"
import type { Html } from "@effect/ui/html"
import type { Calendar } from "@effect/ui-kit"
import { DateTime } from "effect"
import { cn } from "#lib/cn"

/** What the kit's `Calendar` passes to its `view` closure. A `DatePicker`'s parts fit too. */
export type CalendarParts = Pick<
  Component.Headless.Parts<typeof Calendar>,
  "root" | "heading" | "previous" | "next" | "grid" | "weekday" | "cell" | "day" | "weeks" | "weekdays" | "title"
>

const rootVariants = {
  card: "rounded-lg border bg-card p-3 shadow-sm",
  ghost: ""
} as const

const navClass = "inline-grid size-7 shrink-0 place-items-center rounded-sm border border-input bg-background " +
  "text-foreground transition-colors duration-150 hover:bg-accent data-[disabled]:cursor-not-allowed " +
  "data-[disabled]:opacity-40 data-[disabled]:hover:bg-background [&_svg]:size-3.5 rtl:[&_svg]:rotate-180"

const cellClass = "p-0 text-center"

const dayClass = "inline-grid size-8 place-items-center rounded-sm p-0 text-[13px] text-foreground tabular-nums " +
  "transition-colors duration-150 hover:bg-accent focus-visible:relative focus-visible:outline-offset-1 " +
  "not-data-[selected]:data-[today]:bg-accent data-[today]:font-semibold " +
  "not-data-[selected]:data-[outside-month]:text-muted-foreground not-data-[selected]:data-[outside-month]:opacity-55 " +
  "data-[selected]:bg-brand data-[selected]:text-brand-foreground data-[selected]:hover:bg-brand " +
  "data-[unavailable]:cursor-not-allowed data-[unavailable]:text-muted-foreground data-[unavailable]:line-through " +
  "data-[unavailable]:hover:bg-transparent " +
  "data-[disabled]:cursor-not-allowed data-[disabled]:text-muted-foreground data-[disabled]:opacity-35! " +
  "data-[disabled]:hover:bg-transparent"

const Chevron = <Msg>(h: Html.H<Msg>, d: string) =>
  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", d))

/** The element around the header and the grid. `variant: "ghost"` drops the card, for a calendar inside one. */
export const CalendarRoot = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: { readonly variant?: keyof typeof rootVariants | undefined; readonly class?: string | undefined } = {}
) =>
  calendar.root(
    h.div.class(
      cn(
        "inline-flex w-fit flex-col gap-2.5 text-[13px] outline-none data-[disabled]:opacity-60",
        rootVariants[options.variant ?? "card"],
        options.class
      )
    )
  )

/** The month and year. It labels the grid, so keep it in the tree: hide it with `class: "sr-only"`. */
export const CalendarHeading = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: { readonly class?: string | undefined } = {}
) =>
  calendar.heading(h.div.class(cn("flex-1 text-center text-[13px] font-medium", options.class))).children(
    calendar.title
  )

export const CalendarPrevious = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: { readonly class?: string | undefined } = {}
) => calendar.previous(h.button.class(cn(navClass, options.class))).children(Chevron(h, "M15 6l-6 6 6 6"))

export const CalendarNext = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: { readonly class?: string | undefined } = {}
) => calendar.next(h.button.class(cn(navClass, options.class))).children(Chevron(h, "M9 6l6 6-6 6"))

/** The usual header: previous, the title, next. Compose the three parts yourself for another layout. */
export const CalendarHeader = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: { readonly class?: string | undefined } = {}
) =>
  h.div.class(cn("flex min-h-7 items-center gap-1", options.class)).children(
    CalendarPrevious(h, calendar),
    CalendarHeading(h, calendar),
    CalendarNext(h, calendar)
  )

/**
 * The whole table: the weekday row and a button for every day.
 * `unavailable` strikes a day through: it stays focusable but can't be picked.
 */
export const CalendarGrid = <Msg>(
  h: Html.H<Msg>,
  calendar: CalendarParts,
  options: {
    readonly unavailable?: ((date: DateTime.Utc) => boolean) | undefined
    readonly class?: string | undefined
    /** Replace the look of every cell or day, as `#ui/range-calendar` does. */
    readonly cellClass?: string | undefined
    readonly dayClass?: string | undefined
  } = {}
) =>
  calendar.grid(h.table.class(cn("border-separate border-spacing-x-0 border-spacing-y-0.5", options.class))).children(
    h.thead(
      h.tr(
        ...calendar.weekdays.map((weekday, index) =>
          calendar.weekday({
            element: h.th.class("h-6 w-8 p-0 text-xs font-normal text-muted-foreground"),
            index
          }).children(weekday.short)
        )
      )
    ),
    h.tbody(
      ...calendar.weeks.map((week) =>
        h.tr(
          ...week.map((date) =>
            calendar.cell({ element: h.td.class(options.cellClass ?? cellClass), date }).children(
              calendar.day({
                element: h.button.class(options.dayClass ?? dayClass),
                date,
                unavailable: options.unavailable?.(date)
              }).children(String(DateTime.getPartUtc(date, "day")))
            )
          )
        )
      )
    )
  )
```

```ts
import { CalendarRoot, CalendarHeading, CalendarPrevious, CalendarNext, CalendarHeader, CalendarGrid } from "#ui/calendar"
```

## Usage

The selected date and the shown month both live in the Model.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"

const CalendarBasicMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc }
})

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

export const CalendarBasic = Html.component({
  name: "CalendarBasic",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc
  }),
  message: CalendarBasicMessage
}).make({
  init: () => ({ date: today, month: today }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    Calendar(h, {
      props: { value: model.date, month: model.month, today, locale: "en-US" },
      onEmit: {
        Picked: ({ date }) => CalendarBasicMessage.PickedCalendarDate({ date }),
        RequestedMonth: ({ month }) => CalendarBasicMessage.RequestedCalendarMonth({ month })
      },
      view: (calendar) =>
        CalendarRoot(h, calendar).children(
          CalendarHeader(h, calendar),
          CalendarGrid(h, calendar)
        )
    })
})
```

## Examples

### Min and max

Days outside the bounds are disabled, and keyboard focus stops at them.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"

const CalendarBoundsMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc }
})

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

export const CalendarBounds = Html.component({
  name: "CalendarBounds",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc
  }),
  message: CalendarBoundsMessage
}).make({
  init: () => ({ date: null, month: today }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-3.5").children(
      Calendar(h, {
        // Days outside min and max are disabled, and so are the arrows past them.
        props: { value: model.date, month: model.month, today, min: today, max: lastDay, locale: "en-US" },
        onEmit: {
          Picked: ({ date }) => CalendarBoundsMessage.PickedCalendarDate({ date }),
          RequestedMonth: ({ month }) => CalendarBoundsMessage.RequestedCalendarMonth({ month })
        },
        view: (calendar) =>
          CalendarRoot(h, calendar).children(
            CalendarHeader(h, calendar),
            CalendarGrid(h, calendar)
          )
      }),
      h.p.class("text-xs text-muted-foreground tabular-nums").children("Bookable from today and 30 days ahead.")
    )
})
```

### Unavailable days

Mark single days as unavailable from the view, here every weekend.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"

const CalendarUnavailableMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc }
})

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

const isWeekend = (date: DateTime.Utc) => {
  const weekDay = DateTime.getPartUtc(date, "weekDay")
  return weekDay === 0 || weekDay === 6
}

export const CalendarUnavailable = Html.component({
  name: "CalendarUnavailable",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc
  }),
  message: CalendarUnavailableMessage
}).make({
  init: () => ({ date: null, month: today }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-3.5").children(
      Calendar(h, {
        props: { value: model.date, month: model.month, today, locale: "en-US" },
        onEmit: {
          Picked: ({ date }) => CalendarUnavailableMessage.PickedCalendarDate({ date }),
          RequestedMonth: ({ month }) => CalendarUnavailableMessage.RequestedCalendarMonth({ month })
        },
        view: (calendar) =>
          CalendarRoot(h, calendar).children(
            CalendarHeader(h, calendar),
            CalendarGrid(h, calendar, { unavailable: isWeekend })
          )
      }),
      h.p.class("text-xs text-muted-foreground tabular-nums").children("Weekends keep their place in the arrow-key order but can't be picked.")
    )
})
```

### Month and year controls

The month is a prop, so selects and a Today button can jump anywhere.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { Button } from "#ui/button"
import { CalendarGrid, CalendarHeading, CalendarNext, CalendarPrevious, CalendarRoot } from "#ui/calendar"

const CalendarControlsMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc },
  PickedMonthSelect: { month: Schema.Number },
  PickedYearSelect: { year: Schema.Number },
  ClickedToday: {}
})

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

const months = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
]

const years = Array.from({ length: 11 }, (_, index) => 2021 + index)

const select = "h-7 rounded-sm border border-input bg-background px-1.5 text-[13px] hover:bg-accent"

export const CalendarControls = Html.component({
  name: "CalendarControls",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc
  }),
  message: CalendarControlsMessage
}).make({
  init: () => ({ date: today, month: DateTime.startOf(today, "month") }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month }),
    PickedMonthSelect: ($, { month }) => $.evolve({ month: DateTime.setPartsUtc({ month }) }),
    PickedYearSelect: ($, { year }) => $.evolve({ month: DateTime.setPartsUtc({ year }) }),
    ClickedToday: ($) => $.evolve({ date: today, month: DateTime.startOf(today, "month") })
  },
  view: (h, model) =>
    Calendar(h, {
      props: {
        value: model.date,
        month: model.month,
        today,
        min: DateTime.makeUnsafe("2021-01-01T00:00:00Z"),
        max: DateTime.makeUnsafe("2031-12-31T00:00:00Z"),
        locale: "en-US"
      },
      onEmit: {
        Picked: ({ date }) => CalendarControlsMessage.PickedCalendarDate({ date }),
        RequestedMonth: ({ month }) => CalendarControlsMessage.RequestedCalendarMonth({ month })
      },
      view: (calendar) =>
        CalendarRoot(h, calendar).children(
          h.div.class("flex items-center gap-1").children(
            CalendarPrevious(h, calendar),
            // The selects stand in for the title, which stays for screen readers.
            CalendarHeading(h, calendar, { class: "sr-only" }),
            h.div.class("flex flex-1 justify-center gap-1").children(
              h.select.class(select).attr("aria-label", "Month")
                .value(String(DateTime.getPartUtc(calendar.month, "month")))
                .on("change", ({ value }) => CalendarControlsMessage.PickedMonthSelect({ month: Number(value) }))
                .children(
                  ...months.map((name, index) => h.option.attr("value", String(index + 1)).children(name))
                ),
              h.select.class(select).attr("aria-label", "Year")
                .value(String(DateTime.getPartUtc(calendar.month, "year")))
                .on("change", ({ value }) => CalendarControlsMessage.PickedYearSelect({ year: Number(value) }))
                .children(
                  ...years.map((year) => h.option.attr("value", String(year)).children(String(year)))
                )
            ),
            CalendarNext(h, calendar)
          ),
          CalendarGrid(h, calendar),
          h.div.class("flex items-center justify-between gap-2 border-t pt-2.5").children(
            h.span.class("text-xs text-muted-foreground tabular-nums").children(
              model.date === null ? "No date picked" : DateTime.formatIsoDateUtc(model.date)
            ),
            Button(h, { variant: "outline", size: "sm", onPress: CalendarControlsMessage.ClickedToday() }).children("Today")
          )
        )
    })
})
```

### In a card

A borderless calendar inside a card that prints the picked date.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { Button } from "#ui/button"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"

const CalendarCardMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc },
  ClickedClear: {}
})

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

// The calendar's dates are midnight UTC, so format them in UTC.
const longDate = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "UTC" })

export const CalendarCard = Html.component({
  name: "CalendarCard",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc
  }),
  message: CalendarCardMessage
}).make({
  init: () => ({ date: DateTime.add(today, { days: 4 }), month: today }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month }),
    ClickedClear: ($) => $.evolve({ date: null })
  },
  view: (h, model) =>
    h.section.class("flex w-fit flex-col gap-3.5 rounded-lg border bg-card p-4 shadow-sm").children(
      h.div(
        h.h3.class("m-0 text-sm font-semibold").children("Schedule delivery"),
        h.p.class("m-0 mt-0.5 text-[13px] text-muted-foreground").children(
          "Pick the day the courier should arrive."
        )
      ),
      Calendar(h, {
        props: { value: model.date, month: model.month, today, min: today, locale: "en-US" },
        onEmit: {
          Picked: ({ date }) => CalendarCardMessage.PickedCalendarDate({ date }),
          RequestedMonth: ({ month }) => CalendarCardMessage.RequestedCalendarMonth({ month })
        },
        view: (calendar) =>
          CalendarRoot(h, calendar, { variant: "ghost" }).children(
            CalendarHeader(h, calendar),
            CalendarGrid(h, calendar)
          )
      }),
      h.div.class("flex min-h-7 items-center justify-between gap-2 border-t pt-3").children(
        model.date === null
          ? h.span.class("text-[13px] text-muted-foreground").children("No delivery day yet")
          : h.span.class("text-[13px] font-medium tabular-nums").children(
            DateTime.formatIntl(model.date, longDate)
          ),
        Button(h, {
          variant: "ghost",
          size: "sm",
          props: { disabled: model.date === null },
          onPress: CalendarCardMessage.ClickedClear()
        }).children("Clear")
      )
    )
})
```

### Locale and week start

Titles, weekday names and the first day of the week follow the locale.

```ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Calendar } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { Button } from "#ui/button"
import { CalendarGrid, CalendarHeader, CalendarRoot } from "#ui/calendar"

const Locale = Schema.Literals(["en-US", "de-DE", "fr-FR", "ja-JP", "pt-BR"])
const WeekStart = Schema.Literals(["locale", "sunday", "monday"])

const CalendarLocaleMessage = UiSchema.TaggedEnum({
  PickedCalendarDate: { date: Schema.DateTimeUtc },
  RequestedCalendarMonth: { month: Schema.DateTimeUtc },
  PickedLocale: { locale: Locale },
  PickedWeekStart: { weekStart: WeekStart }
})

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

const weekStarts = [
  { value: "locale", label: "Locale default" },
  { value: "sunday", label: "Sunday" },
  { value: "monday", label: "Monday" }
] as const

export const CalendarLocale = Html.component({
  name: "CalendarLocale",
  model: Schema.Struct({
    date: Schema.NullOr(Schema.DateTimeUtc),
    month: Schema.DateTimeUtc,
    locale: Locale,
    weekStart: WeekStart
  }),
  message: CalendarLocaleMessage
}).make({
  init: () => ({ date: today, month: today, locale: "de-DE", weekStart: "locale" }),
  update: {
    PickedCalendarDate: ($, { date }) => $.evolve({ date }),
    RequestedCalendarMonth: ($, { month }) => $.evolve({ month }),
    PickedLocale: ($, { locale }) => $.evolve({ locale }),
    PickedWeekStart: ($, { weekStart }) => $.evolve({ weekStart })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-3.5").children(
      h.div.class("flex flex-wrap items-center justify-center gap-1").children(
        ...Locale.literals.map((locale) =>
          Button(h, {
            size: "sm",
            variant: model.locale === locale ? "secondary" : "ghost",
            onPress: CalendarLocaleMessage.PickedLocale({ locale })
          }).attr("aria-pressed", String(model.locale === locale)).children(locale)
        )
      ),
      Calendar(h, {
        props: {
          value: model.date,
          month: model.month,
          today,
          locale: model.locale,
          // Left out, the week starts where the locale says it does.
          ...(model.weekStart === "locale" ? {} : { firstDayOfWeek: model.weekStart === "sunday" ? 0 : 1 })
        },
        onEmit: {
          Picked: ({ date }) => CalendarLocaleMessage.PickedCalendarDate({ date }),
          RequestedMonth: ({ month }) => CalendarLocaleMessage.RequestedCalendarMonth({ month })
        },
        view: (calendar) =>
          CalendarRoot(h, calendar).children(
            CalendarHeader(h, calendar),
            CalendarGrid(h, calendar)
          )
      }),
      h.div.class("flex flex-wrap items-center justify-center gap-1").children(
        h.span.class("me-1.5 text-xs text-muted-foreground tabular-nums").children("Week starts on"),
        ...weekStarts.map(({ label, value }) =>
          Button(h, {
            size: "sm",
            variant: model.weekStart === value ? "secondary" : "ghost",
            onPress: CalendarLocaleMessage.PickedWeekStart({ weekStart: value })
          }).attr("aria-pressed", String(model.weekStart === value)).children(label)
        )
      )
    )
})
```

## Anatomy

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

| Part | Description |
| --- | --- |
| `root` | Any element around the header and the grid. Keep it mounted across month changes. |
| `heading` | Any element showing calendar.title; it labels the grid. |
| `previous` | The <button> that asks for the previous month. |
| `next` | The <button> that asks for the next month. |
| `grid` | The <table> with role="grid". |
| `weekday` | A <th> of the header row: weekday({ element, index }). |
| `cell` | A <td> of the body: cell({ element, date }). |
| `day` | The <button> in each cell: day({ element, date, unavailable? }). |
| `weeks, weekdays, month, title` | Values: the grid's dates in whole weeks, the weekday names, the first of the month and its title. |

## API reference

### Props

| Prop | Type | Description |
| --- | --- | --- |
| `value` | `DateTime \| null` | The selected date, normalized to midnight UTC. |
| `month` | `DateTime` | Any date in the month to show. Controlled. |
| `today` | `DateTime` | The current day, passed in so rendering stays pure. |
| `min / max` | `DateTime` | Inclusive bounds. Days outside are natively disabled. |
| `firstDayOfWeek` | `0 \| 1 \| ... \| 6` | Sunday is 0. Defaults to the locale's first day. |
| `locale` | `string \| string[]` | BCP 47 tag for titles, weekday names and day labels. |
| `disabled` | `boolean` | Disables every button and all interaction. |
| `readOnly` | `boolean` | Keeps focus and navigation but suppresses Picked. |

### Events

| Event | Description |
| --- | --- |
| `Picked` | { date }: the user pressed an enabled, available day. |
| `RequestedMonth` | { month }: an arrow or the keyboard asked for another month. |

### Data attributes

| Attribute | Description |
| --- | --- |
| `data-selected` | On day: the day is value. |
| `data-today` | On day: the day is today. |
| `data-outside-month` | On day: the day belongs to the previous or next month. |
| `data-unavailable` | On day: drawn with unavailable: true. |
| `data-focused` | On day: the day has keyboard focus. |
| `data-disabled` | On day, previous, next, root, heading and grid. |
| `data-readonly` | On root, heading and grid. |

## Related components

- [Button](https://www.effectcn.com/components/button.md): A native button that can stay focusable while disabled.
