Dialog
A centered, backdrop-blurred modal built on the native <dialog>
element. Open it with showModal().
| Class | Does what |
|---|---|
dialog | The dialog itself: fixed, centered, blurred backdrop, hidden unless open |
dialog-box | The panel inside: card surface, scrolls internally, capped width |
dialog-header | Header row inside the panel |
dialog-actions | Footer row inside the panel: top border, right-aligned actions |
Basic
<button class="btn" data-dialog="demo-dialog">Open dialog</button>
<dialog id="demo-dialog" class="dialog">
<form method="dialog">
<article class="dialog-box prose">
<header class="dialog-header">
<h3>Dialog heading</h3>
</header>
<p>Scrolls internally when the content is taller than the viewport, and locks
scrolling on the page behind it.</p>
<footer class="dialog-actions">
<button value="cancel" class="btn btn-secondary">Cancel</button>
<button value="ok" class="btn">OK</button>
</footer>
</article>
</form>
</dialog>
<button data-dialog="demo-dialog">Open dialog</button>
<dialog id="demo-dialog">
<form method="dialog">
<article>
<header>
<h3>Dialog heading</h3>
</header>
<p>Scrolls internally when the content is taller than the viewport, and locks
scrolling on the page behind it.</p>
<footer>
<button value="cancel" class="secondary">Cancel</button>
<button value="ok">OK</button>
</footer>
</article>
</form>
</dialog>
The docs script wires [data-dialog] to showModal(). In your own
app that is one line: dialog.showModal().
Form nesting
Both nestings work, so the close button needs no JavaScript:
dialog > form > .dialog-box and
dialog > .dialog-box > form.
<button class="btn" data-dialog="inner-form-dialog">Open</button>
<dialog id="inner-form-dialog" class="dialog">
<article class="dialog-box prose">
<form method="dialog">
<header class="dialog-header"><h3>Rename</h3></header>
<label class="label">
Name
<input type="text" class="input" value="Untitled" />
</label>
<footer class="dialog-actions">
<button value="cancel" class="btn btn-secondary">Cancel</button>
<button value="save" class="btn">Save</button>
</footer>
</form>
</article>
</dialog>
<button data-dialog="inner-form-dialog">Open</button>
<dialog id="inner-form-dialog">
<article>
<form method="dialog">
<header><h3>Rename</h3></header>
<label>
Name
<input type="text" value="Untitled" />
</label>
<footer>
<button value="cancel" class="secondary">Cancel</button>
<button value="save">Save</button>
</footer>
</form>
</article>
</dialog>
Classless: the panel is an <code>article</code>, so it picks up the card styling automatically. With classes, <code>.dialog-box</code> includes it.
Composition
Common ways to put this together with other components. The general rules are on Composition.
A form in a dialog
A short form that opens over the page, with its actions at the foot of the box.
<button class="btn" data-dialog="comp-dialog">Book a call</button>
<dialog id="comp-dialog" class="dialog">
<form method="dialog">
<article class="dialog-box">
<header class="dialog-header prose"><h3>Book a call</h3><p class="text-secondary">Pick a time and we will confirm by email.</p></header>
<label class="label"><p>Email</p><input class="input" type="email" /></label>
<label class="label">
<p>Time</p>
<select class="select"><option>Tuesday morning</option><option>Thursday afternoon</option></select>
</label>
<footer class="dialog-actions">
<button value="cancel" class="btn btn-secondary">Cancel</button>
<button value="ok" class="btn">Book</button>
</footer>
</article>
</form>
</dialog>
- Surface:
.dialog-boxis a card, and.dialog-actionsis.card-actionsaligned right, so a dialog composes like any card. - Text:
.prosegoes on the header only; the fields space themselves.
Confirming a destructive action
The dialog names what will be lost, the safe choice is secondary, and the destructive one takes the error colour.
<button class="btn btn-secondary" data-dialog="comp-confirm">Delete project</button>
<dialog id="comp-confirm" class="dialog">
<form method="dialog">
<article class="dialog-box prose">
<h3>Delete this project?</h3>
<p>Its files and history go with it. This cannot be undone.</p>
<footer class="dialog-actions">
<button value="cancel" class="btn btn-secondary">Keep it</button>
<button value="ok" class="btn error">Delete</button>
</footer>
</article>
</form>
</dialog>
- Boundary:
.errorworks on any element, so the delete button becomes an error button with no button-specific class. - Actions: the button that opens it is
.btn-secondary; a destructive action should not be the loudest thing on the page.