mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 16:43:16 +00:00
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
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<HTMLDivElement>(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 = (
|
|
<div className="fixed left-0 top-0 z-30 flex h-screen w-screen items-center justify-center bg-gray-alpha bg-opacity-50">
|
|
<div
|
|
ref={modalRef}
|
|
className={`relative w-11/12 rounded-2xl bg-white p-8 dark:bg-[#26272E] sm:w-[512px] ${className}`}
|
|
>
|
|
{!isPerformingTask && (
|
|
<button
|
|
className="absolute right-4 top-3 z-50 m-2 w-3"
|
|
onClick={close}
|
|
>
|
|
<img className="filter dark:invert" src={Exit} alt="Close" />
|
|
</button>
|
|
)}
|
|
<div className={`${contentClassName}`}>{children}</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
return createPortal(modalContent, document.body);
|
|
}
|