@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
43 lines (38 loc) • 1.04 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _react = require("react");
const useIntersect = ({
root = null,
rootMargin,
threshold = 0
} = {}) => {
const [entry, updateEntry] = (0, _react.useState)(null);
const [node, setNode] = (0, _react.useState)(null);
const ref = (0, _react.useCallback)(el => {
setNode(el);
}, []);
const observer = (0, _react.useRef)(null);
(0, _react.useEffect)(() => {
if ("IntersectionObserver" in window === false) {
return () => {};
}
observer.current = new window.IntersectionObserver(([ent]) => updateEntry(ent), {
root,
rootMargin,
threshold
});
const {
current: currentObs
} = observer;
if (node) currentObs == null ? void 0 : currentObs.observe(node);
return () => currentObs == null ? void 0 : currentObs.disconnect();
}, [node, root, rootMargin, threshold]);
return {
ref,
entry,
observer: observer.current
};
};
var _default = useIntersect;
exports.default = _default;