Sub topic 9
Nested popovers and how they close each other
This chapter assumes you can already open a popover with popovertarget, and that you know the difference between popover="auto" and popover="manual". It covers what happens when more than one popover is open at once.
The problem with opening a second popover
Open an auto popover, then open a different auto popover. The first one closes. Browsers do this so that two unrelated menus never sit on screen together.
That rule breaks a common pattern, a menu with a submenu. Opening the submenu must not close the menu that contains it. The browser handles this with a popover stack.
The popover stack
The browser keeps an ordered list of open auto popovers. When you open a new one, the browser looks for an ancestor of it that is already open. The new popover counts as a descendant of an open popover in either of these cases:
- It is nested in the open popover's DOM subtree.
- Its invoker button (the element with
popovertarget) is inside the open popover.
If the browser finds such an ancestor, it keeps that ancestor and everything below it in the stack, and it closes any open popovers above it. If it finds none, it closes all open auto popovers first. The new popover then goes on top of the stack.
A menu with a submenu
The submenu's invoker button sits inside the menu, so the two popovers are linked in the stack.
<button popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<button popovertarget="more">More</button>
<div id="more" popover>
<a href="/settings">Settings</a>
<a href="/help">Help</a>
</div>
</div>Clicking More opens more on top of menu. Both stay visible. The invoker is inside menu, so menu counts as the ancestor.
The submenu does not have to be nested in the markup. If more were a sibling of menu in the DOM, the same button would still link them, because the invoker is what counts.
How closing works
The stack also controls how popovers close.
- Escape closes only the topmost popover. Pressing it again closes the next one.
- Clicking outside all the popovers closes the whole stack.
- Clicking inside a lower popover closes everything above it and leaves that popover open.
- Calling
hidePopover()on a lower popover closes it and every popover above it.
This means a child can never stay open after its parent closes.
const menu = document.getElementById('menu');
const more = document.getElementById('more');
menu.showPopover();
more.showPopover();
menu.hidePopover();
console.log(more.matches(':popover-open')); // falseHere more.showPopover() opens the submenu on top of the menu. Hiding the menu then closes the submenu too, so the last line logs false.
Where manual popovers fit
A manual popover is not part of this light-dismiss behavior. It does not close other popovers when it opens, and opening an auto popover does not close it. Escape and outside clicks leave it alone.
Use this on purpose, for example for a toast notification that must survive while the user works with a menu. The cost is that your code, or an explicit hide button, has to close it.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Opening the submenu closes the menu | The invoker for the submenu is outside the menu, and the submenu is not nested in it |
| Escape closes only one layer | Expected behavior: Escape closes the topmost popover only |
| A popover never closes on outside click | It is `popover="manual"` |
Exercise
A page has an open auto popover named filters. The user clicks a button outside filters that opens another auto popover named sort. Neither popover is nested in the other. What is open afterward, and what would change if the button were inside filters?
Show answer
Only sort is open, because no ancestor relationship exists and the browser closes all open auto popovers before showing it. If the button were inside filters, filters would count as the ancestor of sort. Both would stay open, with sort on top of the stack.