add-to-calendar-button

repository·main·Indexed 23 days ago

https://github.com/add2cal/add-to-calendar-button

A JavaScript Web Component that allows developers to embed configurable buttons on websites, enabling users to add events to calendar services such as Google, Apple, Microsoft, and Yahoo. It supports installation via CDN or npm, provides a programmatic API through the atcb_action function, and offers flexible configuration via HTML attributes or JSON inner HTML.

Tokens
4.6K
Snippets
10
Records
30
Agent score
81%

What's inside add-to-calendar-button

  1. How to use the Add to Calendar Button Demo

    main

    The add-to-calendar-button-demo sub-repository is a Nuxt-based demonstration of the Add to Calendar Button.

    Important: This sub-repository cannot be used standalone because it relies on the button script from the parent repository via npm workspaces. To run the demo, you should execute commands from the root of the parent repository instead of inside the demo/ directory. Refer to the parent repository's package.json scripts for the correct commands.

  2. Configure the add-to-calendar-button Web Component

    main

    The button is implemented as a Web Component using the <add-to-calendar-button> custom element. You configure its behavior by adding options as attributes directly to the HTML tag.

    Attribute Rules:

    • Boolean values can be set as optionName="true" or simply by adding the attribute name (e.g., inline).
    • If an attribute is not set, it defaults to false.

    Minimal Configuration

    For a basic event, you must provide a name and startDate. While not required, providing options (calendar types) and timeZone is recommended.

    <add-to-calendar-button
      name="Add the title of your event"
      startDate="2022-02-21"
      options="['Google']"
      timeZone="America/Los_Angeles"
    />

    Advanced Configuration

    You can specify detailed event information, preferred calendar services, display styles, and more:

    <add-to-calendar-button
      name="Add the title of your event"
      description="A nice description does not hurt"
      startDate="2022-02-21"
      endDate="2022-03-24"
      startTime="10:13"
      endTime="17:57"
      location="Somewhere over the rainbow"
      options="['Apple','Google','iCal','Microsoft365','Outlook.com','Yahoo']"
      timeZone="Europe/Berlin"
      trigger="click"
      inline
      listStyle="modal"
      iCalFileName="Reminder-Event"
    />

    PRO Configuration

    If using the PRO version, you can use a proKey to link to a specific event structure:

    <add-to-calendar-button proKey="prokey-of-your-event" />
    <add-to-calendar-button
      name="Add the title of your event"
      description="A nice description does not hurt"
      startDate="2022-02-21"
      endDate="2022-03-24"
      startTime="10:13"
      endTime="17:57"
      location="Somewhere over the rainbow"
      options="['Apple','Google','iCal','Microsoft365','Outlook.com','Yahoo']"
      timeZone="Europe/Berlin"
      trigger="click"
      inline
      listStyle="modal"
      iCalFileName="Reminder-Event"
    />
  3. Install add-to-calendar-button via CDN

    main

    For simple integration into standard HTML webpages, you can load the script directly using the jsDelivr CDN. Place the following script tag inside your <head> section:

    <script src="https://cdn.jsdelivr.net/npm/add-to-calendar-button" async defer></script>
  4. Install add-to-calendar-button via npm

    main

    For modern JavaScript projects, install the package using npm and then import the module into your project or component. Note that depending on your framework (React, Angular, Vue, etc.), you may need to make minor adjustments to the configuration.

    npm install add-to-calendar-button
    import 'add-to-calendar-button';
  5. Use the AddToCalendarButton Web Component

    main

    The add-to-calendar-button is a custom HTML element (<add-to-calendar-button>) that renders an interactive calendar button. It can be configured via HTML attributes or by providing a JSON object within its inner HTML.

    Configuration Methods

    1. Attributes: Pass configuration directly as attributes on the element (e.g., name, dates, options).
    2. JSON Inner HTML: Provide a JSON string inside the element tags. This is useful for complex nested objects.
    3. PRO Configuration: If using the PRO version, provide a proKey attribute to fetch configuration from the server.

    Example: Basic Usage (Attributes)

    <add-to-calendar-button
      name="Conference"
      description="A great conference"
      startDate="2023-10-01"
      startTime="10:00"
      endTime="12:00"
      timeZone="America/New_York"
      options="['Google', 'Apple', 'Outlook.com']"
    ></add-to-calendar-button>

    Example: JSON Inner HTML

    <add-to-calendar-button>
    {
      "name": "Conference",
      "description": "A great conference",
      "dates": [
        {
          "startDate": "2023-10-01",
          "startTime": "10:00",
          "endTime": "12:00",
          "timeZone": "America/New_York"
        }
      ],
      "options": ["Google", "Apple", "Outlook.com"]
    }
    </add-to-calendar-button>
  6. Handle Content Security Policy (CSP) with cspnonce

    main

    If your website uses a strict Content Security Policy (CSP), you can provide a nonce to the component to allow it to inject necessary styles and scripts.

    Add the cspnonce attribute to the <add-to-calendar-button> element. The component will then apply this nonce to any <style> or <link> elements it creates within its shadow DOM.

  7. Configure PRO features with proKey

    main

    To use Add to Calendar PRO features, include the proKey attribute on your <add-to-calendar-button> element. The component will automatically fetch your remote configuration from the PRO servers.

    Attributes

    • proKey: Your unique license key.
    • prokey: Lower-case alternative for proKey.
    • proOverride: A boolean attribute (or string 'true') that allows you to override PRO settings with local attributes.

    When a proKey is detected, the component prioritizes the remote configuration but allows local attributes to override specific fields if proOverride is enabled.

  8. Configure i18n settings for the demo

    main

    The defineI18nConfig function is used to configure internationalization (i18n) settings, including locales, fallback behavior, and formatting for dates, times, and numbers. This configuration defines how localized strings and formats are presented across different languages like English (en) and German (de).

    export default defineI18nConfig(() => ({
      locale: 'en',
      fallbackLocale: 'en',
      legacy: false,
      globalInjection: true,
      datetimeFormats: {
        en: {
          short: {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric',
          },
          long: {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric',
            hour: 'numeric',
            minute: 'numeric',
          },
        },
        de: {
          short: {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric',
          },
          long: {
            year: 'numeric',
            month: 'numeric',
            day: 'numeric',
            hour: 'numeric',
            minute: 'numeric',
          },
        },
      },
      numberFormats: {
        en: {
          currency: {
            style: 'currency',
            currency: 'USD',
            useGrouping: true,
            currencyDisplay: 'symbol',
            notation: 'standard',
          },
          decimal: {
            style: 'decimal',
            minimumFractionDigits: 0,
            maximumFractionDigits: 2,
          },
        },
        de: {
          currency: {
            style: 'currency',
            currency: 'EUR',
            useGrouping: true,
            currencyDisplay: 'symbol',
            notation: 'standard',
          },
          decimal: {
            style: 'decimal',
            minimumFractionDigits: 0,
            maximumFractionDigits: 2,
          },
        },
      },
    }));