From 449fab49d7e01020fd5019b46c87b870db043d5a Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 22 Feb 2024 18:55:39 +0800 Subject: [PATCH 1/2] change to keyup --- src/components/PopoverProvider/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/PopoverProvider/index.tsx b/src/components/PopoverProvider/index.tsx index 69728d7be126..75a4e06e76f2 100644 --- a/src/components/PopoverProvider/index.tsx +++ b/src/components/PopoverProvider/index.tsx @@ -65,9 +65,9 @@ function PopoverContextProvider(props: PopoverContextProps) { } closePopover(); }; - document.addEventListener('keydown', listener, true); + document.addEventListener('keyup', listener, true); return () => { - document.removeEventListener('keydown', listener, true); + document.removeEventListener('keyup', listener, true); }; }, [closePopover]); From 175c108dfb827b8e6740ce499a600650c5aaabee Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 22 Feb 2024 18:55:50 +0800 Subject: [PATCH 2/2] stop propagation when pressing ESC --- src/components/PopoverProvider/index.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/PopoverProvider/index.tsx b/src/components/PopoverProvider/index.tsx index 75a4e06e76f2..b8d4efbd916d 100644 --- a/src/components/PopoverProvider/index.tsx +++ b/src/components/PopoverProvider/index.tsx @@ -21,14 +21,15 @@ function PopoverContextProvider(props: PopoverContextProps) { const [isOpen, setIsOpen] = useState(false); const activePopoverRef = useRef(null); - const closePopover = useCallback((anchorRef?: RefObject) => { + const closePopover = useCallback((anchorRef?: RefObject): boolean => { if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) { - return; + return false; } activePopoverRef.current.close(); activePopoverRef.current = null; setIsOpen(false); + return true; }, []); useEffect(() => { @@ -63,7 +64,9 @@ function PopoverContextProvider(props: PopoverContextProps) { if (e.key !== 'Escape') { return; } - closePopover(); + if (closePopover()) { + e.stopImmediatePropagation(); + } }; document.addEventListener('keyup', listener, true); return () => {