react-box-tools
Version:
Box tools react components, utils and hooks
37 lines (36 loc) • 924 B
JavaScript
import { useState as f, useEffect as v, useRef as p } from "react";
function y(r, {
threshold: s = 0,
root: e = null,
rootMargin: n = "0%",
freezeOnceVisible: t = !1
}) {
const [o, b] = f(), c = (o == null ? void 0 : o.isIntersecting) && t, a = ([u]) => {
b(u);
};
return v(() => {
const u = r == null ? void 0 : r.current;
if (!!!window.IntersectionObserver || c || !u) return;
const O = { threshold: s, root: e, rootMargin: n }, i = new IntersectionObserver(a, O);
return i.observe(u), () => {
i.disconnect();
};
}, [r, JSON.stringify(s), e, n, c]), o;
}
function z(r) {
const s = p(null);
return v(() => {
const e = s.current;
if (!e) return;
const n = new ResizeObserver((t) => {
!Array.isArray(t) || !t.length || r(t[0].contentRect);
});
return n.observe(e), () => {
n.unobserve(e);
};
}, [r]), s;
}
export {
z as a,
y as u
};