Vanilla Calendar Pro
repository·main·Indexed 22 days ago
https://github.com/uvarov-frontend/vanilla-calendar-proA lightweight, dependency-free JavaScript date and time picker. It supports multiple instances, custom HTML layouts, and is compatible with any framework, including React and Vue. Key features include date and time range selection, automatic light/dark theme switching, accessibility via ARIA labels, and highly customizable CSS styling.
What's inside vanilla-calendar-pro
- Vanilla Calendar Pro is a lightweight, dependency-free JavaScript plugin for date and time selection. It is designed to be highly performant and easily customizable via CSS and HTML, making it suitable for everything from simple date displays to complex web applications requiring advanced features like time selection, range selection, and interactive actions.
Overview of the Vanilla Calendar Pro API
mainThe Vanilla Calendar Pro API is organized into several functional areas to help you customize and control the calendar's behavior, appearance, and accessibility. The API surface includes:
- Instance Creation: Methods for initializing the calendar.
- Utilities: Date formatting functions.
- Methods: Functions to interact with an existing calendar instance.
- Settings: Configuration options for behavior and display.
- Actions: Event handlers for processing user interactions.
- Popups: Tools for displaying information when hovering over specific days.
- Layouts: Templates for modifying the DOM structure and adding custom HTML.
- Styles: A CSS class object for applying custom styles or CSS frameworks (e.g., Tailwind CSS).
- Aria-labels: Localization strings for accessibility labels.
Use the 'default' calendar type for single month selection
mainThe'default'calendar type is the standard display mode. It shows a single month, allows day selection, provides navigation arrows to move between months, and includes headers to select the month and year directly.Use the 'year' calendar type to select year and month
mainThe'year'calendar type displays a list of years. Users can select a year from the list and a month from the corresponding header. This mode is specifically designed to restrict user selection to only the year and month, preventing the selection of specific days.How layouts work in Vanilla Calendar Pro
mainLayouts allow you to customize the DOM structure of the calendar and inject your own HTML elements (like custom buttons). Each calendar view type has its own default template that you can override via the
layoutsconfiguration object.Registered Components
When defining a layout, you use special tags starting with
#to represent registered calendar components.- Self-closing tags: Most components are self-closing, e.g.,
<#Month />or<#ArrowPrev [month] />. - Wrapping tags: The
<#Multiple>component is a special case that wraps one or more months and requires a closing tag:<#Multiple> ... <#/Multiple>.
All default templates provided by the library include all possible components for that specific layout type, which you can use as a reference when building your own.
- Self-closing tags: Most components are self-closing, e.g.,
Customize calendar structure using Layouts
mainLayouts allow you to redefine the HTML structure of the calendar. You can move or remove components by providing a custom string to the
layoutsoption in theCalendarconstructor.Components are identified by tags containing a
#and ending with a/(e.g.,<#Month />). Some components can accept parameters in brackets, such as<#ArrowPrev [month] />.new Calendar('#calendar', { layouts: { default: ` <div class="vc-header" data-vc="header" role="toolbar" aria-label="Calendar Navigation"> <#ArrowPrev [month] /> <div class="vc-header__content" data-vc-header="content"> <#Month /> <#Year /> </div> <#ArrowNext [month] /> </div> <div class="vc-wrapper" data-vc="wrapper"> <#WeekNumbers /> <div class="vc-content" data-vc="content"> <#Week /> <#Dates /> <#DateRangeTooltip /> </div> </div> <#ControlTime /> ` } });Initialize calendar in a wrapper vs. as an «Input» popup
mainYou can choose how the calendar is presented to the user by how you target the element and which settings you use:
Calendar Wrapper
If the target element is a container (like a
<div>), the calendar will be initialized and rendered directly inside that element.<div id="calendar"></div>new Calendar('#calendar');«Input» Popup
If you want the calendar to appear as a popup when a specific element is clicked, treat that element as an «Input». An «Input» can be any HTML element (e.g.,
<input>,<div>,<span>). You must setinputMode: truein the settings object.<input type="text" id="input"> <!-- or --> <div id="input"></div>new Calendar('#input', { inputMode: true });// Popup mode configuration new Calendar('#input', { inputMode: true, });Key Features of Vanilla Calendar Pro
mainVanilla Calendar Pro provides several advanced capabilities for calendar widgets:
- Performance & Size: Lightweight, minified, and optimized for fast loading; completely standalone (no dependencies).
- Customization: Easy localization, CSS/HTML-based styling, and support for multiple instances on a single page.
- Display Options: Custom week start days, custom weekend definitions, and week number displays.
- Theming: Automatic light/dark theme switching and support for custom themes.
- Interaction: Date and time range selection with min/max limits, pop-ups with custom information, and tooltips for range selections.
- Accessibility: Built-in ARIA labels,
tabindex, and full keyboard navigation. - Flexibility: Not restricted to being tied to an
<input>element.
Set date boundaries (Min/Max)
mainYou can restrict the range of dates the calendar considers or allows users to select using two different approaches:
1. Hard Boundaries (
dateMin,dateMax)These define the absolute range of dates the calendar will even create/render. Dates outside this range do not exist in the calendar's context.
2. Selection Boundaries (
displayDateMin,displayDateMax)These allow the dates to exist and be visible, but they are disabled and cannot be selected by the user.
Supported formats for all date parameters:
Dateobjectnumber(timestamp)'YYYY-MM-DD'string'today'
new Calendar('#calendar', { dateMin: '1970-01-01', dateMax: '2470-12-31', displayDateMin: '2022-07-01', displayDateMax: '2024-07-01', });Use custom themes
mainBeyond the default light and dark themes, you can use custom themes. You can either create your own theme definitions or import existing ones provided by the library. When using custom themes, ensure thatthemeAttrDetectis configured appropriately so that your manual theme selection or custom theme application is not overridden by automatic attribute detection.Use the 'multiple' calendar type to select multiple dates
mainThe'multiple'calendar type allows the display of multiple months simultaneously, enabling users to select individual days across different months. To enable this mode, set thetypeparameter to'multiple'and theselectionDatesModeparameter to'multiple'in your calendar configuration.Use the 'month' calendar type to select months and years
mainThe'month'calendar type changes the calendar view to display a list of months instead of a day grid. This mode allows users to select a month and a year from the headers, making it ideal for interfaces where specific day selection is not required (e.g., selecting a birth month or a billing period).