@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.
49 lines (44 loc) • 1.46 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState, useEffect, useRef } from "react";
export default (function (_ref) {
var _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;
if (typeof window === "undefined") {
return {
ref: null,
entry: null
};
}
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 observer = useRef(null);
useEffect(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: setNode,
entry: entry
};
});