import React, { useEffect, useRef } from "react"; import { X } from "lucide-react"; import { Card } from "./ui/card"; import { Button } from "./ui/button"; interface ModalProps { isOpen: boolean; onClose: () => void; title: string; children: React.ReactNode; description?: React.ReactNode; footer?: React.ReactNode; maxWidth?: string; isFa?: boolean; } export const Modal: React.FC = ({ isOpen, onClose, title, children, description, footer, maxWidth = "max-w-lg", }) => { const cardRef = useRef(null); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") { onClose(); } }; if (isOpen) { document.body.style.overflow = "hidden"; document.addEventListener("keydown", handleKeyDown); } else { document.body.style.overflow = "unset"; } return () => { document.body.style.overflow = "unset"; document.removeEventListener("keydown", handleKeyDown); }; }, [isOpen, onClose]); useEffect(() => { if (!isOpen) return; const focusTimer = window.setTimeout(() => { const activeElement = document.activeElement; if (activeElement instanceof HTMLElement && cardRef.current?.contains(activeElement)) { return; } const firstTextInput = cardRef.current?.querySelector( 'input:not([type]), input[type="text"], input[type="search"], input[type="email"], input[type="tel"], input[type="password"], input[type="number"], textarea', ); firstTextInput?.focus(); }, 0); return () => window.clearTimeout(focusTimer); }, [isOpen]); if (!isOpen) return null; return (
e.stopPropagation()} >

{title}

{description && (

{description}

)} {children}
{footer && (
{footer}
)}
); };