accordion-js

repository·master·Indexed 19 days ago

https://github.com/michu2k/accordion

A lightweight and accessible JavaScript module for creating accordion UI components, such as FAQ lists, featuring an extensible API and support for custom configuration.

Tokens
2.1K
Snippets
8
Records
8
Agent score
14%

What's inside accordion-js

  1. Install accordion-js via CDN

    master

    Include the CSS and JavaScript files directly in your HTML using unpkg.

    <link rel="stylesheet" href="https://unpkg.com/accordion-js@3.4.1/dist/accordion.min.css" />
    <script src="https://unpkg.com/accordion-js@3.4.1/dist/accordion.min.js"></script>
  2. Install accordion-js via npm

    master

    Install the accordion-js package and import the required JavaScript and CSS files into your project.

    npm install accordion-js
    import Accordion from "accordion-js";
    import "accordion-js/dist/accordion.min.css";
  3. Configure Accordion options

    master

    Customize the accordion behavior using the following options in the configuration object:

    | Option | Type | Default | Description |
    | --- | --- | --- | --- |
    | `duration` | number | `500` | Animation duration in ms |
    | `ariaEnabled` | boolean | `true` | Add ARIA elements to the HTML structure |
    | `collapse` | boolean | `true` | Allow collapse expanded panel |
    | `showMultiple` | boolean | `false` | Show multiple elements at the same time |
    | `onlyChildNodes` | boolean | `true` | If `false`, finds all items in container. Warning: setting to `false` may break nested accordions |
    | `openOnInit` | array | `[]` | Show accordion elements during initialization |
    | `elementClass` | string | `"ac"` | Element class |
    | `triggerClass` | string | `"ac-trigger"` | Trigger class |
    | `panelClass` | string | `"ac-panel"` | Panel class |
    | `activeClass` | string | `"is-active"` | Active element class |
    | `beforeOpen` | function | - | Called before the item is opened. `(currElement) => {}` |
    | `onOpen` | function | - | Called when the item is opened. `(currElement) => {}` |
    | `beforeClose` | function | - | Called before the item is closed. `(currElement) => {}` |
    | `onClose` | function | - | Called when the item is closed. `(currElement) => {}` |
  4. Create an accordion HTML layout

    master

    To use the accordion, structure your HTML with a container and individual accordion items. Each item should consist of a header (containing a trigger button) and a panel. By default, the module looks for the following classes:

    • Element: ac
    • Trigger: ac-trigger
    • Panel: ac-panel
    <div class="accordion-container">
      <div class="ac">
        <h2 class="ac-header">
          <button type="button" class="ac-trigger">Lorem ipsum dolor sit amet.</button>
        </h2>
        <div class="ac-panel">
          <p class="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        </div>
      </div>
    
      <div class="ac">
        <h2 class="ac-header">
          <button type="button" class="ac-trigger">Lorem ipsum dolor sit amet.</button>
        </h2>
        <div class="ac-panel">
          <p class="ac-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        </div>
      </div>
    </div>
  5. Use Accordion instance methods

    master

    Once an accordion is initialized, you can control it using the following methods:

    | Method | Description | Arguments |
    | --- | --- | --- |
    | `attachEvents()` | Attach events | - |
    | `detachEvents()` | Detach events | - |
    | `open(idx)` | Open the accordion element at the given index | `idx` (element index) |
    | `close(idx)` | Close the accordion element at the given index | `idx` (element index) |
    | `toggle(idx)` | Toggle the accordion element at the given index | `idx` (element index) |
    | `openAll()` | Open all accordion elements (without animation) | - |
    | `closeAll()` | Close all accordion elements (without animation) | - |
    | `update()` | Update the accordion (useful for lazy-loaded items) | - |
    | `destroy()` | Destroy instance: opens elements, removes events, IDs & ARIA | - |
  6. Use the Accordion constructor

    master

    The Accordion constructor creates a new accordion instance.

    Signature: new Accordion(container, options)

    Parameters:

    • container (required): A selector string, a DOM element, or an array of selector strings or HTMLElements that specify the accordion container(s).
    • options (optional): A configuration object to customize behavior.
    // Initialize with custom options
    new Accordion(".container-second", {
      duration: 400,
      showMultiple: true,
      onOpen: function (currentElement) {
        console.log(currentElement);
      }
    });
    
    // Initialize multiple containers at once using an array of selectors
    new Accordion([".container-first", ".container-second"], {});
    
    // Initialize using an array of HTMLElements
    const accordions = Array.from(document.querySelectorAll(".accordion-container"));
    new Accordion(accordions, {});
  7. Initialize the Accordion module

    master

    Initialize the accordion by passing a selector string, a DOM element, or an array of either to the Accordion constructor.

    <script>
      // Initialize using a selector
      new Accordion(".accordion-container");
    </script>
  8. Configure Prettier for accordion-js

    master

    The accordion-js project uses a Prettier configuration file (prettier.config.mjs) to enforce code style. When contributing or working on the codebase, ensure your local Prettier environment respects these settings to maintain consistency.

    /** @type {import("prettier").Config} */
    export default {
      printWidth: 120,
      bracketSameLine: true,
      bracketSpacing: false,
      trailingComma: "none",
      quoteProps: "consistent"
    };