diff --git a/src/components/ui/Modal.tsx b/src/components/ui/Modal.tsx new file mode 100644 index 0000000..85d451c --- /dev/null +++ b/src/components/ui/Modal.tsx @@ -0,0 +1,77 @@ +'use client' + +import { cn } from "@/lib/utils" +import { X } from 'lucide-react' +import React, { useEffect, useRef } from "react" + +interface ModalProps { + isOpen: boolean + onClose: () => void + children: React.ReactNode + title?: string + className?: string +} + +export default function Modal({ + isOpen, + onClose, + children, + title, + className +}: ModalProps) { + const overlayRef = useRef(null) + + useEffect(() => { + if (isOpen) { + document.body.style.overflow = "hidden" + } else { + document.body.style.overflow = "" + } + return () => { + document.body.style.overflow = "" + } + }, [isOpen]) + + useEffect(() => { + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === "Escape" && isOpen) { + onClose() + } + } + window.addEventListener("keydown", handleKeyDown) + return () => window.removeEventListener("keydown", handleKeyDown) + }, [isOpen, onClose]) + + if (!isOpen) return null + + return ( +
{ + if (e.target === overlayRef.current) onClose() + }} + > +
+
+ {title && ( +

{title}

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