# Alert Dialog

> Confirms an action that cannot be undone.

An alert dialog asks people to confirm an action that cannot be undone, such as deleting a release, before it runs.

## When to use

- Use it only for actions that cannot be undone: deleting, cancelling a subscription, withdrawing a contract.
- Use a [Dialog](https://design.sosialoka.tech/components/dialog/) for a short form, and an undo [Toast](https://design.sosialoka.tech/components/toast/) when the action can be reversed.
- Write the title as a question ("Hapus rilis?") and name what goes away in the description.
- Label the action with its verb ("Hapus"), never "Ya" or "OK".

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/alert-dialog/_

### Do and don't

- **Do:** Name the action in the title and on the button, and focus Cancel first.
- **Don't:** Don't ask a vague question with a lone OK button.

## Anatomy

1. Warning icon
2. Title that names the action
3. Cancel, focused first
4. Destructive action

A title, a description, and a footer band with **Batal** and the action. The window sits on `popover` with a `border`, `radius-xl` and `shadow-lg`, over the `overlay` token. The focus starts on **Batal**, and the destructive action sits last, on the right.

## Variants

There is one style. Use the `destructive` button for the action; keep the default button only for actions that don't delete, such as signing out of every device.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `open` / `onOpenChange` | `v-model:open` | `showShadDialog` | boolean | `false` | Opens it from code. |
| `AlertDialogAction variant` | `AlertDialogAction variant` | `ShadButton.destructive` | text | `default` | The action button. |
| `AlertDialogCancel autoFocus` | `AlertDialogCancel` (focused first) | `ShadButton.outline(autofocus: true)` | boolean | none | Starts the focus on Cancel. |
| `AlertDialogTrigger render` | `AlertDialogTrigger as-child` | the button that calls `showShadDialog` | element | none | The button that opens it. |

## Code

**React**
```tsx
import { useState } from "react"
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

export default function AlertDialogDemo() {
  const [deleted, setDeleted] = useState(false)
  if (deleted) return <p className="text-sm text-muted-foreground">"Senja di Kota" dihapus.</p>
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="outline" />}>Hapus rilis</AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Hapus rilis?</AlertDialogTitle>
          <AlertDialogDescription>"Senja di Kota" dan semua datanya akan dihapus. Kamu tidak bisa mengembalikannya.</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel autoFocus>Batal</AlertDialogCancel>
          <AlertDialogAction variant="destructive" onClick={() => setDeleted(true)}>
            Hapus
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { ref } from "vue"
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

const deleted = ref(false)
</script>

<template>
  <p v-if="deleted" class="text-sm text-muted-foreground">"Senja di Kota" dihapus.</p>
  <AlertDialog v-else>
    <AlertDialogTrigger as-child>
      <Button variant="outline">Hapus rilis</Button>
    </AlertDialogTrigger>
    <AlertDialogContent>
      <AlertDialogHeader>
        <AlertDialogTitle>Hapus rilis?</AlertDialogTitle>
        <AlertDialogDescription>"Senja di Kota" dan semua datanya akan dihapus. Kamu tidak bisa mengembalikannya.</AlertDialogDescription>
      </AlertDialogHeader>
      <AlertDialogFooter>
        <AlertDialogCancel>Batal</AlertDialogCancel>
        <AlertDialogAction variant="destructive" @click="deleted = true">Hapus</AlertDialogAction>
      </AlertDialogFooter>
    </AlertDialogContent>
  </AlertDialog>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class AlertDialogDemo extends StatelessWidget {
  const AlertDialogDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ShadButton.outline(
      child: const Text('Hapus rilis'),
      onPressed: () => showShadDialog<bool>(
        context: context,
        builder: (context) => ShadDialog.alert(
          title: const Text('Hapus rilis?'),
          description: const Text('"Senja di Kota" dan semua datanya akan dihapus. Kamu tidak bisa mengembalikannya.'),
          actions: [
            // Cancel takes the focus first; the destructive action sits last.
            ShadButton.outline(autofocus: true, child: const Text('Batal'), onPressed: () => Navigator.of(context).pop(false)),
            ShadButton.destructive(child: const Text('Hapus'), onPressed: () => Navigator.of(context).pop(true)),
          ],
        ),
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/alert-dialog.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/alert-dialog.json
```

## Accessibility

- It has `role="alertdialog"`, named by its title and described by its description.
- The focus starts on Cancel, so pressing Enter by habit does not delete anything.
- Escape cancels; a click outside does not close it.
- The focus stays inside until it closes, then goes back to the trigger.
