Rule
A modal dialog moves focus inside itself when it opens, keeps focus inside while it is open, and returns focus to the control that opened it when it closes. Escape, the close button, and a Cancel action all close it.
Why
A modal says "deal with this first". If focus stays on the page behind it, keyboard and screen reader users are still operating a page they cannot see. If focus does not return on close, they are dropped at the top of the document and must find their place again.
Must
- Use the native
dialogelement withshowModal()where you can. It makes the page behind inert and handles Escape. - Name the dialog with
aria-labelledbypointing at its heading. - On open, move focus to the first useful control. For a destructive confirmation, that is the safe action, such as Cancel.
- Keep Tab and Shift+Tab inside the dialog.
- On close, return focus to the element that opened the dialog. Remember that element at open time.
- Close on Escape and on an explicit close button with an accessible name.
Should
- Close on backdrop click for dialogs with nothing to lose. Do not close on backdrop click when the dialog holds typed input.
- Ask before closing a dialog with unsaved changes.
- Lock page scroll while the dialog is open.
Gotchas
- On iOS Safari,
overflow: hiddenonbodydoes not stop the page scrolling behind an overlay. Fix the body in place instead: setposition: fixedandtopto minus the current scroll offset, then restore the scroll position on close. - If the opener is removed while the dialog is open, such as a deleted row, send focus to a sensible neighbour and not to
body. - A dialog opened from inside a menu must close the menu first, or focus returns to an element that is no longer visible.
- Two stacked modals are a design smell. Replace the content of the first.
Validation checklist
- Opening moves focus into the dialog.
- Tab never reaches the page behind.
- Escape closes the dialog, and focus returns to the opener.
- A screen reader announces the dialog title on open.
- The page behind does not scroll on a phone.
Code Examples
Svelte
<script lang="ts">
import { onDestroy, tick } from 'svelte';
let open = false;
let trigger: HTMLButtonElement | null = null;
let closeButton: HTMLButtonElement | null = null;
let previousBodyOverflow = '';
let isBodyScrollLocked = false;
function lockBodyScroll() {
if (typeof document === 'undefined' || isBodyScrollLocked) return;
previousBodyOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
isBodyScrollLocked = true;
}
function unlockBodyScroll() {
if (typeof document === 'undefined' || !isBodyScrollLocked) return;
document.body.style.overflow = previousBodyOverflow;
previousBodyOverflow = '';
isBodyScrollLocked = false;
}
async function openDialog() {
open = true;
lockBodyScroll();
await tick();
closeButton?.focus();
}
function closeDialog() {
open = false;
unlockBodyScroll();
trigger?.focus();
}
function onBackdropClick(event: MouseEvent) {
if (event.target === event.currentTarget) {
closeDialog();
}
}
function onKeydown(event: KeyboardEvent) {
if (!open) return;
if (event.key === 'Escape') closeDialog();
}
onDestroy(() => {
unlockBodyScroll();
});
</script>
<svelte:window on:keydown={onKeydown} />
<button bind:this={trigger} type="button" on:click={openDialog}>Open modal</button>
{#if open}
<div class="backdrop" on:click={onBackdropClick}>
<section
role="dialog"
aria-modal="true"
aria-labelledby="dialog-title"
on:click|stopPropagation
>
<h2 id="dialog-title">Delete project</h2>
<p>Return focus to the trigger when the dialog closes.</p>
<button bind:this={closeButton} type="button" on:click={closeDialog}>Close</button>
</section>
</div>
{/if}
<style>
.backdrop {
position: fixed;
inset: 0;
background: rgb(15 18 28 / 0.48);
backdrop-filter: blur(10px);
}
</style>Trap focus inside the dialog, prevent background scrolling, blur the page behind the modal, close when the user clicks the backdrop, support Escape, and always restore focus to the trigger when the dialog closes.