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.

53 lines (46 loc) 1.65 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { useState, useEffect, useRef, useCallback } from "react"; var useIntersect = function useIntersect() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$root = _ref.root, root = _ref$root === void 0 ? null : _ref$root, rootMargin = _ref.rootMargin, _ref$threshold = _ref.threshold, threshold = _ref$threshold === void 0 ? 0 : _ref$threshold; var _useState = useState(null), _useState2 = _slicedToArray(_useState, 2), entry = _useState2[0], updateEntry = _useState2[1]; var _useState3 = useState(null), _useState4 = _slicedToArray(_useState3, 2), node = _useState4[0], setNode = _useState4[1]; var ref = useCallback(function (el) { setNode(el); }, []); var observer = useRef(null); useEffect(function () { if ("IntersectionObserver" in window === false) { return function () {}; } observer.current = new window.IntersectionObserver(function (_ref2) { var _ref3 = _slicedToArray(_ref2, 1), ent = _ref3[0]; return updateEntry(ent); }, { root: root, rootMargin: rootMargin, threshold: threshold }); var currentObs = observer.current; if (node) currentObs === null || currentObs === void 0 ? void 0 : currentObs.observe(node); return function () { return currentObs === null || currentObs === void 0 ? void 0 : currentObs.disconnect(); }; }, [node, root, rootMargin, threshold]); return { ref: ref, entry: entry }; }; export default useIntersect;