Skip to content

Instantly share code, notes, and snippets.

@i-like-robots
Created July 19, 2016 16:08
Show Gist options
  • Save i-like-robots/1f98e64e9c1c0a684463b0c820046820 to your computer and use it in GitHub Desktop.
Save i-like-robots/1f98e64e9c1c0a684463b0c820046820 to your computer and use it in GitHub Desktop.
universal accessible toggle button
module.exports = function (buttons, target, callback) {
buttons = buttons.length === undefined ? [ buttons ] : Array.from(buttons);
const toggle = function (e) {
const state = target.classList.toggle('is-active');
buttons.forEach(button => {
button.setAttribute('aria-expanded', state);
});
target.setAttribute('aria-hidden', !state);
e && e.preventDefault();
callback && callback(e, state ? 'open' : 'close');
};
buttons.forEach(button => {
if (button.nodeName !== 'BUTTON') {
button.setAttribute('role', 'button');
}
button.setAttribute('aria-expanded', 'false');
button.addEventListener('click', toggle);
});
target.setAttribute('aria-hidden', 'true');
return toggle;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment