@payfit/unity-components
Version:
78 lines (77 loc) • 1.59 kB
JavaScript
import { detectScroll as e } from "../utils/scroll-detection.js";
import { useCallback as t, useEffect as n, useLayoutEffect as r, useState as i } from "react";
//#region src/hooks/use-has-scroll.ts
function a(a, o = {}) {
let { strategy: s = "size", debounceMs: c = 100, watchMutations: l = !0, watchResize: u = !0 } = o, [d, f] = i({
hasVerticalScroll: !1,
hasHorizontalScroll: !1,
hasAnyScroll: !1,
detectionMethod: {
size: {
vertical: !1,
horizontal: !1
},
style: {
vertical: !1,
horizontal: !1
}
}
}), p = t(() => {
let t = a.current;
t && f(e(t, s));
}, [a, s]);
return r(() => {
if (!a.current) return;
p();
let e = requestAnimationFrame(() => {
p();
});
return () => {
cancelAnimationFrame(e);
};
}, [p]), n(() => {
let e = !0, t, n = () => {
let e = a.current;
if (!e) return;
let n, r = () => {
clearTimeout(n), n = setTimeout(p, c);
};
p(), requestAnimationFrame(() => {
p();
});
let i = [];
if (u) {
let t = new ResizeObserver(r);
t.observe(e), i.push(t);
}
if (l) {
let t = new MutationObserver(r);
t.observe(e, {
childList: !0,
subtree: !0,
characterData: !0,
attributes: !0,
attributeFilter: ["style", "class"]
}), i.push(t);
}
t = () => {
clearTimeout(n), i.forEach((e) => {
e.disconnect();
});
};
}, r = () => {
e && (a.current ? n() : requestAnimationFrame(r));
};
return r(), () => {
e = !1, t?.();
};
}, [
a,
p,
c,
u,
l
]), d;
}
//#endregion
export { a as useHasScroll };