@uswds/uswds
Version:
Open source UI components and visual style guide for U.S. government websites
28 lines (21 loc) • 646 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 controls = 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;
};