Sub topic 5
Light dismiss: clicking outside and pressing Escape
This chapter assumes you can add the popover attribute and wire a popovertarget button to it, as covered earlier. It also assumes you know the difference between popover (auto) and popover="manual".
What light dismiss means
Light dismiss is the set of everyday ways a user closes a popover without pressing a dedicated close button. For an auto popover, the browser handles three of them:
- Clicking or tapping outside the popover.
- Pressing Escape.
- Opening a different auto popover that is not related to it.
You write no script for any of this. It is built into popover and popover="auto", which mean the same thing.
<button popovertarget="tips">Show tips</button>
<div id="tips" popover>
<p>Click anywhere else, or press Escape, to close this.</p>
</div>Clicking outside
A click counts as outside when the page registers it on something that is not the popover or one of its descendants. The browser then closes the popover.
There are two details to know:
- The press and the release must both land outside the popover. If a user presses inside a text field in the popover and drags the pointer out before releasing, the popover stays open. This stops text selection from closing it by accident.
- Clicking the invoker button of an open popover toggles it closed. It does not close it and then reopen it. The browser treats the invoker as part of the popover for this purpose.
Pressing Escape
Escape closes the topmost open auto popover. If several are stacked, each press closes one, starting with the newest. Focus returns to the element that was focused before the popover opened, which is usually the invoker.
Only one unrelated popover at a time
Opening an auto popover closes any other open auto popover that is not its ancestor. Popovers that are nested inside each other stay open together, which the nesting chapter covers. This is why a row of menu buttons behaves like a menu bar. Opening one closes the previous one.
Turning light dismiss off
Use popover="manual" when the popover must not vanish unexpectedly. Examples are a save confirmation or a toast message. A manual popover ignores outside clicks and Escape. It also does not close other popovers when it opens. You must close it with a hide button or with hidePopover().
Observing a dismissal
Light dismiss goes through the same state change as any other close, so the toggle event reports it. The event does not say what caused the close, only the new state.
<button popovertarget="note">Open note</button>
<div id="note" popover>
<p>Dismiss me with Escape or an outside click.</p>
</div>
<script>
const note = document.getElementById('note');
note.addEventListener('toggle', (event) => {
if (event.newState === 'closed') {
console.log('The note was closed.');
}
});
</script>Common errors
- Expecting Escape to close a manual popover. It will not. Switch to auto, or add your own
keydownhandler that callshidePopover(). - Trying to cancel a dismissal. The
beforetoggleevent is cancelable only when the popover is about to open. You cannot stop a close by callingpreventDefault(). If the popover must stay open, usemanual. - Opening a popover from script and seeing it close at once. An outside click that happens in the same gesture, or a second popover opening right after, can dismiss it. Check that nothing else opens an auto popover at the same moment.
- Putting a custom trigger outside the popover without
popovertarget. The browser does not know your script button belongs to the popover, so a click on it counts as an outside click. Wire the button withpopovertarget, or accept that it will close and reopen.
Exercise
A popover with the manual value is open. The user presses Escape and clicks elsewhere on the page. What happens, and how can the user close it without editing the popover type?
Show answer
Nothing happens, because manual popovers ignore both outside clicks and Escape. The user needs a control that closes it, such as a button with popovertarget pointing at the popover and popovertargetaction="hide", or code that calls hidePopover().