react-box-tools
Version:
Box tools react components, utils and hooks
30 lines (29 loc) • 663 B
JavaScript
import { useLayoutEffect as f, useEffect as c, useRef as a } from "react";
const d = typeof window < "u" ? f : c;
function E(t, s, n, r) {
const u = a(s);
d(() => {
u.current = s;
}, [s]), c(() => {
const o = (n == null ? void 0 : n.current) ?? window;
if (!(o && o.addEventListener))
return;
const e = (i) => {
u.current(i);
};
return o.addEventListener(t, e, r), () => {
o.removeEventListener(t, e);
};
}, [t, n, r]);
}
function w(t, s, n = "mousedown") {
E(n, (r) => {
const u = t == null ? void 0 : t.current;
!u || u.contains(r.target) || s(r);
});
}
export {
w as a,
d as b,
E as u
};