No results found

Calendar

A customizable date picker that helps users select dates through an intuitive grid interface.

March 2025
SuMoTuWeThFrSa

Selected date:

Features

  • WAI ARIA Calendar design pattern implementation
  • Full keyboard navigation support (arrow keys, home/end, page up/down)
  • Month and year switching controls
  • Optional week numbers display
  • Localization support (dates, labels, formatting)
  • Full/partial week display options
  • Popover positioning and management
  • Date range selection controls
  • Current date highlighting
  • Selected date management
  • Focus management and trap within calendar
  • Custom date formatting
  • Screen reader announcements
  • Date validation and parsing
  • Responsive calendar grid layout

Anatomy

PartDescription
<Calendar.Root>The root calendar component that manages state and provides context
<Calendar.Grid>A component that renders the main calendar grid structure
<Calendar.Header>A component that renders the calendar header section
<Calendar.Previous>A button component that handles navigation to the previous month
<Calendar.Next>A button component that handles navigation to the next month
<Calendar.Title>A component that displays the current month and year
<Calendar.GridDay>A component that renders a single day cell in the calendar grid

Examples

Basic Usage

The calendar component provides a simple way to implement date selection functionality. The example below shows the basic setup with core components and date change handling.

March 2025
SuMoTuWeThFrSa

Selected date:

This example demonstrates:

Component State

Using Component State

The Calendar component's state can be controlled through several props on the Calendar.Root component:

  1. Default Date Set the initial date to display when the calendar loads using the defaultDate prop:
<Calendar.Root defaultDate="2024-03-15">
  {/* Calendar content */}
</Calendar.Root>
  1. Selected Date Track the currently selected date using the date prop:
<Calendar.Root date={selectedDate}>
  {/* Calendar content */}
</Calendar.Root>
  1. Display Options Control what information is shown in the calendar:
<Calendar.Root 
  showWeekNumber={true}  // Show week numbers
  showDaysOfWeek={true}  // Show day names header
  fullWeeks={true}       // Show complete weeks including adjacent month days
>
  {/* Calendar content */}
</Calendar.Root>

State Interactions

The Calendar provides ways to respond to date selection through event handlers:

  1. Date Selection Listen for date changes using the onDateChange$ prop on either the Calendar.Root or Calendar.GridDay components:
<Calendar.Root
  onDateChange$={(date) => {
    console.log('Selected date:', date);
  }}
>
  {/* Calendar content */}
</Calendar.Root>

As shown in the hero example, you can track the selected date in your application state:

const selectedDate = useSignal<LocalDate>();
<Calendar.GridDay
  onDateChange$={$((date) => {
    selectedDate.value = date;
  })}
/>
  1. Popover State Control the calendar popover's open state using the bind:open prop:
const isOpen = useSignal(false);
<Calendar.Root bind:open={isOpen}>
  {/* Calendar content */}
</Calendar.Root>

The calendar component maintains its own internal state for:

Based on the provided implementation and examples, I'll document the Calendar component's configuration options.

Core Configuration

Locale and Date Format

The Calendar component supports localization through the locale prop on Calendar.Root. Currently, it supports:

Initial Date Selection

The calendar can be configured with:

Week Display

The calendar provides two week display modes controlled by the fullWeeks prop:

type WeekDisplayProps = {
  // When true, shows complete weeks including days from adjacent months
  fullWeeks?: boolean; // default: false
}

Advanced Configuration

Date Change Handling

The calendar supports date selection handling through:

type DateChangeProps = {
  onDateChange$?: (date: LocalDate) => void;
}

Where LocalDate is typed as:

type LocalDate = `${number}-${number}-${number}`;

Grid Customization

The calendar grid can be customized through:

Technical Constraints

  1. Date Format Validation
    • Dates must follow the YYYY-MM-DD format
    • Invalid formats will throw an error during initialization
  2. Month Navigation
    • Month navigation is handled internally
    • Year boundaries are automatically managed when navigating between December and January
  3. Focus Management
    • Focus is automatically managed within the calendar grid
    • Only one date cell is focusable at a time (tabIndex management)
    • Focus is preserved during month navigation The calendar maintains internal state for:

API Reference

Calendar Root

Inherits from: <div />

Props

PropTypeDefaultDescription
localeLocale"en"
defaultDateLocalDate-
showWeekNumberbooleanfalse
fullWeeksbooleanfalse
dateLocalDate-
showDaysOfWeekbooleantrue
onDateChange$QRL<(date: LocalDate) => void>-
"bind:open"Signal<boolean>-

Data Attributes

AttributeDescription
data-themeControls the visual theme of the calendar

Calendar Grid Day

Inherits from: <button />

Props

PropTypeDefaultDescription
onDateChange$(date: LocalDate) => void-

Data Attributes

AttributeDescription
data-currentIndicates if this date is the current date
data-selectedIndicates if this date is currently selected
data-valueStores the date value for this calendar cell

Calendar Grid

Inherits from: <button />

Props

PropTypeDefaultDescription
buttonPropsPropsOf<"button">-
onDateChange$QRL<(date: LocalDate) => void>-
daysnumber-
monthsnumber-

Calendar Popover

Inherits from: <div />

Props

PropTypeDefaultDescription
floatingboolean | TPlacement-

Data Attributes

AttributeDescription
data-openIndicates if the popover is currently open
data-closedIndicates if the popover is currently closed

Calendar Header

Inherits from: <header />

Calendar Next

Inherits from: <svg />

Calendar Previous

Inherits from: <svg />

Calendar Title

Inherits from: <div />

Icons

Inherits from: <svg />

Accessibility

Keyboard Interactions

KeyDescription
EnterWhen focus is on a date button, selects that date
SpaceWhen focus is on a date button, selects that date
ArrowDownWhen focus is on a date button, moves focus to the date one week later
ArrowUpWhen focus is on a date button, moves focus to the date one week earlier
ArrowLeftWhen focus is on a date button, moves focus to the previous date
ArrowRightWhen focus is on a date button, moves focus to the next date
HomeWhen focus is on a date button, moves focus to the first date of the current week
EndWhen focus is on a date button, moves focus to the last date of the current week
PageUpWhen focus is on a date button, moves focus to the same date in the previous month
PageDownWhen focus is on a date button, moves focus to the same date in the next month