@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.
62 lines (51 loc) • 1.78 kB
JavaScript
;
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 _default = function _default(_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 = (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 observer = (0, _react.useRef)(null);
(0, _react.useEffect)(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: setNode,
entry: entry
};
};
exports.default = _default;