Sub topic 9
Closing when the user clicks the backdrop
This chapter assumes you can open a dialog with showModal(), you know that the dialog sits in the top layer, and you can style ::backdrop. You also need basic DOM event listeners.
Why the browser does not do this for you
A modal dialog closes when the user presses Escape or submits a method="dialog" form. It does not close when the user clicks outside it. Many users expect that behavior, so you add it with a small click handler. Developers call this "light dismiss".
The difficulty is that ::backdrop is a pseudo-element. You cannot attach a listener to it or select it from JavaScript. Browsers instead report a backdrop click as a click on the <dialog> element itself. The handler has to tell that case apart from a click on the dialog's content.
The core idea
When you click inside the dialog on a child such as a paragraph or button, event.target is that child. When you click on the backdrop, event.target is the dialog. A check like event.target === dlg is therefore enough, as long as the dialog has no area of its own that a click could land on.
That condition is why the earlier styling chapter set padding: 0 on the dialog. If the dialog has padding, a click on the padding also targets the dialog, and the handler would close it by mistake. An inner wrapper carries the padding instead.
dialog {
border: none;
border-radius: 8px;
padding: 0;
max-width: min(32rem, 90vw);
}
dialog > .content {
padding: 1.5rem;
}
dialog::backdrop {
background: rgb(0 0 0 / 0.5);
}Now the markup must wrap everything in .content:
<button id="open">Edit profile</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<div class="content">
<h2 id="dlg-title">Edit profile</h2>
<form method="dialog">
<label>Name <input name="name" required autofocus></label>
<menu>
<button value="cancel" formnovalidate>Cancel</button>
<button value="save">Save</button>
</menu>
</form>
</div>
</dialog>The click handler
const dlg = document.getElementById('dlg');
dlg.addEventListener('click', (event) => {
// With padding: 0 on the dialog and an inner wrapper for content,
// the only way to target the dialog itself is to click the backdrop.
if (event.target === dlg) {
dlg.close('dismissed');
}
});Passing 'dismissed' to close() sets returnValue. Your close event listener can then tell a backdrop dismissal apart from "save" or "cancel".
How the pieces fit
A more defensive version
Sometimes you cannot keep the dialog free of padding, for example when a design system styles every dialog. You can then compare the click position with the dialog's bounding box. Clicks on the backdrop fall outside the rectangle.
const dlg = document.getElementById('dlg');
dlg.addEventListener('click', (event) => {
const rect = dlg.getBoundingClientRect();
const inside =
event.clientX >= rect.left &&
event.clientX <= rect.right &&
event.clientY >= rect.top &&
event.clientY <= rect.bottom;
if (!inside) {
dlg.close('dismissed');
}
});This version works with padding. It has a known weakness: a keyboard activation of a button, such as pressing Enter, can produce a click with clientX and clientY set to 0. That point is outside the rectangle, so the dialog would close. Guard against it by also checking event.target === dlg:
if (event.target === dlg && !inside) {
dlg.close('dismissed');
}Common errors
- The dialog closes when the user clicks the padding. The dialog still has padding. Move it to an inner wrapper.
- The dialog closes when the user selects text and releases the mouse over the backdrop. The browser fires
clickon the common ancestor of the mousedown and mouseup targets, which can be the dialog. If this matters, record themousedowntarget and close only if it was also the dialog. - The handler runs on a non-modal dialog. A dialog opened with
show()has no backdrop, so clicks outside it never reach the dialog. Light dismiss makes no sense there. - Forgetting
returnValue. Callingclose()with no argument leaves the previousreturnValuein place, so yourcloselistener may read stale data.
Exercise
Your dialog has padding: 1.5rem set directly on the <dialog> element, and you use if (event.target === dlg) dlg.close(). Users report the dialog closes when they click near its edges. Why does this happen, and what are two ways to fix it?
Show answer
Clicks on the dialog's own padding have the dialog element as event.target, so the handler treats them as backdrop clicks. Fix it by setting padding: 0 on the dialog and moving the padding to an inner .content wrapper, or by comparing the click coordinates with dlg.getBoundingClientRect() so only clicks outside the box close the dialog.
Exercise
What value does dlg.returnValue have in the close listener after a backdrop click handled by dlg.close('dismissed')?
Show answer
The string "dismissed". The argument passed to close() becomes returnValue, so your listener can distinguish a backdrop dismissal from "save" or "cancel".