Directory
- Welcome
Calendar Component
The Calendar component provides an intuitive interface for date selection. It features smooth animations, internationalization, keyboard navigation, and comprehensive accessibility support.
Key Features
-
Interactive Calendar: Visual date selection with month/year navigation
-
Smart Date Management: Handles date constraints and validations
-
Keyboard Navigation: Full keyboard support for date selection
-
Localization: Adapts to user’s locale settings
-
Accessibility: Built with ARIA support and screen reader compatibility
Keyboard Support
Arrow Keys
: Navigate between daysPageUp/PageDown
: Previous/Next monthEnter/Space
: Select focused dateTab
: Navigate focusable elementsEscape
: Close month picker
Date Picker
The Date Picker is a combination of a calendar component and a popover component. Creating an easy to use form element for date selection.
<DatePicker />
Basic Usage
Create a simple calendar with default settings:
<Calendar class="w-min" />
With Initial Value
Specify an initial selected date as a Date object:
<Calendar value={new Date(2024, 0, 24)} />
Multiple Date Selection
The Calendar component also supports multiple date selection. To enable this feature, set the mode
prop to multiple
. Click one date to select it and click another date to select it.
<Calendar mode="multiple" value={[new Date(2024, 0, 24), new Date(2024, 0, 25)]}/>
Multiple Selection Constraints
You can set the maxSelectable
prop to limit the number of dates that can be selected in multiple mode. Selecting more than the maximum will deselect in the order that the dates were selected.
<Calendar mode="multiple" maxSelectable={3} />
Date Range Selection
The Calendar component also supports date range selection. To enable this feature, set the mode
prop to range
. Click one date to select the start date and click another date to select the end date.
<Calendar mode="range" />
With Initial Value
Specify an initial selected date range as a Date object:
<Calendar value={{ start: new Date(2025, 2, 24), end: new Date(2025, 2, 28) }} mode="range" />
Range Selection Constraints
You can set the minRange
and maxRange
props to limit the range of dates that can be selected. The hover higlighting will only be applied to dates within the range. Clicking a date outside of the range will start a new range selection.
<Calendar mode="range" minRange={3} maxRange={5} />
More Date Selection Constraints
Disable Dates Outside of a Range
Limit selectable dates with availableDateRange
prop. If the todays date is outside of the range, the calendar will automatically adjust to the nearest enabled date:
<Calendar availableDateRange={{ start: new Date(2024, 2, 1), end: new Date(2024, 7, 21) }}/>
Available Days of Week
You can set the availableDaysOfWeek
prop to limit the days of the week that can be selected. Start counting from 0 for Sunday and 6 for Saturday.
<Calendar availableDaysOfWeek={[0, 1, 3, 4]} />
Custom Event Listeners
You can listen to the selected custom event emitted by the calendar component, returns the selected date(s) in ISO string format. Read Component Architecture for more information on how to use custom events.
Waiting for events to be emitted… (Click on the calendar to see the events)
CustomEvent<{ id?: string; name?: string; mode?: 'single' | 'multiple' | 'range'; selectedDates?: //selected dates as ISO strings | string //single mode | string[] //multiple mode | { start: string; end: string }; //range mode}>;
Calendar Component Props API
Prop | Type | Default | Description |
---|---|---|---|
availableDaysOfWeek | number[] | Array of available weekdays (0-6) | |
availableDateRange | { start?: Date, end?: Date } | Disable dates outside of this range | |
disabled | boolean | false | Disables the calendar |
maxSelectable | number | Maximum number of dates that can be selected in multiple mode | |
mode | single | multiple | range | single | Date selection mode |
maxRange | number | Maximum number of days that can be selected in range mode | |
minRange | number | Minimum number of days that can be selected in range mode | |
modalProps | HTMLAttributesWithData<"div"> | Props for the calendar modal | |
name | string | Input field name for form submission | |
required | boolean | false | Makes the field required |
value | Date | Date[] | { start: Date; end: Date } | Initial selected date(s) |
Calendar Functions API
The calendar component provides a set of functions to interact with the calendar:
Function | Type | Description |
---|---|---|
set: availableDateRange | ({ start?: Date, end?: Date }) => void | Sets a range of dates that are available for selection, can specify start and end or just one |
set: availableDates | (dates: Date[]) => void | Sets specific dates that are available for selection |
set: unavailableDates | (dates: Date[]) => void | Sets specific dates that are unavailable for selection |
set: dates | (dates: CalendarDate) => void | Sets the currently selected date(s) |
get: dates | () => CalendarDate | undefined | Returns the currently selected date(s) |
set: availableDaysOfWeek | (days: number[]) => void | Sets the days of the week that are available for selection, 0-6 where 0 is Sunday |
set: mode | (mode: "single" | "multiple" | "range") => void | Sets the mode of the calendar and resets the selection |
resetDate | () => void | Resets the calendar selection to no date selected |
resetAvailability | () => void | Resets the calendar availability to full availability |
validate | () => boolean | Validates the calendar selection (does not check if the date is required, only if the date is within the available date range) |
export type CalendarDate = Date | Date[] | { start: Date | undefined; end: Date | undefined };
Function Examples
The Calendar component exposes several methods for programmatic control. Below are interactive examples of how to use each method.
Error State
The calendar component also supports an error state when the state
dataset property is set to error
. The only time that the error state will be set by the component automatically is if available/unavailable dates are modified and a selected date is no longer available.
<Calendar data-state="error" />
Limited Time Launch Sale
Save time and effort with our Calendar component. Get the AstroJS starter template now and access a complete UI toolkit.
GET 60% OFF!