Import Cally as a module
mainIf you have installed Cally via npm, import it in your JavaScript entry point to register the custom elements.
import "cally";repository·main·Indexed 23 days ago
https://github.com/wickynilliams/callyA library of small, feature-rich, and accessible calendar web components (version 0.9.2). Cally is framework-independent and themeable via CSS, supporting single dates, multiple dates, and date ranges. It includes components such as <calendar-date>, <calendar-range>, and <calendar-month>, along with utility classes like PlainDate and PlainYearMonth for date arithmetic and comparison.
If you have installed Cally via npm, import it in your JavaScript entry point to register the custom elements.
import "cally";Install the cally package using npm to use the calendar components in your project.
npm install callyYou can use Cally directly in an HTML file without a build step by importing the module from a CDN.
<script type="module" src="https://unpkg.com/cally"></script>Cally components are web components that you can use directly in your HTML. For example, to create a range selector that displays two months, use the <calendar-range> component with the months attribute and include multiple <calendar-month> elements. You can use the offset attribute on <calendar-month> to control which month is displayed relative to the range.
<calendar-range months="2">
<calendar-month></calendar-month>
<calendar-month offset="1"></calendar-month>
</calendar-range>The CalendarBase component provides event listeners to react to user interactions with specific dates. These events are dispatched as CustomEvent objects containing a PlainDate object.
onselectday: Triggered when a user selects a date. Expects a callback with signature (e: CustomEvent<PlainDate>) => void.onfocusday: Triggered when a date gains focus. Expects a callback with signature (e: CustomEvent<PlainDate>) => void.onhoverday: Triggered when a user hovers over a date. Expects a callback with signature (e: CustomEvent<PlainDate>) => void.Cally provides a set of web components for calendar functionality. You can import the following components from the main entry point:
CalendarMonth: Represents a single month view.CalendarDate: Represents a single date selection.CalendarRange: Represents a range of dates.CalendarMulti: Represents multiple discrete date selections.CalendarSelectYear: A component for selecting a year.CalendarSelectMonth: A component for selecting a month.CalendarHeading: A component for displaying calendar headings.import {
CalendarMonth,
CalendarDate,
CalendarRange,
CalendarMulti,
CalendarSelectYear,
CalendarSelectMonth,
CalendarHeading,
} from "cally";The PlainYearMonth class represents a specific month in a specific year. It is useful for calendar logic that does not require a specific day.
Key features:
new PlainYearMonth(year, month) or convert from a PlainDate using .toPlainYearMonth()..add(duration) where duration can include months or years..equals(dateOrYearMonth) to compare against a PlainDate or another PlainYearMonth, or use the static PlainYearMonth.compare(a, b) method..toPlainDate() to get a PlainDate representing the 1st day of that month.The PlainDate class provides a lightweight way to handle calendar dates (year, month, day) with arithmetic and comparison capabilities. It is designed to be a drop-in replacement for the upcoming Temporal API.
Key features:
new PlainDate(year, month, day) or use PlainDate.from(value) where value is an ISO date string (YYYY-MM-DD) or a JavaScript Date object..add(duration) to add days, months, or years. Note that arithmetic is constrained to ensure valid dates (e.g., adding 1 month to March 31st results in April 30th).PlainDate.compare(a, b) to get a result of -1, 0, or 1, or use .equals(otherDate) for a boolean check..toString() for ISO format or .toPlainYearMonth() to convert to a PlainYearMonth instance.The <calendar-date> component exposes the following interface:
change: Dispatched when the selected date changes.onFocusDay: A CustomEvent<Date> dispatched when a specific day gains focus.onPageChange: A CustomEvent<PageChangeDetail> dispatched when the calendar view changes (e.g., navigating to a different month).focus(options?: CalendarFocusOptions): Programmatically sets focus within the calendar. Accepts an optional options object of type CalendarFocusOptions.The CalendarBase component exposes several CSS Shadow Parts that allow you to customize its internal structure via your own CSS:
container: The main wrapper for the calendar (a div with role="group").header: The top section containing navigation and the heading.heading: The slot for the calendar heading.months: The slot where the month views are rendered.button: The base style for the navigation buttons. Note that buttons can also have specific parts like button previous or button next depending on the implementation, and a button disabled part when the onclick handler is missing.The CalendarBase component accepts several configuration properties to control its behavior, date range, and display. These properties are used to manage the calendar's state and appearance.
| Property | Type | Default | Description |
|---|---|---|---|
value | String | "" | The currently selected date(s). |
min | String | "" | The minimum allowed date. |
max | String | "" | The maximum allowed date. |
today | String | "" | The date representing 'today'. |
isDateDisallowed | Function | (date: Date) => false | A callback to determine if a specific date should be disabled. |
formatWeekday | String | "narrow" | The format for weekdays ("narrow" or "short"). |
getDayParts | Function | (date: Date): string => "" | A function to extract parts from a date. |
firstDayOfWeek | Number | 1 | The index of the first day of the week. |
showOutsideDays | Boolean | false | Whether to show days from adjacent months. |
locale | String | undefined | The locale string for date formatting. |
months | Number | 1 | The number of months to display. |
focusedDate | String | undefined | The date that currently has focus. |
pageBy | String | "months" | The pagination mode (e.g., "months"). |
showWeekNumbers | Boolean | false | Whether to display week numbers. |
If you are building wrappers for Cally components in frameworks like React or Vue, you can use the exported Props types to ensure type safety. These types represent the component's properties, excluding standard HTMLElement and AtomicoThis properties.
Available prop types:
CalendarMonthPropsCalendarDatePropsCalendarRangePropsCalendarMultiPropsCalendarSelectYearPropsCalendarSelectMonthPropsCalendarHeadingProps