fix: properly trigger prompt modal close (#309)

Co-authored-by: MatriQi <matri@aifi.io>
This commit is contained in:
Matri 2023-03-31 09:14:17 +08:00 committed by GitHub
parent 5cdd3e56b7
commit 2114b7296e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -25,16 +25,21 @@ export const PromptModal: FC<Props> = ({ prompt, onClose, onUpdatePrompt }) => {
};
useEffect(() => {
const handleOutsideClick = (e: MouseEvent) => {
const handleMouseDown = (e: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(e.target as Node)) {
onClose();
window.addEventListener('mouseup', handleMouseUp);
}
};
window.addEventListener('click', handleOutsideClick);
const handleMouseUp = (e: MouseEvent) => {
window.removeEventListener('mouseup', handleMouseUp);
onClose();
};
window.addEventListener('mousedown', handleMouseDown);
return () => {
window.removeEventListener('click', handleOutsideClick);
window.removeEventListener('mousedown', handleMouseDown);
};
}, [onClose]);