Sub topic 12
Popover or dialog: choosing the right tool
This chapter assumes you can already open and close popovers with popovertarget, and that you know the difference between popover=auto and popover=manual. It also assumes you have seen the <dialog> element and its showModal() method. The goal is to decide which of the two fits a given piece of interface, and to avoid the mistakes that come from mixing them up.
Two tools with overlapping looks
Both features put content above the page in the top layer. Both can be styled with ::backdrop. Both can close on Escape. Because of this, it is easy to treat them as interchangeable. They are not.
The central difference is modality.
- A modal dialog (opened with
showModal()) makes the rest of the page inert. Users cannot click, tab to, or select anything outside it until it closes. - A popover is non-modal. The page behind it stays interactive. Clicking elsewhere usually closes an
autopopover, but the page itself keeps working.
Everything else follows from that choice: focus handling, dismissal, and what assistive technology is told.
What each one gives you
| Behavior | Popover | Modal dialog (`showModal()`) |
|---|---|---|
| Rest of page | Stays interactive | Inert |
| Top layer | Yes | Yes |
| Opens without JavaScript | Yes, with `popovertarget` | No, needs a script (or the newer `command` and `commandfor` attributes where supported) |
| Escape closes it | Yes, for `auto` | Yes |
| Click outside closes it | Yes, for `auto` | No |
| Focus on open | Not moved, unless a child has `autofocus` | Moves to the first focusable element, or to a child with `autofocus` |
| Focus on close | Returns to the previously focused element if focus was inside the popover | Returns to the element that had focus before opening |
| Implicit role | None | `dialog` |
| Several open at once | Yes, as a stack or with `manual` | Yes, stacked, but only the topmost is usable |
| Typical content | Menus, hints, toasts, pickers | Confirmations, forms, anything that needs an answer |
Two rows deserve more explanation.
Focus on open. A modal dialog assumes the user must deal with it, so it moves focus inside. A popover assumes the user may ignore it, so it leaves focus where it was. If your popover contains a form field that the user will almost certainly want, add autofocus to that field. For a menu, leaving focus on the trigger is often fine, because the next Tab press moves into the popover content when it directly follows the invoker in the DOM.
Implicit role. A bare <div popover> has no semantic role. Screen readers announce the invoker's expanded state, but they do not announce a "dialog". If the content behaves like a dialog, give it a role yourself, or use an element that has one.
A decision flow
The questions below cover most cases.
Read the first question carefully. "Must respond" means that continuing without an answer would be wrong or dangerous. Deleting an account, discarding unsaved work, and completing a login step all qualify. "Would be nice to read" does not.
Worked example: the same button, two answers
Suppose a table row has an actions button. It offers "Rename", "Duplicate", and "Delete". These are two separate interface pieces.
The action list is a menu. The user may ignore it, and clicking elsewhere should close it. That is an auto popover.
Delete is different. It needs an explicit confirmation, and the user should not click other rows while the question is open. That is a modal dialog.
<button popovertarget="row-actions">Actions</button>
<div id="row-actions" popover>
<button type="button">Rename</button>
<button type="button">Duplicate</button>
<button type="button" id="delete-btn">Delete…</button>
</div>
<dialog id="confirm-delete" aria-labelledby="confirm-title">
<form method="dialog">
<h2 id="confirm-title">Delete this item?</h2>
<p>This cannot be undone.</p>
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>
<script>
const menu = document.getElementById('row-actions');
const dialog = document.getElementById('confirm-delete');
document.getElementById('delete-btn').addEventListener('click', () => {
menu.hidePopover();
dialog.showModal();
});
dialog.addEventListener('close', () => {
if (dialog.returnValue === 'delete') {
console.log('delete confirmed');
}
});
</script>Several details in this code are deliberate:
- The menu is hidden before the dialog opens. A modal dialog makes everything outside it inert, including an open popover, so leaving the menu open would leave a dead, unclickable menu behind the backdrop.
autofocussits on Cancel, the safe choice. For destructive actions, start focus on the harmless button.<form method="dialog">closes the dialog on submit and setsreturnValueto the clicked button'svalue. No click handlers are needed for the buttons.- The
closeevent reports the result. The dialog'sreturnValueis an empty string if the user pressed Escape, so the check above treats that as not confirmed.
Using the dialog element as a popover
The popover attribute works on any element, including <dialog>:
<button popovertarget="quick-note">Add note</button>
<dialog id="quick-note" popover>
<form method="dialog">
<label for="note">Note</label>
<input id="note" autofocus>
<button>Save</button>
</form>
</dialog>This gives you a non-modal overlay that carries the dialog role. It is useful for something like a quick-entry panel that should be announced as a dialog, but must not block the page.
Be aware of the consequences:
- It is opened with
showPopover()orpopovertarget. It is not a modal, whatever the markup looks like. - Do not call
showModal()on it. Choose one mechanism per element. Mixing them leads to errors or states that are hard to reason about. - The
popoverattribute changes the dismissal rules. The dialog now closes on outside click, because it is anautopopover.
Failure modes to watch for
Using a popover for something blocking
If a popover holds a required choice, a user can click outside it and lose the question. Worse, keyboard users can Tab out of it into the page behind. Use showModal() for anything where leaving the overlay is not a valid outcome.
Using a modal dialog for a menu or hint
A modal dialog for a three-item menu is heavy. It blocks the page, dims it, and takes focus. Users who only wanted a glance at some help text now have to dismiss a dialog. Use an auto popover instead.
Forgetting to name the thing
Neither a bare popover nor a dialog without a label is well described to assistive technology. Give a dialog aria-labelledby or aria-label. For a popover that acts as a dialog, add role="dialog" and a label. Menus built with popover do not become ARIA menus automatically. Plain buttons and links in a list are usually the best choice, and they need no special roles.
Stacking a modal and popovers
Order matters in the top layer. The most recently shown element sits on top. A popover opened from inside an open modal dialog appears above it and works normally, because it belongs to the dialog's subtree. A popover opened elsewhere, before the dialog, is inert and sits below it. If you have a toast that must stay visible during a modal, show it as a manual popover after the dialog has opened.
Rules of thumb
- Ask whether the user must answer. If so, use a modal dialog.
- If the content is optional and disposable, use an
autopopover. - If the content persists independently of user clicks, such as a toast or a status panel, use a
manualpopover. - If you need a dialog role without blocking, use
<dialog popover>. - If the content is part of the page flow, such as an FAQ answer, prefer
<details>or plain content. An overlay is not always the right choice.
Exercise
A settings page needs a "Copy link" confirmation that says "Link copied" for three seconds and must not interrupt typing in a nearby field. Which should you use, a modal dialog, an auto popover, or a manual popover, and why?
Show answer
Use a manual popover. It does not block the page, so typing continues. A manual popover is not closed by clicks elsewhere or by focus moving, so it stays visible for the full three seconds. You would call showPopover() when the link is copied and hidePopover() after the timeout. An auto popover could vanish early if the user clicks into the field, and a modal dialog would block exactly the interaction you want to allow.
Exercise
In the worked example, why does the Delete click handler call menu.hidePopover() before dialog.showModal()?
Show answer
A modal dialog makes everything outside it inert. If the menu popover stayed open, it would remain visible but unusable behind the dialog. Hiding it first leaves the page in a clean state and lets focus return sensibly once the dialog closes.