UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

134 lines (118 loc) 4.83 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from 'react'; import * as PropTypes from 'prop-types'; import raf from 'raf'; var StickyContainer = /*#__PURE__*/function (_React$PureComponent) { _inherits(StickyContainer, _React$PureComponent); var _super = _createSuper(StickyContainer); function StickyContainer(props) { var _this; _classCallCheck(this, StickyContainer); _this = _super.call(this, props); _this.subscribers = []; _this.events = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load']; _this.createRef = _this.createRef.bind(_assertThisInitialized(_this)); _this.getParent = _this.getParent.bind(_assertThisInitialized(_this)); _this.subscribe = _this.subscribe.bind(_assertThisInitialized(_this)); _this.unsubscribe = _this.unsubscribe.bind(_assertThisInitialized(_this)); _this.notifySubscribers = _this.notifySubscribers.bind(_assertThisInitialized(_this)); return _this; } _createClass(StickyContainer, [{ key: "getChildContext", value: function getChildContext() { return { subscribe: this.subscribe, unsubscribe: this.unsubscribe, getParent: this.getParent }; } }, { key: "subscribe", value: function subscribe(handler) { this.subscribers = this.subscribers.concat(handler); } }, { key: "unsubscribe", value: function unsubscribe(handler) { this.subscribers = this.subscribers.filter(function (current) { return current !== handler; }); } }, { key: "notifySubscribers", value: function notifySubscribers(evt) { var _this2 = this; if (!this.framePending) { var currentTarget = evt.currentTarget; raf(function () { _this2.framePending = false; var _this2$node$getBoundi = _this2.node.getBoundingClientRect(), top = _this2$node$getBoundi.top, bottom = _this2$node$getBoundi.bottom; _this2.subscribers.forEach(function (handler) { return handler({ distanceFromTop: top, distanceFromBottom: bottom, eventSource: currentTarget === window ? document.body : _this2.node }); }); }); this.framePending = true; } } }, { key: "createRef", value: function createRef(node) { this.node = node; } }, { key: "getParent", value: function getParent() { return this.node; } }, { key: "componentDidMount", value: function componentDidMount() { var _this3 = this; this.events.forEach(function (event) { return window.addEventListener(event, _this3.notifySubscribers); }); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this4 = this; this.events.forEach(function (event) { return window.removeEventListener(event, _this4.notifySubscribers); }); } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement("div", _extends({}, this.props, { ref: this.createRef, onScroll: this.notifySubscribers, onTouchStart: this.notifySubscribers, onTouchMove: this.notifySubscribers, onTouchEnd: this.notifySubscribers })); } }]); return StickyContainer; }(React.PureComponent); export { StickyContainer as default }; StickyContainer.defaultProps = {}; StickyContainer.childContextTypes = { subscribe: PropTypes.func, unsubscribe: PropTypes.func, getParent: PropTypes.func };