'use client'; import React, { useEffect, useRef } from 'react'; import { cn } from '@/utils/helpers'; interface ModalProps { isOpen: boolean; onClose: () => void; title?: string; children: React.ReactNode; size?: 'sm' | 'md' | 'lg' | 'xl'; showCloseButton?: boolean; } export const Modal: React.FC = ({ isOpen, onClose, title, children, size = 'md', showCloseButton = true, }) => { const modalRef = useRef(null); useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } }; if (isOpen) { document.addEventListener('keydown', handleEscape); document.body.style.overflow = 'hidden'; } return () => { document.removeEventListener('keydown', handleEscape); document.body.style.overflow = 'unset'; }; }, [isOpen, onClose]); const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }; if (!isOpen) return null; const sizes = { sm: 'max-w-md', md: 'max-w-lg', lg: 'max-w-2xl', xl: 'max-w-4xl', }; return (
{(title || showCloseButton) && (
{title && ( )} {showCloseButton && ( )}
)}
{children}
); };