@researchgate/react-intersection-list
Version:
React List Component using the Intersection Observer API
117 lines (93 loc) • 4.73 kB
JavaScript
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; }
import React, { Component } from 'react';
import Observer from '@researchgate/react-intersection-observer';
import PropTypes from 'prop-types';
import { computeRootMargin } from './utils';
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: computeRootMargin(props)
};
_this.target = /*#__PURE__*/React.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: 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.createElement(Observer, {
ref: function ref(instance) {
_this2.observer = instance;
},
disabled: typeof rootElement === 'undefined',
root: rootElement,
rootMargin: rootMargin,
onChange: onChange
}, this.target);
};
return Sentinel;
}(Component);
process.env.NODE_ENV !== "production" ? Sentinel.propTypes = {
axis: PropTypes.oneOf(['x', 'y']).isRequired,
threshold: PropTypes.string.isRequired,
setRef: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired
} : void 0;
export default Sentinel;