Dialog

A centered, backdrop-blurred modal built on the native <dialog> element. Open it with showModal().

ClassDoes what
dialogThe dialog itself: fixed, centered, blurred backdrop, hidden unless open
dialog-boxThe panel inside: card surface, scrolls internally, capped width
dialog-headerHeader row inside the panel
dialog-actionsFooter 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>

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>

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.

Same markup in both builds
<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>

Confirming a destructive action

The dialog names what will be lost, the safe choice is secondary, and the destructive one takes the error colour.

Same markup in both builds
<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>