Interaction · Complete runnable example

Native modal dialog

A focused dialog using the browser’s built-in modal behavior and clear close controls.

Live example

This preview runs in an isolated document. Clipboard writes and saved preferences may be blocked, and printing requires the standalone file. A dialog affects only the example. Reset reloads the original example state.

When to use it

Use a modal dialog when a short, focused interaction needs its own temporary surface. This example uses the native dialog element instead of constructing a focus trap from generic containers.

Try the important details

  1. Open the dialog with the keyboard and inspect where focus starts.
  2. Press Escape or use the close button, then check that focus returns to the opener.
  3. Reopen it and use a short browser viewport to check that the contents can scroll.

How it works

  • showModal opens the dialog in the browser top layer and makes the rest of its document inert.
  • An accessible name comes from the visible heading, and an explicit close button provides a clear exit.
  • The script records the opener and restores focus after closing. The layout limits dialog height and allows internal scrolling.

The complete source

The preview, copied text, and downloaded file use this same document.

HTML

<!doctype html>
<!--
MIT License
Copyright (c) 2026 MRHTML

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; img-src data:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'"
    />
    <title>Native dialog | MRHTML</title>
    <style>
      /* MRHTML standalone example. No external styles, fonts, or assets. */
      :root {
        color-scheme: light;
        --paper: #f5f3ee;
        --surface: #fff;
        --ink: #1e2b42;
        --muted: #59657a;
        --line: #d9dee7;
        --soft: #eef1fb;
        --blue: #344ed5;
        --blue-hover: #263caf;
        --button-text: #fff;
        --focus: #b65716;
        --error: #ad253d;
        font:
          16px/1.6 system-ui,
          -apple-system,
          BlinkMacSystemFont,
          'Segoe UI',
          sans-serif;
        color: var(--ink);
        background: var(--paper);
      }
      * {
        box-sizing: border-box;
      }
      body {
        margin: 0;
        min-height: 100svh;
        display: grid;
        place-items: center;
        padding: 40px 24px;
      }
      main {
        width: min(680px, 100%);
        min-width: 0;
      }
      h1,
      h2,
      h3,
      p {
        margin: 0;
      }
      h1,
      h2,
      h3 {
        line-height: 1.15;
        letter-spacing: -0.045em;
      }
      h1 {
        font-size: clamp(1.9rem, 5vw, 2.65rem);
        font-weight: 650;
        max-width: 570px;
      }
      h2 {
        font-size: 1.35rem;
        font-weight: 650;
      }
      p {
        color: var(--muted);
      }
      button,
      input,
      textarea {
        font: inherit;
      }
      button {
        cursor: pointer;
      }
      button:disabled {
        cursor: not-allowed;
        opacity: 0.65;
      }
      button,
      a,
      input,
      textarea {
        -webkit-tap-highlight-color: transparent;
      }
      :focus-visible {
        outline: 3px solid var(--focus);
        outline-offset: 4px;
      }
      .eyebrow {
        display: flex;
        align-items: center;
        gap: 10px;
        color: var(--muted);
        font-size: 0.65rem;
        font-weight: 650;
        letter-spacing: 0.13em;
        text-transform: uppercase;
        margin-bottom: 22px;
      }
      .eyebrow::before {
        content: '';
        width: 8px;
        height: 8px;
        background: var(--blue);
        border-radius: 50%;
      }
      .intro {
        font-size: 0.94rem;
        max-width: 540px;
        margin-top: 17px;
      }
      .card {
        margin-top: 28px;
        padding: 28px;
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 16px;
        box-shadow: 0 8px 28px #14264a05;
      }
      .card-label {
        color: var(--muted);
        font-size: 0.62rem;
        font-weight: 650;
        letter-spacing: 0.11em;
        text-transform: uppercase;
        margin-bottom: 11px;
      }
      .button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 13px;
        padding: 12px 19px;
        min-height: 46px;
        border: 1px solid transparent;
        border-radius: 8px;
        background: var(--blue);
        color: var(--button-text);
        font-size: 0.85rem;
        font-weight: 600;
        line-height: 1.45;
        transition:
          background 0.15s,
          transform 0.15s;
      }
      .button:hover:not(:disabled) {
        background: var(--blue-hover);
        transform: translateY(-1px);
      }
      .button.secondary {
        background: var(--surface);
        border-color: var(--line);
        color: var(--ink);
      }
      .button.secondary:hover:not(:disabled) {
        background: var(--soft);
      }
      .button svg {
        display: block;
        width: 18px;
        height: 18px;
        flex: none;
      }
      .hint {
        font-size: 0.74rem;
        line-height: 1.75;
        margin-top: 20px;
        max-width: 570px;
      }
      .status {
        min-height: 1.6em;
        margin-top: 17px;
        font-size: 0.8rem;
        color: var(--ink);
        overflow-wrap: anywhere;
      }
      .actions {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
      }
      [hidden] {
        display: none !important;
      }
      @media (max-width: 430px) {
        body {
          padding: 30px 18px;
        }
        .card {
          padding: 23px 20px;
        }
        .eyebrow {
          font-size: 0.6rem;
          letter-spacing: 0.11em;
        }
        .intro {
          font-size: 0.88rem;
        }
      }
      @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
          animation: none !important;
          transition: none !important;
        }
        .button:hover:not(:disabled) {
          transform: none;
        }
      }

      .project-card {
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 22px;
        align-items: center;
      }
      .project-card p:not(.card-label) {
        font-size: 0.84rem;
        margin-top: 11px;
        max-width: 330px;
      }
      .project-symbol {
        display: grid;
        place-items: center;
        width: 68px;
        height: 68px;
        background: var(--soft);
        color: var(--blue);
        border: 1px solid #dce3fc;
        border-radius: 18px;
        font-size: 31px;
      }
      .project-card .actions {
        grid-column: 1 / -1;
        padding-top: 4px;
      }
      dialog {
        width: min(490px, calc(100% - 32px));
        max-height: calc(100vh - 32px);
        max-height: calc(100dvh - 32px);
        padding: 0;
        border: 1px solid var(--line);
        border-radius: 18px;
        background: var(--surface);
        color: var(--ink);
        box-shadow: 0 25px 90px #07132940;
        overflow-y: auto;
        overscroll-behavior: contain;
      }
      dialog::backdrop {
        background: #142139a8;
      }
      .dialog-header {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 16px;
        padding: 25px 25px 0;
      }
      .dialog-header h2 {
        margin-top: 7px;
        font-size: 1.7rem;
      }
      .close-button {
        display: grid;
        place-items: center;
        width: 44px;
        height: 44px;
        flex: none;
        border: 1px solid var(--line);
        border-radius: 9px;
        background: var(--surface);
        color: var(--ink);
        padding: 0;
      }
      .close-button:hover {
        background: var(--soft);
      }
      .close-button svg {
        display: block;
        width: 18px;
        height: 18px;
      }
      .dialog-content {
        padding: 22px 25px 27px;
      }
      .dialog-content > p {
        font-size: 0.87rem;
      }
      .brief-list {
        padding: 0;
        list-style: none;
        margin: 23px 0;
        border-top: 1px solid var(--line);
      }
      .brief-list li {
        display: grid;
        grid-template-columns: 75px 1fr;
        gap: 15px;
        font-size: 0.82rem;
        padding: 15px 0;
        border-bottom: 1px solid var(--line);
      }
      .brief-list span {
        color: var(--muted);
      }
      .brief-list strong {
        font-weight: 550;
      }
      .dialog-content .hint {
        font-size: 0.71rem;
      }
      @media (max-width: 430px) {
        .project-card {
          grid-template-columns: 1fr;
        }
        .project-symbol {
          display: none;
        }
        .dialog-header {
          padding: 22px 20px 0;
        }
        .dialog-content {
          padding: 20px;
        }
        .dialog-header h2 {
          font-size: 1.5rem;
        }
      }
    </style>
  </head>
  <body>
    <main>
      <p class="eyebrow">MRHTML / Native dialog</p>
      <h1>A focused moment.<br />A clearer next step.</h1>
      <p class="intro">
        Open a project brief without leaving the page. The browser handles modal
        focus and the Escape key.
      </p>
      <section class="card project-card" aria-labelledby="project-heading">
        <div>
          <p class="card-label">A small project, thoughtfully made</p>
          <h2 id="project-heading">The MRHTML field guide</h2>
          <p>
            A sample brief for a collection of useful, carefully designed web
            patterns.
          </p>
        </div>
        <span class="project-symbol" aria-hidden="true">✳</span>
        <div class="actions">
          <button class="button" id="open-dialog" type="button" disabled>
            Read the brief
            <svg viewBox="0 0 20 20" fill="none" aria-hidden="true">
              <path
                d="M4 10h12m-5-5 5 5-5 5"
                stroke="currentColor"
                stroke-width="1.6"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
            </svg>
          </button>
        </div>
      </section>
      <p class="hint">
        Try Tab, Shift+Tab, and Escape. In an embedded preview, the dialog is
        modal only inside that preview.
      </p>
      <p class="status" id="dialog-status" role="status"></p>
      <noscript
        ><p class="hint">
          JavaScript is off. Enable it to open this native dialog example.
        </p></noscript
      >
    </main>
    <dialog
      id="project-dialog"
      aria-labelledby="dialog-title"
      aria-describedby="dialog-description"
    >
      <div class="dialog-header">
        <div>
          <p class="card-label">Project brief / MRHTML</p>
          <h2 id="dialog-title">Small details.<br />Useful outcomes.</h2>
        </div>
        <button
          class="close-button"
          id="close-dialog"
          type="button"
          autofocus
          aria-label="Close project brief"
        >
          <svg viewBox="0 0 20 20" fill="none" aria-hidden="true">
            <path
              d="m5 5 10 10M15 5 5 15"
              stroke="currentColor"
              stroke-width="1.6"
              stroke-linecap="round"
            />
          </svg>
        </button>
      </div>
      <div class="dialog-content">
        <p id="dialog-description">
          A fictional brief for a small library of web patterns that people can
          understand, adapt, and use.
        </p>
        <ul class="brief-list">
          <li>
            <span>Goal</span><strong>Make one everyday task easier.</strong>
          </li>
          <li>
            <span>Approach</span
            ><strong
              >Start with native HTML. Add only the JavaScript you need.</strong
            >
          </li>
          <li>
            <span>Check</span
            ><strong
              >Keyboard, small screens, clear feedback, and reduced
              motion.</strong
            >
          </li>
        </ul>
        <button class="button secondary" id="done-dialog" type="button">
          Back to the project
        </button>
        <p class="hint">
          This dialog does not submit, save, or change any data.
        </p>
      </div>
    </dialog>

    <script>
      // MRHTML native dialog example. The browser supplies modality and Escape.
      const dialog = document.getElementById('project-dialog');
      const opener = document.getElementById('open-dialog');
      const status = document.getElementById('dialog-status');
      if (typeof dialog.showModal === 'function') {
        opener.disabled = false;
        opener.addEventListener('click', () => {
          if (!dialog.open) {
            status.textContent = '';
            dialog.showModal();
          }
        });
        document
          .getElementById('close-dialog')
          .addEventListener('click', () => dialog.close());
        document
          .getElementById('done-dialog')
          .addEventListener('click', () => dialog.close());
        // Keep native cancel behavior. Both Escape and close buttons return focus here.
        dialog.addEventListener('close', () => {
          opener.focus();
          status.textContent = 'Project brief closed.';
        });
      } else {
        status.textContent =
          'This browser does not support native dialogs. Try a current browser to open the brief.';
      }
    </script>
  </body>
</html>