Pattern
Confirmation Dialog
A pattern for confirming destructive or important actions before they are executed.
Preview
Installation
Usage
With Hook
import { useConfirmation } from "@/hooks/use-confirmation"
function DeleteButton() {
const confirm = useConfirmation()
async function handleDelete() {
const confirmed = await confirm({
title: "Delete item",
description: "Are you sure you want to delete this item? This action cannot be undone.",
confirmText: "Delete",
cancelText: "Cancel",
variant: "destructive",
})
if (confirmed) {
// Perform delete action
}
}
return <Button onClick={handleDelete}>Delete</Button>
}With Component
import { ConfirmationDialog, ConfirmationTrigger } from "@/components/confirmation-dialog"
<ConfirmationDialog
title="Delete item"
description="Are you sure you want to delete this item?"
onConfirm={handleDelete}
variant="destructive"
>
<ConfirmationTrigger asChild>
<Button variant="destructive">Delete</Button>
</ConfirmationTrigger>
</ConfirmationDialog>API
useConfirmation Hook
const confirm = useConfirmation()
const result = await confirm(options: ConfirmationOptions)ConfirmationOptions
| Option | Type | Default | Description |
|---|---|---|---|
title | string | - | The dialog title |
description | string | - | The dialog description |
confirmText | string | "Confirm" | Text for the confirm button |
cancelText | string | "Cancel" | Text for the cancel button |
variant | "default" | "destructive" | "default" | Visual variant for the confirm button |
Behavior
- Focus management: Focus is trapped within the dialog and restored on close
- Keyboard support: Escape closes without confirming, Enter confirms
- Async/await: The hook returns a Promise that resolves to
trueorfalse - Accessible: Full screen reader support with proper ARIA attributes
Keyboard Interactions
| Key | Description |
|---|---|
Escape | Closes dialog without confirming |
Enter | Confirms the action |
Tab | Moves focus between buttons |
Best Practices
- Use destructive variant for irreversible actions
- Keep the description clear and concise
- Explain the consequences of the action
- Provide clear action labels (e.g., "Delete" instead of "OK")