Vanilla Calendar Pro

repository·main·Indexed 22 days ago

https://github.com/uvarov-frontend/vanilla-calendar-pro

A 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.

Tokens
17.7K
Snippets
61
Records
121
Agent score
78%

What's inside vanilla-calendar-pro

  1. Introduction to Vanilla Calendar Pro

    main
    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.
  2. Overview of the Vanilla Calendar Pro API

    main

    The 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.
  3. How layouts work in Vanilla Calendar Pro

    main

    Layouts 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 layouts configuration 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.

  4. Customize calendar structure using Layouts

    main

    Layouts allow you to redefine the HTML structure of the calendar. You can move or remove components by providing a custom string to the layouts option in the Calendar constructor.

    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 />
        `
      }
    });
  5. Initialize calendar in a wrapper vs. as an «Input» popup

    main

    You 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 set inputMode: true in 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,
    });
  6. Key Features of Vanilla Calendar Pro

    main

    Vanilla 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.
  7. Set date boundaries (Min/Max)

    main

    You 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:

    • Date object
    • number (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',
    });