2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-03-07 19:45:13 +08:00
|
|
|
import addEventListener from '../vc-util/Dom/addEventListener';
|
2019-01-12 11:33:27 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
2019-03-12 12:04:40 +08:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 10:50:19 +08:00
|
|
|
import Base from '../base';
|
2020-01-18 21:34:23 +08:00
|
|
|
import { getListeners } from '../_util/props-util';
|
2020-03-07 19:45:13 +08:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
2018-03-05 19:05:23 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getDefaultTarget() {
|
|
|
|
return window;
|
2018-03-05 19:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const BackTopProps = {
|
|
|
|
visibilityHeight: PropTypes.number,
|
|
|
|
// onClick?: React.MouseEventHandler<any>;
|
|
|
|
target: PropTypes.func,
|
|
|
|
prefixCls: PropTypes.string,
|
2019-03-12 12:04:40 +08:00
|
|
|
// visible: PropTypes.bool, // Only for test. Don't use it.
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-05 19:05:23 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
const BackTop = {
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ABackTop',
|
2018-03-05 19:05:23 +08:00
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
|
|
|
...BackTopProps,
|
|
|
|
visibilityHeight: PropTypes.number.def(400),
|
|
|
|
},
|
2019-03-12 12:04:40 +08:00
|
|
|
inject: {
|
2019-09-11 22:35:25 +08:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-12 12:04:40 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
this.scrollEvent = null;
|
2018-03-05 19:05:23 +08:00
|
|
|
return {
|
|
|
|
visible: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2018-03-05 19:05:23 +08:00
|
|
|
this.$nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const getTarget = this.target || getDefaultTarget;
|
|
|
|
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll);
|
|
|
|
this.handleScroll();
|
|
|
|
});
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
2018-03-05 19:05:23 +08:00
|
|
|
if (this.scrollEvent) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.scrollEvent.remove();
|
2018-03-05 19:05:23 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
getCurrentScrollTop() {
|
|
|
|
const getTarget = this.target || getDefaultTarget;
|
|
|
|
const targetNode = getTarget();
|
2018-03-05 19:05:23 +08:00
|
|
|
if (targetNode === window) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
|
2018-03-05 19:05:23 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return targetNode.scrollTop;
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
scrollToTop(e) {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { target = getDefaultTarget } = this;
|
|
|
|
scrollTo(0, {
|
|
|
|
getContainer: target,
|
|
|
|
});
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('click', e);
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
handleScroll() {
|
|
|
|
const { visibilityHeight, target = getDefaultTarget } = this;
|
|
|
|
const scrollTop = getScroll(target(), true);
|
2018-03-05 19:05:23 +08:00
|
|
|
this.setState({
|
|
|
|
visible: scrollTop > visibilityHeight,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-01-18 21:34:23 +08:00
|
|
|
const { prefixCls: customizePrefixCls, $slots } = this;
|
2019-03-12 12:04:40 +08:00
|
|
|
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-12 12:04:40 +08:00
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
2018-03-05 19:05:23 +08:00
|
|
|
|
|
|
|
const defaultElement = (
|
|
|
|
<div class={`${prefixCls}-content`}>
|
|
|
|
<div class={`${prefixCls}-icon`} />
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-05 19:05:23 +08:00
|
|
|
const divProps = {
|
|
|
|
on: {
|
2020-01-18 21:34:23 +08:00
|
|
|
...getListeners(this),
|
2018-03-05 19:05:23 +08:00
|
|
|
click: this.scrollToTop,
|
|
|
|
},
|
|
|
|
class: prefixCls,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-05 19:05:23 +08:00
|
|
|
|
|
|
|
const backTopBtn = this.visible ? (
|
2019-01-12 11:33:27 +08:00
|
|
|
<div {...divProps}>{$slots.default || defaultElement}</div>
|
|
|
|
) : null;
|
|
|
|
const transitionProps = getTransitionProps('fade');
|
|
|
|
return <transition {...transitionProps}>{backTopBtn}</transition>;
|
2018-03-05 19:05:23 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-05 19:05:23 +08:00
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
/* istanbul ignore next */
|
2019-01-12 11:33:27 +08:00
|
|
|
BackTop.install = function(Vue) {
|
2019-08-28 10:50:19 +08:00
|
|
|
Vue.use(Base);
|
2019-01-12 11:33:27 +08:00
|
|
|
Vue.component(BackTop.name, BackTop);
|
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default BackTop;
|