Sub topic 9
Nested popovers and how they close each other
This chapter explains how the browser decides which open popovers to close when you open another one. It assumes you know the popover attribute, the popovertarget invoker button, the difference between auto and manual popovers, and how light dismiss works. You should also be comfortable with showPopover() and hidePopover().
The popover stack
The browser keeps an ordered list of open auto popovers. This list is the popover stack. The oldest open popover is at the bottom and the newest is at the top. Three rules govern it:
- Opening an
autopopover pushes it onto the stack. - Closing a popover also closes every popover above it in the stack.
- Before an
autopopover opens, the browser closes any openautopopovers that are not its ancestors.
The third rule is why two unrelated popovers cannot be open at once. It is also why a submenu can stay open together with its parent menu.
manual popovers are not on this stack. They never close other popovers when they open, and light dismiss and Escape do not close them. The chapter on choosing between auto and manual covers that difference. Here, only auto popovers are in play unless the text says otherwise.
What counts as an ancestor
Two relationships make one popover an ancestor of another.
- DOM nesting. The second popover is a descendant of the first element in the document.
- Invoker nesting. The button with
popovertargetthat opens the second popover sits inside the first popover.
Either relationship is enough. A button inside popover A that targets popover B makes A an ancestor of B, even if B is somewhere else in the document.
This example is a menu with a submenu. The submenu's invoker lives inside the menu, and the submenu is a separate sibling element.
<button popovertarget="menu">Open menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<button popovertarget="more">More options</button>
</div>
<div id="more" popover>
<a href="/export">Export data</a>
<a href="/delete">Delete account</a>
</div>When you open menu, it goes on the stack. When you then activate "More options", the browser finds that the invoker is inside menu. So menu is an ancestor of more, and menu stays open. The stack is now menu, more.
Now compare an unrelated pair.
<button popovertarget="a">Open A</button>
<button popovertarget="b">Open B</button>
<div id="a" popover><p>Popover A</p></div>
<div id="b" popover><p>Popover B</p></div>Both invokers are outside both popovers. Opening A and then clicking "Open B" closes A first, because A is not an ancestor of B. Only B is open afterward.
What happens when you show a popover
The following flowchart shows the decision for an auto popover that is about to open.
The result is that the stack always forms a single chain from a root popover down to the newest one. Each popover in the chain is an ancestor of the next.
Light dismiss with a stack
Light dismiss means closing a popover by clicking outside it or pressing Escape. With a stack, the click target decides how much closes.
- A click inside the topmost popover closes nothing.
- A click inside a lower popover closes everything above it and leaves that popover and the ones beneath it open.
- A click outside every popover closes the whole stack.
- Escape closes only the topmost popover. Pressing it again closes the next one down.
With the menu and submenu above, clicking on empty page content closes both. Clicking the "Profile" area of menu while more is open closes more and keeps menu open.
Closing a parent closes its children
The rule "closing a popover closes everything above it" applies to every way of closing. Calling hidePopover() on a parent, activating a hide button for it, or opening an unrelated popover all follow it. This script closes a parent while a child is open:
const menu = document.getElementById('menu');
const more = document.getElementById('more');
menu.showPopover();
more.showPopover();
console.log(menu.matches(':popover-open')); // true
console.log(more.matches(':popover-open')); // true
menu.hidePopover();
console.log(menu.matches(':popover-open')); // false
console.log(more.matches(':popover-open')); // falseThe reverse does not hold. Calling more.hidePopover() closes only more, and menu stays open.
Showing nested popovers from script
The two showPopover() calls above work for the same reason as the buttons. The more popover is not a DOM descendant of menu, and a script call sets up no invoker relationship. So this code only works if some other relationship exists between the two elements.
Consider the HTML from earlier, where more's invoker button is inside menu. The relationship comes from the popovertarget attribute on that button, not from the script. If you remove the button and call showPopover() on more while menu is open, the browser sees no ancestor relationship and closes menu first.
To make the relationship explicit when you control the markup, nest the child inside the parent in the DOM.
<div id="menu" popover>
<a href="/profile">Profile</a>
<div id="more" popover>
<a href="/export">Export data</a>
</div>
</div>Here more is a DOM descendant of menu. Opening more from script keeps menu open, with no invoker button required. A nested popover is still hidden by default and does not render inside the parent's box. It appears in the top layer, so the parent's overflow or transform does not clip it.
Observing the order of closing
The beforetoggle and toggle events fire on each popover that closes as a side effect, so you can log what the browser does. This script logs every state change across all popovers.
document.querySelectorAll('[popover]').forEach((el) => {
el.addEventListener('toggle', (event) => {
console.log(`${el.id}: ${event.oldState} -> ${event.newState}`);
});
});Open a, then use an invoker outside both popovers to open b. The log shows a going from open to closed and b going from closed to open. Use this log when a popover closes and you can't tell why.
Mixing manual and auto popovers
A manual popover can be nested like any other element, but it is outside the stack logic.
- Opening a
manualpopover does not close anyautopopover. - Opening an
autopopover does not close anymanualpopover. - Light dismiss and Escape skip
manualpopovers.
This makes manual the right choice for persistent UI such as a toast or a status panel. The panel stays put while the user opens and closes menus. The cost is that your code must close it.
<div id="status" popover="manual">
<p>Syncing...</p>
<button popovertarget="status" popovertargetaction="hide">Hide</button>
</div>Common errors
- A submenu closes its parent. The invoker is not inside the parent, and the submenu is not a DOM descendant of it. Move the invoker button into the parent or nest the submenu inside it.
- A tooltip popover closes a dialog-like popover. Both are
autoand unrelated. Make the tooltipmanualif it should not affect the other. - Two popovers refuse to be open together. This is the intended behavior for unrelated
autopopovers. Usemanualif you need both. - Escape closes a popover you expected to stay. Escape closes the topmost
autopopover. Check whether a child opened on top of it.
Quick reference
| Situation | Result |
|---|---|
| Open an auto popover while an unrelated auto popover is open | The unrelated one closes |
| Open an auto popover whose invoker is inside an open auto popover | The parent stays open |
| Open an auto popover nested in the DOM inside an open auto popover | The parent stays open |
| Close a parent popover | Its open children close too |
| Close a child popover | The parent stays open |
| Open or dismiss anything while a manual popover is open | The manual popover is unaffected |
Exercise
A page has a popover settings that contains a button with popovertarget="theme". The popover theme is a separate element elsewhere in the document, and both are auto. A user opens settings, then theme, then clicks a blank area of the page. Which popovers are open at each step, and what changes if theme is manual?
Show answer
After opening settings, only settings is open. After opening theme, both are open, because the invoker for theme is inside settings, which makes settings an ancestor. Clicking blank page space light dismisses the whole stack, so both close. If theme is manual, it is not on the stack: opening it would not close or keep settings in any special way, and the blank click closes only settings. theme stays open until its own hide button or a script closes it.