Sub topic 4
Choosing between auto and manual popovers
This chapter covers the two popover types and how to pick one. It assumes you can write basic HTML, CSS and JavaScript, and that you have used the popover attribute with a popovertarget button from earlier chapters.
The two types
The popover attribute takes a value that sets the popover's type. Two types matter in practice.
| Attribute | Type | Closes on outside click | Closes on Escape | Closes other popovers when opened |
|---|---|---|---|---|
| `popover` | auto | Yes | Yes | Yes (unrelated auto popovers) |
| `popover="auto"` | auto | Yes | Yes | Yes |
| `popover="manual"` | manual | No | No | No |
An empty popover attribute and popover="auto" mean the same thing. The shorthand is the usual way to write an auto popover.
The two types differ in how they close. Both types open the same way, with an invoker button or the JavaScript API. Both appear in the top layer, and both use the same styling hooks. The type decides who is responsible for closing the popover: the browser (auto) or you (manual).
Auto popovers
An auto popover gives you three behaviors without any code.
- Light dismiss. Clicking or tapping outside the popover closes it. The press and the release must both happen outside, so dragging a text selection out of the popover does not close it.
- Escape. Pressing Escape closes the topmost open auto popover. On some platforms, other "close request" gestures, such as the Android back gesture, do the same.
- One at a time. Opening an auto popover closes any other open auto popover, with one exception: popovers in the same nesting chain stay open (see the nesting chapter).
Here is a typical auto popover, a navigation menu:
<button popovertarget="menu" popovertargetaction="toggle">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
<button popovertarget="menu" popovertargetaction="hide">Close</button>
</div>You did not write any script, and the menu still closes in four ways: the Close button, the Menu toggle, an outside click, and Escape.
Focus behavior
When an auto popover closes, the browser returns focus to the element that had focus before the popover opened, as long as focus was inside the popover when it closed. Keyboard users therefore land back on the button that opened it. This is one of the main reasons to prefer auto for interactive content.
Manual popovers
A manual popover has none of these automatic behaviors. It stays open until something explicitly closes it. Opening it does not close other popovers, and opening other popovers does not close it. Several manual popovers can be open at once.
<div id="notice" popover="manual">
<p>Your changes were saved.</p>
<button popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>
<script>
const notice = document.getElementById('notice');
notice.showPopover();
</script>Two things in this example are required, not optional:
- A way to close it. Without the Dismiss button (or a script call to
hidePopover()), a keyboard user has no way to get rid of the notice. - A way to open it. A manual popover starts hidden. Here a script opens it. A
popovertargetbutton works too.
A manual popover that closes itself
Manual popovers suit transient messages that should not interrupt what the user is doing. This toast closes after a delay. It also guards against stacking timers when the function runs twice:
<button id="save-btn">Save</button>
<div id="toast" popover="manual">Saved.</div>
<script>
const toast = document.getElementById('toast');
let timer;
document.getElementById('save-btn').addEventListener('click', () => {
toast.showPopover();
clearTimeout(timer);
timer = setTimeout(() => toast.hidePopover(), 3000);
});
</script>Clicking Save a second time while the toast is open would normally call showPopover() on an open popover. In current browsers that call does nothing, but in older implementations it throws an InvalidStateError. If you must support both, guard it with if (!toast.matches(':popover-open')).
The toast does not take focus, so it does not disturb typing in a form. An auto popover would have closed whenever the user clicked anywhere else, and would have closed any menu the user had open when it appeared.
The key difference: who else is affected
An auto popover can close other things. A manual popover cannot, and nothing closes it either. That has practical consequences:
- If a notification (manual) is open and the user opens a menu (auto), the notification stays. With two auto popovers, the first would have closed.
- If the user presses Escape with only a manual popover open, nothing happens. The browser does not offer a built-in way to close it from the keyboard.
- If the user clicks outside a manual popover, the popover stays on screen and the page behind it remains fully interactive. Manual popovers are non-modal, like all popovers.
How to decide
The table below maps common patterns to a type.
| Pattern | Type | Reason |
|---|---|---|
| Dropdown or navigation menu | auto | Users expect Escape and outside click to close it |
| Tooltip-like help box opened by a button | auto | Short-lived, dismissible by any outside action |
| Date picker or color picker | auto | Selecting a value or clicking away ends the task |
| Toast or status message | manual | Appears without user action and must not close other UI |
| Persistent side help panel | manual | Stays while the user works elsewhere on the page |
| Cookie or consent banner | manual | Must not vanish on a stray click |
| Several simultaneous floating tools | manual | Auto would close all but one |
A useful test: "If the user clicks somewhere else, is the popover now irrelevant?" If yes, choose auto. If the content has its own lifetime, choose manual.
Common errors
Manual popover with no close control. The popover cannot be dismissed by keyboard or pointer. Always add a hide button, a timer, or an explicit script path.
Opening an auto popover from a script and expecting it to survive. Auto popovers still light dismiss when opened with showPopover(). If a click outside, or opening another auto popover, closes yours unexpectedly, you probably want manual.
Opening two auto popovers and expecting both to stay. The second one closes the first unless they are nested. If you need both visible, use manual for at least one.
Using manual to avoid dealing with dismissal. If you pick manual just because you do not want the popover to close, you take on the work of Escape handling, focus return and outside clicks. For menus and pickers, auto is less code and behaves consistently with the browser's other controls.
Misspelled value. As noted above, an invalid value behaves as manual, so the popover looks like it ignores Escape.
Checking state wrongly. Use element.matches(':popover-open') to see whether a popover is showing. Checking hidden or style.display does not work, because the browser hides popovers through its own default styles.
const panel = document.getElementById('panel');
function isOpen(el) {
return el.matches(':popover-open');
}
console.log(isOpen(panel)); // false until it is shownSummary of the trade-off
Auto trades control for convenience. You get Escape, outside click, single-open behavior and focus return for free, and you give up the ability to keep a popover open across those events. Manual gives you full control, and you must supply every way of closing it. Start with auto. Switch to manual only when the popover must outlive an outside click or must coexist with other open popovers.
Exercise
A page shows a "Draft saved" message after the user clicks Save. The user may have a dropdown menu open at that moment, and the message should disappear by itself after a few seconds. Which popover type should the message use, and why? Name one thing you must add.
Show answer
Use popover="manual". An auto popover would close the open dropdown menu when it appeared, and it would also close on any outside click. A manual popover leaves other popovers alone. You must add a way to close it, such as a setTimeout that calls hidePopover(), and ideally a Dismiss button with popovertargetaction="hide" for users who want it gone sooner.
Exercise
You write <div id="tip" popover="manaul"> and notice that pressing Escape does not close the popover. What is happening and how do you fix it?
Show answer
"manaul" is an invalid value, and browsers treat an invalid popover value as manual, which ignores Escape and outside clicks. Fix the typo, or if you want auto behavior, use popover or popover="auto".