UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

143 lines (122 loc) 4.9 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; 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 classNames from 'classnames'; import omit from 'lodash/omit'; import { CSSTransition } from 'react-transition-group'; import raf from 'raf'; import noop from '../_utils/noop'; import ToTopOutlined from '@jdcfe/icons-react/ToTopOutlined'; var easeInOutCubic = function easeInOutCubic(t, b, c, d) { var cc = c - b; t /= d / 2; if (t < 1) { return cc / 2 * t * t * t + b; } else { return cc / 2 * ((t -= 2) * t * t + 2) + b; } }; var ToTop = /*#__PURE__*/function (_React$PureComponent) { _inherits(ToTop, _React$PureComponent); var _super = _createSuper(ToTop); function ToTop(props) { var _this; _classCallCheck(this, ToTop); _this = _super.call(this, props); _this.handleScroll = function () { var displayHeight = _this.props.displayHeight; _this.setState({ visible: window.pageYOffset > displayHeight }); }; _this.scrollToTop = function (e) { var _document = document, documentElement = _document.documentElement; var scrollTop = window.pageYOffset || document.body.scrollTop || documentElement && documentElement.scrollTop || 0; var startTime = Date.now(); var frameFunc = function frameFunc() { var timestamp = Date.now(); var time = timestamp - startTime; ToTop.setScrollTop({ value: easeInOutCubic(time, scrollTop, 0, 450) }); if (time < 450) { raf(frameFunc); } else { ToTop.setScrollTop({ value: 0 }); } }; raf(frameFunc); (_this.props.onClick || noop)(e); }; _this.state = { visible: false }; return _this; } _createClass(ToTop, [{ key: "componentDidMount", value: function componentDidMount() { window.addEventListener('scroll', this.handleScroll); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('scroll', this.handleScroll); } }, { key: "render", value: function render() { var _this$props = this.props, prefixCls = _this$props.prefixCls, _this$props$className = _this$props.className, className = _this$props$className === void 0 ? '' : _this$props$className, children = _this$props.children, style = _this$props.style; var cls = classNames(prefixCls, className); var defaultElement = /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-content") }, /*#__PURE__*/React.createElement(ToTopOutlined, { className: "".concat(prefixCls, "-icon") })); var divProps = omit(this.props, ['prefixCls', 'className', 'children', 'displayHeight', 'visible']); var visible = 'visible' in this.props ? this.props.visible : this.state.visible; return /*#__PURE__*/React.createElement(CSSTransition, { in: visible, timeout: 300, classNames: 'fade', unmountOnExit: true }, /*#__PURE__*/React.createElement("div", _extends({}, divProps, { style: style, className: cls, onClick: this.scrollToTop }), children || defaultElement)); } }], [{ key: "setScrollTop", value: function setScrollTop(parameters) { var value = parameters.value; var _document2 = document, documentElement = _document2.documentElement; document.body.scrollTop = value; if (documentElement) { documentElement.scrollTop = value; } } }]); return ToTop; }(React.PureComponent); ToTop.defaultProps = { prefixCls: 'Yep-to-top', style: {}, displayHeight: 0 }; export default ToTop;