<!-- index: /slotted/llms.txt -->
# dialog

`data-ui="dialog"` · Modal dialog and alert dialog.

## Import

```tsx
import { Dialog, DialogBody, DialogClose, DialogContent, DialogDefaultsProvider, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@slotted/react';
```

```tsx
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogIcon, DialogBody, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { useState } from 'react';

import { Button } from '@slotted/react/button';
import {
  Dialog,
  DialogBody,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@slotted/react/dialog';
import { Field, FieldLabel } from '@slotted/react/field';
import { Inline } from '@slotted/react/inline';
import { TextField } from '@slotted/react/text-field';
import { Text } from '@slotted/react/typography';

export default function DialogBasic() {
  const [open, setOpen] = useState(false);
  const [email, setEmail] = useState('ap@northwindtraders.com');
  const [draft, setDraft] = useState(email);

  return (
    <Inline align="center" gap={3}>
      <Text size="sm">{email}</Text>
      <Dialog
        open={open}
        onOpenChange={(next) => {
          if (next) setDraft(email);
          setOpen(next);
        }}
      >
        <DialogTrigger
          render={(props) => (
            <Button {...props} size="sm" tone="secondary" variant="outline">
              Edit billing email
            </Button>
          )}
        />
        <DialogContent>
          <DialogHeader>
            <DialogTitle>Billing email</DialogTitle>
            <DialogDescription>
              Invoices and payment receipts for Northwind Traders go to this address.
            </DialogDescription>
            <DialogClose />
          </DialogHeader>
          <DialogBody>
            <Field>
              <FieldLabel>Email</FieldLabel>
              <TextField type="email" value={draft} onValueChange={setDraft} />
            </Field>
          </DialogBody>
          <DialogFooter>
            <Button variant="ghost" tone="secondary" onClick={() => setOpen(false)}>
              Cancel
            </Button>
            <Button
              onClick={() => {
                setEmail(draft);
                setOpen(false);
              }}
            >
              Save
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </Inline>
  );
}
```

## Anatomy

### dialog

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, in a portal | yes |
| `trigger` | button, or the element render returns | no |
| `backdrop` | div | yes |
| `viewport` | div | yes |
| `content` | div | yes |
| `header` | header, or the element render returns | no |
| `title` | h2, or the element render returns | no |
| `description` | p, or the element render returns | no |
| `body` | div, or the element render returns | no |
| `footer` | footer, or the element render returns | no |
| `close` | button | no |
| `fullscreen` | button | no |

### alertDialog

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, in a portal | yes |
| `trigger` | button, or the element render returns | no |
| `backdrop` | div | yes |
| `viewport` | div | yes |
| `content` | div | yes |
| `header` | header, or the element render returns | no |
| `title` | h2, or the element render returns | no |
| `description` | p, or the element render returns | yes |
| `body` | div, or the element render returns | no |
| `footer` | footer, or the element render returns | no |
| `icon` | span | no |
| `cancel` | button | yes |
| `action` | button | yes |

## Props

See [dialog.props.md](./dialog.props.md).

## Events

### dialog

| Name | Payload | When |
| --- | --- | --- |
| `onOpenChange` | `(open: boolean, request: DialogChangeRequest)` | Before the dialog opens or closes, whatever caused it. The request carries the new value, the previous value, the reason (pointer, keyboard, dismiss, escape, outsidePress, back, submit or programmatic), the result of the close, the originating event, and cancel(), which keeps the dialog as it is. One handler therefore guards the ✕, Escape, a press outside, the way back and the ref handle together. |
| `onOpenChangeComplete` | `(open: boolean)` | After the entrance or the exit has ended: true once the layer is settled open, false once it has left — the moment an application that renders the layer conditionally removes it, so the exit runs. A layer mounted open enters from its closed style, as one opened later does. |
| `onFullscreenChange` | `(fullscreen: boolean, request: DialogFullscreenRequest)` | Before the panel expands to the whole layer or is restored. The request carries the new value, the previous value, the reason (pointer, keyboard or programmatic), the originating event, and cancel(), which keeps the panel as it is. |
| `onClick, onKeyDown and the other native events, on every part` | `the element’s own event` | Whenever the element fires them. Your handler runs before the dialog’s own, and calling preventDefault() in it cancels the dialog’s behavior, such as opening on a trigger press or closing on a close press. |

### alertDialog

| Name | Payload | When |
| --- | --- | --- |
| `onOpenChange` | `(open: boolean, request: DialogChangeRequest)` | Before the dialog opens or closes, whatever caused it. The request carries the new value, the previous value, the reason (pointer, keyboard, dismiss, escape, outsidePress, back, submit or programmatic), the result of the close, the originating event, and cancel(), which keeps the dialog as it is. One handler therefore guards the ✕, Escape, a press outside, the way back and the ref handle together. |
| `onOpenChangeComplete` | `(open: boolean)` | After the entrance or the exit has ended: true once the layer is settled open, false once it has left — the moment an application that renders the layer conditionally removes it, so the exit runs. A layer mounted open enters from its closed style, as one opened later does. |
| `onFullscreenChange` | `(fullscreen: boolean, request: DialogFullscreenRequest)` | Before the panel expands to the whole layer or is restored. The request carries the new value, the previous value, the reason (pointer, keyboard or programmatic), the originating event, and cancel(), which keeps the panel as it is. |
| `onClick, onKeyDown and the other native events, on every part` | `the element’s own event` | Whenever the element fires them. Your handler runs before the dialog’s own, and calling preventDefault() in it cancels the dialog’s behavior, such as opening on a trigger press or closing on a close press. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg`, `xl`, `full` | `md`, `sm` |
| `placement` | `center`, `top` | `center` |
| `scroll` | `content`, `viewport` | `content` |
| `surface` | `panel`, `plain` | `panel` |
| `mount` | `active`, `lazy`, `eager` | `active` |
| `fullScreen` | `never`, `narrow`, `always` | `narrow`, `never` |
| `modal` | `true`, `false` | `true` |
| `escape` | `close`, `none` | `close` |
| `outsidePress` | `close`, `none` | `close`, `none` |
| `back` | `close`, `none` | `close` |
| `tone` | `accent`, `secondary`, `info`, `success`, `warning`, `danger` | `danger` |
| `initialFocus` | `first`, `container`, `cancel` | `first`, `cancel` |

## Rules

- Dialog follows the WAI-ARIA dialog (modal) pattern. While a modal dialog is open, Tab and Shift+Tab move through the focusable elements inside it and wrap from the last to the first and back. Escape closes the topmost dialog only; a dialog that refuses the close keeps the Escape from reaching the dialog beneath it.
- The panel carries role="dialog", aria-labelledby pointing at the title and aria-describedby pointing at the description, each only while that part is rendered. An aria-label of your own on DialogContent replaces aria-labelledby.
- aria-modal="true" is written only while the dialog is modal. A modal dialog makes everything outside it inert, which removes the page from the accessibility tree and from the pointer, and locks the page scroll. modal={false} does none of this and writes no aria-modal.
- When the dialog opens, focus moves to its first focusable element, to the element initialFocus names, or to the panel itself with initialFocus="container", for content that is read before it is used. When it closes, focus returns to the element that had it when the dialog opened, or to the element finalFocus names. If that element has left the document, focus goes to its nearest ancestor still in the document.
- Containment, inertness and Escape are implemented in JavaScript rather than with the native dialog element, so every overlay of the library shares one stacking order: a select opened inside a dialog paints above it. Without JavaScript, there is no dialog.
- The close button’s target is at least 24 by 24 pixels, and its mark is drawn from a glyph, not a character.
- The panel has a border as well as a shadow, so its edge stays visible under forced colors, where shadows are removed and the scrim becomes opaque.
- AlertDialog follows the WAI-ARIA alert dialog pattern: the modal dialog pattern with role="alertdialog" and a required description. The keyboard is the same: Tab and Shift+Tab wrap inside the panel, and Escape closes it.
- aria-describedby is required. AlertDialog throws an error when no DialogDescription is rendered, so a confirmation is never announced without the sentence that says what it does.
- Focus starts on AlertDialogCancel, the least destructive answer, as the pattern advises for a step that cannot be undone easily.
- A press outside never closes an alert dialog, and it is always modal. Escape closes it, which answers with the cancel; set escape="none" when cancelling is not safe either.
- There is no close part: the reader answers with the cancel or the action. Rendering DialogClose inside AlertDialog throws an error.
- The tone is carried by the icon’s shape as well as by its color, so it survives forced colors and readers who cannot tell the colors apart.

## Tokens

`--slotted-action-gap`, `--slotted-border-subtle`, `--slotted-border-width-thick`, `--slotted-border-width-thin`, `--slotted-control-font-family`, `--slotted-control-icon-size-lg`, `--slotted-control-inner-size-lg`, `--slotted-control-radius`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-dialog-backdrop-filter`, `--slotted-dialog-body-color`, `--slotted-dialog-border-color`, `--slotted-dialog-close-active-background`, `--slotted-dialog-close-color`, `--slotted-dialog-close-hover-background`, `--slotted-dialog-color`, `--slotted-dialog-description-color`, `--slotted-dialog-footer-gap`, `--slotted-dialog-footer-surface`, `--slotted-dialog-measure`, `--slotted-dialog-measure-lg`, `--slotted-dialog-measure-md`, `--slotted-dialog-measure-sm`, `--slotted-dialog-measure-xl`, `--slotted-dialog-padding-block`, `--slotted-dialog-padding-block-sm`, `--slotted-dialog-padding-inline`, `--slotted-dialog-padding-inline-sm`, `--slotted-dialog-radius`, `--slotted-dialog-rule-color`, `--slotted-dialog-scrim`, `--slotted-dialog-scrim-backdrop-filter`, `--slotted-dialog-surface`, `--slotted-dialog-title-color`, `--slotted-dialog-title-font-family`, `--slotted-duration-long`, `--slotted-duration-medium`, `--slotted-duration-short`, `--slotted-easing-exit`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-family-heading`, `--slotted-font-family-sans`, `--slotted-font-size-lg`, `--slotted-font-size-md`, `--slotted-font-size-xl`, `--slotted-font-weight-semibold`, `--slotted-form-measure`, `--slotted-line-height-md`, `--slotted-radius-lg`, `--slotted-radius-sm`, `--slotted-scrim`, `--slotted-shadow-lg`, `--slotted-space-12`, `--slotted-space-2`, `--slotted-space-4`, `--slotted-space-5`, `--slotted-space-6`, `--slotted-space-7`, `--slotted-space-8`, `--slotted-surface-raised`, `--slotted-surface-sunken`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-text`, `--slotted-tone-danger-text`, `--slotted-tone-info-text`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`, `--slotted-tone-secondary-text`, `--slotted-tone-success-text`, `--slotted-tone-warning-text`, `--slotted-z-index-overlay`

## Examples

See [dialog.examples.md](./dialog.examples.md).
