---
title: Alert Dialog
description: A modal dialog that interrupts the user with important content and expects a response.
---
## Installation
CLI
Manual
```bash
npx lightswind@latest add alert-dialog
```
Copy and paste the following code into your project.
```tsx
import * as React from "react";
import { cn } from "@/components/lib/utils";
import { buttonVariants } from "./button";
import { motion, AnimatePresence } from "framer-motion"; // Import motion and AnimatePresence
interface AlertDialogContextType {
open: boolean;
setOpen: React.Dispatch>;
}
const AlertDialogContext = React.createContext(undefined);
interface AlertDialogProps {
children: React.ReactNode;
defaultOpen?: boolean;
open?: boolean;
onOpenChange?: (open: boolean) => void;
}
const AlertDialog: React.FC = ({
children,
defaultOpen = false,
open: controlledOpen,
onOpenChange,
}) => {
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen);
const isControlled = controlledOpen !== undefined;
const open = isControlled ? controlledOpen : uncontrolledOpen;
const setOpen = React.useCallback((value: boolean | ((prev: boolean) => boolean)) => {
if (!isControlled) {
setUncontrolledOpen(value);
}
if (onOpenChange) {
const newValue = typeof value === "function" ? value(open) : value;
onOpenChange(newValue);
}
}, [isControlled, onOpenChange, open]);
return (
{children}
);
};
interface AlertDialogTriggerProps {
children: React.ReactNode;
asChild?: boolean;
}
const AlertDialogTrigger = React.forwardRef>(
({ children, asChild = false, ...props }, ref) => {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error("AlertDialogTrigger must be used within an AlertDialog");
}
const { setOpen } = context;
const handleClick = (e: React.MouseEvent) => {
setOpen(true);
// Call the original onClick if it exists
if (props.onClick) {
props.onClick(e);
}
};
// Remove onClick from props to avoid duplicate handlers
const { onClick, ...otherProps } = props;
if (asChild) {
return (
<>
{React.Children.map(children, child => {
if (React.isValidElement(child)) {
const element = child as React.ReactElement;
return React.cloneElement(element, {
...element.props,
ref,
onClick: handleClick
});
}
return child;
})}
>
);
}
return (
);
}
);
AlertDialogTrigger.displayName = "AlertDialogTrigger";
const AlertDialogPortal: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return <>{children}>;
};
const AlertDialogOverlay = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes
>(({ className, ...props }, ref) => {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error("AlertDialogOverlay must be used within an AlertDialog");
}
const { open } = context;
// It's less likely for AlertDialogOverlay to have conflicting props,
// but if the error points here, apply the same filtering.
// For now, we'll assume the primary issue is in AlertDialogContent.
return (
{open && (
{
if (key === 'onDrag' || key === 'onAnimationStart' || key === 'onTransitionEnd') {
return acc;
}
acc[key] = (props as any)[key];
return acc;
}, {})}
/>
)}
);
});
AlertDialogOverlay.displayName = "AlertDialogOverlay";
const AlertDialogContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes
>(({ className, children, ...props }, ref) => {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error("AlertDialogContent must be used within an AlertDialog");
}
const { open, setOpen } = context;
// Add click outside functionality
const contentRef = React.useRef(null);
React.useEffect(() => {
if (!open) return;
const handleClickOutside = (event: MouseEvent) => {
if (contentRef.current && !contentRef.current.contains(event.target as Node)) {
setOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [open, setOpen]);
// Use AnimatePresence and motion.div for animation
return (
{open && (
{
// Standard ref handling
if (typeof ref === "function") {
ref(node);
} else if (ref) {
(ref as React.MutableRefObject).current = node;
}
// Content ref for click outside
(contentRef as React.MutableRefObject).current = node;
}}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg sm:rounded-lg",
className
)}
initial={{ opacity: 0, scale: 0.3, x: "-50%", y: "-50%" }}
animate={{ opacity: 1, scale: 1, x: "-50%", y: "-50%" }}
exit={{ opacity: 0, scale: 0.3, x: "-50%", y: "-50%" }}
transition={{
type: "spring",
damping: 25,
stiffness: 400,
opacity: { duration: 0.2 }
}}
// Filter out properties that conflict with Framer Motion's types
// This is the crucial part for resolving the type error.
{...Object.keys(props).reduce((acc: { [key: string]: any }, key) => {
// Add any other conflicting HTML attributes you might discover here.
// 'onDrag' is the most common one.
if (key === 'onDrag' || key === 'onAnimationStart' || key === 'onTransitionEnd') {
return acc; // Omit this property
}
acc[key] = (props as any)[key]; // Keep other properties
return acc;
}, {})}
>
{children}
)}
);
});
AlertDialogContent.displayName = "AlertDialogContent";
const AlertDialogHeader = ({
className,
...props
}: React.HTMLAttributes) => (
);
AlertDialogHeader.displayName = "AlertDialogHeader";
const AlertDialogFooter = ({
className,
...props
}: React.HTMLAttributes) => (
);
AlertDialogFooter.displayName = "AlertDialogFooter";
const AlertDialogTitle = React.forwardRef<
HTMLHeadingElement,
React.HTMLAttributes
>(({ className, ...props }, ref) => (
));
AlertDialogTitle.displayName = "AlertDialogTitle";
const AlertDialogDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes
>(({ className, ...props }, ref) => (
));
AlertDialogDescription.displayName = "AlertDialogDescription";
interface AlertDialogActionProps extends React.ButtonHTMLAttributes { }
const AlertDialogAction = React.forwardRef<
HTMLButtonElement,
AlertDialogActionProps
>(({ className, ...props }, ref) => {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error("AlertDialogAction must be used within an AlertDialog");
}
const { setOpen } = context;
const handleClick = (e: React.MouseEvent) => {
setOpen(false);
// Call the original onClick if it exists
if (props.onClick) {
props.onClick(e);
}
};
// Remove onClick from props to avoid duplicate handlers
const { onClick, ...otherProps } = props;
return (
);
});
AlertDialogAction.displayName = "AlertDialogAction";
interface AlertDialogCancelProps extends React.ButtonHTMLAttributes { }
const AlertDialogCancel = React.forwardRef<
HTMLButtonElement,
AlertDialogCancelProps
>(({ className, ...props }, ref) => {
const context = React.useContext(AlertDialogContext);
if (!context) {
throw new Error("AlertDialogCancel must be used within an AlertDialog");
}
const { setOpen } = context;
const handleClick = (e: React.MouseEvent) => {
setOpen(false);
// Call the original onClick if it exists
if (props.onClick) {
props.onClick(e);
}
};
// Remove onClick from props to avoid duplicate handlers
const { onClick, ...otherProps } = props;
return (
);
});
AlertDialogCancel.displayName = "AlertDialogCancel";
export {
AlertDialog,
AlertDialogPortal,
AlertDialogOverlay,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogFooter,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogAction,
AlertDialogCancel,
};
```
## Usage
```tsx
import { AlertDialog, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogAction, AlertDialogCancel } from "@/components/lightswind/alert-dialog"
```
```tsx
import {
AlertDialog,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogAction,
AlertDialogCancel,
} from "@/components/lightswind/alert-dialog";
import { Button } from "@/components/lightswind/button";
export function AlertDialogDemo() {
return (
Are you absolutely sure?
This action cannot be undone. This will permanently delete your
account and remove your data from our servers.
Cancel
Continue
);
}
```