# Dialog

Modal dialogs on the native `<dialog>`, opened by invoker commands.

Source: https://www.zazz.sh/api/primitives/dialog/

## Installation

```bash
pnpm dlx zazz-ui add dialog
```

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<button data-ui="button" type="button" command="show-modal" commandfor="dialog-example-1">
  Dialog
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
    <rect width="256" height="256" fill="none" />
    <line
      x1="64"
      y1="192"
      x2="192"
      y2="64"
      fill="none"
      stroke="currentColor"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="16"
    />
    <polyline
      points="88 64 192 64 192 168"
      fill="none"
      stroke="currentColor"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="16"
    />
  </svg>
</button>
<dialog id="dialog-example-1" data-ui="dialog" closedby="any">
  <div data-dialog-slot="content">
    <header data-dialog-slot="header">
      <h2
        style="
          --font-size: var(--font-size-lg);
          --font-weight: strong;
          --font-family: var(--font-family-heading);
        "
      >
        Dialog title
      </h2>
      <p style="--text: var(--color-muted-foreground)">
        This dialog was opened using an
        <a
          href="https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API"
          target="_blank"
          style="--text: var(--color-primary)"
          >invoker command</a
        >.
      </p>
    </header>
    <div data-dialog-slot="body">
      <p>Set the size of the dialog with the <code>data-dialog-size</code> attribute.</p>
    </div>
    <footer data-dialog-slot="footer">
      <button data-ui="button" type="button" commandfor="dialog-example-1" command="close">
        Cancel
      </button>
      <button
        data-ui="button"
        data-button-variant="primary"
        commandfor="dialog-example-1"
        command="close"
      >
        Accept
      </button>
    </footer>
  </div>
  <button
    data-ui="button"
    data-dialog-slot="close"
    data-button-size="icon"
    data-button-variant="ghost"
    type="button"
    commandfor="dialog-example-1"
    command="close"
  >
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
      <rect width="256" height="256" fill="none" />
      <line
        x1="200"
        y1="56"
        x2="56"
        y2="200"
        stroke="currentColor"
        stroke-linecap="round"
        stroke-linejoin="round"
        stroke-width="16"
      />
      <line
        x1="200"
        y1="200"
        x2="56"
        y2="56"
        stroke="currentColor"
        stroke-linecap="round"
        stroke-linejoin="round"
        stroke-width="16"
      />
    </svg>
  </button>
</dialog>
```

### With form

```html
<button data-ui="button" type="button" command="show-modal" commandfor="form-dialog-1">
  New project
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
    <rect width="256" height="256" fill="none" />
    <line
      x1="40"
      y1="128"
      x2="216"
      y2="128"
      fill="none"
      stroke="currentColor"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="16"
    />
    <line
      x1="128"
      y1="40"
      x2="128"
      y2="216"
      fill="none"
      stroke="currentColor"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="16"
    />
  </svg>
</button>
<dialog id="form-dialog-1" data-ui="dialog" closedby="any" data-dialog-size="sm">
  <div data-dialog-slot="content">
    <header data-dialog-slot="header">
      <h2
        style="
          --font-size: var(--font-size-lg);
          --font-weight: strong;
          --font-family: var(--font-family-heading);
        "
      >
        New project
      </h2>
      <p style="--text: var(--color-muted-foreground)">
        Add a project to your workspace. You can change these settings later.
      </p>
    </header>
    <form
      id="f-new-project-1"
      style="
        --display: flex-col;
        --gap: 6;
      "
      data-dialog-slot="body"
      method="dialog"
    >
      <fieldset data-ui="field-group">
        <legend style="--sr: only">Contact</legend>
        <div
          style="
            --display: grid;
            --grid-cols: 2;
            --gap: 4;
          "
        >
          <div data-ui="field">
            <label data-field-slot="label" for="fd-name">Name</label>
            <input
              data-ui="input"
              id="fd-name"
              name="name"
              type="text"
              inputmode="text"
              placeholder="Ada Lovelace"
              required
            />
          </div>
          <div data-ui="field">
            <label data-field-slot="label" for="fd-email">Email</label>
            <input
              data-ui="input"
              id="fd-email"
              name="email"
              type="email"
              inputmode="email"
              required
              placeholder="you@example.com"
              autocomplete="email"
              aria-describedby="fd-email-hint"
            />
            <div data-field-slot="description">
              <span data-field-slot="hint" id="fd-email-hint"
                >We'll only use this to send receipts.</span
              >
              <span data-field-slot="error" role="alert">Please enter a valid email address.</span>
            </div>
          </div>
          <label data-ui="field" style="--col-span: 2">
            <span data-field-slot="label">Repository</span>
            <div data-ui="input-group">
              <input
                data-ui="input"
                type="search"
                name="repo"
                id="fd-repo"
                inputmode="search"
                placeholder="Search repositories…"
              />
              <span data-input-group-slot="addon">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
                  <rect width="256" height="256" fill="none" />
                  <circle
                    cx="112"
                    cy="112"
                    r="80"
                    fill="none"
                    stroke="currentColor"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    stroke-width="16"
                  />
                  <line
                    x1="168.57"
                    y1="168.57"
                    x2="224"
                    y2="224"
                    fill="none"
                    stroke="currentColor"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    stroke-width="16"
                  />
                </svg>
              </span>
            </div>
          </label>
        </div>
      </fieldset>

      <fieldset data-ui="field-group">
        <div data-ui="field">
          <label data-field-slot="label" for="fd-message">Description</label>
          <textarea
            data-ui="textarea"
            id="fd-message"
            name="description"
            inputmode="text"
            placeholder="What is this project for?"
          ></textarea>
        </div>
      </fieldset>

      <fieldset data-ui="radio-group">
        <legend>Billing</legend>
        <label data-ui="field" data-field-orientation="horizontal">
          <input data-ui="radio" type="radio" name="billing" value="monthly" />
          <span data-field-slot="label">Monthly billing</span>
        </label>
        <label data-ui="field" data-field-orientation="horizontal">
          <input data-ui="radio" type="radio" name="billing" value="annual" checked />
          <span data-field-slot="label">Annual billing</span>
        </label>
      </fieldset>

      <label data-ui="field" data-field-orientation="horizontal">
        <input type="checkbox" name="terms" required />
        <span data-field-slot="label">Accept terms &amp; conditions</span>
      </label>
    </form>
    <footer data-dialog-slot="footer">
      <button
        data-ui="button"
        type="button"
        value="cancel"
        commandfor="form-dialog-1"
        command="close"
      >
        Cancel
      </button>
      <button
        data-ui="button"
        data-button-variant="primary"
        type="submit"
        form="f-new-project-1"
        value="save"
      >
        Create project
      </button>
    </footer>
    <button
      data-ui="button"
      data-dialog-slot="close"
      data-button-size="icon"
      data-button-variant="ghost"
      type="button"
      commandfor="form-dialog-1"
      command="close"
      aria-label="Close dialog"
    >
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256">
        <rect width="256" height="256" fill="none" />
        <line
          x1="200"
          y1="56"
          x2="56"
          y2="200"
          stroke="currentColor"
          stroke-linecap="round"
          stroke-linejoin="round"
          stroke-width="16"
        />
        <line
          x1="200"
          y1="200"
          x2="56"
          y2="56"
          stroke="currentColor"
          stroke-linecap="round"
          stroke-linejoin="round"
          stroke-width="16"
        />
      </svg>
    </button>
  </div>
</dialog>
```

## Sizes

`data-dialog-size` follows the [layout](/api/primitives/layout/) sizes. `sm`, `md`, `lg`, `xl`, and `2xl` cap the width at that band (40, 48, 64, 80, and 96rem) and at the screen, inside the gutters. `full` spans the screen inside the gutters. `bleed` fills it edge to edge, with no radius or shadow, which is what the mobile menu and the lightbox use. With no size a dialog is `sm`.

```html
<dialog data-ui="dialog" data-dialog-size="lg" closedby="any">…</dialog>
```

Below the `sm` band on a touch screen (a narrow desktop window keeps its preset), every dialog takes the size in `--ui-dialog-mobile-size` over its preset: `layout-bleed` by default, edge to edge and full height, with the content slot stretched to the bottom of the screen above the home indicator and only the body scrolling. `layout-full` keeps the gutters, and `none` keeps the dialog's own preset. The hook is read on the dialog's parent, so set it on `:root` for the site, or on one dialog's parent element for that dialog alone.

```css
:root {
  --ui-dialog-mobile-size: layout-full;
}
```

## Light dismiss

`closedby="any"` on the `<dialog>` lets a click on the backdrop close it, next to Esc. Chrome 134 and Firefox 141 do this natively. Safari 26 knows `closedby` but only its `none` and `closerequest` values, so `any` falls back to `closerequest` and the backdrop does nothing. The kit's dialog lifecycle script fills that gap: where `any` does not round-trip through the `closedBy` property, a press and release on the backdrop of a `closedby="any"` dialog calls `requestClose()`, which fires `cancel` as the native path does. Nothing in the markup changes, and the fallback stays idle where the browser handles it. A dialog that holds a form or a search still deserves a visible close button, for the people who find neither the backdrop nor Esc. `closedby="none"` (alert dialogs) and the default are left alone in both cases.

## Scrolling inside

The content slot is a three-row grid, and each slot owns its row whatever the order it comes in: the header on top, the body in the flexible middle, the footer at the bottom. A tall body scrolls on its own, inside the dialog's height cap, so the header, the footer, and the close button hold still around it; that is true of a dialog with a body and nothing else too.

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="dialog"` | identity | free value |
| `data-dialog-size` | preset | `2xl` `bleed` `full` `lg` `md` `sm` `xl` |
| `data-dialog-slot` | slot | `body` `close` `content` `footer` `header` |

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-dialog-backdrop-color` | `var(--color-shade-900)` | — |
| `--ui-dialog-backdrop-filter` | `none` | — |
| `--ui-dialog-backdrop-transition-duration` | `calc(var(--ui-dialog-transition-duration) / 2)` | — |
| `--ui-dialog-backdrop-transition-timing-function` | `var(--bezier-easing)` | — |
| `--ui-dialog-bg` | `var(--color-card)` | — |
| `--ui-dialog-border-color` | `var(--color-border)` | — |
| `--ui-dialog-border-style` | `solid` | — |
| `--ui-dialog-border-width` | `1px` | — |
| `--ui-dialog-corner-shape` | `var(--radius-corner-shape)` | — |
| `--ui-dialog-h` | `fit-content` | — |
| `--ui-dialog-mobile-size` | `layout-bleed` | — |
| `--ui-dialog-opacity--end` | `1` | end |
| `--ui-dialog-opacity--start` | `0` | start |
| `--ui-dialog-radius` | `var(--radius-lg)` | — |
| `--ui-dialog-scale--end` | `1` | end |
| `--ui-dialog-scale--start` | `0.98` | start |
| `--ui-dialog-shadow` | `var(--ui-alert-dialog-shadow)` | — |
| `--ui-dialog-text` | `var(--color-card-foreground)` | — |
| `--ui-dialog-transition-delay--end` | `0ms` | end |
| `--ui-dialog-transition-delay--start` | `0ms` | start |
| `--ui-dialog-transition-duration` | `var(--default-transition-duration)` | — |
| `--ui-dialog-transition-timing-function` | `var(--default-transition-timing-function)` | — |
| `--ui-dialog-translate--end` | `0, 0, 0` | end |
| `--ui-dialog-translate--start` | `0, 0, 0` | start |
| `--ui-dialog-w` | `var(--ui-alert-dialog-w)` | — |
