diff --git a/src/components/ui/Modal.tsx b/src/components/ui/Modal.tsx new file mode 100644 index 0000000..215b7b9 --- /dev/null +++ b/src/components/ui/Modal.tsx @@ -0,0 +1,74 @@ +'use client' + +import React, { useEffect, useRef, useCallback } from 'react' +import { X } from 'lucide-react' +import { cn } from "@/lib/utils" + +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 contentRef = useRef(null) + + const handleEsc = useCallback( + (e: KeyboardEvent) => { + if (e.key === 'Escape') onClose() + }, + [onClose] + ) + + useEffect(() => { + if (open) { + document.addEventListener('keydown', handleEsc) + document.body.style.overflow = 'hidden' + // Focus trap: focus content on open + contentRef.current?.focus() + } + return () => { + document.removeEventListener('keydown', handleEsc) + document.body.style.overflow = '' + } + }, [open, handleEsc]) + + if (!open) return null + + return ( +
{ + if (e.target === overlayRef.current) onClose() + }} + role="dialog" + aria-modal="true" + aria-label={title || 'Modal dialog'} + > +
+ + {title && ( +

{title}

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