packages/uswds-core/src/js/utils/toggle.js
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;
};