Alert Dialog
A modal dialog that interrupts the user with important content and expects a response.
Installation
Install Button
if you haven't already.
Install the following dependencies.
pnpm add @radix-ui/react-alert-dialog
Copy and paste the following code into your project.
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog";
import { cn } from "~/utils/tailwind";
import {
dialogContentVariants,
dialogDescriptionVariants,
dialogFooterVariants,
dialogHeaderVariants,
dialogOverlayVariants,
dialogTitleVariants,
} from "~/components/ui/_Dialog";
import { buttonVariants, type ButtonProps } from "~/components/ui/Button";
const AlertDialog = AlertDialogPrimitive.Root;
const AlertDialogTrigger = AlertDialogPrimitive.Trigger;
const AlertDialogPortal = AlertDialogPrimitive.Portal;
function AlertDialogOverlay({
className,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Overlay>) {
return (
<AlertDialogPrimitive.Overlay
className={dialogOverlayVariants({ className })}
{...props}
/>
);
}
function AlertDialogContent({
className,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Content>) {
return (
<AlertDialogPortal>
<AlertDialogOverlay />
<AlertDialogPrimitive.Content
className={dialogContentVariants({ className })}
{...props}
/>
</AlertDialogPortal>
);
}
function AlertDialogHeader({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
className={dialogHeaderVariants({
className: cn("pb-0 xs:pb-0", className),
})}
{...props}
/>
);
}
function AlertDialogFooter({
className,
...props
}: React.ComponentProps<"div">) {
return <div className={dialogFooterVariants({ className })} {...props} />;
}
function AlertDialogTitle({
className,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Title>) {
return (
<AlertDialogPrimitive.Title
className={dialogTitleVariants({ className })}
{...props}
/>
);
}
function AlertDialogDescription({
className,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Description>) {
return (
<AlertDialogPrimitive.Description
className={dialogDescriptionVariants({ className })}
{...props}
/>
);
}
function AlertDialogAction({
className,
variant = "destructive-outline",
size,
subject,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Action> & ButtonProps) {
return (
<AlertDialogPrimitive.Action
className={cn(buttonVariants({ variant, size, subject, className }))}
{...props}
/>
);
}
function AlertDialogCancel({
className,
variant = "outline",
size,
subject,
...props
}: React.ComponentProps<typeof AlertDialogPrimitive.Cancel> & ButtonProps) {
return (
<AlertDialogPrimitive.Cancel
className={cn(buttonVariants({ variant, size, subject, className }))}
{...props}
/>
);
}
export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
};
Update the import paths to match your project setup.
Usage
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "~/components/ui/AlertDialog";
<AlertDialog>
<AlertDialogTrigger>Open</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
Examples
Default
Controlled
Use the controlled props to programmatically close the Alert Dialog. Here, I'm emulating an asynchronous action to delay the closing of the dialog.