GithubHelp home page GithubHelp logo

lefthandmedia / vanilla-js-accordion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thec2group/vanilla-js-accordion

0.0 1.0 0.0 122 KB

Accessible vanilla JS accordion using CSS transition to expand and contract.

License: MIT License

CSS 1.03% JavaScript 89.22% HTML 9.75%

vanilla-js-accordion's Introduction

accordion

Accessible accordion using CSS transition to expand and contract. Aria states are automatically added.

To get Started

CommonJS

$ npm install vanilla-js-accordion
var Accordion = require('vanilla-js-accordion');

Browser Global

<script src="iife/accordion.js"></script>

Basic Example

<div class="Accordion">
    <div class="item" data-status="contracted">
        <div class="target">...</div>
        <div class="panel">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target">...</div>
        <div class="panel">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target">...</div>
        <div class="panel">...</div>
    </div>
</div>
// This wouldn't be necessary since all these options are the defaults
var options = {
    item: '.item',
    target: '.target',
    control: '.target', // in this case the target is also acting as the control
    panel: '.panel',
    allowMultiple: true,
    attribute: 'data-status',
    expanded: 'expanded',
    contracted: 'contracted',
    prefix: 'Accordion-',
    transition: 'height .3s',
    setFocus: 'none', // options: none, item, panel, target, control, first
    hashEnabled: true // use hash in URL to open accordion item
};

new Accordion('.Accordion', options);

Since the height of the "control" and the height of the "panel" make up the height of the item. It is sometimes not the case that the "target" is the same as the "control". If you want an item to be open on page load, change the data-status to "expanded". Below is an example of this:

<div class="Accordion">
    <div class="item" data-status="expanded">
        <div class="control">
            <a class="target" href="#">...</a>
        </div>
        <div class="panel">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="control">
            <a class="target" href="#">...</a>
        </div>
        <div class="panel">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="control">
            <a class="target" href="#">...</a>
        </div>
        <div class="panel">...</div>
    </div>
</div>
new Accordion('.Accordion', {
    control: '.control'
});

Example with Custom IDs

Add an ID to each accordion target and an aria-labelledby attribute to each panel.

<div class="Accordion">
    <div class="item" data-status="contracted">
        <div class="target" id="Uno">...</div>
        <div class="panel" aria-labelledby="Uno">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" id="Dos">...</div>
        <div class="panel" aria-labelledby="Dos">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" id="Tres">...</div>
        <div class="panel" aria-labelledby="Tres">...</div>
    </div>
</div>

API

var accordion = new Accordion(document.querySelector('.Example1'));

accordion.activate(0);    // toggles accordion to the index
accordion.expand(0);      // expands accordion to the index
accordion.contract(0);    // contracts accordion to the index
accordion.contractAll(0); // contracts all the accordions, the optional parameter is an index that gets ignored
accordion.expandAll();    // expands all the accordions
accordion.destroy()       // removes event listeners and aria properties

Accessibility

http://www.w3.org/TR/wai-aria-practices/#accordion

Here is what the markup looks like after accessibility is added with JavaScript. It may be beneficial to add the aria attributes before JavaScript runs.

<div class="Accordion" role="tablist" aria-multiselectable="true">
    <div class="item" data-status="expanded">
        <div class="target" role="tab" aria-expanded="true" tabindex="0" id="Accordion-1-1">...</div>
        <div class="panel" role="tabpanel" aria-hidden="false" aria-labelledby="Accordion-1-1">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" role="tab" aria-expanded="false" tabindex="0" id="Accordion-1-2">...</div>
        <div class="panel" role="tabpanel" aria-hidden="true" aria-labelledby="Accordion-1-2">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" role="tab" aria-expanded="false" tabindex="0" id="Accordion-1-3">...</div>
        <div class="panel" role="tabpanel" aria-hidden="true" aria-labelledby="Accordion-1-3">...</div>
    </div>
</div>

Or if allowMultiple is set to false.

<div class="Accordion" role="tablist">
    <div class="item" data-status="expanded">
        <div class="target" role="tab" aria-expanded="true" aria-selected="true" tabindex="0" id="Accordion-2-1">...</div>
        <div class="panel" role="tabpanel" aria-hidden="false" aria-labelledby="Accordion-2-1">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" role="tab" aria-expanded="false" aria-selected="false" tabindex="0" id="Accordion-2-2">...</div>
        <div class="panel" role="tabpanel" aria-hidden="true" aria-labelledby="Accordion-2-2">...</div>
    </div>
    <div class="item" data-status="contracted">
        <div class="target" role="tab" aria-expanded="false" aria-selected="false" tabindex="0" id="Accordion-2-3">...</div>
        <div class="panel" role="tabpanel" aria-hidden="true" aria-labelledby="Accordion-2-3">...</div>
    </div>
</div>

For Developers Working on this Module

  • After you pull down the project, run npm install to get all of the node modules
  • You will want to work in the accordion.js file in the root
  • To compile your changes, run npm run build (creates the CJS, IIFE and UMD versions of the module), then npm run bundle-example (creates the example file)
  • To test your changes, open example/index.html in a browser

License

MIT © The C2 Group

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.