react-lazily-render
Version:
Lazily render react components
225 lines (183 loc) • 8.79 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 * as React from 'react';
import scrollParent from 'scrollparent';
import _getViewportBounds from './utils/getViewportBounds';
import _getElementBounds from './utils/getElementBounds';
import convertOffsetToBounds from './utils/convertOffsetToBounds';
import isElementInViewport from './utils/isElementInViewport';
import isBackCompatMode from './utils/isBackCompatMode';
import eventListenerOptions from './utils/eventListenerOptions';
var LazilyRender =
/*#__PURE__*/
function (_React$Component) {
_inherits(LazilyRender, _React$Component);
function LazilyRender() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, LazilyRender);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(LazilyRender)).call.apply(_getPrototypeOf2, [this].concat(args)));
_defineProperty(_assertThisInitialized(_this), "raf", void 0);
_defineProperty(_assertThisInitialized(_this), "element", void 0);
_defineProperty(_assertThisInitialized(_this), "container", void 0);
_defineProperty(_assertThisInitialized(_this), "state", {
hasBeenScrolledIntoView: false
});
_defineProperty(_assertThisInitialized(_this), "update", function () {
cancelAnimationFrame(_this.raf);
_this.raf = requestAnimationFrame(function () {
var elementBounds = _this.getElementBounds();
var viewportBounds = _this.getViewportBounds();
var offsetBounds = _this.getOffsetBounds();
if (!elementBounds || !viewportBounds) {
return;
}
if (isElementInViewport(elementBounds, viewportBounds, offsetBounds)) {
_this.stopListening(_this.container);
_this.setState({
hasBeenScrolledIntoView: true
}, function () {
var onRender = _this.props.onRender;
if (onRender) {
onRender();
}
});
}
});
});
_defineProperty(_assertThisInitialized(_this), "handleMount", function (element) {
var scrollContainer = _this.props.scrollContainer;
_this.element = element;
_this.container = _this.getContainer(scrollContainer);
});
return _this;
}
_createClass(LazilyRender, [{
key: "getContainer",
value: function getContainer(scrollContainer) {
if (scrollContainer) {
return scrollContainer;
} else {
if (this.element) {
var container = scrollParent(this.element);
if (container === document.scrollingElement || container === document.documentElement || !isBackCompatMode() && container == document.body) {
return window;
} else {
return container;
}
} else {
return undefined;
}
}
}
}, {
key: "getViewportBounds",
value: function getViewportBounds() {
return _getViewportBounds(this.container);
}
}, {
key: "getElementBounds",
value: function getElementBounds() {
return _getElementBounds(this.element);
}
}, {
key: "getOffsetBounds",
value: function getOffsetBounds() {
var offset = this.props.offset;
return convertOffsetToBounds(offset);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this2 = this;
var prevContainer = prevProps.scrollContainer;
var nextContainer = this.props.scrollContainer; // If a scroll container was defined before, do some cleanup
// and bootstrap the next scroll container.
if (prevContainer !== nextContainer) {
// If the previous container was already utilised, no cleanup
// is required - already done in LazilyRender.update().
if (!prevState.hasBeenScrolledIntoView) {
this.stopListening(prevContainer);
} // Set a new listener if the scrollContainer is defined, and update
// the container property accordingly. Note: this should only be done
// when the next container is different.
this.container = this.getContainer(nextContainer);
this.startListening(this.container); // Signal that the element has not been scrolled into view and
// recompute its position. This will essentially 'reset' the node's
// current status back to a placeholder item if need be.
this.setState({
hasBeenScrolledIntoView: false
}, function () {
_this2.update();
});
}
}
}, {
key: "startListening",
value: function startListening(container) {
if (container) container.addEventListener('scroll', this.update, eventListenerOptions);
window.addEventListener('resize', this.update);
}
}, {
key: "stopListening",
value: function stopListening(container) {
if (container) container.removeEventListener('scroll', this.update, eventListenerOptions);
window.removeEventListener('resize', this.update);
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
this.update();
this.startListening(this.container);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.stopListening(this.container);
}
}, {
key: "renderChildren",
value: function renderChildren() {
var _this$props = this.props,
placeholder = _this$props.placeholder,
content = _this$props.content,
children = _this$props.children;
var hasBeenScrolledIntoView = this.state.hasBeenScrolledIntoView;
if (!hasBeenScrolledIntoView && placeholder) {
return placeholder;
}
if (hasBeenScrolledIntoView && content) {
return content;
}
if (children) {
return children(hasBeenScrolledIntoView);
}
return null;
}
}, {
key: "render",
value: function render() {
var _this$props2 = this.props,
className = _this$props2.className,
component = _this$props2.component;
return React.createElement(component || 'div', {
ref: this.handleMount,
className: className,
children: this.renderChildren()
});
}
}]);
return LazilyRender;
}(React.Component);
export { LazilyRender as default };