Expand Toggle
![Coverage Status](https://coveralls.io/repos/github/Threespot/expand-toggle/badge.svg)
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
import ExpandToggle from "@threespot/expand-toggle";
const toggles = document.querySelectorAll("[data-expands]");
toggles.forEach(el => new ExpandToggle(el));
for (var i = 0, len = toggles.length; i < len; i++) {
new ExpandToggle(toggles[i]);
}
Styles
The following minimum styles are required:
.expandable {
$transition-speed: 400ms;
&,
.js &[aria-hidden="false"] {
overflow: hidden;
transition: max-height $transition-speed ease-in-out,
visibility 0s linear 0s;
visibility: visible;
}
&[aria-hidden="true"],
.js &:not([aria-hidden]) {
max-height: 0 !important;
transition: max-height $transition-speed ease-in-out,
visibility 0s linear $transition-speed;
visibility: hidden;
}
}
.no-js [data-expands] {
display: none;
}
Markup
<button type="button" data-expands="demo" data-expands-class="is-expanded" data-expands-height>
<span data-expands-text="Close">Open</span>
</button>
<div class="expandable" id="demo">
<p>This content will be hidden to start.</p>
</div>
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:
new ExpandToggle(el, {
expandedClasses: "is-expanded",
shouldToggleHeight: true,
activeToggleText: "Close",
});
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.
![Threespot](https://avatars3.githubusercontent.com/u/370822?v=3&s=100)