choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
138 lines (121 loc) • 4.53 kB
JavaScript
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 _createSuper from "@babel/runtime/helpers/createSuper";
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import Animate from '../animate';
import addEventListener from '../_util/addEventListener';
import getScroll from '../_util/getScroll';
import scrollTo from '../_util/scrollTo';
import ConfigContext from '../config-provider/ConfigContext';
function getDefaultTarget() {
return window;
}
var BackTop = /*#__PURE__*/function (_PureComponent) {
_inherits(BackTop, _PureComponent);
var _super = _createSuper(BackTop);
function BackTop() {
var _this;
_classCallCheck(this, BackTop);
_this = _super.apply(this, arguments);
_this.state = {
visible: false
};
_this.scrollToTop = function (e) {
var _this$props = _this.props,
_this$props$target = _this$props.target,
target = _this$props$target === void 0 ? getDefaultTarget : _this$props$target,
onClick = _this$props.onClick;
scrollTo(0, {
getContainer: target
});
if (typeof onClick === 'function') {
onClick(e);
}
};
_this.handleScroll = function () {
var _this$props2 = _this.props,
visibilityHeight = _this$props2.visibilityHeight,
_this$props2$target = _this$props2.target,
target = _this$props2$target === void 0 ? getDefaultTarget : _this$props2$target;
var scrollTop = getScroll(target(), true);
_this.setState({
visible: scrollTop > visibilityHeight
});
};
return _this;
}
_createClass(BackTop, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props$target2 = this.props.target,
target = _this$props$target2 === void 0 ? getDefaultTarget : _this$props$target2;
this.scrollEvent = addEventListener(target(), 'scroll', this.handleScroll);
this.handleScroll();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
}
}, {
key: "setScrollTop",
value: function setScrollTop(value) {
var _this$props$target3 = this.props.target,
target = _this$props$target3 === void 0 ? getDefaultTarget : _this$props$target3;
var targetNode = target();
if (targetNode === window) {
document.body.scrollTop = value;
document.documentElement.scrollTop = value;
} else {
targetNode.scrollTop = value;
}
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
customizePrefixCls = _this$props3.prefixCls,
_this$props3$classNam = _this$props3.className,
className = _this$props3$classNam === void 0 ? '' : _this$props3$classNam,
propsVisible = _this$props3.visible,
children = _this$props3.children;
var getPrefixCls = this.context.getPrefixCls;
var stateVisible = this.state.visible;
var prefixCls = getPrefixCls('back-top', customizePrefixCls);
var classString = classNames(prefixCls, className);
var defaultElement = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-icon")
})); // fix https://fb.me/react-unknown-prop
var divProps = omit(this.props, ['prefixCls', 'className', 'children', 'visibilityHeight', 'target', 'visible']);
var visible = 'visible' in this.props ? propsVisible : stateVisible;
var backTopBtn = visible ? /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
className: classString,
onClick: this.scrollToTop
}), children || defaultElement) : null;
return /*#__PURE__*/React.createElement(Animate, {
component: "",
transitionName: "fade"
}, backTopBtn);
}
}], [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}]);
return BackTop;
}(PureComponent);
export { BackTop as default };
BackTop.displayName = 'BackTop';
BackTop.defaultProps = {
visibilityHeight: 400
};
//# sourceMappingURL=index.js.map