UNPKG

react-lazily-render

Version:
243 lines (190 loc) 9.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var React = _interopRequireWildcard(require("react")); var _scrollparent = _interopRequireDefault(require("scrollparent")); var _getViewportBounds2 = _interopRequireDefault(require("./utils/getViewportBounds")); var _getElementBounds2 = _interopRequireDefault(require("./utils/getElementBounds")); var _convertOffsetToBounds = _interopRequireDefault(require("./utils/convertOffsetToBounds")); var _isElementInViewport = _interopRequireDefault(require("./utils/isElementInViewport")); var _isBackCompatMode = _interopRequireDefault(require("./utils/isBackCompatMode")); var _eventListenerOptions = _interopRequireDefault(require("./utils/eventListenerOptions")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } } 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; } 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 ((0, _isElementInViewport["default"])(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 = (0, _scrollparent["default"])(this.element); if (container === document.scrollingElement || container === document.documentElement || !(0, _isBackCompatMode["default"])() && container == document.body) { return window; } else { return container; } } else { return undefined; } } } }, { key: "getViewportBounds", value: function getViewportBounds() { return (0, _getViewportBounds2["default"])(this.container); } }, { key: "getElementBounds", value: function getElementBounds() { return (0, _getElementBounds2["default"])(this.element); } }, { key: "getOffsetBounds", value: function getOffsetBounds() { var offset = this.props.offset; return (0, _convertOffsetToBounds["default"])(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["default"]); window.addEventListener('resize', this.update); } }, { key: "stopListening", value: function stopListening(container) { if (container) container.removeEventListener('scroll', this.update, _eventListenerOptions["default"]); 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); exports["default"] = LazilyRender;