-
-
Save WesleySmits/fce1638bc0028976710fcb831773e133 to your computer and use it in GitHub Desktop.
Accessible Accordion - Accordion wrapper
This file contains 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
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