Modal Dialog
An accessible dialog with focus trap, scroll lock, Escape and backdrop close.
AI prompt
Build an accessible confirm dialog using the native dialog element opened with showModal() (which provides the backdrop, focus trap and Escape handling). A trigger button "Delete project" opens it. The dialog has a title ("Delete this project?"), a body explaining that the action cannot be undone, and two buttons: "Cancel" (secondary, autofocus) and "Delete project" (danger red). Use aria-labelledby and aria-describedby. Clicking the backdrop closes it (detect clicks outside the dialog's rectangle). Lock page scroll while open and return focus to the trigger on close, which the native element does automatically. Animate the panel with scale 0.96 to 1 and opacity over 220ms cubic-bezier(0.2,0.8,0.2,1) and the ::backdrop with a 200ms fade, including a closing animation using a class before calling close(). On widths under 520px present it as a bottom sheet sliding up. Reduced motion: opacity only.About this pattern
Definition
A window that appears above the page and blocks interaction with everything behind it until it is dismissed. A dimmed backdrop shows that the page is inactive. Keyboard focus is held inside the dialog.
Purpose & UX principles
- Reserve for decisions or short tasks that must be completed before continuing (confirm a deletion, finish a short form).
- If the user can ignore the content, a toast, popover or inline message is better.
- Open with a clear title, state the consequence in the body and use specific button labels ("Delete project", not "Yes").
Interaction states
- Closed, opening, open, closing.
- While open: focus moves to the first focusable element (or the dialog), Tab and Shift+Tab cycle inside, Escape closes, clicking the backdrop closes, background scroll is locked and the page behind is
inert. - On close, focus returns to the control that opened it.
Motion specification
- Backdrop: opacity 0 to 1, 200ms.
- Panel: scale 0.96 to 1 and opacity, 220ms cubic-bezier(0.2, 0.8, 0.2, 1); bottom sheet on phones translates from 100% to 0.
- Close: reverse in 160ms.
- Reduced motion: opacity only.
Responsive behaviour
- Desktop: centred, maximum width 440px.
- Phones: slides up as a bottom sheet with a larger top radius and full width.
- Content that is taller than the screen scrolls inside the dialog, not behind it.
Code
<button class="open" id="open">Delete project</button>
<p class="state" id="state" role="status"></p>
<dialog id="dlg" aria-labelledby="t" aria-describedby="d">
<h2 id="t">Delete this project?</h2>
<p id="d">“Aurora Redesign” and its 24 files will be permanently removed. This action cannot be undone.</p>
<div class="act"><button id="cancel" autofocus>Cancel</button><button id="ok" class="danger">Delete project</button></div>
</dialog>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;cursor:pointer}
.open{height:44px;padding:0 22px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font-weight:600}
.state{margin-top:14px;color:var(--muted);font-size:14px;min-height:22px;text-align:center}
dialog{width:min(440px,calc(100vw - 32px));padding:24px;border:1px solid var(--line);border-radius:20px;background:var(--surface);color:var(--text);box-shadow:0 24px 60px rgba(0,0,0,.18);opacity:0;transform:scale(.96);transition:opacity .22s cubic-bezier(.2,.8,.2,1),transform .22s cubic-bezier(.2,.8,.2,1)}
dialog[open]{opacity:1;transform:none}
dialog.closing{opacity:0;transform:scale(.97);transition-duration:.16s}
dialog::backdrop{background:rgba(0,0,0,.32);backdrop-filter:blur(3px);opacity:0;transition:opacity .2s}
dialog[open]::backdrop{opacity:1}dialog.closing::backdrop{opacity:0}
h2{font-size:20px;margin-bottom:8px}dialog p{color:var(--muted);font-size:14.5px}
.act{display:flex;gap:10px;justify-content:flex-end;margin-top:22px}
.act button{height:42px;padding:0 18px;border-radius:10px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font-weight:600}
.act .danger{background:var(--danger);border-color:var(--danger);color:#fff}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (max-width:520px){dialog{margin:auto 0 0;width:100vw;max-width:none;border-radius:24px 24px 0 0;transform:translateY(100%)}dialog[open]{transform:none}dialog.closing{transform:translateY(100%)}}
@media (prefers-reduced-motion:reduce){dialog,dialog.closing{transform:none!important;transition:opacity .15s}}const dlg=document.getElementById('dlg'),state=document.getElementById('state');
function open(){dlg.classList.remove('closing');dlg.showModal();document.documentElement.style.overflow='hidden';}
function close(msg){dlg.classList.add('closing');setTimeout(()=>{dlg.close();dlg.classList.remove('closing');document.documentElement.style.overflow='';if(msg)state.textContent=msg;},160);}
document.getElementById('open').onclick=()=>{state.textContent='';open();};
document.getElementById('cancel').onclick=()=>close('Cancelled');
document.getElementById('ok').onclick=()=>close('Project deleted');
dlg.addEventListener('cancel',e=>{e.preventDefault();close('Cancelled');});
dlg.addEventListener('click',e=>{const r=dlg.getBoundingClientRect();if(e.clientX<r.left||e.clientX>r.right||e.clientY<r.top||e.clientY>r.bottom)close('Cancelled');});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Modal Dialog — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;cursor:pointer}
.open{height:44px;padding:0 22px;border-radius:12px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font-weight:600}
.state{margin-top:14px;color:var(--muted);font-size:14px;min-height:22px;text-align:center}
dialog{width:min(440px,calc(100vw - 32px));padding:24px;border:1px solid var(--line);border-radius:20px;background:var(--surface);color:var(--text);box-shadow:0 24px 60px rgba(0,0,0,.18);opacity:0;transform:scale(.96);transition:opacity .22s cubic-bezier(.2,.8,.2,1),transform .22s cubic-bezier(.2,.8,.2,1)}
dialog[open]{opacity:1;transform:none}
dialog.closing{opacity:0;transform:scale(.97);transition-duration:.16s}
dialog::backdrop{background:rgba(0,0,0,.32);backdrop-filter:blur(3px);opacity:0;transition:opacity .2s}
dialog[open]::backdrop{opacity:1}dialog.closing::backdrop{opacity:0}
h2{font-size:20px;margin-bottom:8px}dialog p{color:var(--muted);font-size:14.5px}
.act{display:flex;gap:10px;justify-content:flex-end;margin-top:22px}
.act button{height:42px;padding:0 18px;border-radius:10px;border:1px solid var(--line);background:var(--surface-2);color:var(--text);font-weight:600}
.act .danger{background:var(--danger);border-color:var(--danger);color:#fff}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (max-width:520px){dialog{margin:auto 0 0;width:100vw;max-width:none;border-radius:24px 24px 0 0;transform:translateY(100%)}dialog[open]{transform:none}dialog.closing{transform:translateY(100%)}}
@media (prefers-reduced-motion:reduce){dialog,dialog.closing{transform:none!important;transition:opacity .15s}}
</style></head>
<body><div class="stage">
<button class="open" id="open">Delete project</button>
<p class="state" id="state" role="status"></p>
<dialog id="dlg" aria-labelledby="t" aria-describedby="d">
<h2 id="t">Delete this project?</h2>
<p id="d">“Aurora Redesign” and its 24 files will be permanently removed. This action cannot be undone.</p>
<div class="act"><button id="cancel" autofocus>Cancel</button><button id="ok" class="danger">Delete project</button></div>
</dialog>
</div>
<script>
const dlg=document.getElementById('dlg'),state=document.getElementById('state');
function open(){dlg.classList.remove('closing');dlg.showModal();document.documentElement.style.overflow='hidden';}
function close(msg){dlg.classList.add('closing');setTimeout(()=>{dlg.close();dlg.classList.remove('closing');document.documentElement.style.overflow='';if(msg)state.textContent=msg;},160);}
document.getElementById('open').onclick=()=>{state.textContent='';open();};
document.getElementById('cancel').onclick=()=>close('Cancelled');
document.getElementById('ok').onclick=()=>close('Project deleted');
dlg.addEventListener('cancel',e=>{e.preventDefault();close('Cancelled');});
dlg.addEventListener('click',e=>{const r=dlg.getBoundingClientRect();if(e.clientX<r.left||e.clientX>r.right||e.clientY<r.top||e.clientY>r.bottom)close('Cancelled');});
</script>
</body></html>