Created
February 17, 2022 01:50
-
-
Save simaodeveloper/2410427de7279375c49eb3a200778c6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pex.add({ | |
name: 'Accordion', | |
init: (_elements, options) => { | |
const elements = Array.from(_elements); | |
// tools | |
const isTitle = target => target.classList.contains('accordion__title'); | |
const isOpen = target => target.classList.contains('accordion--open'); | |
const runThrough = (elements, callback) => elements.forEach(callback); | |
// core | |
const close = element => element.classList.remove('accordion--open'); | |
const open = element => element.classList.add('accordion--open'); | |
const toggle = element => isOpen(element) ? close(element) : open(element); | |
const destroy = () => { | |
runThrough(elements, element => { | |
close(element); | |
removeHandler(element); | |
}); | |
} | |
// handlers | |
const onHandler = element => event => isTitle(event.target) && toggle(element); | |
const addHandler = element => element.addEventListener('click', onHandler(element)); | |
const removeHandler = element => element.removeEventListener('click', onHandler(element)); | |
const loadListeners = () => runThrough(elements, addHandler); | |
const initialize = () => { | |
loadListeners(); | |
}; | |
initialize(); | |
return { | |
open, | |
close, | |
toggle, | |
destroy, | |
}; | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment