Skip to Content
NarakUI

Confirm Dialog

A small dialog that asks people to confirm an action before it runs.

Basic usage

ConfirmDialog is always controlled: pass open and onOpenChange, a title, and a description. It renders a structured Dialog with a close button, Cancel, and a primary confirm button. Cancel, the close button, Escape, and a click outside close it; confirming only calls onConfirm, so close the dialog there yourself.

Name the action in confirmText. Without it the buttons read Confirm and Cancel.

Destructive confirmation

Use variant="destructive" for actions that cannot be undone; the confirm button turns red. While the action runs, pass isLoading: the confirm button shows a spinner and Cancel and the close button are disabled. Keep the dialog open until the work settles, and ignore onOpenChange meanwhile so Escape can't close it.

Acknowledgement

children render in the body between dividers, for acknowledgements or form fields. Pair them with confirmDisabled until they are complete. titleIcon adds an icon before the title. Keep interactive controls out of description, which screen readers announce as plain text.

Installation

pnpm dlx shadcn@latest add @narakui/confirm-dialog

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/confirm-dialog.json.

Usage

import { ConfirmDialog } from '@/components/ui/confirm-dialog';

<ConfirmDialog
  open={open}
  onOpenChange={setOpen}
  title="Delete this prompt?"
  description="Its answer history will be permanently deleted."
  confirmText="Delete prompt"
  variant="destructive"
  onConfirm={handleDelete}
/>

When the body holds a form whose submit handler does the work, pass that form's id as form: the confirm button becomes its submit button and onConfirm is not called on click. To give Cancel the initial focus, pass cancelButtonRef and focus it from onOpenAutoFocus.

Properties

PropTypeDefaultDescription
open (Required)boolean—Whether the dialog is open
onOpenChange (Required)(open: boolean) => void—Called when Cancel, the close button, Escape, or a click outside closes the dialog
title (Required)ReactNode—Dialog title
description (Required)ReactNode—Explains what confirming does. Announced as the dialog's description
onConfirm (Required)() => void—Called when the confirm button is clicked. Does not close the dialog
confirmTextstring'Confirm'Label of the confirm button
cancelTextstring'Cancel'Label of the cancel button
closeTextstring'Close dialog'Accessible label of the close button
variant'default' | 'destructive''default'Use destructive for actions that cannot be undone
isLoadingbooleanfalseShows a spinner on the confirm button and disables every button
confirmDisabledbooleanfalseDisables the confirm button
titleIconReactNode—Icon before the title
childrenReactNode—Body content between dividers, such as acknowledgements or form fields
formstring—Id of a form. The confirm button submits it instead of calling onConfirm
confirmModeButtonMode—Overrides the confirm button style. Defaults to primary, or destructive with variant="destructive"
cancelModeButtonMode'secondary'Style of the cancel button
confirmClassNamestring—Classes for the confirm button
cancelButtonRefRef<HTMLButtonElement>—Ref to the cancel button, for example to focus it when the dialog opens
onOpenAutoFocus(event: Event) => void—Called when the dialog opens. Nothing is focused automatically, so focus the element that should start focused here
onCloseAutoFocus(event: Event) => void—Called when focus returns after the dialog closes
size'md' | 'lg' | 'xl''md'Panel width: 440, 640, or 800 pixels. lg and xl also set a minimum height
contentClassNamestring—Classes for the dialog panel
overlayZIndex'dialog' | 'drawer''dialog'Stacking layer. Use drawer to show the dialog above a drawer