mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 09:18:30 +02:00
useClickOutside account for right click
This commit is contained in:
@@ -13,21 +13,27 @@ export function useClickOutside(
|
|||||||
ignored?: RefObject<HTMLElement | null>,
|
ignored?: RefObject<HTMLElement | null>,
|
||||||
) {
|
) {
|
||||||
const savedCallback = useRef(onClickAway);
|
const savedCallback = useRef(onClickAway);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
savedCallback.current = onClickAway;
|
savedCallback.current = onClickAway;
|
||||||
}, [onClickAway]);
|
}, [onClickAway]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handler = (event: MouseEvent) => {
|
const handler = (event: MouseEvent) => {
|
||||||
if (ref.current == null || !(event.target instanceof HTMLElement)) return;
|
if (ref.current == null || !(event.target instanceof HTMLElement)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const isIgnored = ignored?.current?.contains(event.target);
|
const isIgnored = ignored?.current?.contains(event.target);
|
||||||
const clickedOutside = !ref.current.contains(event.target);
|
const clickedOutside = !ref.current.contains(event.target);
|
||||||
if (!isIgnored && clickedOutside) {
|
if (!isIgnored && clickedOutside) {
|
||||||
savedCallback.current(event);
|
savedCallback.current(event);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
document.addEventListener('click', handler);
|
document.addEventListener('click', handler, { capture: true });
|
||||||
|
document.addEventListener('contextmenu', handler, { capture: true });
|
||||||
return () => {
|
return () => {
|
||||||
document.removeEventListener('click', handler);
|
document.removeEventListener('click', handler);
|
||||||
|
document.removeEventListener('contextmenu', handler);
|
||||||
};
|
};
|
||||||
}, [ignored, ref]);
|
}, [ignored, ref]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user