Skip to content

Commit

Permalink
Merge pull request #37077 from bernhardoj/fix/36902-popover-esc-propa…
Browse files Browse the repository at this point in the history
…gation

Fix pressing ESC closes both video player popover and the attachment preview
  • Loading branch information
Julesssss authored Feb 22, 2024
2 parents 220a406 + 175c108 commit e6b6aa9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/components/PopoverProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ function PopoverContextProvider(props: PopoverContextProps) {
const [isOpen, setIsOpen] = useState(false);
const activePopoverRef = useRef<AnchorRef | null>(null);

const closePopover = useCallback((anchorRef?: RefObject<View | HTMLElement>) => {
const closePopover = useCallback((anchorRef?: RefObject<View | HTMLElement>): boolean => {
if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) {
return;
return false;
}

activePopoverRef.current.close();
activePopoverRef.current = null;
setIsOpen(false);
return true;
}, []);

useEffect(() => {
Expand Down Expand Up @@ -63,11 +64,13 @@ function PopoverContextProvider(props: PopoverContextProps) {
if (e.key !== 'Escape') {
return;
}
closePopover();
if (closePopover()) {
e.stopImmediatePropagation();
}
};
document.addEventListener('keydown', listener, true);
document.addEventListener('keyup', listener, true);
return () => {
document.removeEventListener('keydown', listener, true);
document.removeEventListener('keyup', listener, true);
};
}, [closePopover]);

Expand Down

0 comments on commit e6b6aa9

Please sign in to comment.