Sub topic 5
Light dismiss: clicking outside and pressing Escape
This chapter assumes you can add the popover attribute and connect it to a button with popovertarget, and that you know the difference between popover="auto" and popover="manual". You also need basic event listeners in JavaScript. Here you will learn exactly what closes a popover without any code from you, and what does not.
What light dismiss means
Light dismiss is the browser closing a popover because the user did something lightweight. They clicked somewhere else, pressed Escape, or opened a different popover. The user never has to find a Close button.
Light dismiss belongs to auto popovers. A bare popover attribute and popover="auto" mean the same thing, so both get it. A popover="manual" element gets none of it.
<button popovertarget="tips">Tips</button>
<div id="tips" popover>
<p>Click anywhere outside, or press Escape, to close me.</p>
</div>
<button popovertarget="status">Status</button>
<div id="status" popover="manual">
<p>I stay open until something explicitly closes me.</p>
<button popovertarget="status" popovertargetaction="hide">Close</button>
</div>Open both and try to dismiss them. The tips popover goes away on an outside click or Escape. The status popover ignores both.
The three triggers
An auto popover closes in three situations.
- A click outside it. The user presses and releases the pointer on something that is not the popover and not its invoker.
- A close request. On desktop this is the Escape key. Some platforms have an equivalent, such as the back gesture on Android.
- Another auto popover opens. The browser closes any open auto popover that is not related to the new one.
The third trigger surprises people most. Opening a second popover is itself a dismissal of the first, even though the user never clicked "outside" in the usual sense.
What counts as an outside click
The browser checks where the press started and where it ended. If both are outside the popover, it closes. If the user presses inside a text field in the popover and drags the selection out past the edge, the release lands outside but the press did not, so the popover stays open. This protects text selection.
The invoker button is treated as part of the popover for this check. That matters because of what would happen otherwise:
- The popover is open and the user clicks its invoker to close it.
- The press counts as outside, so light dismiss closes the popover.
- The button's own click then runs
toggleand opens it again.
The popover would flicker and never close. Because the browser counts the invoker as "inside", only the button's own action runs, and the click closes the popover once.
Focus is not part of the check. Tabbing out of an open popover, or clicking into the browser's address bar, does not close it.
The Escape key
Pressing Escape closes the topmost open auto popover. If you have a stack of nested popovers, each press closes one level, starting from the innermost. The next chapter covers stacks in detail.
Escape does not close manual popovers. If you open one with showPopover() and the user has no Close button, the only way out is code.
| Behavior | `auto` | `manual` |
|---|---|---|
| Closes on outside click | Yes | No |
| Closes on Escape | Yes | No |
| Closes when another popover opens | Yes, if unrelated | No |
| Closes other popovers when it opens | Yes, if unrelated | No |
| Has a built-in close path | Yes | Only what you write |
Manual popovers do not affect auto popovers
The last row of the table has a useful consequence. Opening a manual popover never closes an auto popover, and an auto popover never closes a manual one. The two kinds are independent.
That makes manual popovers fit for things like toast notifications. A toast can appear while the user has a menu open, and neither disturbs the other.
Adding dismissal behavior to a manual popover
Sometimes you want a manual popover that closes on Escape but not on an outside click. A user typing in a form should not lose it by accident. You can write that yourself:
<button id="open-help">Open help</button>
<div id="help" popover="manual">
<p>Press Escape to close this help panel.</p>
<button popovertarget="help" popovertargetaction="hide">Close</button>
</div>
<script>
const help = document.getElementById('help');
document.getElementById('open-help').addEventListener('click', () => {
help.showPopover();
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && help.matches(':popover-open')) {
help.hidePopover();
}
});
</script>The matches(':popover-open') check matters. Calling hidePopover() on a popover that is already closed is harmless in current browsers, but the check keeps the intent clear. Do not call showPopover() on an open popover blindly, because that throws an InvalidStateError in current browsers.
An outside-click handler for a manual popover is also possible. If you are writing one, you are usually better off using auto and letting the browser handle it. The built-in version already deals with the invoker, text selection, and stacks.
Observing a dismissal
Your code cannot easily tell why an auto popover closed. The toggle event reports oldState and newState, but not the cause. If you only need to react to a close, that is enough:
const tips = document.getElementById('tips');
tips.addEventListener('toggle', (event) => {
if (event.newState === 'closed') {
console.log('tips closed, by light dismiss or otherwise');
document.querySelector('[popovertarget="tips"]').focus();
}
});The beforetoggle event fires first. When it is for an open, it is cancelable, so you can call event.preventDefault() to stop the popover opening. The event for a close is not cancelable. You cannot veto light dismiss from a listener. If a popover must not close from an outside click, it should be manual, not auto with a workaround.
Focus after dismissal
When light dismiss closes a popover, the browser returns focus to the previously focused element, generally the invoker that opened it. The user's place in the page is kept, which matters for keyboard use. The code above refocuses explicitly, which is only needed if you opened the popover from script and focus would otherwise land elsewhere. Test with the keyboard before adding manual focus code, since the default is often already right.
Common mistakes
- Expecting Escape to close a manual popover. It will not. Add a key handler or a Close button.
- Putting a popover inside a stylesheet-hidden parent and expecting outside clicks to work. Light dismiss uses the real DOM structure, so check where the popover sits in the tree if dismissal behaves oddly.
- Opening two unrelated auto popovers at once. Opening the second closes the first. Use
manualfor one of them if both must show. - Calling
showPopover()from a handler that also triggers light dismiss. Apointerdownhandler that opens a popover can open it and then have the same interaction close it. Useclickfor the opening action. - Preventing default on Escape. A global
keydownhandler that callspreventDefault()on Escape can interfere with the browser's close request on some platforms. Avoid it unless you have a clear reason.
Exercise
You have a cookie notice that must stay visible when the user clicks elsewhere on the page, but it should close when the user presses Escape. Which popover value do you use, and what extra code do you need?
Show answer
Use popover="manual", because an auto popover would close on any outside click. Add a keydown listener on document that checks event.key === 'Escape' and calls hidePopover() on the notice when it matches :popover-open. Also include a Close button using popovertargetaction="hide".
Exercise
A user opens an auto popover, then clicks its invoker button again. Why does the popover close once instead of closing and immediately reopening?
Show answer
The browser treats the invoker as part of the popover for the light dismiss check, so the press on the button does not count as an outside click. Only the button's own toggle action runs, and it closes the popover once.
Exercise
You attach a beforetoggle listener and call event.preventDefault() when newState is 'closed', hoping to stop light dismiss. Does it work? What should you do instead?
Show answer
No. The beforetoggle event for closing is not cancelable, so the popover still closes. If the popover must not close from outside clicks or Escape, use popover="manual" and close it only through your own code or a Close button.