Sub topic 4
Choosing between auto and manual popovers
This chapter assumes you can add the popover attribute and wire it to a button with popovertarget. It also assumes you know basic HTML events. Here you will learn the two popover types and how to pick one.
Two values, two behaviors
The popover attribute takes a value that sets its type.
popoverorpopover="auto"makes an auto popover. An empty value means auto.popover="manual"makes a manual popover.
The type decides how the popover closes. It does not change how it opens. Both types open from an invoker button, from showPopover(), or from togglePopover(), and both appear in the top layer.
| Behavior | auto | manual |
|---|---|---|
| Click outside closes it | Yes | No |
| Escape closes it | Yes | No |
| Opening another popover closes it | Yes, if unrelated | No |
| Focus returns to the invoker on close | Yes | No |
A value that is not auto or manual, such as popover="menu", is treated as manual. Check your spelling, because a typo silently removes light dismiss.
Auto: the default for transient UI
An auto popover is for content the user looks at briefly and then leaves, such as menus, tooltips with extra content, and pickers. The browser closes it when the user clicks elsewhere or presses Escape. This is called light dismiss, and the next chapter covers it in detail.
Auto popovers also keep each other tidy. Opening one closes any other auto popover that is not its ancestor. The page never ends up with three unrelated menus open at once.
<button popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>Manual: you control the lifetime
A manual popover stays open until your code or an explicit button closes it. Clicking outside does nothing. Escape does nothing. Opening another popover leaves it alone.
This suits content that must persist while the user does other things, such as a toast notification or a floating help panel.
<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();
setTimeout(() => notice.hidePopover(), 5000);
</script>The popover shows on load and hides itself after five seconds. The Dismiss button lets the user close it sooner.
How to decide
Start with auto. Switch to manual only when light dismiss would be wrong. For example, an auto toast would vanish the moment the user clicked anywhere on the page.
Common errors
- Using manual with no close control. The user has no keyboard way to close it. Always add a hide button or a timer.
- Expecting an auto popover to survive a click elsewhere. If a click in your own page logic opens a second auto popover, the first one closes. Make the second popover a manual one, or nest it, if both must stay open.
- Forgetting that manual does not restore focus. Move focus yourself if the popover took it.
Exercise
A page shows a "Saved" message that must stay visible while the user keeps typing and clicking elsewhere, until it times out. Which popover type fits, and what must you add?
Show answer
Use popover="manual", because auto would close on the first outside click. Add a close path, such as a Dismiss button with popovertargetaction="hide" or a timer that calls hidePopover().