Pattern

Confirmation Dialog

A pattern for confirming destructive or important actions before they are executed.

Preview

Installation

pnpm dlx shadcn@latest add "https://ui-registry.com/r/confirmation-dialog.json"

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

OptionTypeDefaultDescription
titlestring-The dialog title
descriptionstring-The dialog description
confirmTextstring"Confirm"Text for the confirm button
cancelTextstring"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 true or false
  • Accessible: Full screen reader support with proper ARIA attributes

Keyboard Interactions

KeyDescription
EscapeCloses dialog without confirming
EnterConfirms the action
TabMoves 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")