# Dialog

A modal window over a dimmed page, and the Alert Dialog that asks before something can't be undone. Built on ui-sdk: the page behind is inert, focus stays inside and starts on the first control, Escape closes, and focus returns to the trigger.

- Beta: ready to use; its API can still change in a minor release.
- Category: Overlays

```tsx
import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle } from "@leap-crm/design-system";
```

## Best practices

- Do: Use `Dialog` for a short task that holds the page until it is done, like editing a contact's details. Use `AlertDialog` to ask before something can't be undone, like archiving a contact.
- Do: Write an `AlertDialogTitle` as a question, like Archive contact?, say what happens in `AlertDialogDescription`, and name the confirming button with the action, like Archive.
- Do: Put `data-agent-confirm` on the confirming button in the footer, not on the trigger, so Firm Pilot can't archive, send or delete without the lawyer.
- Do: Let closing return focus on its own: it goes back to the trigger, or to what had focus before the dialog opened. Pass `finalFocus` only when that control is gone, like the row a delete removed.
- Don't: Turn off `closeButton` on `DialogHeader`. A dialog always needs a visible way out besides Escape; only `AlertDialog` leaves it out, because its choice is in the footer.
- Don't: Use a dialog for a long form or a record read beside the page, like a whole contact. Use `Drawer`, the CRM's side panel.

## Keyboard

| Keys | What they do |
| --- | --- |
| Enter, Space | Open the dialog from its trigger. |
| Tab | Moves through the dialog in order, wrapping from the last element to the first. |
| Shift + Tab | From the first element, wraps to the last. |
| Escape | Closes it and returns focus to the trigger. |

## Related

- [Drawer](./drawer.md)
- [Popover](./popover.md)
- [Toast](./toast.md)

## Examples

What the examples import:

```tsx
import { Archive, Pencil } from "lucide-react";
import { useId, useState } from "react";
import { AlertDialog, AlertDialogBody, AlertDialogClose, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, Button, Checkbox, Dialog, DialogBody, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const sizes = ["sm", "md", "lg", "xl"] as const;

function ArchiveContact() {
  const [result, setResult] = useState("");
  const alsoOpportunities = useId();
  return (
    <div className="flex items-center gap-3">
      <AlertDialog
        onOpenChange={(open, details) => {
          if (!open) setResult(details.returnValue ?? "dismissed");
        }}
      >
        <AlertDialogTrigger
          render={
            <Button intent="destructive" icon={<Archive />}>
              Archive contact
            </Button>
          }
        />
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>Archive contact?</AlertDialogTitle>
            <AlertDialogDescription>
              Jane Citizen will be hidden from search. Her matters stay in LEAP.
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogBody>
            <div className="flex items-center gap-2">
              <Checkbox id={alsoOpportunities} defaultChecked />
              <label
                htmlFor={alsoOpportunities}
                className="text-neutral-text-secondary text-base"
              >
                Also archive 2 open opportunities
              </label>
            </div>
          </AlertDialogBody>
          <AlertDialogFooter>
            <AlertDialogClose
              value="cancel"
              render={<Button intent="outline">Cancel</Button>}
            />
            <AlertDialogClose
              value="archive"
              render={
                <Button intent="destructive" icon={<Archive />}>
                  Archive
                </Button>
              }
            />
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
      {result && (
        <span className="text-body text-neutral-text-muted">
          Closed with: {result}
        </span>
      )}
    </div>
  );
}

function EditDialog({ size }: { size: (typeof sizes)[number] }) {
  return (
    <Dialog>
      <DialogTrigger
        render={
          <Button intent="outline" icon={<Pencil />} className="uppercase">
            {size}
          </Button>
        }
      />
      <DialogContent size={size}>
        <DialogHeader>
          <DialogTitle>Edit details</DialogTitle>
          <DialogDescription>
            Changes save to the contact and sync to LEAP.
          </DialogDescription>
        </DialogHeader>
        <DialogBody>
          <p className="text-body text-neutral-text-secondary">
            The {size} dialog is {{ sm: 448, md: 512, lg: 672, xl: 900 }[size]}{" "}
            px wide, and shrinks to the screen with 16 px either side.
          </p>
        </DialogBody>
        <DialogFooter>
          <DialogClose render={<Button intent="outline">Cancel</Button>} />
          <DialogClose render={<Button>Save</Button>} />
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
```

### Alert dialog

Asks before archiving. No close button and a press outside doesn't close it: the choice is in the footer. Each action's value reaches onOpenChange as details.returnValue.

```tsx
<ArchiveContact />
```

### Sizes

Header with a rule under it and the close button on the right, a padded body that scrolls when tall, and the actions on the subtle grey footer.

```tsx
<div className="flex gap-2">
  {sizes.map(size => (
    <EditDialog key={size} size={size} />
  ))}
</div>
```

## Props

Dialog takes ui-sdk's options (open, defaultOpen, onOpenChange, modal, closeOnOutsidePress, presentation, controller); AlertDialog the same minus modal and outside press. DialogContent takes initialFocus, finalFocus and keepMounted. DialogClose and AlertDialogClose take render and value.

### DialogContent

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | What it shows inside. |
| `size` | `"sm" \| "md" \| "lg" \| "xl"` | `"sm"` | sm 448 · md 512 · lg 672 · xl 900, shrinking to the screen. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### DialogHeader

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `closeButton` | `boolean` | `true` | Shows the close button on the right. Leave it on: a dialog should always have a visible way out besides Escape. |

### DialogBody

No props of its own.

### DialogFooter

No props of its own.

### DialogTitle

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### DialogDescription

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### AlertDialog

No props of its own.

### AlertDialogContent

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | What it shows inside. |
| `size` | `"sm" \| "md" \| "lg" \| "xl"` | `"sm"` | sm 448 · md 512 · lg 672 · xl 900, shrinking to the screen. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
