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

View File

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

View File

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