Sub topic 3
Nested popovers and choosing the right overlay
Assume basic HTML, CSS selectors, and JavaScript event listeners. This chapter focuses on how popovers interact when several overlays are open.
Nested popovers and how they close each other
An auto popover participates in the browser's popover stack. Opening a child popover from an open parent keeps the parent available. Opening an unrelated auto popover usually closes the previous popover stack. A manual popover does not use this automatic dismissal behavior.
Design nested controls so each trigger remains usable. Avoid opening several unrelated menus at once.
Focus, keyboard use, and accessibility
A popover is non-modal. It does not make the rest of the page inert and does not trap focus like a modal dialog. Use a real, labeled <button> as the invoker. Keep keyboard focus visible, and ensure every interactive element has a useful name.
auto popovers normally close when the user presses Escape or clicks outside. A manual popover requires an explicit close action or JavaScript. Test nested popovers with Tab, Shift+Tab, Escape, and screen-reader navigation.
> callout note > A popover does not automatically make arbitrary content accessible. Give the content a suitable structure and use a dialog only when the interaction truly requires modal behavior. > ```
Animating entry and exit
Use @starting-style for the opening values. Discrete transitions keep display and the top-layer overlay active until the closing animation finishes.
[popover] {
opacity: 0;
transform: translateY(-8px);
transition:
opacity 0.2s,
transform 0.2s,
overlay 0.2s allow-discrete,
display 0.2s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
transform: none;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: translateY(-8px);
}
}Popover or dialog
Choose a popover for menus, help text, pickers, and other non-modal content. Choose a modal dialog when the user must finish or cancel a task before returning to the page. A dialog provides modal semantics, focus management, and background inertness.
Positioning near the invoker
CSS anchor positioning can place a popover beside its button:
#account-button {
anchor-name: --account-button;
}
#account-menu {
position: fixed;
inset: auto;
position-anchor: --account-button;
top: anchor(bottom);
left: anchor(left);
margin-top: 0.5rem;
}Provide a sensible fallback for browsers that do not support anchor positioning.
Feature detection and fallbacks
Detect the popover API before depending on it:
const supportsPopover =
typeof HTMLElement !== "undefined" &&
Object.prototype.hasOwnProperty.call(HTMLElement.prototype, "popover");
if (!supportsPopover) {
document.documentElement.classList.add("no-popover");
}The fallback can use a small JavaScript controller that toggles a class, manages Escape, and restores focus.
Exercise
Which overlay should you choose for a non-modal account menu, and which behavior should it use?
Show answer
Use an auto popover. It supports light dismissal, Escape, and automatic interaction with the popover stack.