UNPKG

@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.

44 lines (39 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: 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 || currentObs === void 0 ? void 0 : currentObs.observe(node); return () => currentObs === null || currentObs === void 0 ? void 0 : currentObs.disconnect(); }, [node, root, rootMargin, threshold]); return { ref, entry }; }; var _default = useIntersect; exports.default = _default;