react-sign
Version:
Easy load more in react. Is my component on user's viewport?!
63 lines (52 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var React = require('react');
var React__default = _interopDefault(React);
var ReactSign = function ReactSign(_ref) {
var onEnter = _ref.onEnter,
onLeave = _ref.onLeave,
children = _ref.children,
_ref$threshold = _ref.threshold,
threshold = _ref$threshold === void 0 ? 0 : _ref$threshold,
_ref$rootMargin = _ref.rootMargin,
rootMargin = _ref$rootMargin === void 0 ? '0px' : _ref$rootMargin;
var isInitializeToggle = React.useRef(true);
var wrapperRef = React.useRef(null);
var observerRef = React.useRef();
var rootMarginStringValue = React.useMemo(function () {
return typeof rootMargin === 'number' ? rootMargin + "px" : rootMargin;
}, [rootMargin]);
var handleIntersect = function handleIntersect(entries // observer: IntersectionObserver
) {
if (entries[0].isIntersecting) {
onEnter && onEnter();
} else {
onLeave && !isInitializeToggle.current && onLeave();
}
if (isInitializeToggle.current) {
isInitializeToggle.current = false;
}
};
React.useEffect(function () {
if (wrapperRef.current) {
var options = {
root: null,
rootMarginStringValue: rootMarginStringValue,
threshold: threshold
};
observerRef.current = new IntersectionObserver(handleIntersect, options);
observerRef.current.observe(wrapperRef.current);
}
return function () {
var _observerRef$current;
if (wrapperRef.current) (_observerRef$current = observerRef.current) == null ? void 0 : _observerRef$current.unobserve(wrapperRef.current);
};
}, []);
return React__default.createElement("div", {
ref: wrapperRef
}, children);
};
exports.ReactSign = ReactSign;
exports.default = ReactSign;
//# sourceMappingURL=react-sign.cjs.development.js.map