Confirmation Dialog

A dialog for confirming user actions with customizable variants for different use cases.

Confirmation Dialog

A specialized dialog component for confirming user actions. Use this component when you need to confirm destructive actions, important changes, or any action that requires explicit user consent.

Installation

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

Usage

import {
  ConfirmationDialog,
  useConfirmationDialog,
} from '@/components/ui/confirmation-dialog';
const [open, setOpen] = useState(false);

<ConfirmationDialog open={open} onOpenChange={setOpen} title="Delete item?" description="This action cannot be undone." onConfirm={() => handleDelete()} onCancel={() => setOpen(false)} variant="destructive" />

Examples

Basic Confirmation

<ConfirmationDialog
  open={open}
  onOpenChange={setOpen}
  title="Confirm action"
  description="Are you sure you want to proceed?"
  onConfirm={handleConfirm}
/>

Destructive Action

<ConfirmationDialog
  open={open}
  onOpenChange={setOpen}
  title="Delete account"
  description="This will permanently delete your account and all associated data. This action cannot be undone."
  confirmText="Delete"
  cancelText="Keep account"
  variant="destructive"
  onConfirm={handleDeleteAccount}
/>

Warning Variant

<ConfirmationDialog
  open={open}
  onOpenChange={setOpen}
  title="Unsaved changes"
  description="You have unsaved changes. Are you sure you want to leave?"
  confirmText="Leave"
  cancelText="Stay"
  variant="warning"
  onConfirm={handleLeave}
/>

Using the Hook

function DeleteButton() {
  const { confirm, dialogProps } = useConfirmationDialog({
    title: 'Delete item?',
    description: 'This action cannot be undone.',
    variant: 'destructive',
    confirmText: 'Delete',
  });

const handleDelete = async () => { const confirmed = await confirm(); if (confirmed) { // Perform deletion await deleteItem(); } };

return ( <> <Button variant="destructive" onClick={handleDelete}> Delete </Button> <ConfirmationDialog {...dialogProps} /> </> ); }

With Loading State

function SaveButton() {
  const { confirm, dialogProps, setLoading } = useConfirmationDialog({
    title: 'Save changes?',
    description: 'This will update the record.',
  });

const handleSave = async () => { const confirmed = await confirm(); if (confirmed) { setLoading(true); await saveChanges(); setLoading(false); } };

return ( <> <Button onClick={handleSave}>Save</Button> <ConfirmationDialog {...dialogProps} /> </> ); }

API Reference

ConfirmationDialog

PropTypeDefault
openboolean-
onOpenChange(open: boolean) => void-
onConfirm() => void-
onCancel() => void-
titlestring"Are you sure?"
descriptionstring"This action cannot be undone."
confirmTextstring"Confirm"
cancelTextstring"Cancel"
variant"default" \"destructive" \"warning""default"
loadingbooleanfalse
classNamestring-

useConfirmationDialog

Returns an object with:

PropertyTypeDescription
openbooleanCurrent open state
loadingbooleanCurrent loading state
setLoading(loading: boolean) => voidSet loading state
confirm() => PromiseOpen dialog and wait for response
dialogPropsConfirmationDialogPropsProps to spread on the dialog