import React, { useEffect, useRef } from 'react'; import { createPortal } from 'react-dom'; import Exit from '../assets/exit.svg'; type WrapperModalPropsType = { children: React.ReactNode; close: () => void; isPerformingTask?: boolean; className?: string; contentClassName?: string; }; export default function WrapperModal({ children, close, isPerformingTask = false, className = '', contentClassName = '', }: WrapperModalPropsType) { const modalRef = useRef(null); useEffect(() => { if (isPerformingTask) return; const handleClickOutside = (event: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(event.target as Node)) close(); }; const handleEscapePress = (event: KeyboardEvent) => { if (event.key === 'Escape') close(); }; document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleEscapePress); return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleEscapePress); }; }, [close, isPerformingTask]); const modalContent = (
{!isPerformingTask && ( )}
{children}
); return createPortal(modalContent, document.body); }