Skip to content

Calendar

A flexible calendar component for date selection. Perfect for date pickers, scheduling, and date-related interfaces.

Installation

bash
npx @zizigy/capsule add Calendar

Usage

Basic Calendar

html
<capsule-calendar></capsule-calendar>

With Selected Date

html
<capsule-calendar value="2024-03-15"></capsule-calendar>

Display Specific Month

html
<capsule-calendar
  display-year="2024"
  display-month="5"
></capsule-calendar>

Date Range Restrictions

You can restrict selectable dates using min-date and max-date attributes.

html
<capsule-calendar
  min-date="2024-01-01"
  max-date="2024-12-31"
>
</capsule-calendar>

Disabled Dates

Disable specific dates by providing a comma-separated list.

html
<capsule-calendar disabled-dates="2024-03-10,2024-03-15,2024-03-20">
</capsule-calendar>

Localization

The calendar supports different locales for weekday names.

html
<capsule-calendar locale="ru-RU"></capsule-calendar>
html
<capsule-calendar locale="de-DE"></capsule-calendar>

Attributes

AttributeTypeDefaultDescription
valuestring''Selected date in YYYY-MM-DD format
display-yearnumbercurrentYear to display
display-monthnumbercurrentMonth to display (0-11)
min-datestringnullMinimum selectable date (YYYY-MM-DD)
max-datestringnullMaximum selectable date (YYYY-MM-DD)
disabled-datesstring''Comma-separated list of disabled dates
localestringen-USLocale for weekday names (e.g., 'ru-RU', 'de')

Methods

setSelectedDate(dateString)

Programmatically set the selected date.

javascript
const calendar = document.querySelector('capsule-calendar');
calendar.setSelectedDate('2024-03-15');

getSelectedDate()

Get the currently selected date.

javascript
const calendar = document.querySelector('capsule-calendar');
const selectedDate = calendar.getSelectedDate();
console.log(selectedDate); // "2024-03-15" or null

setDisplayDate(year, month)

Change the displayed month and year.

javascript
const calendar = document.querySelector('capsule-calendar');
calendar.setDisplayDate(2024, 5); // June 2024 (month is 0-indexed)

getDisplayDate()

Get the currently displayed month and year.

javascript
const calendar = document.querySelector('capsule-calendar');
const { year, month } = calendar.getDisplayDate();
console.log(year, month); // 2024, 5

Events

input

Fired when a date is selected or deselected.

javascript
calendar.addEventListener('input', (e) => {
  console.log('Selected date:', e.detail.value);
  console.log('Type:', e.detail.type); // 'single'
});

change

Fired when the selected date changes.

javascript
calendar.addEventListener('change', (e) => {
  console.log('New value:', e.detail.value);
  console.log('Old value:', e.detail.oldValue);
  console.log('Type:', e.detail.type); // 'single'
});

CSS Parts

The calendar exposes several CSS parts for styling:

PartDescription
weekdaysContainer for weekday headers
days-gridGrid container for day buttons
dayIndividual day button
day todayCurrent day button
day selectedSelected day button
day disabledDisabled day button
day other-monthDays from previous/next month

Custom Styling Example

css
capsule-calendar::part(day selected) {
  background: #007bff;
  color: white;
}

capsule-calendar::part(day today) {
  border-color: #28a745;
  color: #28a745;
}

capsule-calendar::part(day):hover {
  background: #f0f0f0;
}

Accessibility

  • ✅ Keyboard navigation support
  • ✅ Proper ARIA attributes
  • ✅ Focus management
  • ✅ Disabled state handling
  • ✅ Semantic button elements for dates

Released under the MIT License.