Vue Cal

repository·main·Indexed 43 days ago

https://github.com/antoniandre/vue-cal

A highly customizable and responsive calendar component library for Vue 3. It supports multiple views (day, week, month, year), robust event management with drag-and-drop and resizing, and timezone support in version 5.0.1-rc.46. Features include a flexible configuration via VueCalConfig, programmatic control through VueCalView, and extensive UI customization using scoped slots.

Tokens
9.3K
Snippets
23
Records
38
Agent score
55%

What's inside Vue Cal

  1. Key features in Vue Cal v5

    main

    Vue Cal v5 introduces several performance and usability improvements over previous versions:

    • Timezone support: Schedule events across different regions accurately.
    • event.patch(): Allows updating a single event in place rather than replacing the entire events array, which is ideal for streaming API updates.
    • Smarter API-driven updates: When the events array is refreshed, Vue Cal merges items by id and performs targeted re-renders to maintain UI performance.
    • Responsive Design: Fully responsive layouts for various screen sizes.
    • Multiple Views: Support for day, week, month, and other view modes.
    • Event Management: Built-in support for adding, editing, dragging, dropping, and resizing events.
  2. Configure Special and Business Hours

    main

    You can define specific time ranges that are highlighted or restricted using specialHours or businessHours. These can be applied globally to weekdays or overridden for specific schedules.

    Special Hours Configuration:

    • from / to: Minutes from the start of the day.
    • class: CSS class for the range.
    • label: Text label for the range.
    • allowEvents: If false, events may not overlap this range (prevents creation, drag-and-drop, or resize).

    Structure: specialHours is a Partial<Record<VueCalWeekdays, VueCalSpecialHoursConfig | VueCalSpecialHoursDay>>.

    • Global Weekday Rule: specialHours: { mon: [{ from: 0, to: 480, allowEvents: false }] }
    • Schedule-Specific Rule: Use VueCalSpecialHoursDay to provide schedules overrides for specific VueCalScheduleIds.
    const config: VueCalConfig = {
      specialHours: {
        mon: {
          default: [
            { from: 0, to: 480, class: 'closed-period', allowEvents: false }
          ],
          schedules: {
            'room-1': [{ from: 600, to: 1000, label: 'Open' }]
          }
        }
      }
    };
  3. Understand the VueCal instance state structure

    main

    The state object returned by useVueCal contains the following reactive properties that govern the calendar's behavior:

    • uid: Unique ID for the instance.
    • emit: The component's emit function.
    • texts: Localized text definitions.
    • dateUtils: Date utility functions localized to the instance.
    • now: The current Date object.
    • config: The merged configuration object (from props and attributes).
    • eventsManager: The manager handling calendar events.
    • view: Details about the current view (e.g., visible events and view dimensions).
    • dnd: The drag-and-drop module state.
    • touch: Gesture-related states, including:
      • isDraggingCell: Boolean indicating if a cell is being dragged.
      • isDraggingEvent: Boolean indicating if an event is being dragged.
      • isResizingEvent: Boolean indicating if an event is being resized.
      • currentHoveredCell: The cell currently being hovered during an event resize operation.
  4. Configure real-time updates with watchRealTime

    main

    When the watchRealTime configuration option is enabled, Vue Cal maintains an internal clock to keep the UI accurate even when the user is idle.

    Behavior:

    • It initializes a time ticker that snaps to the next round minute and then updates every 60 seconds.
    • It updates the now reference, which triggers re-renders of the 'now line' (the indicator showing the current time in the day/week/days view).
    • It ensures that the 'Today' date remains accurate.

    Requirements:

    • The time option must be enabled for the 'now line' to be visible.
    • The 'now line' only shows in day, days, or week views and only if the current time falls within the configured timeFrom and timeTo range.
  5. Understand the difference between viewDate and selectedDate

    main

    Vue Cal distinguishes between the date the calendar is currently looking at and the date that is currently selected:

    • viewDate: The anchor date for the current view. Changing this (via updateViewDate) changes what the user sees on the screen (e.g., moving from January to February).
    • selectedDate: The specific date that is currently 'active' or 'highlighted' (e.g., via a date picker). Changing this (via updateSelectedDate) updates the selection state but does not necessarily move the calendar view unless the new date is outside the current visible range.
  6. Customize VueCal with slots

    main

    VueCal uses scoped slots to allow deep customization of its internal parts. Most slots receive a params object containing relevant data for that specific part of the calendar.

    Header & Navigation Slots

    • #header: Replaces the entire header component. Receives params.
    • #previous-button: Customizes the previous button. Receives params.
    • #next-button: Customizes the next button. Receives params.
    • #today-button: Customizes the today button. Receives params.
    • #title: Customizes the main title. Receives params.
    • #title.day, #title.days, #title.week, #title.month, #title.year, #title.years: Specific title templates for different views. Receives params.
    • #schedule-heading: Customizes the schedule heading. Receives params.

    Body & Content Slots

    • #cell: Replaces the entire cell rendering. Receives params.
    • #cell-date: Customizes the date display within a cell. Receives params.
    • #cell-content: Customizes the content area of a cell. Receives params.
    • #cell-events: Customizes how events are rendered within a cell. Receives params.
    • #event: Customizes the rendering of an event. Receives params.
    • #event.all-day: Customizes the rendering of all-day events. Receives params.
    • #event-count: Customizes the display of the event count. Receives params.
    • #now-line: Customizes the current time indicator line. Receives params.
    • #time-cell: Customizes the time labels in the time column. Receives params.
    • #current-time-label: Customizes the current time label. Receives params.
    • #weekday-heading: Customizes the weekday headings. Receives params.

    DIY Mode

    • #diy: If provided, VueCal will skip its default internal rendering and instead render the content of this slot. The slot receives the internal vuecal instance as a prop, allowing you to build a completely custom UI while leveraging the library's logic.
  7. Validate and intercept event resizing

    main

    Vue Cal provides hooks to validate or reject event resizing via the eventListeners configuration. This is useful for enforcing business rules (e.g., preventing meetings from being moved to certain times or overlapping specific schedules).

    Available Listeners:

    • resize-start: Fired when the user starts dragging a resizer.
    • resize: Fired during the resize movement. If this returns false, the resize is rejected and the event reverts to its last accepted state.
    • resize-end: Fired when the user releases the resizer. If this returns false, the event reverts to its original state before the resize attempt.

    Implementation Pattern:

    // In your Vue Cal configuration
    const config = {
      eventListeners: {
        event: {
          resize: async ({ e, event, overlaps }) => {
            // Return true to accept, false to reject
            return !overlaps.some(ev => ev.schedule === event.schedule);
          },
          'resize-end': async ({ e, event, original, overlaps }) => {
            // Final validation
            return true;
          }
        }
      }
    }
  8. Configure Vue Cal text translations and localization

    main

    Vue Cal supports localization via the locale prop. When the locale prop changes, the component asynchronously loads the corresponding JSON translation file from the i18n directory. If a requested locale is not found, it falls back to en-us and issues a warning.

    To customize specific text strings (like 'today', 'noEvent', or date formats) without changing the entire locale, you can override the texts configuration. The default texts object includes:

    • weekDays: Array of 7 strings
    • weekDaysShort: Array of short weekday strings
    • months: Array of 12 strings
    • years, year, month, week, day: String labels
    • today, noEvent, allDay, deleteEvent, createEvent: UI labels
    • dateFormat: Default is 'dddd MMMM D, YYYY'
    • am, pm: Meridiem indicators
    • truncations: Boolean
  9. Disable specific days and set date boundaries

    main

    You can restrict user interaction by disabling specific dates or setting minimum/maximum date boundaries.

    • disableDays: An array of dates (as 'YYYY-MM-DD' strings or Date objects) that should be non-interactive.
    • minDate: A string or Date object representing the earliest allowed date.
    • maxDate: A string or Date object representing the latest allowed date.
    // Example usage
    const disableDays = ['2023-12-25', '2024-01-01'];
    const minDate = '2023-01-01';
    const maxDate = new Date();
  10. Configure event snapping with `snapToInterval`

    main

    When dragging and dropping events, you can force the event's start time to align with specific time increments using the snapToInterval configuration option. This is useful for ensuring events start on the hour, half-hour, or other specific intervals.

    Example: Setting snapToInterval: 30 will cause dropped events to snap to the nearest 30-minute mark.

    // Example config to snap events to 30-minute intervals
    const config = {
      snapToInterval: 30
    }
  11. Configure available calendar views

    main

    You can control which views are available in the calendar using the views prop.

    • Default views: If no views prop is provided, the calendar uses all standard views: day, days, week, month, year, and years.
    • Date Picker mode: If the datePicker prop is active, the default available views are restricted to month, year, and years.
    • Customizing views: You can pass an array of view names or an object to define custom dimensions (columns and rows) for specific views.

    Available standard views and their default dimensions:

    • day: 1 col, 1 row
    • days: 10 cols, 1 row
    • week: 7 cols, 1 row
    • month: 7 cols, 6 rows
    • year: 4 cols, 3 rows
    • years: 5 cols, 5 rows (covers a 25-year range)
    // Example: Providing a custom set of views with specific dimensions
    const views = {
      day: { cols: 1, rows: 1 },
      week: { cols: 7, rows: 1 },
      month: { cols: 7, rows: 8 } // Customizing month to have 8 rows
    };