Loading...
let isOpen = false;
button.addEventListener('click', () => {
isOpen = !isOpen;
isOpen ? menu.showPopover() : menu.hidePopover();
});button.addEventListener('click', () => menu.togglePopover());
menu.addEventListener('toggle', (e) => {
button.setAttribute('aria-expanded', String(e.newState === 'open'));
});The popover's own state is the source of truth, so Esc and outside clicks keep aria-expanded correct.
isOpen variable that drifts when the user closes the popover with Esc.popovertarget, not a click handler with togglePopover().togglePopover() is used to know the new state, not a separate flag.aria-expanded is updated in the toggle event.It returns true if the popover is open after the call and false if it is closed.
Yes. Pass true to only open it or false to only close it, like togglePopover(true).