import React, { Fragment, useEffect } from 'react'; interface ModalProps { isOpen: boolean; onClose: () => void; title?: string; children: React.ReactNode; showCloseButton?: boolean; size?: 'sm' | 'md' | 'lg' | 'xl'; closeOnOverlayClick?: boolean; } export const Modal: React.FC = ({ isOpen, onClose, title, children, showCloseButton = true, size = 'md', closeOnOverlayClick = true, }) => { // Close on ESC key press useEffect(() => { const handleEscKey = (e: KeyboardEvent) => { if (e.key === 'Escape' && isOpen) { onClose(); } }; if (isOpen) { document.addEventListener('keydown', handleEscKey); // Prevent body scrolling when modal is open document.body.style.overflow = 'hidden'; } return () => { document.removeEventListener('keydown', handleEscKey); document.body.style.overflow = 'auto'; }; }, [isOpen, onClose]); // Handle overlay click const handleOverlayClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget && closeOnOverlayClick) { onClose(); } }; if (!isOpen) return null; // Size mapping const sizeClasses = { sm: 'max-w-md', md: 'max-w-lg', lg: 'max-w-2xl', xl: 'max-w-4xl', }; return ( {/* Modal backdrop */}
{/* Modal panel */}
e.stopPropagation()} > {/* Modal header */} {(title || showCloseButton) && (
{title && ( )} {showCloseButton && ( )}
)} {/* Modal content */}
{children}
); };