Dialog
Overlay & FeedbackModal dialog with backdrop and close button.
Preview
Usage
example.jsx
import { Dialog, DialogHeader, DialogTitle, DialogDescription, DialogFooter, DialogClose } from "@/components/ui/dialog";
export default function Example() {
return <Dialog />;
}Source Code
Copy this file into components/ui/dialog.jsx in your project.
dialog.jsx
"use client";
import { forwardRef, useEffect } from "react";
import { cn } from "@/lib/utils";
const Dialog = forwardRef(({ className, open, onClose, children, ...props }, ref) => {
useEffect(() => {
if (open) document.body.style.overflow = "hidden";
return () => { document.body.style.overflow = ""; };
}, [open]);
useEffect(() => {
const handler = (e) => { if (e.key === "Escape") onClose?.(); };
if (open) document.addEventListener("keydown", handler);
return () => document.removeEventListener("keydown", handler);
}, [open, onClose]);
if (!open) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center">
<div className="fixed inset-0 bg-black/50 backdrop-blur-sm" onClick={onClose} />
<div ref={ref} className={cn("relative z-50 w-full max-w-lg rounded-lg border bg-background p-6 shadow-lg animate-in fade-in-0 zoom-in-95", className)} {...props}>
{children}
</div>
</div>
);
});
Dialog.displayName = "Dialog";
const DialogHeader = forwardRef(({ className, ...props }, ref) => (
<div ref={ref} className={cn("flex flex-col space-y-1.5 text-center sm:text-left", className)} {...props} />
));
DialogHeader.displayName = "DialogHeader";
const DialogTitle = forwardRef(({ className, ...props }, ref) => (
<h2 ref={ref} className={cn("text-lg font-semibold leading-none tracking-tight", className)} {...props} />
));
DialogTitle.displayName = "DialogTitle";
const DialogDescription = forwardRef(({ className, ...props }, ref) => (
<p ref={ref} className={cn("text-sm text-muted-foreground", className)} {...props} />
));
DialogDescription.displayName = "DialogDescription";
const DialogFooter = forwardRef(({ className, ...props }, ref) => (
<div ref={ref} className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)} {...props} />
));
DialogFooter.displayName = "DialogFooter";
const DialogClose = forwardRef(({ className, onClose, ...props }, ref) => (
<button ref={ref} onClick={onClose} className={cn("absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", className)} {...props}>
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /></svg>
</button>
));
DialogClose.displayName = "DialogClose";
export { Dialog, DialogHeader, DialogTitle, DialogDescription, DialogFooter, DialogClose };
Quick Install
Make sure you have the cn() utility set up. It requires clsx and tailwind-merge.
npm install clsx tailwind-merge