We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content

Directory

Calendar Component

The Calendar component provides an intuitive interface for date selection. It features smooth animations, internationalization, keyboard navigation, and comprehensive accessibility support.

  • Check Circle

    Interactive Calendar: Visual date selection with month/year navigation

  • Check Circle

    Smart Date Management: Handles date constraints and validations

  • Check Circle

    Keyboard Navigation: Full keyboard support for date selection

  • Check Circle

    Localization: Adapts to user’s locale settings

  • Check Circle

    Accessibility: Built with ARIA support and screen reader compatibility

  • Arrow Keys: Navigate between days
  • PageUp/PageDown: Previous/Next month
  • Enter/Space: Select focused date
  • Tab: Navigate focusable elements
  • Escape: Close month 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 />

Create a simple calendar with default settings:

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar class="w-min" />

Specify an initial selected date as a Date object:

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar value={new Date(2024, 0, 24)} />

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.

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar mode="multiple" value={[new Date(2024, 0, 24), new Date(2024, 0, 25)]}/>

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.

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar mode="multiple" maxSelectable={3} />

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.

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar mode="range" />

Specify an initial selected date range as a Date object:

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar value={{ start: new Date(2025, 2, 24), end: new Date(2025, 2, 28) }} mode="range" />

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.

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar mode="range" minRange={3} maxRange={5} />

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:

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar
availableDateRange={{ start: new Date(2024, 2, 1), end: new Date(2024, 7, 21) }}
/>

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.

Su
Mo
Tu
We
Th
Fr
Sa
<Calendar availableDaysOfWeek={[0, 1, 3, 4]} />

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.

Su
Mo
Tu
We
Th
Fr
Sa
Calendar Mode
Waiting for events to be emitted… (Click on the calendar to see the events)
Calendar Event Details
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 Extends <div> HTMLElement
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)

The calendar component provides a set of functions to interact with the calendar:

Calendar Component Functions
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)
CalendarDate
export type CalendarDate = Date | Date[] | { start: Date | undefined; end: Date | undefined };

The Calendar component exposes several methods for programmatic control. Below are interactive examples of how to use each method.

Sets the selected date(s) programmatically.

Expects a Date object when in single mode or an array of Date objects when in multiple mode.

Set Date Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>('#set-date-calendar')
).then((calendar) => {
if (!calendar) return;
document
.querySelector<HTMLButtonElement>('#set-date-button')
?.addEventListener('click', () => {
calendar.dates = new Date();
});
});
Su
Mo
Tu
We
Th
Fr
Sa

Clears the current selection.

Reset Date Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>('#reset-date-calendar')
).then((calendar) => {
document
.querySelector<HTMLButtonElement>('#reset-date-button')
?.addEventListener('click', () => {
calendar?.resetDate();
});
});
Su
Mo
Tu
We
Th
Fr
Sa

Defines a date range where selection is allowed.

Set Available Range Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>('#set-available-range-calendar')
).then((calendar) => {
if (!calendar) return;
document
.querySelector<HTMLButtonElement>('#set-available-range-button')
?.addEventListener('click', () => {
const today = new Date();
const endDate = new Date(today);
endDate.setDate(endDate.getDate() + 7);
calendar.availableDateRange = {
start: today,
end: endDate,
};
});
});
Su
Mo
Tu
We
Th
Fr
Sa

Specifies individual dates that are available for selection.

Set Available Dates Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>('#set-available-dates-calendar')
).then((calendar) => {
if (!calendar) return;
document
.querySelector<HTMLButtonElement>('#set-available-dates-button')
?.addEventListener('click', () => {
const dates = [];
const today = new Date();
// Get all weekends in the current month
const currentMonth = today.getMonth();
const date = new Date(today.getFullYear(), currentMonth, 1);
while (date.getMonth() === currentMonth) {
if (date.getDay() === 0 || date.getDay() === 6) {
dates.push(new Date(date));
}
date.setDate(date.getDate() + 1);
}
calendar.availableDates = dates;
});
});
Su
Mo
Tu
We
Th
Fr
Sa

Specifies dates that should be disabled for selection.

Set Unavailable Dates Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>(
'#unavailable-dates-this-month-calendar'
)
).then((calendar) => {
if (!calendar) return;
document
.querySelector<HTMLButtonElement>(
'#set-unavailable-dates-this-month-button'
)
?.addEventListener('click', () => {
const dates = [];
const today = new Date();
const currentMonth = today.getMonth();
const date = new Date(today.getFullYear(), currentMonth, 1);
while (date.getMonth() === currentMonth) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}
calendar.unavailableDates = dates;
});
});
Su
Mo
Tu
We
Th
Fr
Sa

Sets which days of the week are available for selection (0-6, where 0 is Sunday).

Set Available Days Example
import type { CalendarElement } from '@/components/NST-components/UI/calendar/data';
import { ready } from '@/components/NST-components/base';
ready(
document.querySelector<CalendarElement>('#available-days-of-week-calendar')
).then((calendar) => {
if (!calendar) return;
document
.querySelector<HTMLButtonElement>('#set-available-days-of-week-button')
?.addEventListener('click', () => {
const days = [1, 3, 5];
calendar.availableDaysOfWeek = days;
});
});
Su
Mo
Tu
We
Th
Fr
Sa

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.

Su
Mo
Tu
We
Th
Fr
Sa
<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!
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes