'use client' import React, { useEffect, useRef } from 'react' import { createPortal } from 'react-dom' export interface ModalProps { isOpen: boolean onClose: () => void title?: string children: React.ReactNode size?: 'sm' | 'md' | 'lg' | 'xl' closeOnOverlayClick?: boolean closeOnEscape?: boolean showCloseButton?: boolean } const sizeStyles: Record = { sm: 'max-w-sm', md: 'max-w-md', lg: 'max-w-lg', xl: 'max-w-xl', } export const Modal = ({ isOpen, onClose, title, children, size = 'md', closeOnOverlayClick = true, closeOnEscape = true, showCloseButton = true, }: ModalProps) => { const modalRef = useRef(null) const previousActiveElement = useRef(null) // Handle escape key useEffect(() => { if (!isOpen || !closeOnEscape) return const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose() } } document.addEventListener('keydown', handleEscape) return () => document.removeEventListener('keydown', handleEscape) }, [isOpen, closeOnEscape, onClose]) // Handle focus trap and return focus useEffect(() => { if (isOpen) { previousActiveElement.current = document.activeElement as HTMLElement modalRef.current?.focus() // Prevent body scroll document.body.style.overflow = 'hidden' } else { // Restore body scroll document.body.style.overflow = '' // Return focus to previous element previousActiveElement.current?.focus() } return () => { document.body.style.overflow = '' } }, [isOpen]) if (!isOpen) return null const handleOverlayClick = (e: React.MouseEvent) => { if (closeOnOverlayClick && e.target === e.currentTarget) { onClose() } } const modal = (
{/* Header */} {(title || showCloseButton) && (
{title && ( )} {showCloseButton && ( )}
)} {/* Body */}
{children}
) return createPortal(modal, document.body) }