Smooth Accessible Accordion Plugin In Pure JavaScript – Badger Accordion

Category: Accordion , Javascript | October 2, 2017
Author: stuartjnelson
Views Total: 340
Official Page: Go to website
Last Update: October 2, 2017
License: MIT

Preview:

Smooth Accessible Accordion Plugin In Pure JavaScript – Badger Accordion

Description:

Badger Accordion is a simple, performant, accessible JavaScript accordion plugin that uses CSS3 animations for the smooth expand/collapse effects.

How to use it:

Install and import the Badger Accordion into your project.

$ npm install badger-accordion --save

Or you can include the Badger Accordion’s JavaScript and Stylesheet on your web page.

<link rel="stylesheet" href="css/badger-accordion.css">
<script src="js/badger-accordion.js""></script>

The example HTML structure for the accordion.

<dl class="badger-accordion js-badger-accordion">
  <dt>
      <button class="badger-accordion__header js-badger-accordion-header">
          <div class="badger-accordion__header-title">
              Accordion Header
          </div>
          <div class="badger-accordion__header-icon">
          </div>
      </button>
  </dt>
  <dd class="badger-accordion__panel js-badger-accordion-panel">
      <div class="badger-accordion__panel-inner text-module js-badger-accordion-panel-inner">
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
      </div>
  </dd>
  <dt>
      <button class="badger-accordion__header js-badger-accordion-header">
          <div class="badger-accordion__header-title">
              Accordion Header
          </div>
          <div class="badger-accordion__header-icon">
          </div>
      </button>
  </dt>
  <dd class="badger-accordion__panel js-badger-accordion-panel">
      <div class="badger-accordion__panel-inner text-module js-badger-accordion-panel-inner">
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
          <p>
              Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
      </div>
  </dd>
</dl>

Create a new Badger Accordion instance and done.

const myAccordion = new BadgerAccordion('.js-badger-accordion');

Default configuration options of the accordion.

const myAccordion = new BadgerAccordion('.js-badger-accordion',{
      headerClass: '.js-badger-accordion-header',
      panelClass: '.js-badger-accordion-panel',
      panelInnerClass: '.js-badger-accordion-panel-inner',
      hidenClass: 'is-hidden',
      initalisedClass: 'badger-accordion--initalised',
      headerDataAttr: 'data-badger-accordion-header-id',
      openMultiplePanels: false,
      openHeadersOnLoad: [],
      headerOpenLabel: 'Open accordion panel',
      headerCloseLabel: 'Close accordion panel'
});

API methods.

// gets current state
// Arguments: headerId/s - array
myAccordion.getState()

// opens a specific accordion panel
myAccordion.open(headerIndex)

// closes a specific accordion panel
myAccordion.close(headerIndex)

// toggles accordion panels 
// animationAction is either open or closed.
myAccordion.togglePanel(animationAction, headerIndex)

// opens all accordion panel
myAccordion.openAll()

// closes all accordion panel
myAccordion.closeAll()

Leave a Reply