Skip to content

Calendar

An element for creating interactive calendar.

Showcase

import {
Calendar,
CalendarButton,
CalendarCell,
CalendarGrid,
CalendarHeader,
CalendarTitle
} from '@klnjs/react-calendar'
import classes from './calendar.module.css'
export default () => (
<Calendar
aria-label="Event date"
locale="en-GB"
className={classes.calendar}
>
<CalendarHeader className={classes.header}>
<CalendarButton action="dec" className={classes.button} />
<CalendarTitle className={classes.title} />
<CalendarButton action="inc" className={classes.button} />
</CalendarHeader>
<CalendarGrid className={classes.grid}>
{({ type, date }) => (
<CalendarCell
key={`${type}-${date.toString()}`}
type={type}
date={date}
className={classes.cell}
/>
)}
</CalendarGrid>
</Calendar>
)

Features

  • Controlled or uncontrolled.

  • Multiple selection modes - one, many and range.

  • Keyboard navigation and screen reader support.

  • Supports calendar systems used around the world.

  • Localisation, formatting and right-to-left support.

Installation

The calendar component relies on a temporal polyfill for handling date operations. Ensure the polyfill is included in your project dependencies.

Terminal window
bun install @klnjs/react-calendar temporal-polyfill

Definitions

Components

Calendar

Contains all the parts of a calendar. As the calendar does not have html semantics for providing a label, it is recommended to provide an aria-label to ensure good accessibility.

PropTypeDefault
asChild
boolean
false
autoFocus
boolean
false
calendar
LocaleCalendar
-
defaultValue
CalendarSelectValue
-
disabled
boolean
false
locale
string
en
max
PlainDate
-
min
PlainDate
-
pagination
CalendarPagination
visible
readOnly
boolean
false
select
CalendarSelect
one
value
CalendarSelectValue
-
visibleDuration
CalendarVisibleDuration
{ months: 1 }
onChange
(value: CalendarSelectValue) => void
-

Used to group calendar header components.

PropTypeDefault
asChild
boolean
false

Title

Display a localised title representing the current visible range of the calendar.

PropTypeDefault
asChild
boolean
false

Button

Facilitates navigation in the calendar, to ensure keyboard accessibility the active day will be moved to the best matching day within the visible range.

Use the action to either increment or decrement the visible range. When unit is set to months, it will use root properties pagination and visibleDuration to determine the distance.

The button will be disabled when the action would cause the visible range to land outside of the root properties min and max.

PropTypeDefault
asChild
boolean
false
action
'inc' | 'dec'
-
unit
'months' | 'years'
months

Group

Used to group grid components when working with multiple months.

PropTypeDefault
asChild
boolean
false

Grid

Given the dynamic length of the visible range in the calendar, this utility component provides means to render the cells relevant to the visible range using a render function as child.

The properties week and weekdays can be used to enable week day and week number headers, when both properties are enabled blank cells can occur in the grid. To differentiate between the different cell types, the render function provides a type property.

Set overflow: 'align' to expand overflow at the end of a month, to match the longest possible month. This can prevent misalignment when working with multiple months.

PropTypeDefault
asChild
boolean
false
week
boolean
false
weekdays
boolean
false
overflow
'auto' | 'align'
auto
children
(cell: CalendarGridCellItem ) => ReactNode
-

Cell

Used to render individual cells in the grid, it should be used in conjunction with the grid utility component. Cell will render a different native element based on the type prop.

Cell provides accessibility like localised labeling and keyboard navigation. For sighted users, it provides data-* attributes enabling styling of the different cell types and states.

PropTypeDefault
asChild
boolean
false
type
CalendarCellType
-
date
PlainDate
-
Data attributePresent when
data-cellCalendarCellType
data-todaycell is today
data-tommorowcell is tommorow
data-yesterdaycell is yesterday
data-weekendcell is weekend
data-focusedcell is focused
data-overflowcell is outside month
data-disabledcell is disabled
data-selectedcell is selected
data-week-endcell is last day of week
data-week-startcell is first day of week
data-range-endcell is last day of range
data-range-startcell is first day of range
data-range-betweencell is inside range