diff --git a/src/components/ui/Modal.tsx b/src/components/ui/Modal.tsx new file mode 100644 index 0000000..f0ae926 --- /dev/null +++ b/src/components/ui/Modal.tsx @@ -0,0 +1,77 @@ +'use client' + +import { cn } from "@/lib/utils" +import { useEffect, useRef, useCallback } 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 closeButtonRef = useRef(null) + + const handleKeyDown = useCallback( + (e: KeyboardEvent) => { + if (e.key === "Escape") { + onClose() + } + }, + [onClose] + ) + + useEffect(() => { + if (open) { + document.addEventListener("keydown", handleKeyDown) + // Focus trap: focus close button when modal opens + setTimeout(() => closeButtonRef.current?.focus(), 100) + document.body.style.overflow = "hidden" + } else { + document.body.style.overflow = "" + } + return () => { + document.removeEventListener("keydown", handleKeyDown) + document.body.style.overflow = "" + } + }, [open, handleKeyDown]) + + if (!open) return null + + return ( +
{ + if (e.target === overlayRef.current) onClose() + }} + role="dialog" + aria-modal="true" + aria-label={title || "Modal"} + > +
+ {title && ( +

{title}

+ )} + + {children} +
+
+ ) +} \ No newline at end of file