Sub topic 12
Popover or dialog: choosing the right tool
This chapter assumes you know how popover, popovertarget, and showPopover() work from the earlier chapters. It also assumes you have used the <dialog> element, or at least know that it exists.
Popovers and dialogs both render in the top layer, so they can look alike. They differ in how they treat the rest of the page, and that difference should drive your choice.
The deciding question
Ask whether the user must deal with the overlay before doing anything else.
- If yes, use a modal dialog. Examples are confirming a deletion, a required sign-in, or a form that must be finished or cancelled.
- If no, use a popover. Examples are a menu, a help tip, a toast, or a list of suggestions.
How they differ
| Behavior | Popover | Modal dialog |
|---|---|---|
| Rest of the page | Stays interactive | Becomes inert |
| Focus | Not trapped | Kept inside the dialog |
| Light dismiss | Yes for `auto` | No, but Escape closes it |
| Opens without JavaScript | Yes, with `popovertarget` | No, needs `showModal()` |
| Backdrop | Transparent by default | Dimmed by default |
| Built-in semantics | None | `dialog` role |
Inert means the browser ignores clicks, focus, and assistive technology reading for everything outside the dialog. A popover never does this. That is why a popover suits lightweight, optional content.
The same task, both ways
A menu is optional, so it should be a popover:
<button popovertarget="actions">Actions</button>
<div id="actions" popover>
<button>Rename</button>
<button>Duplicate</button>
</div>A destructive confirmation should block the page, so it should be a modal dialog:
<button id="delete-btn">Delete project</button>
<dialog id="confirm">
<form method="dialog">
<p>Delete this project permanently?</p>
<button value="cancel">Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
<script>
const dialog = document.getElementById('confirm');
document.getElementById('delete-btn').addEventListener('click', () => {
dialog.showModal();
});
dialog.addEventListener('close', () => {
console.log(dialog.returnValue);
});
</script>The form with method="dialog" closes the dialog and stores the clicked button's value in returnValue. Popovers have no equivalent, so you would need your own code to report a result.
Common errors
- Using a popover for a required decision. The user can click outside, press Escape, or ignore it and keep working. If that outcome is unacceptable, use
showModal(). - Assuming a popover has dialog semantics. The
popoverattribute adds no role. A plain<div popover>is announced as generic content. - Building a fake modal. A popover with a large
::backdroplooks modal, but the page behind it is still clickable and focusable. UseshowModal()instead.
Combining the two
You can put both attributes on one element, as in <dialog popover>. You get the dialog role and the popover's invoker buttons and light dismiss. It is still non-modal. Use this for a non-blocking panel that should be announced as a dialog, such as a settings pane.
Other patterns are sometimes a better fit than either tool:
- A
<details>element works for content that expands in place. - A
<select>or<datalist>works for choosing a value. - A live region, such as
role="status", works for messages that need no interaction.
Exercise
A checkout page must stop the user from clicking anything else while they confirm a payment. Should you use a popover=auto element or a modal <dialog>, and why?
Show answer
Use a modal <dialog> opened with showModal(). It makes the rest of the page inert and keeps focus inside the dialog. A popover=auto element leaves the page interactive and closes when the user clicks outside it or presses Escape.
Exercise
What does <dialog popover> give you that a plain <div popover> does not, and is it modal?
Show answer
It gives you the dialog role and semantics. It is not modal, because it opens through the popover mechanism and the background stays interactive.