Modal

A short task or a confirm that must finish before going on. Built on the browser's own dialog, so focus stays inside and returns when it closes.

Try it

Open one, then try Tab, Escape, or clicking outside. Focus stays inside while it is open and returns to the button when it closes.

Project: Harbor Bakery site

Delete this project?

Its pages and files are removed for good. This cannot be undone.

Rename project

When to use a modal

Code

Plain HTML for any site, or React. Both use the same styles, so the modal looks the same wherever it lands.

ReadyAny website, Shopify, WordPress, Square, plain hosting

<link rel="stylesheet" href="tokens.css">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="parts.css">

<!-- Modal: the browser's own dialog. showModal() keeps focus inside, Escape closes it,
     the page behind cannot be reached, and focus returns to the button that opened it. -->
<button type="button" class="prism-btn prism-btn--danger" id="open-delete">Delete project</button>

<dialog class="prism-modal" id="delete-dialog" aria-labelledby="delete-title">
  <div class="prism-modal__inner">
    <div class="prism-modal__head">
      <h2 class="prism-modal__title" id="delete-title">Delete this project?</h2>
      <button type="button" class="prism-btn prism-btn--ghost prism-btn--icon" aria-label="Close" title="Close" data-close>
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18" stroke-linecap="round"/></svg>
      </button>
    </div>
    <p class="prism-modal__text">Its pages and files are removed for good. This cannot be undone.</p>
    <div class="prism-modal__actions">
      <!-- autofocus on the safe choice, so Enter never deletes by accident. -->
      <button type="button" class="prism-btn prism-btn--secondary" data-close autofocus>Keep it</button>
      <button type="button" class="prism-btn prism-btn--danger" id="confirm-delete">Delete project</button>
    </div>
  </div>
</dialog>

<script>
  const dialog = document.getElementById("delete-dialog");
  document.getElementById("open-delete").addEventListener("click", () => dialog.showModal());
  dialog.querySelectorAll("[data-close]").forEach((b) => b.addEventListener("click", () => dialog.close()));
  dialog.addEventListener("click", (e) => { if (e.target === dialog) dialog.close(); });
  document.getElementById("confirm-delete").addEventListener("click", () => { /* delete, then */ dialog.close(); });
</script>

Accessibility