项目作者: oncode

项目描述 :
Accessible W3C conform accordion written in ES6.
高级语言: JavaScript
项目地址: git://github.com/oncode/handorgel.git
创建时间: 2017-03-08T23:25:43Z
项目社区:https://github.com/oncode/handorgel

开源协议:MIT License

下载


Handorgel

NPM version
Coding Style
MIT License

Accessible W3C conform accordion written in ES6. Handorgel is the Swiss German name for accordion.

Visit the demo

Features

  • ARIA accessible
  • Keyboard interaction
  • Extensive API
  • Animated collapsing
  • Fully customizable via CSS
  • No external dependencies
  • Lightweight (~3kb minified and gziped)

Installation

Package manager

Manager Command
npm npm install handorgel
yarn yarn add handorgel

CDN / Download

File CDN
CSS handorgel@1.0/lib/css/handorgel.css">handorgel.css
CSS (minified) handorgel@1.0/lib/css/handorgel.min.css">handorgel.min.css
JS handorgel@1.0/lib/js/umd/handorgel.js">handorgel.js
JS (minified) handorgel@1.0/lib/js/umd/handorgel.min.js">handorgel.min.js

Usage

Markup

  1. <div class="handorgel">
  2. <h3 class="handorgel__header">
  3. <button class="handorgel__header__button">
  4. Title
  5. </button>
  6. </h3>
  7. <div class="handorgel__content" data-open>
  8. <div class="handorgel__content__inner">
  9. Content openened by default
  10. </div>
  11. </div>
  12. <h3 class="handorgel__header">
  13. <button class="handorgel__header__button">
  14. Title 2
  15. </button>
  16. </h3>
  17. <div class="handorgel__content">
  18. <div class="handorgel__content__inner">
  19. Content closed by default
  20. </div>
  21. </div>
  22. ...
  23. </div>

Note: Use the heading tags that fit into your content to output semantic markup.

CSS

Import the SASS file from your node_modules folder to make use of the variables:

  1. // e.g. changing opening/closing transition times
  2. $handorgel__content--open-transition-height-time: .1s;
  3. $handorgel__content--open-transition-opacity-time: .2s;
  4. $handorgel__content-transition-height-time: .05s;
  5. $handorgel__content-transition-opacity-time: .05s;
  6. //...
  7. @import '~handorgel/src/scss/style';

Alternatively you can just include the built CSS file inside the /lib folder file or from the CDN.

Javascript

Initialization (with all options and their defaults):

  1. var accordion = new handorgel(document.querySelector('.handorgel'), {
  2. // whether multiple folds can be opened at once
  3. multiSelectable: true,
  4. // whether the folds are collapsible
  5. collapsible: true,
  6. // whether ARIA attributes are enabled
  7. ariaEnabled: true,
  8. // whether W3C keyboard shortcuts are enabled
  9. keyboardInteraction: true,
  10. // whether to loop header focus (sets focus back to first/last header when end/start reached)
  11. carouselFocus: true,
  12. // attribute for the header or content to open folds at initialization
  13. initialOpenAttribute: 'data-open',
  14. // whether to use transition at initial open
  15. initialOpenTransition: true,
  16. // delay used to show initial transition
  17. initialOpenTransitionDelay: 200,
  18. // header/content element selectors or array of elements
  19. headerElements: '.handorgel__header',
  20. contentElements: '.handorgel__content',
  21. // header/content class if fold is open
  22. headerOpenClass: 'handorgel__header--open',
  23. contentOpenClass: 'handorgel__content--open',
  24. // header/content class if fold has been opened (transition finished)
  25. headerOpenedClass: 'handorgel__header--opened',
  26. contentOpenedClass: 'handorgel__content--opened',
  27. // header/content class if fold has been focused
  28. headerFocusClass: 'handorgel__header--focus',
  29. contentFocusClass: 'handorgel__content--focus',
  30. // header/content class if fold is disabled
  31. headerDisabledClass: 'handorgel__header--disabled',
  32. contentDisabledClass: 'handorgel__content--disabled',
  33. })

API

Events

Event Description Parameters
destroy Accordeon is about to be destroyed.
destroyed Accordeon has been destroyed.
fold:open Fold is about to be opened. HandorgelFold: Fold instance
fold:opened Fold has opened. HandorgelFold: Fold instance
fold:close Fold is about to be closed. HandorgelFold: Fold instance
fold:closed Fold has closed. HandorgelFold: Fold instance
fold:focus Fold button has been focused. HandorgelFold: Fold instance
fold:blur Fold button has lost focus. HandorgelFold: Fold instance

How to listen for events:

  1. var accordion = new handorgel(document.querySelector('.handorgel'))
  2. // listen for event
  3. accordion.on('fold:open', (fold) => {
  4. // ...
  5. })
  6. // listen for event once
  7. accordion.once('fold:open', (fold) => {
  8. // ...
  9. })
  10. // remove event listener
  11. accordion.off('fold:open', fn)

Methods

Handorgel Class

Method Description Parameters
update Update fold instances (use if you dynamically append/remove DOM nodes).
focus Set focus to a new header button (you can also directly use the native focus() method on the button). target: New header button to focus (next, previous, last or first)
destroy Destroy fold instances, remove event listeners and ARIA attributes.

Example:

  1. var accordion = new handorgel(document.querySelector('.handorgel'))
  2. // destroy
  3. accordion.destroy()

HandorgelFold Class

Method Description Parameters
open Open content. transition: Whether transition should be active during opening (default: true).
close Close content. transition: Whether transition should be active during closing (default: true).
toggle Toggle content. transition: Whether transition should be active during toggling (default: true).
disable Disable fold.
enable Enable fold.
focus Set focus to fold button.
blur Remove focus from fold button.
destroy Remove event listeners and ARIA attributes.

Example:

  1. var accordion = new handorgel(document.querySelector('.handorgel'))
  2. // close first fold
  3. accordion.folds[0].close()

Browser compatibility

  • Newest two browser versions of Chrome, Firefox, Safari and Edge

Development

  • npm run build - Build production version of the feature.
  • npm run demo - Build demo of the feature, run watchers and start browser-sync.
  • npm run test - Test the feature.

License

MIT LICENSE