Get Started
Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Collapsible
- Combobox
- Command
- Confirm Dialog
- Container
- Context Menu
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input Number
- Input OTP
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Tailwind Indicator
- Textarea
- Toggle
- Toggle Group
- Tooltip
"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.
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.
| Option | Type | Default |
|---|---|---|
title | string | - |
description | string | ReactNode | - |
type | "default" | "icon" | "media" | "default" |
variant | "default" | "success" | "warning" | "danger" | "default" |
icon | ElementType | variant icon |
size | "sm" | "default" | "default" |
confirmText | string | "Yes, Confirm" |
cancelButton | boolean | true |
cancelText | string | "No, Cancel" |
onConfirm | () => void | - |
onCancel | () => void | - |
customActions | ({ confirm, cancel }) => ReactNode | - |