A custom element for building accessible disclosure widgets.
import { DisclosureElement } from 'inclusive-elements';
window.customElements.define('ui-disclosure', DisclosureElement);
<ui-disclosure>
<button type="button">Summary</button>
<div>Details</div>
</ui-disclosure>
-
The first descendant that is a
<button>
or hasrole="button"
will be given thearia-expanded
attribute, which will reflect the open state of the disclosure widget. -
Clicking the button will toggle the disclosure widget. The
hidden
attribute will be toggled on the content element.
const disclosure = document.querySelector('ui-disclosure');
// Programatically open and close the widget.
disclosure.open = true;
disclosure.addEventListener('toggle', callback);
/* Transitions can be applied to the content using hello-goodbye */
@media (prefers-reduced-motion: no-preference) {
ui-disclosure > .enter-active,
ui-disclosure > .leave-active {
transition: all 0.5s;
}
ui-disclosure > .enter-from,
ui-disclosure > .leave-to {
opacity: 0;
transform: scale(0.5);
}
}