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.

64 lines (53 loc) 1.97 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = require("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 = (0, _react.useState)(null), _useState2 = (0, _slicedToArray2.default)(_useState, 2), entry = _useState2[0], updateEntry = _useState2[1]; var _useState3 = (0, _react.useState)(null), _useState4 = (0, _slicedToArray2.default)(_useState3, 2), node = _useState4[0], setNode = _useState4[1]; var ref = (0, _react.useCallback)(function (el) { setNode(el); }, []); var observer = (0, _react.useRef)(null); (0, _react.useEffect)(function () { if ("IntersectionObserver" in window === false) { return function () {}; } observer.current = new window.IntersectionObserver(function (_ref2) { var _ref3 = (0, _slicedToArray2.default)(_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 }; }; var _default = useIntersect; exports.default = _default;