@uswds/uswds
Version:
Open source UI components and visual style guide for U.S. government websites
33 lines (26 loc) • 827 B
JavaScript
const EXPANDED = "aria-expanded";
const CONTROLS = "aria-controls";
const HIDDEN = "hidden";
module.exports = (button, expanded) => {
let safeExpanded = expanded;
if (typeof safeExpanded !== "boolean") {
safeExpanded = button.getAttribute(EXPANDED) === "false";
}
button.setAttribute(EXPANDED, safeExpanded);
const id = button.getAttribute(CONTROLS);
const root =
typeof button.getRootNode === "function" ? button.getRootNode() : document;
const controls =
typeof root.getElementById === "function"
? root.getElementById(id)
: document.getElementById(id);
if (!controls) {
throw new Error(`No toggle target found with id: "${id}"`);
}
if (safeExpanded) {
controls.removeAttribute(HIDDEN);
} else {
controls.setAttribute(HIDDEN, "");
}
return safeExpanded;
};