Fix an issue with the OutsideWatcher

The OutsideWatcher was disabling hotkeys because it was always being active -- whether the object was pinned or not. Modified the hook to now take a new optional argument called "req" which is a boolean that indicates whether to trigger it or not.

We can pass this from the component to control when the outside watcher should work and when it shouldn't.
This commit is contained in:
blessedcoolant 2022-10-31 05:36:56 +13:00 committed by psychedelicious
parent 6a6fbe24a3
commit f60d22b29b
3 changed files with 17 additions and 8 deletions

View File

@ -2,7 +2,8 @@ import { RefObject, useEffect } from 'react';
const useClickOutsideWatcher = (
ref: RefObject<HTMLElement>,
callback: () => void
callback: () => void,
req = true
) => {
useEffect(() => {
function handleClickOutside(e: MouseEvent) {
@ -10,11 +11,15 @@ const useClickOutsideWatcher = (
callback();
}
}
document.addEventListener('mousedown', handleClickOutside);
if (req) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
if (req) {
document.removeEventListener('mousedown', handleClickOutside);
}
};
}, [ref, callback]);
}, [ref, req, callback]);
};
export default useClickOutsideWatcher;

View File

@ -111,13 +111,13 @@ export default function ImageGallery() {
};
const handleCloseGallery = () => {
if (shouldPinGallery) return;
// if (shouldPinGallery) return;
dispatch(setShouldShowGallery(false));
dispatch(
setGalleryScrollPosition(
galleryContainerRef.current ? galleryContainerRef.current.scrollTop : 0
)
);
dispatch(setShouldShowGallery(false));
dispatch(setShouldHoldGalleryOpen(false));
// dispatch(setNeedsCache(true));
};
@ -249,7 +249,7 @@ export default function ImageGallery() {
setShouldShowButtons(galleryWidth >= 280);
}, [galleryWidth]);
useClickOutsideWatcher(galleryRef, handleCloseGallery);
useClickOutsideWatcher(galleryRef, handleCloseGallery, !shouldPinGallery);
return (
<CSSTransition

View File

@ -84,7 +84,11 @@ const InvokeOptionsPanel = (props: Props) => {
// dispatch(setNeedsCache(true));
}, [dispatch, shouldPinOptionsPanel]);
useClickOutsideWatcher(optionsPanelRef, handleCloseOptionsPanel);
useClickOutsideWatcher(
optionsPanelRef,
handleCloseOptionsPanel,
!shouldPinOptionsPanel
);
const setCloseOptionsPanelTimer = () => {
timeoutIdRef.current = window.setTimeout(