react-intersection
Version:
A React interface for the Intersection Observer API
127 lines (96 loc) • 4.98 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = void 0;
var React = _interopRequireWildcard(require("react"));
var _intersectionObserver = _interopRequireDefault(require("./intersection-observer"));
var _intersectionRootContext = require("./intersection-root-context");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var deepCompareArray = function deepCompareArray(a, b) {
return a.every(function (item, i) {
return item === b[i];
});
};
var IntersectionRoot =
/*#__PURE__*/
function (_React$Component) {
_inheritsLoose(IntersectionRoot, _React$Component);
function IntersectionRoot(props) {
var _this;
_this = _React$Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_this), "intersectionObserver", void 0);
_defineProperty(_assertThisInitialized(_this), "node", React.createRef());
_this.initIntersectionObserver(_this.props);
return _this;
}
var _proto = IntersectionRoot.prototype;
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
var _this$props = this.props,
viewport = _this$props.viewport,
margin = _this$props.margin,
threshold = _this$props.threshold;
var thresholdHasChanged = prevProps.threshold && threshold && deepCompareArray(prevProps.threshold, threshold);
if (prevProps.viewport !== viewport || prevProps.margin !== margin || thresholdHasChanged) {
this.initIntersectionObserver(this.props);
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (this.intersectionObserver) {
this.intersectionObserver.disconnect();
}
};
_proto.initIntersectionObserver = function initIntersectionObserver(_ref) {
var viewport = _ref.viewport,
margin = _ref.margin,
threshold = _ref.threshold;
var root = viewport === true ? null : this.node.current;
if (root && root instanceof HTMLElement || root === null) {
var props = {
root: root,
rootMargin: margin,
threshold: threshold
};
if (this.intersectionObserver) {
this.intersectionObserver.reconnect(props);
} else {
this.intersectionObserver = new _intersectionObserver["default"](props);
}
}
};
_proto.render = function render() {
var _this2 = this;
var _this$props2 = this.props,
children = _this$props2.children,
viewport = _this$props2.viewport;
var contextValue = {
observe: function observe(child, onChange) {
return _this2.intersectionObserver && _this2.intersectionObserver.observe(child, onChange);
},
unobserve: function unobserve(child) {
return _this2.intersectionObserver && _this2.intersectionObserver.unobserve(child);
}
};
return React.createElement(_intersectionRootContext.IntersectionRootContext.Provider, {
value: contextValue
}, viewport ? children : React.Children.only(React.cloneElement(children, {
ref: function ref(node) {
_this2.node.current = node;
var ref = children.ref;
if (typeof ref === 'function') {
ref(node);
} else if (ref !== null) {
ref.current = node;
}
}
})));
};
return IntersectionRoot;
}(React.Component);
exports["default"] = IntersectionRoot;
_defineProperty(IntersectionRoot, "defaultProps", {
viewport: false
});