Confirm Dialog

A dialog that asks the user to confirm or cancel an action, opened from anywhere with a function call.

"use client"

import { Button } from "@/components/ui/button"
import { useConfirm } from "@/components/ui/confirm-dialog"

Installation

pnpm slabui add confirm-dialog

Usage

Render ConfirmDialog once, near the root of your app. It stays closed until a confirmation is requested.

app/layout.tsx
import { ConfirmDialog } from "@/components/ui/confirm-dialog"
 
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <main>{children}</main>
        <ConfirmDialog />
      </body>
    </html>
  )
}

Then call confirm from any client component.

import { useConfirm } from "@/components/ui/confirm-dialog"
 
function DeleteButton() {
  const { confirm } = useConfirm()
 
  return (
    <Button
      onClick={() =>
        confirm({
          title: "Delete this project?",
          description: "This action cannot be undone.",
          variant: "danger",
          onConfirm: () => deleteProject(),
        })
      }
    >
      Delete
    </Button>
  )
}

The dialog closes after the user confirms or cancels. To close it from code, call confirm.dismiss().

Variants

Use variant to set the intent of the confirmation. With type="media" the dialog shows the icon of the variant in a tinted box; with type="icon" the icon goes next to the title.

"use client"

import { Button } from "@/components/ui/button"
import { useConfirm } from "@/components/ui/confirm-dialog"

Danger

Combine variant="danger" with your own icon, the sm size and custom button texts for destructive actions.

"use client"

import { Trash2Icon } from "lucide-react"

import { Button } from "@/components/ui/button"

Custom actions

Use customActions to render your own buttons. It receives the confirm and cancel handlers, which also close the dialog.

confirm({
  title: "Unsaved changes",
  description: "Save the changes before leaving?",
  customActions: ({ confirm, cancel }) => (
    <>
      <Button variant="ghost" onClick={cancel}>
        Discard
      </Button>
      <Button onClick={confirm}>Save and leave</Button>
    </>
  ),
})

API Reference

confirm

The options accepted by confirm.

OptionTypeDefault
titlestring-
descriptionstring | ReactNode-
type"default" | "icon" | "media""default"
variant"default" | "success" | "warning" | "danger""default"
iconElementTypevariant icon
size"sm" | "default""default"
confirmTextstring"Yes, Confirm"
cancelButtonbooleantrue
cancelTextstring"No, Cancel"
onConfirm() => void-
onCancel() => void-
customActions({ confirm, cancel }) => ReactNode-