Expand Toggle
Simple and accessible expandable functionality, similar to jQuery’s slideToggle()
method.
Inspired by https://inclusive-components.design/menus-menu-buttons#truemenus.
Install
yarn add @threespot/expand-toggle
Usage
JavaScript
; const toggles = document; // ES6toggles; // ES5for var i = 0 len = toggleslength; i < len; i++ togglesi;
Styles
The following minimum styles are required:
// This class name is just an example .expandable // We also suggest hiding the button when JS is disabled. // Note: Modernizr looks for a “no-js” class on the html tag and replaces it with “js” on load. // If not using Modernizr, see https://www.paulirish.com/2009/avoiding-the-fouc-v3/ .no-js
Markup
Open This content will be hidden to start.
Options
data-expands-class
defines a class (or multiple classes) to apply to the toggle button and expandable element when expanded
data-expands-height
transitions the menu height using max-height
and CSS transitions (see required CSS above)
data-expands-text
defines button text to use when expanded
These options can also be set in JavaScript:
el expandedClasses: "is-expanded" shouldToggleHeight: true activeToggleText: "Close";
Events
ready
Since the ready
event may be trigger immediately, bind using the onReady
option:
const toggle = el { console; };
expand
Triggered when component is expanded
toggle;
collapse
Triggered when component is collapsed
toggle;
destroy
Triggered when component is destroyed
toggle;
License
This is free software and may be redistributed under the terms of the MIT license.
About Threespot
Threespot is an independent digital agency hell-bent on helping those, and only those, who are committed to helping others. Find out more at https://www.threespot.com.