import React, { useEffect, useRef } from 'react'; import { createPortal } from 'react-dom'; import Exit from '../assets/exit.svg'; interface WrapperModalPropsType { children: React.ReactNode; close: () => void; isPerformingTask?: boolean; className?: string; contentClassName?: string; } export default function WrapperModal({ children, close, isPerformingTask = false, className = '', // Default width, but can be overridden contentClassName = '', // Default padding, but can be overridden }: 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]); const modalContent = (
{!isPerformingTask && ( )}
{children}
); return createPortal(modalContent, document.body); }