# Confirm dialog

The replacement for `window.confirm`: a focused alert dialog, or `confirm()` as a promise.

Live variants: https://design.arcadia.is/components/confirm-dialog

```tsx
import { ConfirmDialog, ConfirmHost, confirm } from "@arcadia/ui";
```

```tsx
import { useState } from "react";
import { confirm, Button } from "@arcadia/ui";

function Imperative() {
  const [result, setResult] = useState<string | null>(null);
  const remove = async () => {
    const ok = await confirm({
      title: "Delete “Quarterly report”",
      description: "The chat and its checkpoints are removed. This can't be undone.",
      confirmLabel: "Delete chat",
      tone: "danger",
    });
    setResult(ok ? "deleted" : "cancelled");
  };
  return (
    <>
      <Button variant="danger" icon="trash" onClick={remove}>Delete chat</Button>
      {result && <span {...sx("cmp-result", cmp.result)}>Last result: {result}</span>}
    </>
  );
}
```

## Accessibility

The platform's modal `dialog` in the `alertdialog` role, named by its title and described by its description, that ignores clicks outside. Initial focus lands on the confirm button in both tones, so Enter confirms and Esc cancels; the title and the button name the action, and `danger` draws it red. Focus returns to the button that asked.

- Enter: Confirms, unless focus is on another button
- Esc: Cancels
- Tab: Moves between Cancel and the action

## API reference

### ConfirmDialog

- `open` (`boolean`)
- `defaultOpen` (`boolean`, default `false`)
- `onOpenChange` (`(open: boolean) => void`)
- `onOpenChangeComplete` (`(open: boolean) => void`): Called after the close animation.
- `onConfirm` (`() => unknown`): Runs on confirm. A returned promise shows the loading state; a rejection keeps the dialog open and shows its message, or the confirm label's action ("Couldn't delete chat.") when it says nothing. The dialog closes when it resolves.
- `onCancel` (`() => void`)
- `loading` (`boolean`): Controlled loading state (overrides the async one).
- `error` (`ReactNode`): Controlled error message (overrides a caught rejection).
- `modal` (`boolean`, default `true`): `false` for inline previews.
- `initialFocus` (`ComponentProps<typeof DialogContent>["initialFocus"]`): Override the initial focus (default: the confirm button, in either tone; `false` leaves focus where it is, for inline previews).
- `container` (`PortalContainer`, default `document.body`): Portal container.
- `title` (`ReactNode`, required)
- `description` (`ReactNode`)
- `confirmLabel` (`string`, required): A verb and its object: "Delete chat", "Discard changes".
- `cancelLabel` (`string`, default `"Cancel"`)
- `tone` (`"neutral" | "danger"`, default `"neutral"`)
- `confirmProps` (`ButtonMarks`): The product's own marks on the confirm button: its `data-*` only.
- `cancelProps` (`ButtonMarks`): The product's own marks on Cancel: its `data-*` only.
- `children` (`ReactNode`): Extra content between the description and the actions (a "don't ask again" switch).

### confirm()

- `title` (`ReactNode`, required)
- `description` (`ReactNode`)
- `confirmLabel` (`string`, required): A verb and its object: "Delete chat", "Discard changes".
- `cancelLabel` (`string`, default `"Cancel"`)
- `tone` (`"neutral" | "danger"`, default `"neutral"`)
- `confirmProps` (`ButtonMarks`): The product's own marks on the confirm button: its `data-*` only.
- `cancelProps` (`ButtonMarks`): The product's own marks on Cancel: its `data-*` only.
