Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Last active October 30, 2022 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WesleySmits/fce1638bc0028976710fcb831773e133 to your computer and use it in GitHub Desktop.
Save WesleySmits/fce1638bc0028976710fcb831773e133 to your computer and use it in GitHub Desktop.
Accessible Accordion - Accordion wrapper
class Accordion extends HTMLElement {
#curtains = [];
connectedCallback() {
this.#curtains = Array.from(this.querySelectorAll('.nh-curtain-new'));
this.#curtains.forEach((curtain) => {
curtain.addEventListener('toggle', this.#handleToggle.bind(this, curtain));
});
}
disconnectedCallback() {
this.#curtains.forEach((curtain) => {
curtain.removeEventListener('toggle', this.#handleToggle.bind(this, curtain));
});
}
#handleToggle(curtain) {
if (!curtain.open) {
return;
}
this.#closeOtherCurtains(curtain);
}
#closeOtherCurtains(curtain) {
const otherCurtains = this.#curtains.filter((c) => curtain !== c);
otherCurtains.forEach((otherCurtain) => {
const animations = otherCurtain.getAnimations();
if (animations.length === 0) {
otherCurtain.close();
return;
}
Promise.all(animations.map((animation) => { animation.finished })).then(() => {
otherCurtain.close();
});
otherCurtain.close();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment