UNPKG

@cantonjs/react-scroll-view

Version:

react scroll component using intersection observer API

159 lines (138 loc) 6.01 kB
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import createStyles from './RefreshControlObserver.styles'; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { RefreshContext } from '../Contexts'; import { PullThreshold } from '../constants'; var RefreshControlObserver = function (_Component) { _inherits(RefreshControlObserver, _Component); function RefreshControlObserver() { var _ref; var _temp, _this, _ret; _classCallCheck(this, RefreshControlObserver); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = RefreshControlObserver.__proto__ || Object.getPrototypeOf(RefreshControlObserver)).call.apply(_ref, [this].concat(args))), _this), _this.styles = createStyles(), _this.state = { isActive: false }, _this.domRef = function (dom) { _this.dom = dom; }, _this.renderChildren = function (refreshState) { if (!_this.refreshState) { _this.refreshState = refreshState; refreshState.mount(_this); } var _this2 = _this, _this2$props = _this2.props, children = _this2$props.children, style = _this2$props.style, isRefreshing = _this2$props.isRefreshing, onRefresh = _this2$props.onRefresh, other = _objectWithoutProperties(_this2$props, ['children', 'style', 'isRefreshing', 'onRefresh']), isActive = _this2.state.isActive, styles = _this2.styles; return React.createElement( 'div', _extends({}, other, { style: styles.container(style, isRefreshing), ref: _this.domRef }), children({ isRefreshing: isRefreshing, isActive: isActive }) ); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(RefreshControlObserver, [{ key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { if (!prevProps.isRefreshing && this.props.isRefreshing) { this.enableTransition(); this.setHeight(PullThreshold); } else if (prevProps.isRefreshing && !this.props.isRefreshing) { this.end(); this.setHeight(0); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.refreshState.unmount(); } }, { key: 'requestRefresh', value: function requestRefresh() { var onRefresh = this.props.onRefresh; onRefresh && onRefresh(); } }, { key: 'setHeight', value: function setHeight(val) { var max = PullThreshold; var height = val > 0 ? val > max ? max + (val - max) / 2 : val : 0; var isActive = this.state.isActive, dom = this.dom; dom.style.height = height + 'px'; if (height >= max && !isActive) { this.setState({ isActive: true }); } else if (height < max && isActive) { this.setState({ isActive: false }); } } }, { key: 'enableTransition', value: function enableTransition() { this.dom.style.transition = 'height 0.3s ease-out, min-height 0.3s ease-out'; } }, { key: 'disableTransition', value: function disableTransition() { this.dom.style.transition = 'none'; } }, { key: 'end', value: function end() { if (this.state.isActive) { this.enableTransition(); this.setState({ isActive: false }); } } }, { key: 'attemptToRefresh', value: function attemptToRefresh() { var _props = this.props, onRefresh = _props.onRefresh, isRefreshing = _props.isRefreshing, isActive = this.state.isActive; if (onRefresh && !isRefreshing && isActive) { onRefresh(); } this.end(); if (isRefreshing) { this.setHeight(PullThreshold); } else { this.setHeight(0); } } }, { key: 'render', value: function render() { return React.createElement( RefreshContext.Consumer, null, this.renderChildren ); } }]); return RefreshControlObserver; }(Component); RefreshControlObserver.propTypes = { isRefreshing: PropTypes.bool.isRequired, onRefresh: PropTypes.func, style: PropTypes.object, children: PropTypes.func.isRequired }; export default RefreshControlObserver;