Alert Dialog
An alert dialog asks people to confirm an action that cannot be undone, such as deleting a release, before it runs.
When to use
Section titled “When to use”- Use it only for actions that cannot be undone: deleting, cancelling a subscription, withdrawing a contract.
- Use a Dialog for a short form, and an undo 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”.
Do and don’t
Section titled “Do and don’t”Anatomy
Section titled “Anatomy”- 1Warning icon
- 2Title that names the action
- 3Cancel, focused first
- 4Destructive 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
Section titled “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.
| 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. |
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> )}<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>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
Section titled “Install”npx shadcn@latest add https://design.sosialoka.tech/r/react/alert-dialog.jsonnpx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/alert-dialog.jsonAccessibility
Section titled “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.
