angular-calendar

repository·main·Indexed 25 days ago

https://github.com/mattlewis92/angular-calendar

A calendar component for Angular 20.2+ that displays events in month, week, or day views. It supports date manipulation via date-fns or Moment.js adapters and provides customizable accessibility labels through CalendarA11y, custom date formatting via CalendarDateFormatterInterface, and event title customization using CalendarEventTitleFormatter.

Tokens
5.8K
Snippets
9
Records
30
Agent score
83%

What's inside angular-calendar

  1. Manual setup of angular-calendar

    main

    If you prefer manual installation, follow these three steps:

    1. Install the library and date-fns via npm.
    2. Import the global CSS file in your application's global stylesheet (e.g., src/styles.css).
    3. Configure the DateAdapter using provideCalendar in your component's providers.
    ```bash
    npm install angular-calendar date-fns
    /* angular-cli file: src/styles.css */
    @import "../node_modules/angular-calendar/css/angular-calendar.css";
    import { Component } from '@angular/core';
    import { DateAdapter, provideCalendar, CalendarPreviousViewDirective, CalendarTodayDirective, CalendarNextViewDirective, CalendarMonthViewComponent, CalendarWeekViewComponent, CalendarDayViewComponent, CalendarEvent, CalendarView, CalendarDatePipe } from 'angular-calendar';
    import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
    
    @Component({
      imports: [CalendarPreviousViewDirective, CalendarTodayDirective, CalendarNextViewDirective, CalendarMonthViewComponent, CalendarWeekViewComponent, CalendarDayViewComponent, CalendarDatePipe],
      providers: [
        provideCalendar({
          provide: DateAdapter,
          useFactory: adapterFactory,
        }),
      ],
      template: `
        <button mwlCalendarPreviousView [view]=
  2. Override accessibility labels via CalendarA11y

    main

    You can customize the accessibility (ARIA) labels used by the calendar by providing a custom implementation of the CalendarA11y class using Angular's Dependency Injection (DI). This allows you to change how dates, events, and landmarks are described to screen readers.

    To implement this, extend the CalendarA11y class, override the specific methods you wish to change, and then provide your custom class in the providers array of the component that uses the calendar.

    import { A11yParams, CalendarA11y } from 'angular-calendar';
    import { formatDate } from '@angular/common';
    import { Injectable } from '@angular/core';
    
    // adding your own a11y params
    export interface CustomA11yParams extends A11yParams {
      isDrSuess?: boolean;
    }
    
    @Injectable()
    export class CustomCalendarA11y extends CalendarA11y {
      // overriding a function
      public openDayEventsLandmark({ date, locale, isDrSuess }: CustomA11yParams): string {
        if (isDrSuess) {
          return `
            ${formatDate(date, 'EEEE MMMM d', locale)}
             Today you are you! That is truer than true! There is no one alive
             who is you-er than you!
          `;
        }
        return super.openDayEventsLandmark({ date, locale });
      }
    }
    
    // in your component that uses the calendar
    @Component({
      // ...
      providers: [{
       provide: CalendarA11y,
       useClass: CustomCalendarA11y
      }]
    })
    export class MyCalendarComponent { }
  3. Configure angular-calendar installation options

    main

    When using ng add angular-calendar, you can specify several options to tailor the installation to your project structure:

    • dateAdapter: Choose between 'moment' or 'date-fns'. This determines which date library and adapter factory are configured.
    • standalone: A boolean flag. If true, the schematic attempts to install the calendar into a standalone component. If false (default), it installs into an NgModule.
    • projectName: The name of the project in your workspace to install into.
    • installToPath: An optional path to a specific component or module where the calendar imports and providers should be added.
  4. Customize event titles and tooltips using CalendarEventTitleFormatter

    main

    You can customize how event titles and tooltips are displayed in different calendar views (month, week, day) by overriding the CalendarEventTitleFormatter class. To apply your custom logic, extend the CalendarEventTitleFormatter class and provide it in your component's providers array using the CalendarEventTitleFormatter token.

    import { Injectable } from '@angular/core';
    import { CalendarEventTitleFormatter, CalendarEvent } from 'angular-calendar';
    
    @Injectable()
    class CustomEventTitleFormatter extends CalendarEventTitleFormatter {
    
      month(event: CalendarEvent): string {
        return `Custom prefix: ${event.title}`;
      }
    
    }
    
    // In your component
    @Component({
      // ...
      providers: [{
        provide: CalendarEventTitleFormatter,
        useClass: CustomEventTitleFormatter
      }]
    })
    class MyComponent {}
  5. Automatic configuration for Standalone Components

    main

    If you run ng add angular-calendar --standalone, the schematic will:

    1. Locate your standalone component (either at a specified installToPath or the default app component).
    2. Add the following imports from angular-calendar to the component's imports array:
      • CalendarPreviousViewDirective
      • CalendarTodayDirective
      • CalendarNextViewDirective
      • CalendarMonthViewComponent
      • CalendarWeekViewComponent
      • CalendarDayViewComponent
      • CalendarDatePipe
      • DateAdapter
      • provideCalendar
    3. Add the provideCalendar provider to the component's providers array using the appropriate factory for your chosen dateAdapter.
  6. Use the mwlCalendarToday directive to navigate to today

    main

    The mwlCalendarToday directive allows you to create a button or element that, when clicked, updates the calendar's view date to the current day. It uses two-way data binding with the viewDate property to ensure the calendar view synchronizes with the updated date.

    To use it, apply the directive to an element (like a <button>) and bind it to your component's view date variable using [(viewDate)].

    <button
      mwlCalendarToday
      [(viewDate)]="viewDate">
      Today
    </button>
  7. Use Moment.js for date formatting in angular-calendar

    main

    To use Moment.js for all date formatting within the calendar, you must provide both the MOMENT injection token (with the moment library instance) and the CalendarMomentDateFormatter class as the CalendarDateFormatter. This allows the calendar to use Moment's locale and formatting capabilities for all view headers, titles, and time labels.

    import { CalendarDateFormatter, CalendarMomentDateFormatter, MOMENT } from 'angular-calendar';
    import moment from 'moment';
    
    // in your component or module providers
    provide: [{
      provide: MOMENT, useValue: moment
    }, {
      provide: CalendarDateFormatter, useClass: CalendarMomentDateFormatter
    }]
  8. Implement a custom CalendarDateFormatterInterface

    main

    If the default CalendarAngularDateFormatter (which uses Angular's formatDate pipe) does not meet your requirements, you can provide your own implementation of the CalendarDateFormatterInterface.

    To customize the date strings displayed in the month, week, or day views, implement the following methods in your custom class:

    • monthViewColumnHeader({ date, locale }): Returns the week day labels for the month view header.
    • monthViewDayNumber({ date, locale }): Returns the day number for month view cells.
    • monthViewTitle({ date, locale }): Returns the title for the month view.
    • weekViewColumnHeader({ date, locale }): Returns the week day labels for the week view header.
    • weekViewColumnSubHeader({ date, locale }): Returns the sub-header day and month labels for the week view.
    • weekViewTitle({ date, locale, weekStartsOn, excludeDays, daysInWeek }): Returns the title for the week view.
    • weekViewHour({ date, locale }): Returns the time formatting for the left-hand side of the week view.
    • dayViewHour({ date, locale }): Returns the time formatting for the left-hand side of the day view.
    • dayViewTitle({ date, locale }): Returns the title for the day view.