@researchgate/react-intersection-list
Version:
React List Component using the Intersection Observer API
132 lines (100 loc) • 6.11 kB
JavaScript
exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactIntersectionObserver = _interopRequireDefault(require("@researchgate/react-intersection-observer"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _utils = require("./utils");
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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 Sentinel = /*#__PURE__*/function (_Component) {
_inheritsLoose(Sentinel, _Component);
var _super = _createSuper(Sentinel);
function Sentinel(props) {
var _this;
_this = _Component.call(this, props) || this;
_defineProperty(_assertThisInitialized(_this), "setRootElement", function (rootElement) {
_this.setState({
rootElement: rootElement
});
});
_this.state = {
rootElement: undefined,
rootMargin: (0, _utils.computeRootMargin)(props)
};
_this.target = /*#__PURE__*/_react["default"].createElement("template", {
style: {
height: 1,
minWidth: 1,
display: 'block'
}
});
props.setRef(_this.setRootElement);
return _this;
}
Sentinel.getDerivedStateFromProps = function getDerivedStateFromProps(_ref, prevState) {
var threshold = _ref.threshold,
axis = _ref.axis;
var newState = null;
if (threshold !== prevState.threshold || axis !== prevState.axis) {
newState = {
threshold: threshold,
axis: axis,
rootMargin: (0, _utils.computeRootMargin)({
threshold: threshold,
axis: axis
})
};
}
return newState;
};
var _proto = Sentinel.prototype;
_proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps, _ref2) {
var rootMargin = _ref2.rootMargin,
rootElement = _ref2.rootElement;
var _this$state = this.state,
currentRootMargin = _this$state.rootMargin,
currentRootElement = _this$state.rootElement; // When the rootMargin stays the same but the sentinel is repositioned, it can fall within
// its threshold prematurely. In this case we don't get any update from the Observer instance.
// We need to guarantee an update, and re-observing is a cheap way to accomplish this.
if (currentRootMargin === rootMargin && currentRootElement === rootElement) {
this.observer.externalUnobserve();
this.observer.observe();
return false;
}
return true;
};
_proto.render = function render() {
var _this2 = this;
var onChange = this.props.onChange;
var _this$state2 = this.state,
rootElement = _this$state2.rootElement,
rootMargin = _this$state2.rootMargin;
return /*#__PURE__*/_react["default"].createElement(_reactIntersectionObserver["default"], {
ref: function ref(instance) {
_this2.observer = instance;
},
disabled: typeof rootElement === 'undefined',
root: rootElement,
rootMargin: rootMargin,
onChange: onChange
}, this.target);
};
return Sentinel;
}(_react.Component);
process.env.NODE_ENV !== "production" ? Sentinel.propTypes = {
axis: _propTypes["default"].oneOf(['x', 'y']).isRequired,
threshold: _propTypes["default"].string.isRequired,
setRef: _propTypes["default"].func.isRequired,
onChange: _propTypes["default"].func.isRequired
} : void 0;
var _default = Sentinel;
exports["default"] = _default;
;