# Alert Dialog

A window that asks a person to confirm a destructive or important action.

Interrupts to confirm a consequential action (delete, discard, revoke): no close button, and a click outside does not close it. Focus starts on the safe action and the question is announced at once (role="alertdialog"). It is the Dialog with two fixed actions, Cancel and the action named by its verb ("Delete project"); Escape cancels. The window, its motion, its stacking and its phone layout are the Dialog's. Controlled or uncontrolled.

Kind: component · Category: Overlays · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `dialog` machine, through Ark UI

## Add

```bash
npx fndn add alert-dialog
```

It writes the folder `alert-dialog/` into the project's ui folder (`index.ts`, `alert-dialog.tsx`, `AlertDialog.vue`, `AlertDialog.svelte`, `alert-dialog.recipe.ts`), and what it is built from: [Dialog](/components/dialog.md), [Button](/components/button.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { AlertDialog } from "@/components/ui/alert-dialog";
```

```vue
import { AlertDialog } from "@/components/ui/alert-dialog";
```

```svelte
import { AlertDialog } from "$lib/components/ui/alert-dialog";
```

## Parts

In the DOM the root carries `data-slot="alert-dialog"` and each part `data-slot="alert-dialog-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `alert-dialog` |  | Layout-neutral hook (display: contents) around the Dialog, whose own parts (dialog-trigger, dialog-content, dialog-title, dialog-description, dialog-body, dialog-footer) render the window. |
| `cancel` | `button` | `alert-dialog-cancel` |  | The first footer Button (ghost, sm), marked data-alert-dialog="cancel". Takes focus when the window opens. Closes without acting. |
| `action` | `button` | `alert-dialog-action` |  | The second footer Button (sm; danger by default, primary when tone is neutral), marked data-alert-dialog="action". Calls onAction, then closes. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` | Bindable (React: `defaultOpen` and `onOpenChange`; Vue: `v-model:open`; Svelte: `bind:open`). Controlled: open with onOpenChange. Uncontrolled: defaultOpen. |
| `title` | `string` |  | Required: the question, which names the window ("Delete billing-api?"). |
| `description` | `string` |  | What happens, and whether it can be undone. Describes the window. |
| `actionLabel` | `string` | `"Continue"` | Label of the action: say what it does ("Delete project"), never "OK". |
| `cancelLabel` | `string` | `"Cancel"` | Label of the cancel Button. |
| `tone` | `"danger" \| "neutral"` | `"danger"` | danger: the action is a danger Button (a destructive confirm). neutral: a primary Button (an interruption that destroys nothing, such as leaving with unsent changes). |
| `size` | `"sm" \| "md"` | `"md"` | The Dialog's widths: sm 360px, md 440px. |
| `triggerLabel` | `string` |  | Renders the Dialog's default Button trigger with this label. Otherwise pass your own trigger, or control `open`. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `openChange` | (open: boolean) |
| `action` | () the action was pressed (the window then closes) |
| `cancel` | () Cancel, Escape, or a controlled close |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Body content under the description (optional) |
| `trigger` | Your own trigger element (React: the trigger prop; Vue: trigger slot; Svelte: trigger snippet receiving the props to spread) |

## States

`open`, `hover`, `press`, `focus-visible`

## Keyboard and accessibility

Role: alertdialog.

Keyboard:

- Enter / Space on the trigger open it; focus moves to Cancel
- Tab and Shift+Tab cycle between Cancel and the action (and any control in the body); focus cannot leave the window
- Escape cancels and returns focus to the trigger
- A click on the dimmed page does nothing and never moves focus out of the window

Notes:

- role="alertdialog", aria-modal, labelled by the title and described by the description: screen readers read both when it opens.
- Focus starts on Cancel so that Enter pressed by habit does not destroy anything.
- Opening and closing from the keyboard are instant (the Dialog's kbd-instant).

## Motion

- the Dialog's: the window fades and scales from motion.popup-scale around its centre over duration.slow with easing.out-strong, the backdrop fades with it; it closes at duration.fast; instant from the keyboard

## Dependencies

- [Dialog](/components/dialog.md)
- [Button](/components/button.md)

## Examples

### Delete a project

React · `delete-project.tsx`

```tsx
import { AlertDialog } from "@/components/ui/alert-dialog";

async function deleteProject(id: string) {
  await fetch(`/api/projects/${id}`, { method: "DELETE" });
}

/* an alert dialog interrupts to confirm: no close button, a click outside does nothing, focus
   starts on Cancel and the question is announced at once (role="alertdialog") */
export default function DeleteProject() {
  return (
    <div className="flex w-80 items-center gap-3">
      <div className="min-w-0 flex-1">
        <div className="text-sm font-medium text-ink">Delete this project</div>
        <div className="text-xs text-ink-2">Removes billing-api and all its deploys</div>
      </div>
      <AlertDialog
        triggerLabel="Delete project"
        title="Delete billing-api?"
        description="This deletes the project, its 214 deploys and its environment variables for everyone at Northwind. You cannot undo it."
        actionLabel="Delete project"
        onAction={() => deleteProject("billing-api")}
      />
    </div>
  );
}
```

Vue · `delete-project.vue`

```vue
<script setup lang="ts">
import { AlertDialog } from "@/components/ui/alert-dialog";

async function deleteProject(id: string) {
  await fetch(`/api/projects/${id}`, { method: "DELETE" });
}
</script>

<!-- an alert dialog interrupts to confirm: no close button, a click outside does nothing, focus
     starts on Cancel and the question is announced at once (role="alertdialog") -->
<template>
  <div class="flex w-80 items-center gap-3">
    <div class="min-w-0 flex-1">
      <div class="text-sm font-medium text-ink">Delete this project</div>
      <div class="text-xs text-ink-2">Removes billing-api and all its deploys</div>
    </div>
    <AlertDialog
      triggerLabel="Delete project"
      title="Delete billing-api?"
      description="This deletes the project, its 214 deploys and its environment variables for everyone at Northwind. You cannot undo it."
      actionLabel="Delete project"
      @action="deleteProject('billing-api')"
    />
  </div>
</template>
```

Svelte · `delete-project.svelte`

```svelte
<script lang="ts">
  import { AlertDialog } from "$lib/components/ui/alert-dialog";

  async function deleteProject(id: string) {
    await fetch(`/api/projects/${id}`, { method: "DELETE" });
  }
</script>

<!-- an alert dialog interrupts to confirm: no close button, a click outside does nothing, focus
     starts on Cancel and the question is announced at once (role="alertdialog") -->
<div class="flex w-80 items-center gap-3">
  <div class="min-w-0 flex-1">
    <div class="text-sm font-medium text-ink">Delete this project</div>
    <div class="text-xs text-ink-2">Removes billing-api and all its deploys</div>
  </div>
  <AlertDialog
    triggerLabel="Delete project"
    title="Delete billing-api?"
    description="This deletes the project, its 214 deploys and its environment variables for everyone at Northwind. You cannot undo it."
    actionLabel="Delete project"
    onaction={() => deleteProject("billing-api")}
  />
</div>
```

### Promote to Production

React · `deploy-to-production.tsx`

```tsx
import { AlertDialog } from "@/components/ui/alert-dialog";

/* consequential but not destructive: tone="neutral" makes the action a primary button */
export default function DeployToProduction() {
  return (
    <AlertDialog
      tone="neutral"
      triggerLabel="Promote to Production"
      title="Promote a1b2c3d to Production?"
      description="Every visitor of web-app gets this version within a minute. You can roll back from the deploy list."
      actionLabel="Promote to Production"
    />
  );
}
```

Vue · `deploy-to-production.vue`

```vue
<script setup lang="ts">
import { AlertDialog } from "@/components/ui/alert-dialog";
</script>

<!-- consequential but not destructive: tone="neutral" makes the action a primary button -->
<template>
  <AlertDialog
    tone="neutral"
    triggerLabel="Promote to Production"
    title="Promote a1b2c3d to Production?"
    description="Every visitor of web-app gets this version within a minute. You can roll back from the deploy list."
    actionLabel="Promote to Production"
  />
</template>
```

Svelte · `deploy-to-production.svelte`

```svelte
<script lang="ts">
  import { AlertDialog } from "$lib/components/ui/alert-dialog";
</script>

<!-- consequential but not destructive: tone="neutral" makes the action a primary button -->
<AlertDialog
  tone="neutral"
  triggerLabel="Promote to Production"
  title="Promote a1b2c3d to Production?"
  description="Every visitor of web-app gets this version within a minute. You can roll back from the deploy list."
  actionLabel="Promote to Production"
/>
```

### Revoke an API key

React · `revoke-api-key.tsx`

```tsx
import { Trash } from "@phosphor-icons/react";
import { AlertDialog } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

const key = { name: "Production server", hint: "nw_live_…4f2a", lastUsed: "Used 3 min ago" };

/* your own trigger: the row's icon button, named by its tooltip */
export default function RevokeApiKey() {
  return (
    <div className="flex w-80 items-center gap-3">
      <div className="min-w-0 flex-1">
        <div className="text-sm font-medium text-ink">{key.name}</div>
        <div className="text-xs text-ink-2"><span className="font-mono">{key.hint}</span> · {key.lastUsed}</div>
      </div>
      <AlertDialog
        trigger={
          <Tooltip content="Revoke key">
            <Button variant="ghost" size="sm" square muted aria-label={`Revoke ${key.name} key`} icon={<Trash data-slot="icon" weight="bold" />} />
          </Tooltip>
        }
        title={`Revoke the ${key.name} key?`}
        description={`Apps that use ${key.hint} stop working at once. Create a new key before you revoke this one.`}
        actionLabel="Revoke key"
      />
    </div>
  );
}
```

Vue · `revoke-api-key.vue`

```vue
<script setup lang="ts">
import { PhTrash } from "@phosphor-icons/vue";
import { AlertDialog } from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";

const key = { name: "Production server", hint: "nw_live_…4f2a", lastUsed: "Used 3 min ago" };
</script>

<!-- your own trigger: the row's icon button, named by its tooltip -->
<template>
  <div class="flex w-80 items-center gap-3">
    <div class="min-w-0 flex-1">
      <div class="text-sm font-medium text-ink">{{ key.name }}</div>
      <div class="text-xs text-ink-2"><span class="font-mono">{{ key.hint }}</span> · {{ key.lastUsed }}</div>
    </div>
    <AlertDialog
      :title="`Revoke the ${key.name} key?`"
      :description="`Apps that use ${key.hint} stop working at once. Create a new key before you revoke this one.`"
      actionLabel="Revoke key"
    >
      <template #trigger>
        <Tooltip content="Revoke key">
          <Button variant="ghost" size="sm" square muted :aria-label="`Revoke ${key.name} key`">
            <template #icon><PhTrash data-slot="icon" weight="bold" /></template>
          </Button>
        </Tooltip>
      </template>
    </AlertDialog>
  </div>
</template>
```

Svelte · `revoke-api-key.svelte`

```svelte
<script lang="ts">
  import Trash from "phosphor-svelte/lib/TrashIcon";
  import type { HTMLButtonAttributes } from "svelte/elements";
  import { AlertDialog } from "$lib/components/ui/alert-dialog";
  import { Button } from "$lib/components/ui/button";
  import { Tooltip } from "$lib/components/ui/tooltip";

  const key = { name: "Production server", hint: "nw_live_…4f2a", lastUsed: "Used 3 min ago" };
</script>

<!-- your own trigger: the row's icon button, named by its tooltip; the dialog's trigger props and
     the tooltip's go on one Button (the tooltip finds it by its id) -->
{#snippet trigger(props: () => HTMLButtonAttributes)}
  <Tooltip content="Revoke key" triggerId={props().id ?? undefined}>
    {#snippet trigger(tip)}
      <Button {...tip()} {...props()} variant="ghost" size="sm" square muted aria-label={`Revoke ${key.name} key`}>
        {#snippet icon()}<Trash data-slot="icon" weight="bold" />{/snippet}
      </Button>
    {/snippet}
  </Tooltip>
{/snippet}
<div class="flex w-80 items-center gap-3">
  <div class="min-w-0 flex-1">
    <div class="text-sm font-medium text-ink">{key.name}</div>
    <div class="text-xs text-ink-2"><span class="font-mono">{key.hint}</span> · {key.lastUsed}</div>
  </div>
  <AlertDialog
    {trigger}
    title={`Revoke the ${key.name} key?`}
    description={`Apps that use ${key.hint} stop working at once. Create a new key before you revoke this one.`}
    actionLabel="Revoke key"
  />
</div>
```

### Remove a member

React · `remove-member.tsx`

```tsx
import { useState } from "react";
import { AlertDialog } from "@/components/ui/alert-dialog";
import { Checkbox } from "@/components/ui/checkbox";

/* a body under the description: one choice that changes what the action does */
export default function RemoveMember() {
  const [reassign, setReassign] = useState(true);
  return (
    <AlertDialog
      triggerLabel="Remove member"
      title="Remove Daniel Okafor from Northwind?"
      description="Daniel loses access to every project at once. Their deploys and comments stay."
      actionLabel="Remove member"
    >
      <Checkbox
        label="Reassign Daniel's 12 open reviews to me"
        checked={reassign}
        onCheckedChange={(checked) => setReassign(checked === true)}
      />
    </AlertDialog>
  );
}
```

Vue · `remove-member.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { AlertDialog } from "@/components/ui/alert-dialog";
import { Checkbox } from "@/components/ui/checkbox";

const reassign = ref(true);
</script>

<!-- a body under the description: one choice that changes what the action does -->
<template>
  <AlertDialog
    triggerLabel="Remove member"
    title="Remove Daniel Okafor from Northwind?"
    description="Daniel loses access to every project at once. Their deploys and comments stay."
    actionLabel="Remove member"
  >
    <Checkbox v-model:checked="reassign" label="Reassign Daniel's 12 open reviews to me" />
  </AlertDialog>
</template>
```

Svelte · `remove-member.svelte`

```svelte
<script lang="ts">
  import { AlertDialog } from "$lib/components/ui/alert-dialog";
  import { Checkbox } from "$lib/components/ui/checkbox";

  let reassign = $state(true);
</script>

<!-- a body under the description: one choice that changes what the action does -->
<AlertDialog
  triggerLabel="Remove member"
  title="Remove Daniel Okafor from Northwind?"
  description="Daniel loses access to every project at once. Their deploys and comments stay."
  actionLabel="Remove member"
>
  <Checkbox bind:checked={reassign} label="Reassign Daniel's 12 open reviews to me" />
</AlertDialog>
```

## Recipe

`alert-dialog.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* alert-dialog — the Dialog with role="alertdialog": no close button, Cancel and the action in
 * the footer. Everything that shows (window, backdrop, motion, stacking, the phone layout where
 * the two actions share the row) is the dialog recipe's, through the real Dialog. What is left
 * here is the hook around it; the two Buttons are marked data-alert-dialog="cancel|action" and
 * take nothing of their own. */

export const alertDialog = tv({
  slots: {
    base: "contents",
    cancel: "",
    action: "",
  },
});

export type AlertDialogRecipeProps = VariantProps<typeof alertDialog>;
```
