Dialog
Modal window built on native <dialog> + showModal().
Focus trap, Escape key, and backdrop are all browser-native. Animated with @starting-style.
Form Dialog
Dialog with form inputs. Click "Edit Profile" to open.
<button class="btn" data-variant="default"
data-dialog-trigger="edit-dialog"
aria-haspopup="dialog">
Edit Profile
</button>
<dialog id="edit-dialog" class="dialog"
role="dialog" aria-modal="true"
aria-labelledby="edit-title">
<div class="dialog-content">
<div class="dialog-header">
<h2 class="dialog-title" id="edit-title">Edit Profile</h2>
<p class="dialog-description">Make changes to your profile here.</p>
</div>
<div class="dialog-body">
<div style="display:flex;flex-direction:column;gap:1rem;">
<div>
<label class="label" for="name">Name</label>
<input class="input" id="name" type="text" value="Cody Lindley">
</div>
<div>
<label class="label" for="username">Username</label>
<input class="input" id="username" type="text" value="@codylindley">
</div>
</div>
</div>
<div class="dialog-footer">
<button class="btn" data-variant="outline" data-dialog-close>Cancel</button>
<button class="btn" data-variant="default" data-dialog-close>Save changes</button>
</div>
</div>
</dialog>
Confirmation
Destructive action confirmation. Click "Confirm Delete" to open.
<button class="btn" data-variant="outline"
data-dialog-trigger="confirm-dialog"
aria-haspopup="dialog">
Confirm Delete
</button>
<dialog id="confirm-dialog" class="dialog"
role="dialog" aria-modal="true"
aria-labelledby="confirm-title">
<div class="dialog-content">
<div class="dialog-header">
<h2 class="dialog-title" id="confirm-title">Delete account?</h2>
<p class="dialog-description">This action cannot be undone.
This will permanently delete your account.</p>
</div>
<div class="dialog-footer">
<button class="btn" data-variant="outline"
data-dialog-close>Cancel</button>
<button class="btn" data-variant="destructive"
data-dialog-close>Delete account</button>
</div>
</div>
</dialog>
CSS view file
/* -- Dialog component ------------------------------------------ */
@layer components {
dialog.dialog {
border: none;
border-radius: var(--radius-xl);
background-color: var(--popover);
color: var(--popover-foreground);
padding: 0;
margin: auto;
position: fixed;
inset: 0;
max-width: calc(100% - 2rem);
width: calc(100vw - 2rem);
max-height: calc(100vh - 2rem);
box-shadow: 0 0 0 1px color-mix(in oklch, var(--foreground) 10%, transparent);
opacity: 0;
transform: scale(0.95);
transition: opacity 100ms ease, transform 100ms ease, display 100ms allow-discrete, overlay 100ms allow-discrete;
&[open] {
opacity: 1;
transform: scale(1);
}
/* -- Sizes ------------------------------------------------ */
&[data-size="sm"] { max-width: 24rem; }
&[data-size="lg"] { max-width: 32rem; }
&[data-size="xl"] { max-width: 40rem; }
&[data-size="full"] { max-width: calc(100vw - 2rem); }
/* -- Backdrop --------------------------------------------- */
&::backdrop {
background: oklch(0 0 0 / 0);
backdrop-filter: blur(0px);
transition: all 100ms ease, display 100ms allow-discrete, overlay 100ms allow-discrete;
}
&[open]::backdrop {
background: oklch(0 0 0 / 0.10);
backdrop-filter: blur(4px);
}
}
@starting-style {
dialog.dialog[open] {
opacity: 0;
transform: scale(0.95);
}
dialog.dialog[open]::backdrop {
background: oklch(0 0 0 / 0);
backdrop-filter: blur(0px);
}
}
/* -- Content sections ------------------------------------- */
.dialog-content { padding: 1rem; }
.dialog-header { margin-bottom: 1rem; }
.dialog-title { font-size: 1rem; font-weight: 600; margin: 0 0 0.375rem; letter-spacing: -0.01em; }
.dialog-description { font-size: 0.875rem; color: var(--muted-foreground); margin: 0; line-height: 1.25rem; }
.dialog-body { margin-top: 1rem; }
.dialog-footer {
display: flex;
flex-direction: column-reverse;
gap: 0.5rem;
margin: 1rem -1rem -1rem;
padding: 1rem;
border-block-start: 1px solid var(--border);
background: color-mix(in oklch, var(--muted) 50%, transparent);
}
@media (min-width: 40rem) {
dialog.dialog { max-width: 24rem; }
.dialog-footer { flex-direction: row; justify-content: flex-end; }
}
@media (prefers-reduced-motion: reduce) {
dialog.dialog, dialog.dialog::backdrop { transition: none; }
}
}
JavaScript view file
Wire triggers via data-dialog-trigger, close buttons via data-dialog-close, and backdrop click. Focus returns to trigger on close.
// Wires declarative triggers and close controls to native dialogs.
const dialogSelector = 'dialog:not(.alert-dialog):not(.sheet)';
const openers = new WeakMap();
document.addEventListener('click', (event) => {
if (event.defaultPrevented || !(event.target instanceof Element)) return;
const trigger = event.target.closest('[data-dialog-trigger]');
if (trigger && !trigger.matches(':disabled, [aria-disabled="true"]')) {
const dialog = document.getElementById(trigger.dataset.dialogTrigger);
if (dialog instanceof HTMLDialogElement && !dialog.open) {
dialog.showModal();
openers.set(dialog, trigger);
}
return;
}
const dialog = event.target.closest(dialogSelector);
if (!dialog?.open) return;
if (event.target.closest('[data-dialog-close]')?.closest('dialog') === dialog) {
dialog.close();
return;
}
if (event.target === dialog) {
const bounds = dialog.getBoundingClientRect();
if (event.clientX < bounds.left || event.clientX > bounds.right ||
event.clientY < bounds.top || event.clientY > bounds.bottom) dialog.close();
}
});
document.addEventListener('close', (event) => {
const dialog = event.target;
if (!(dialog instanceof HTMLDialogElement) || !dialog.matches(dialogSelector)) return;
const opener = openers.get(dialog);
openers.delete(dialog);
if (opener?.isConnected) opener.focus();
}, true);