Sub topic 12
Pitfalls to avoid
This chapter assumes you can already open a dialog with showModal(), read returnValue after a close event, and style dialog and ::backdrop. It collects the mistakes that most often break a native dialog and shows the fix for each.
Calling showModal() on a dialog that is already open
showModal() throws an InvalidStateError if the dialog is already open in a way it can't switch from. This happens when you call it on a dialog opened with show(), or on one that has the open attribute in the markup. Click handlers that fire twice, or code that opens the same dialog from two places, trigger it easily.
Guard the call with the open property. Reset returnValue at the same time. It keeps the previous result when a dialog is reopened, and Escape does not change it.
function openDialog(dlg) {
if (dlg.open) return;
dlg.returnValue = '';
dlg.showModal();
}Opening with the open attribute
Adding open in the HTML, or running dlg.setAttribute('open', ''), shows the dialog as non-modal. There is no top layer, no backdrop, and the page behind it stays interactive. Use showModal() when you want a modal.
Overriding display on the dialog
A closed dialog is hidden by the browser's default display: none. If you write dialog { display: flex; }, your rule wins, and the dialog is visible all the time. Put layout rules on the open state only.
dialog[open] {
display: flex;
flex-direction: column;
gap: 1rem;
}Validation that blocks Cancel
A required field stops any submit button in the form from closing the dialog, including Cancel. The user sees a validation bubble instead. Add formnovalidate to the Cancel button, as in the first example of this tutorial:
<button value="cancel" formnovalidate>Cancel</button>Padding on the dialog itself
The backdrop-click technique checks event.target === dlg. If the dialog element has padding, a click on that padding also targets the dialog, and the dialog closes when the user clicks inside it. Set padding: 0 on the dialog and put the spacing on an inner .content wrapper.
Treating returnValue as always set
returnValue holds the value of the submit button that closed the dialog. Three cases leave it empty or stale:
- The user pressed Escape. The
cancelevent fires, thenclose, butreturnValuekeeps its old value. - You called
close()with no argument. - A button has no
valueattribute, so the value is an empty string.
Decide what an empty result means, usually "cancelled", and handle it in your close listener.
dlg.addEventListener('close', () => {
if (dlg.returnValue === 'save') {
// save the form data
}
// anything else counts as cancelled
});Forgetting the accessible name
A dialog with no label is announced by screen readers as just "dialog". Point aria-labelledby at the heading, as in <dialog aria-labelledby="dlg-title">. Also check that focus lands somewhere sensible: put autofocus on the control the user should act on first, which is often not the first field.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| `InvalidStateError` on open | Dialog already open or opened with `show()` | Check `dlg.open` first |
| No backdrop, page still clickable | Opened with `open` attribute or `show()` | Use `showModal()` |
| Dialog always visible | `display` set on `dialog` | Scope the rule to `dialog[open]` |
| Cancel does nothing | Required field fails validation | Add `formnovalidate` |
| Dialog closes on inner clicks | Padding on `dialog` | Move padding to an inner wrapper |
| Old result after Escape | `returnValue` not reset | Set `returnValue = ''` before opening |
Exercise
A user opens your dialog, clicks Save, and later reopens it and presses Escape. Your close handler logs "save" again. Why, and how do you fix it?
Show answer
Escape closes the dialog without changing returnValue, so it still holds "save" from the earlier submit. Reset it with dlg.returnValue = '' before each call to showModal(), for example in a shared openDialog() function.
Exercise
You add dialog { display: grid; } to center the content, and the dialog now appears on page load. What is the cause, and what is the fix?
Show answer
The author rule overrides the browser's display: none for closed dialogs. Change the selector to dialog[open] so the layout applies only while the dialog is open.