---
title: "Calendar"
description: "Calendar with accessible presentation and local state."
---

# Calendar

Calendar with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Calendar, CalendarAdvanced from "@rightui/ui"
```

## Usage

Import Calendar, CalendarAdvanced from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, CalendarAdvanced from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state year = 2026
  state month = 3
  state low = "2026-03-09"
  state high = "2026-03-12"
  state single = "2026-03-09"
  action calendarMonth(y: number, m: number)
    year = y
    month = m
  action chooseSingle(start: string, finish: string, values: string[])
    single = start
  action chooseRange(start: string, finish: string, values: string[])
    low = start
    high = finish
  state multipleDates: string[] = ["2026-03-09", "2026-03-12"]
  action chooseMultiple(start: string, finish: string, values: string[])
    multipleDates = values
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      text "Single date with constraints" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      CalendarAdvanced(id="dates-single", low=single, high="", selected=[], mode="single", navigation="both", year=year, month=month, minimum="2026-03-05", maximum="2026-05-25", disabledDates=["2026-03-17"], disabledWeekends=true, disabled=false, changeMonth=calendarMonth, change=chooseSingle)
      text "Date range" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      CalendarAdvanced(id="dates-range", low=low, high=high, selected=[], mode="range", navigation="both", year=year, month=month, minimum="2026-03-01", maximum="2026-05-25", disabledDates=[], disabledWeekends=false, disabled=false, changeMonth=calendarMonth, change=chooseRange)
      text "Disabled" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      CalendarAdvanced(id="dates-disabled", low=single, high="", selected=[], mode="single", navigation="both", year=year, month=month, minimum="", maximum="", disabledDates=[], disabledWeekends=false, disabled=true, changeMonth=calendarMonth, change=chooseSingle)
      text "Multiple selection" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      CalendarAdvanced(id="dates-multiple", low="", high="", selected=multipleDates, mode="multiple", navigation="both", year=year, month=month, minimum="", maximum="", disabledDates=[], disabledWeekends=false, disabled=false, changeMonth=calendarMonth, change=chooseMultiple)

```

## API Reference

| Property | Type |
| --- | --- |
| CalendarAdvanced.id | string |
| CalendarAdvanced.low | string |
| CalendarAdvanced.high | string |
| CalendarAdvanced.selected | string[] |
| CalendarAdvanced.mode | string |
| CalendarAdvanced.navigation | string |
| CalendarAdvanced.year | number |
| CalendarAdvanced.month | number |
| CalendarAdvanced.minimum | string |
| CalendarAdvanced.maximum | string |
| CalendarAdvanced.disabledDates | string[] |
| CalendarAdvanced.disabledWeekends | bool |
| CalendarAdvanced.disabled | bool |
| CalendarAdvanced.changeMonth | action |
| CalendarAdvanced.change | action |
| Calendar.id | string |
| Calendar.value | string |
| Calendar.year | number |
| Calendar.month | number |
| Calendar.changeMonth | action |
| Calendar.end | string |
| Calendar.mode | string |
| Calendar.disabled | bool |
| Calendar.change | action |

## Source

File: packages/ui/calendar.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
use IconButton from "./button.contract"
fn calendarLeap(y: number): bool = y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
fn calendarDays(y: number, m: number): number = m == 2 ? (calendarLeap(y) ? 29 : 28) : ([4, 6, 9, 11].includes(m) ? 30 : 31)
fn calendarOffset(y: number, m: number): number = (floor((m < 3 ? y - 1 : y) + floor((m < 3 ? y - 1 : y) / 4) - floor((m < 3 ? y - 1 : y) / 100) + floor((m < 3 ? y - 1 : y) / 400) + (m == 1 ? 0 : m == 2 ? 3 : m == 3 ? 2 : m == 4 ? 5 : m == 5 ? 0 : m == 6 ? 3 : m == 7 ? 5 : m == 8 ? 1 : m == 9 ? 4 : m == 10 ? 6 : m == 11 ? 2 : 4) + 1)) % 7
fn calendarISO(y: number, m: number, d: number): string = (y < 10 ? "000" : y < 100 ? "00" : y < 1000 ? "0" : "") + y.toString() + "-" + (m < 10 ? "0" : "") + m.toString() + "-" + (d < 10 ? "0" : "") + d.toString()
fn calendarMonth(m: number): string = m == 1 ? "January" : m == 2 ? "February" : m == 3 ? "March" : m == 4 ? "April" : m == 5 ? "May" : m == 6 ? "June" : m == 7 ? "July" : m == 8 ? "August" : m == 9 ? "September" : m == 10 ? "October" : m == 11 ? "November" : "December"
component Calendar
  props
    id: string
    value: string
    year: number
    month: number
    changeMonth: action
    end: string
    mode: string
    disabled: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state hoveredDay = -1
  state active = match parseNumber(value.slice(8, 10)) { case some(d) => max(1, min(31, floor(d))), case none => 1 }
  derive days = calendarDays(year, month)
  derive offset = calendarOffset(year, month)
  derive activeDay = max(1, min(days, active))
  action hoverDay(day: number, inside: bool)
    hoveredDay = inside ? day : -1
  action shiftMonth(delta: number)
    let m = month + delta
    changeMonth(max(1, min(9999, year + (m < 1 ? -1 : m > 12 ? 1 : 0))), m < 1 ? 12 : m > 12 ? 1 : m)
    active = 1
  action select(d: number)
    if not disabled
      change(calendarISO(year, month, d))
      active = d
  action keys(d: number, k: string, e: KeyboardEvent)
    if disabled
      preventDefault()
    else if k == "Enter" or k == " "
      preventDefault()
      select(d)
    else if k == "ArrowLeft" or k == "ArrowRight" or k == "ArrowUp" or k == "ArrowDown" or k == "Home" or k == "End"
      preventDefault()
      let next = k == "Home" ? max(1, d - (offset + d - 1) % 7) : k == "End" ? min(days, d + 6 - (offset + d - 1) % 7) : d + (k == "ArrowLeft" ? -1 : k == "ArrowRight" ? 1 : k == "ArrowUp" ? -7 : 7)
      let m = next < 1 ? (month == 1 ? 12 : month - 1) : next > days ? (month == 12 ? 1 : month + 1) : month
      let y = year + (next < 1 and month == 1 ? -1 : next > days and month == 12 ? 1 : 0)
      let day = next < 1 ? calendarDays(y, m) + next : next > days ? next - days : next
      if next < 1 or next > days
        changeMonth(y, m)
      active = day
      focus(id + "-day-" + day.toString())
    else if k == "PageUp" or k == "PageDown"
      preventDefault()
      let delta = (k == "PageUp" ? -1 : 1) * (e.shiftKey ? 12 : 1)
      let raw = month - 1 + delta
      let y = year + floor(raw / 12)
      let m = ((raw % 12 + 12) % 12) + 1
      let day = min(d, calendarDays(y, m))
      changeMonth(y, m)
      active = day
      focus(id + "-day-" + day.toString())
  action selectOutside(delta: number, day: number)
    let raw = month + delta
    let m = raw < 1 ? 12 : raw > 12 ? 1 : raw
    let y = year + (raw < 1 ? -1 : raw > 12 ? 1 : 0)
    if not disabled
      changeMonth(y, m)
      change(calendarISO(y, m, day))
      active = day
  view
    column id=id testId=id gap="ui.space-16" padding="ui.space-12" width="15.5rem" max-width="100%" box-sizing="border-box" color="ui.foreground" overflow="auto"
      row align-items="center" justify-content="space-between" gap="ui.space-4" min-height="ui.space-32" min-width="14rem"
        IconButton(id=(id + "-previous"), label="Previous month", variant="ghost", disabled=disabled, press=shiftMonth(-1))
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="ui.foreground" stroke-width="ui.stroke-standard" aria-hidden=true
            path d="m15 18-6-6 6-6"
        text (calendarMonth(month) + " " + year.toString()) testId=(id + "-month") font-size="ui.text-sm" font-weight=uiWeights.medium aria-live="polite" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        IconButton(id=(id + "-next"), label="Next month", variant="ghost", disabled=disabled, press=shiftMonth(1))
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="ui.foreground" stroke-width="ui.stroke-standard" aria-hidden=true
            path d="m9 18 6-6-6-6"
      view display="grid" grid-template-columns="repeat(7, minmax(2rem, 1fr))" column-gap="0px" row-gap="ui.space-8" min-width="14rem" role="group" aria-label=(calendarMonth(month) + " " + year.toString())
        for name in ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"] key=name
          text name text-align="center" font-size="ui.text-tiny" color="ui.muted-foreground" height="ui.space-20" display="flex" align-items="center" justify-content="center" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        for cell in range(ceil((offset + days) / 7) * 7) key=cell
          if cell < offset or cell >= offset + days
            button class=Control testId=(id + "-outside-" + cell.toString()) press=selectOutside(cell < offset ? -1 : 1, cell < offset ? calendarDays(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) - offset + cell + 1 : cell - offset - days + 1) disabled=disabled tabindex=-1
              height="ui.space-32" min-height="ui.space-32" padding="0px" border="0" font-size="ui.text-sm" font-weight=uiWeights.regular border-radius="ui.radius-control" background-color="transparent" color="ui.muted-foreground"
              :hover
                background-color="ui.muted"
                color="ui.foreground"
              text (cell < offset ? calendarDays(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) - offset + cell + 1 : cell - offset - days + 1).toString() font-family=uiFontFamily(uiFont) data-rightui-font="body"
          else
            view height="ui.space-32" min-height="ui.space-32" min-width="0px"
              background-color=(mode == "range" and value != "" and end != "" and (value == calendarISO(year, month, cell - offset + 1) or end == calendarISO(year, month, cell - offset + 1)) ? "ui.muted" : "transparent")
              border-radius=(mode == "range" and value != end ? (value == calendarISO(year, month, cell - offset + 1) ? "ui.radius-control 0px 0px ui.radius-control" : end == calendarISO(year, month, cell - offset + 1) ? "0px ui.radius-control ui.radius-control 0px" : "0px") : "ui.radius-control")
              button id=(id + "-day-" + (cell - offset + 1).toString()) testId=(id + "-day-" + (cell - offset + 1).toString()) class=Control
                press=select(cell - offset + 1) key=keys(cell - offset + 1) hover=hoverDay(cell - offset + 1) disabled=disabled tabindex=(activeDay == cell - offset + 1 ? 0 : -1)
                aria-label=calendarISO(year, month, cell - offset + 1) aria-pressed=(value == calendarISO(year, month, cell - offset + 1) or end == calendarISO(year, month, cell - offset + 1))
                height="ui.space-32" min-height="ui.space-32" width="100%" box-sizing="border-box" padding="0px" border="0" font-size="ui.text-sm" font-weight=uiWeights.regular
                border-radius=(mode == "range" and value != "" and end != "" and calendarISO(year, month, cell - offset + 1) > value and calendarISO(year, month, cell - offset + 1) < end ? (cell % 7 == 0 ? "ui.radius-control 0px 0px ui.radius-control" : cell % 7 == 6 ? "0px ui.radius-control ui.radius-control 0px" : "0px") : "ui.radius-control")
                background-color=(value == calendarISO(year, month, cell - offset + 1) or end == calendarISO(year, month, cell - offset + 1) ? "ui.primary" : hoveredDay == cell - offset + 1 ? "ui.muted" : mode == "range" and value != "" and end != "" and calendarISO(year, month, cell - offset + 1) > value and calendarISO(year, month, cell - offset + 1) < end ? "ui.muted" : "transparent")
                color=(value == calendarISO(year, month, cell - offset + 1) or end == calendarISO(year, month, cell - offset + 1) ? "ui.primary-foreground" : "ui.foreground")
                text (cell - offset + 1).toString() font-family=uiFontFamily(uiFont) data-rightui-font="body"

```
