import { useEffect, useRef } from "react"; import { cn } from "../utils/cn.ts"; interface DialogProps { open: boolean; onClose: () => void; title: string; children: React.ReactNode; className?: string; maxWidth?: "sm" | "md" | "lg" | "xl" | "2xl"; } export function Dialog({ open, onClose, title, children, className, maxWidth = "md", }: DialogProps) { const dialogRef = useRef(null); const maxWidthClasses = { sm: "max-w-sm", md: "max-w-md", lg: "max-w-lg", xl: "max-w-xl", "2xl": "max-w-2xl", }; useEffect(() => { const dialog = dialogRef.current; if (!dialog) return; if (open) { dialog.showModal(); } else { dialog.close(); } }, [open]); useEffect(() => { const dialog = dialogRef.current; if (!dialog) return; const handleCancel = (e: Event) => { e.preventDefault(); onClose(); }; dialog.addEventListener("cancel", handleCancel); return () => dialog.removeEventListener("cancel", handleCancel); }, [onClose]); const handleBackdropClick = (e: React.MouseEvent) => { const dialog = dialogRef.current; if (!dialog) return; const rect = dialog.getBoundingClientRect(); const isInDialog = rect.top <= e.clientY && e.clientY <= rect.top + rect.height && rect.left <= e.clientX && e.clientX <= rect.left + rect.width; if (!isInDialog) { onClose(); } }; return (

{title}

{children}
); }