diff --git a/src/components/ui/Modal.tsx b/src/components/ui/Modal.tsx new file mode 100644 index 0000000..62eb37b --- /dev/null +++ b/src/components/ui/Modal.tsx @@ -0,0 +1,81 @@ +'use client' + +import { cn } from "@/lib/utils" +import { useEffect, useRef, useState } from "react" +import { X } from 'lucide-react' + +interface ModalProps { + open: boolean + onClose: () => void + title?: string + children: React.ReactNode + className?: string +} + +export default function Modal({ open, onClose, title, children, className }: ModalProps) { + const overlayRef = useRef(null) + const [visible, setVisible] = useState(false) + + useEffect(() => { + if (open) { + setVisible(true) + document.body.style.overflow = "hidden" + } else { + document.body.style.overflow = "" + // delay removal for animation + const timer = setTimeout(() => setVisible(false), 200) + return () => clearTimeout(timer) + } + return () => { + document.body.style.overflow = "" + } + }, [open]) + + useEffect(() => { + const handleEscape = (e: KeyboardEvent) => { + if (e.key === "Escape") onClose() + } + document.addEventListener("keydown", handleEscape) + return () => document.removeEventListener("keydown", handleEscape) + }, [onClose]) + + if (!visible) return null + + return ( +
{ + if (e.target === overlayRef.current) onClose() + }} + role="dialog" + aria-modal="true" + aria-label={title || "Dialog"} + > +
+
+ {title && ( +

{title}

+ )} + +
+ {children} +
+
+ ) +}