import { useEffect, useId, useRef, type ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { X } from 'lucide-react'; interface ModalProps { isOpen: boolean; onClose: () => void; title: string; children: ReactNode; footer?: ReactNode; closeDisabled?: boolean; } export const Modal = ({ isOpen, onClose, title, children, footer, closeDisabled = false }: ModalProps) => { const ref = useRef(null); const titleId = useId(); useEffect(() => { if (!isOpen) return; const dialog = ref.current; const previousFocus = document.activeElement as HTMLElement | null; const overflow = document.body.style.overflow; dialog?.showModal(); document.body.style.overflow = 'hidden'; return () => { dialog?.close(); document.body.style.overflow = overflow; if (previousFocus?.isConnected) previousFocus.focus(); }; }, [isOpen]); return createPortal( { e.preventDefault(); if (!closeDisabled) onClose(); }} onClick={e => { if (e.target === e.currentTarget && !closeDisabled) onClose(); }}>

{title}

{children}
{footer &&
{footer}
}
, document.body, ); };