mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-25 10:51:57 +01:00
Add .oxfmtignore to skip generated bindings and wasm-pack output. Add npm format script, update DEVELOPMENT.md for Vite+ toolchain, and format all non-generated files with oxfmt. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import type { RefObject } from "react";
|
|
import { useEffect, useRef } from "react";
|
|
|
|
/**
|
|
* Get notified when a mouse click happens outside the target ref
|
|
* @param ref The element to be notified when a mouse click happens outside it
|
|
* @param onClickAway
|
|
* @param ignored Optional outside element to ignore (useful for dropdown triggers)
|
|
*/
|
|
export function useClickOutside(
|
|
ref: RefObject<HTMLElement | null>,
|
|
onClickAway: (event: MouseEvent) => void,
|
|
ignored?: RefObject<HTMLElement | null>,
|
|
) {
|
|
const savedCallback = useRef(onClickAway);
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = onClickAway;
|
|
}, [onClickAway]);
|
|
|
|
useEffect(() => {
|
|
const handler = (event: MouseEvent) => {
|
|
if (ref.current == null || !(event.target instanceof HTMLElement)) {
|
|
return;
|
|
}
|
|
const isIgnored = ignored?.current?.contains(event.target);
|
|
const clickedOutside = !ref.current.contains(event.target);
|
|
if (!isIgnored && clickedOutside) {
|
|
savedCallback.current(event);
|
|
}
|
|
};
|
|
// NOTE: We're using mousedown instead of click to handle some edge cases like when a context
|
|
// menu is open with the ctrl key.
|
|
document.addEventListener("mousedown", handler, { capture: true });
|
|
document.addEventListener("contextmenu", handler, { capture: true });
|
|
return () => {
|
|
document.removeEventListener("mousedown", handler);
|
|
document.removeEventListener("contextmenu", handler);
|
|
};
|
|
}, [ignored, ref]);
|
|
}
|