---
title: "Dialog"
description: "Dialog component - modal dialogs using native <dialog> with showModal()."
slug: "dialog"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeCard, TableCard, ApiSection } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="dialog" title="Dialog" component="dialog">
  Modal window built on native <code>&lt;dialog&gt;</code> + <code>showModal()</code>. Focus trap, Escape key, and backdrop are all browser-native. Animated with <code>@starting-style</code>.
</PageHeader>

<SkillPanel component="dialog">
  <SkillField label="Native basis">
<SkillText><code>&lt;dialog&gt;</code> element + <code>showModal()</code>. The browser provides:</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog" title="native modal element with built-in focus trap and Escape-to-close"><code>&lt;dialog&gt;</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/showModal" title="opens dialog as modal in the top layer with backdrop"><code>HTMLDialogElement.showModal()</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/::backdrop" title="pseudo-element for the overlay behind the modal"><code>::backdrop</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style" title="defines entry animation starting values"><code>@starting-style</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.dialog</code>
    <code>.dialog-content</code>
    <code>.dialog-header</code>
    <code>.dialog-title</code>
    <code>.dialog-description</code>
    <code>.dialog-body</code>
    <code>.dialog-footer</code>
  </SkillRow>
  </SkillField>

  <SkillField label="Sizes (data-size)">
<SkillGrid>
    <Variant name="sm" description="max-width: 24rem" />
    <Variant name="md" description="max-width: 28rem (default)" />
    <Variant name="lg" description="max-width: 32rem" />
    <Variant name="xl" description="max-width: 40rem" />
    <Variant name="full" description="max-width: calc(100vw - 2rem)" />
  </SkillGrid>
  </SkillField>

  <SkillField label="Data attributes">
<SkillText dense>• <code>data-dialog-trigger</code></SkillText>
  <SkillText>• <code>data-dialog-close</code></SkillText>
  </SkillField>

  <SkillField label="Wiring conventions">
<SkillText dense>• <code>data-dialog-trigger="\[id\]"</code> on any element → opens that dialog</SkillText>
  <SkillText dense>• <code>data-dialog-close</code> on any element inside → closes the dialog</SkillText>
  <SkillText dense>• Click on backdrop → closes (click lands on <code>&lt;dialog&gt;</code> itself)</SkillText>
  <SkillText>• Place <code>&lt;dialog&gt;</code> elements as direct children of <code>&lt;body&gt;</code></SkillText>
  </SkillField>

  <SkillField label="Notes">
<SkillText dense>• Animation uses CSS-only enter via <code>@starting-style</code> and exit via <code>transition</code> + <code>allow-discrete</code>.</SkillText>
  <SkillText dense>• While the dialog is modal, <code>html:has(dialog.dialog:modal)</code> sets <code>overflow: hidden</code> + <code>scrollbar-gutter: stable</code> - the page behind cannot scroll and its position is preserved for when the dialog closes (no JS scroll-lock).</SkillText>
  <SkillText dense>• The selector is <code>dialog.dialog</code> (element + class) to avoid styling native <code>&lt;dialog&gt;</code> elements used elsewhere.</SkillText>
  <p class="text-muted-foreground m-0">• For forms inside dialogs, use the <code>dialog-body</code> wrapper for the form content.</p>
  </SkillField>
</SkillPanel>

```html example label="Form Dialog" hint="Dialog with form inputs. Click 'Edit Profile' to open." height="30"
<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="Aron Homberg" />
        </div>
        <div>
          <label class="label" for="username">Username</label>
          <input class="input" id="username" type="text" value="@kyr0" />
        </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>
```

```html example label="Confirmation" hint="Destructive action confirmation. Click 'Confirm Delete' to open." height="30"
<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>
```

```html example label="Density" hint="Set data-density on the {''} root to scale the content padding. A whitespace policy, not a zoom: only padding scales (ratio 0.75 / 1 / 1.25), typography and the width ladder (data-size) stay identical. comfortable matches the unsized default." height="30"
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap">
  <button class="btn" data-variant="outline" data-dialog-trigger="density-compact-dialog" aria-haspopup="dialog"
    >Compact</button
  >
  <button class="btn" data-variant="outline" data-dialog-trigger="density-comfortable-dialog" aria-haspopup="dialog"
    >Comfortable</button
  >
  <button class="btn" data-variant="outline" data-dialog-trigger="density-spacious-dialog" aria-haspopup="dialog"
    >Spacious</button
  >
</div>

<dialog
  id="density-compact-dialog"
  class="dialog"
  data-density="compact"
  aria-labelledby="density-compact-dialog-title"
>
  <div class="dialog-content">
    <div class="dialog-header">
      <h3 class="dialog-title" id="density-compact-dialog-title">Compact</h3>
      <p class="dialog-description">Content padding 1rem (ratio 0.75).</p>
    </div>
    <button class="btn" data-variant="outline" data-dialog-close>Close</button>
  </div>
</dialog>

<dialog id="density-comfortable-dialog" class="dialog" aria-labelledby="density-comfortable-dialog-title">
  <div class="dialog-content">
    <div class="dialog-header">
      <h3 class="dialog-title" id="density-comfortable-dialog-title">Comfortable</h3>
      <p class="dialog-description">Content padding 1.5rem - matches the unsized default.</p>
    </div>
    <button class="btn" data-variant="outline" data-dialog-close>Close</button>
  </div>
</dialog>

<dialog id="density-spacious-dialog" class="dialog" aria-labelledby="density-spacious-dialog-title">
  <div class="dialog-content">
    <div class="dialog-header">
      <h3 class="dialog-title" id="density-spacious-dialog-title">Spacious</h3>
      <p class="dialog-description">Content padding 2rem (ratio 1.25).</p>
    </div>
    <button class="btn" data-variant="outline" data-dialog-close>Close</button>
  </div>
</dialog>

<!-- density scales ONLY the content padding - typography and the data-size width ladder stay identical -->
```

<StatesSection>
  <p class="text-sm text-muted-foreground m-0 mb-3">Named states via the shared State API, driven per instance through the bound <code>api</code>:</p>
  <ul class="text-sm text-muted-foreground m-0 mb-3 ps-5">
    <li><code>default</code> - closed (the authored state)</li>
    <li><code>open</code> - shown modally via <code>showModal()</code></li>
  </ul>
  <p class="text-sm text-muted-foreground">The demo dialog carries <code>data-state-demo</code>; <code>bun run screenshots</code> drives it via <code>el.api.setState(name)</code> and captures <code>screenshots/\{mode\}/dialog-\{state\}.png</code>.</p>

  <p class="text-sm text-muted-foreground m-0 mb-3">Machine contract - verified against <code>dialog.schema.json</code> by <code>bun run verify</code> (§13–§18):</p>

```states
| State | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean` | `true`, `false` | `false` | Modal visibility - observed via the native `open` property; `true` requires `showModal()` (or the `showModal` action), `false` via `close()`. |
```

</StatesSection>

<ApiSection component="dialog" />

<SourceFiles component="dialog" section="css" />

<SourceFiles component="dialog" section="js">
  <SourceNote for="js">Wire triggers via <code>data-dialog-trigger</code>, close buttons via <code>data-dialog-close</code>, and backdrop click. Focus returns to trigger on close.</SourceNote>
</SourceFiles>


</DocPage>
