From 2114b7296e5f2d3099ed997a8eda1265c9e4841e Mon Sep 17 00:00:00 2001 From: Matri Date: Fri, 31 Mar 2023 09:14:17 +0800 Subject: [PATCH] fix: properly trigger prompt modal close (#309) Co-authored-by: MatriQi --- components/Promptbar/PromptModal.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/components/Promptbar/PromptModal.tsx b/components/Promptbar/PromptModal.tsx index 2c640e1..d1faecf 100644 --- a/components/Promptbar/PromptModal.tsx +++ b/components/Promptbar/PromptModal.tsx @@ -25,16 +25,21 @@ export const PromptModal: FC = ({ 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]);