# Alert dialog

Asks people to confirm or cancel one consequential action, optionally by typing a confirmation.

- Family: overlay
- Import: `import { AlertDialog } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/alert-dialog
- JSON: https://design.senvenli.com/docs/components/alert-dialog.json

## Example: Confirm a delete

The title asks, the body says what is lost, the button repeats the verb.

```tsx
import { Button } from "@senvenli/react/action";
import { AlertDialog, DialogTrigger } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <DialogTrigger>
      <Button variant="primary" tone="danger">Delete invoice</Button>
      <AlertDialog title="Delete invoice INV-2041?" confirmLabel="Delete invoice" onConfirm={() => {}}>
        The invoice and its payment history are removed for everyone on the team.
      </AlertDialog>
    </DialogTrigger>
  );
}
```

## Examples

### Typed confirmation

For deletes that cannot be undone: the button enables once the name is typed.

```tsx
import { Button } from "@senvenli/react/action";
import { AlertDialog, DialogTrigger } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <DialogTrigger>
      <Button variant="primary" tone="danger">Delete project</Button>
      <AlertDialog
        title="Delete the project Atlas?"
        confirmLabel="Delete project"
        requireTypedConfirmation="Atlas"
        typedConfirmationLabel="Type the project name to confirm"
        onConfirm={() => {}}
      >
        Its 48 tasks and 12 files are deleted for all 6 members. This cannot be undone.
      </AlertDialog>
    </DialogTrigger>
  );
}
```

### Warning and neutral

A risky step, and a confirm that neither destroys nor risks anything.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";
import { AlertDialog, DialogTrigger } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <DialogTrigger>
        <Button>Change plan</Button>
        <AlertDialog tone="warning" title="Move to the Starter plan?" confirmLabel="Change plan" cancelLabel="Keep Team plan" onConfirm={() => {}}>
          Your team keeps access until March 31. After that, 14 of 20 members lose their seats.
        </AlertDialog>
      </DialogTrigger>
      <DialogTrigger>
        <Button>Reactivate account</Button>
        <AlertDialog tone="neutral" title="Reactivate Ada Lovelace's account?" confirmLabel="Reactivate account" onConfirm={() => {}}>
          She can sign in again and sees her projects as she left them.
        </AlertDialog>
      </DialogTrigger>
    </Stack>
  );
}
```

### An async action

The confirm button shows pending until the promise settles, then the alert closes.

```tsx
import { Button } from "@senvenli/react/action";
import { AlertDialog, DialogTrigger, toast } from "@senvenli/react/overlay";

// Stands in for the request; while it runs the alert cannot be cancelled.
const cancelOrder = () => new Promise<void>((resolve) => setTimeout(resolve, 1500));

export default function Example() {
  return (
    <DialogTrigger>
      <Button>Cancel order</Button>
      <AlertDialog
        tone="warning"
        title="Cancel order ORD-1040?"
        confirmLabel="Cancel order"
        cancelLabel="Keep order"
        onConfirm={async () => {
          await cancelOrder();
          toast.success("Order canceled", { description: "ORD-1040 · the customer was refunded" });
        }}
      >
        The customer is refunded $248.00 and gets an email about it.
      </AlertDialog>
    </DialogTrigger>
  );
}
```

## Guidelines

### When to use it

- Before a destructive or risky action: delete, revoke, discard.

### Use something else when

- [dialog](https://design.senvenli.com/docs/components/dialog.md): The task needs a form or more than a confirm.
- [toast](https://design.senvenli.com/docs/components/toast.md): The action can be undone, so offer Undo instead of asking first.

### Do and avoid

- Title is the question, body is the consequence, the confirm button repeats the verb.

## Accessibility

Keyboard:

- Esc: Cancels.

Built in:

- Role alertdialog; "Cancel" takes the first focus so Enter never destroys by accident.

You provide:

- A question title and the consequence.

## API reference

### AlertDialog

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| isOpen | `boolean` |  | no | Open without a DialogTrigger (a URL, a row click). Inside a DialogTrigger, put `onOpenChange` on the trigger instead: the trigger owns the state there. |
| defaultOpen | `boolean` |  | no |  |
| onOpenChange | `(isOpen: boolean) => void` |  | no |  |
| tone | `"neutral" \| "danger" \| "warning"` |  | no | `danger` (default: a delete, `trash-2`, a danger button), `warning` (`triangle-alert`, a primary button), or `neutral` for a confirm that neither destroys nor risks anything, such as reactivating an account (`info` on a neutral ground, a primary button). |
| title | `React.ReactNode` |  | yes | The action as a question with the object's name: "PG AB 482 filodan silinsin mi?" |
| children | `React.ReactNode` |  | yes | The consequence, and what to do first. |
| confirmLabel | `string` |  | yes | The verb again: "Delete item". |
| cancelLabel | `string` |  | no | The cancel button's text, and so its accessible name ("No, keep it"), in place of the library's "Cancel" . The app passes it already localized; the DS does not translate it. Left out (or `""`), the locale's "Cancel" renders and the DOM is unchanged. A long label wraps inside the button, which takes at most the footer's width; focus, Esc and the pending lock are the same either way. |
| onConfirm | `() => void \| Promise<void>` |  | yes | May be async: the button shows pending until it settles, and while it runs the dialog cannot be cancelled (the action is under way); it closes when the action succeeds and stays open when it fails. |
| requireTypedConfirmation | `string` |  | no | For irreversible deletes: the text the user types before the button enables ("PG AB 482"). |
| isConfirmDisabled | `boolean` |  | no | Holds the confirm back while the app loads what it needs ("Loading linked orders"). The button keeps its name and stays in the dialog, disabled as with `requireTypedConfirmation`: it cannot be pressed (pointer, Enter or Space) and Tab passes it, so "Cancel" stays the one control; screen readers list it as unavailable. Say why in the description or in the element `aria-describedby` names. Esc and "Cancel" still work. |
| aria-describedby | `string` |  | no | More of what the alert says, by id: an element the app renders, inside the alert (a Banner in its body) or outside it (a page notice). Added to the alert's own description in `aria-describedby`, after it, so the alert is announced with both when it opens. |
| typedConfirmationLabel | `string` |  | no | The field's label; "Type ORD-482 to confirm" by default. |
| icon | `IconName` |  | no | Overrides the tone's icon (danger `trash-2`, warning `triangle-alert`). |
| returnFocusRef | `RefObject<HTMLElement \| null>` |  | no | Where focus goes when the alert closes and React Aria's return would leave it on the page or on the opener, as on Dialog. Needed when the element React Aria returns to is gone by the close: an alert opened from a menu whose trigger unmounts when it closes (pass a stable button), or a confirmed action that removes the row with its Menu (pass a stable element). Opened from a DS Menu item it is not needed: React Aria returns focus to the Menu button itself, and a ref naming another element does not override that. Read when the alert closes; if the app has already moved focus elsewhere on close, it stays there; if the ref is empty, focus returns as before. There is no `initialFocusRef`: the alert's controls are its own, and "Cancel" takes the first focus. Nor is there a `titleRef`: the alert is announced with its title and consequence when it opens (`aria-labelledby`, `aria-describedby`), and focus starts on the safe answer, never on its text. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### Tokens it reads

`--ds-background-danger`, `--ds-background-neutral`, `--ds-background-warning`, `--ds-blanket`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-icon`, `--ds-icon-danger`, `--ds-icon-warning`, `--ds-radius-full`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-size-control`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-subtle`

## Related

- [dialog](https://design.senvenli.com/docs/components/dialog.md)
