Calendar
Pick a date, several dates or a range from one or more months, with unavailable dates, an error message and a loading state.
Installation
npx shadcn@latest add @oakoss/calendarUsage
import { getLocalTimeZone, today } from '@internationalized/date';
import { Calendar } from '#/components/ui/inputs/calendar';
<Calendar aria-label="Delivery date" minValue={today(getLocalTimeZone())} />;Values are @internationalized/date objects (CalendarDate, CalendarDateTime
or ZonedDateTime), which the item installs; React Aria doesn't re-export
today, parseDate or the other helpers. selectionMode="multiple" picks
several dates. A calendar always shows six weeks, so it keeps its height from
month to month; weeksInMonth changes that.
A calendar suits dates close to now. For a date of birth or another distant date, typing is faster, so use a date field instead.
Examples
Range and months
RangeCalendar picks a start and an end. visibleMonths shows several months
side by side, wrapping when there's no room; the steppers sit at the outer
edges, and days a neighboring month repeats are hidden.
Month and year dropdowns
heading replaces the month heading. CalendarMonthYearSelect, installed with
calendar-month-year-select, fills it with month and year dropdowns, once
however many months show. visibleYears sets how many years the list holds,
within minValue and maxValue. The dropdowns take the slots month and
year, so a SelectContext with slots can set one; name both, since React
Aria rejects a slot its context leaves out.
Unavailable dates
isDateUnavailable strikes dates through and skips them. Screen readers hear
an unavailable date only as dimmed, not why, so say the rule in text the
calendar points to with aria-describedby.
Weekends and public holidays are unavailable.
Presets
footer renders below the months, inside the calendar. These presets set a
range and show its month; save a preset's id rather than its dates so that
"Last 7 days" stays relative. Buttons in the footer, heading or a custom layout
keep their own names and handlers, and one with slot="close" closes the
popover or dialog around the calendar.
Maximum range length
While the user picks the end of a range, React Aria passes the start to
isDateUnavailable as a second argument, so days too far from it can be
skipped.
Validation
errorMessage shows while isInvalid is set, and describes the selected day.
Pick a weekend day to see it.
Custom days
renderCell draws a day's contents. It replaces the today dot, so mark today
from isToday (this demo bolds it) or with data-today on the cell. Screen
readers hear only the date, so anything it adds, such as a price or a dot,
needs another way to reach them.
isLoading fades the months behind a spinner named by loadingLabel
("Loading"), for availability fetched as the user changes month.
Custom layout
Children replace the layout, built from CalendarHeader (the steppers around
a heading), CalendarHeading and CalendarGrid; pass offset to a heading
and grid for each further month. This one adds a Today button below the grid.
Accessibility
React Aria renders a grid
of days inside an application, and links it up:
- Name: the calendar is named by
aria-labeland the visible months, and each day by its full date, with "Today" and "selected" in the user's language. - Navigation: the steppers are named "Previous" and "Next" in the user's language, and the month heading is announced when it changes.
- Validation: a day's error comes from
errorMessage; an unavailable or disabled day isaria-disabled.
Days are 36px and grow to 44px wherever a touch pointer is present. Today has a dot, never a fill, so it stays visible inside a range. In Windows High Contrast the selected day takes the system's selection colors, an invalid one takes Mark, and the range keeps a border.
| Key | Action |
|---|---|
| ←, → | Moves to the previous or next day |
| ↑, ↓ | Moves to the same day in the previous or next week |
Home, End | Moves to the first or last day of the month |
PageUp, PageDown | Moves to the previous or next month |
Shift + PageUp/PageDown | Moves to the previous or next year |
Enter, Space | Selects the day, or a range's start then its end |
Escape | Cancels a range picked halfway |
The arrow keys follow the reading direction in right-to-left locales.
Today and the month shown first come from the visitor's clock and time zone. A page rendered on the server, or prerendered on an earlier day, disagrees with the browser, and React reports a hydration error, so render calendars on the client only, as this page does.
API
Calendar
Extends React Aria's Calendar.
Props
Prop
Type
Also accepts all React Aria Calendar props
except visibleDuration, which visibleMonths sets. Children replace the
layout.
RangeCalendar
Extends React Aria's RangeCalendar,
with the same props as Calendar.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always calendar |
data-invalid | The calendar is invalid |
data-disabled | isDisabled is set |
CalendarCell
Extends React Aria's CalendarCell.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always calendar-cell |
data-selected | The day is selected |
data-selection-start | The day starts the range |
data-selection-end | The day ends the range |
data-today | The day is today |
data-unavailable | isDateUnavailable returns true |
data-disabled | The day is out of bounds or disabled |
data-outside-month | The day belongs to another month |
data-invalid | The selected day is invalid |
CalendarMonthYearSelect
Props
Prop
Type
Other parts
| Part | Extends | data-slot |
|---|---|---|
CalendarHeader | A div holding the steppers | calendar-header |
CalendarHeading | React Aria's CalendarHeading | calendar-heading |
CalendarGrid | React Aria's CalendarGrid, with cells | calendar-grid |