'use client'; import { ReactNode, useRef, useEffect } from 'react'; interface ModalProps { isOpen: boolean; onClose: () => void; title: string; children: ReactNode; icon?: ReactNode; maxWidth?: string; } export default function Modal({ isOpen, onClose, title, children, icon, maxWidth = 'max-w-md' }: ModalProps) { const modalRef = useRef(null); // Handle click outside to close modal useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(event.target as Node)) { onClose(); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isOpen, onClose]); // Handle ESC key to close modal useEffect(() => { const handleEscKey = (event: KeyboardEvent) => { if (event.key === 'Escape') { onClose(); } }; if (isOpen) { window.addEventListener('keydown', handleEscKey); } return () => { window.removeEventListener('keydown', handleEscKey); }; }, [isOpen, onClose]); if (!isOpen) return null; return (
{/* Background elements */}
{icon && (
{icon}
)}

{title}

{children}
); }