Détail du package

accordion

Alhadis7.6kISC3.0.2

Silky-smooth accordion widgets with no external dependencies.

accordion, widget, component, folding

readme

Accordion

Silky-smooth accordion widgets with no external dependencies.

npm install accordion --save
bower install silk-accordion --save

An simple interactive demo can be found here. More complicated and extreme demos can be found in the demos directory.

Usage

Include the following two files in your project:

src/accordion.css
src/accordion.js

Layout your markup like this:

<div class="accordion">

    <div>
        <h1> Heading </h1>
        <div> Content </div>
    </div>

    <div>
        <h1> Heading </h1>
        <div> Content </div>
    </div>

</div>

Then create an Accordion instance with a reference to a DOM element:

var el = document.querySelector(".accordion");
new Accordion(el);

Options can be passed in a second argument:

new Accordion(el, {
    onToggle: function(fold, isOpen){
        console.log(fold);   // -> Reference to a `Fold` instance
        console.log(isOpen); // -> true / false
    }
});

Styling

The base stylesheet is located at src/accordion.css. Embed it into your application's existing styling, tweaking it if desired.

Note: This stylesheet only includes properties necessary for the Accordion to function. Making it look appealing with colours and fonts is left as an exercise to the developer. Check the source of the bundled demos for some ideas.

Using ES6 modules

If your project uses native JavaScript modules, consider loading src/accordion.mjs instead:

<!-- ES6+ -->
<script type="module">
    import Accordion from "./src/accordion.mjs";
    for(const el of document.querySelectorAll(".accordion"))
        new Accordion(el);
</script>

The old accordion.js file contains only ES5, and can be used as a fallback for older platforms which lack ES module support:

<!-- Fallback to ES5 for legacy browsers -->
<script nomodule src="src/accordion.js"></script>
<script nomodule>
    "use strict";
    var accordions = document.querySelectorAll(".accordion");
    for(var i = 0, l = accordions.length; i < l; ++i)
        new Accordion(accordions[i]);
</script>

IE8 support

For IE8-9 compatibility, install fix-ie:

npm install fix-ie --save
bower install fix-ie --save

Then link to it using a conditional comment, before any other script on the page!

<!DOCTYPE html>
<html lang="en">
    <head>
    <!--[if lte IE 9]>
        <script src="node_modules/fix-ie/dist/ie.lteIE9.js"></script>
    <![endif]-->
    <meta charset="utf-8" />

This fixes IE's buggy handling of Object.defineProperty, which the Accordion makes extensive use of. fix-ie also provides oodles of helpful polyfills to fix IE8's shoddy DOM support.

Options

Name Type Default Description
closeClass String "closed" CSS class used to mark a fold as closed
disabled Boolean false Whether to disable the accordion on creation
disabledClass String undefined CSS class marking an accordion as disabled
edgeClass String "edge-visible" CSS class toggled based on whether the bottom-edge is visible
enabledClass String "accordion" CSS class marking an accordion as enabled
heightOffset Number 0 Distance to offset each fold by
modal Boolean false Whether to close the current fold when opening another
noAria Boolean false Disable the addition and management of ARIA attributes
noKeys Boolean false Disable keyboard navigation
noTransforms Boolean false Disable CSS transforms; positioning will be used instead
onToggle Function undefined Callback executed when opening or closing a fold
openClass String "open" CSS class controlling each fold's "open" state
snapClass String "snap" CSS class for disabling transitions between window resizes
useBorders Boolean "auto" Consider borders when calculating fold heights

changelog

Change Log

This project honours Semantic Versioning.

v3.0.2

September 20th, 2018
Added links to interactive demos, as suggested in #13.

v3.0.1

June 16th, 2018
Fixed a broken link in package's readme file (reported in #12).

v3.0.0

May 18th, 2018

Breaking change:
Removed dist directory. Source code now exclusively located in src. Rationale for removal is explained in the commit notes of e74cd3f.

Bugs fixed:

  • Inability to toggle focussed folds with spacebar
  • Enter key not toggling folds with anchor-tag headings
  • onTouchStart event handlers not marked as passive
  • Accordion class not globalised in module's ES6 version

v2.1.1

July 18th, 2017
This release fixes broken class-toggling in all versions of Explorer. A minor issue with incorrect source-map linkage was also corrected.

v2.1.0

February 8th, 2016
This release adds much-needed support for CSS3 transforms and callbacks. Additionally, it adds proper API and option documentation, as well as an extra folder for various module distribution flavours.

v2.0.1

February 5th, 2016
This release simply adds configuration files for Bower and NPM.

v2.0.0

February 5th, 2016
Half of the first version's code was written before I realised it didn't support accordion nesting. The remaining half was packed full of everted fixes to correct my stupid oversight, which ended up making an illogical mess of everything. Intensely frustrated with the result, I started anew from complete scratch, angrily pounding out one line of fresh ECMAScript after another.

This release is the fruit of that second effort, featuring cleaner, much more efficient code and proper nesting support. Numerous bugs pertaining to height measurement and redraws were found and fixed along the way.

v1.0.0

January 19th, 2016
The original build of this accordion widget, originally started in April 2015 between many exhausted commutes. The aim was creating the smoothest possible accordions for mobile devices, which were stress-tested with an assortment of huge images and text.