import React, { useState, useEffect } from 'react' import { X } from 'lucide-react' interface ModalProps { isOpen: boolean onClose: () => void title: React.ReactNode children: React.ReactNode maxWidth?: | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | '5xl' | '6xl' | '7xl' } const Modal: React.FC = ({ isOpen, onClose, title, children, maxWidth = '4xl' }) => { // State to control if the modal is in the DOM const [isRendered, setIsRendered] = useState(isOpen) // State to control the animation classes const [isAnimating, setIsAnimating] = useState(false) useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose() } } if (isOpen) { setIsRendered(true) document.body.style.overflow = 'hidden' document.addEventListener('keydown', handleEscape) const animationTimeout = setTimeout(() => setIsAnimating(true), 20) return () => clearTimeout(animationTimeout) } else { setIsAnimating(false) const unmountTimeout = setTimeout(() => { setIsRendered(false) document.body.style.overflow = 'unset' document.removeEventListener('keydown', handleEscape) }, 300) return () => clearTimeout(unmountTimeout) } }, [isOpen, onClose]) // Unmount the component completely when not rendered if (!isRendered) return null const maxWidthClasses = { sm: 'max-w-sm', md: 'max-w-md', lg: 'max-w-lg', xl: 'max-w-xl', '2xl': 'max-w-2xl', '3xl': 'max-w-3xl', '4xl': 'max-w-4xl', '5xl': 'max-w-5xl', '6xl': 'max-w-6xl', '7xl': 'max-w-7xl' } return (
{/* Backdrop */}
{/* Modal */}
e.stopPropagation()} > {/* Header */}

{title}

{/* Content */}
{children}
) } export default Modal