2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-06-23 23:12:18 +08:00
|
|
|
import Animate from 'rc-animate';
|
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
|
|
|
import classNames from 'classnames';
|
2016-09-10 17:17:55 +08:00
|
|
|
import omit from 'omit.js';
|
2018-05-22 13:01:28 +08:00
|
|
|
import raf from 'raf';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import getScroll from '../_util/getScroll';
|
2016-09-13 17:10:08 +08:00
|
|
|
|
2017-07-14 15:37:44 +08:00
|
|
|
const easeInOutCubic = (t: number, b: number, c: number, d: number) => {
|
2016-09-13 17:10:08 +08:00
|
|
|
const cc = c - b;
|
|
|
|
t /= d / 2;
|
|
|
|
if (t < 1) {
|
2018-12-07 20:02:01 +08:00
|
|
|
return (cc / 2) * t * t * t + b;
|
2016-09-13 17:10:08 +08:00
|
|
|
} else {
|
2018-12-07 20:02:01 +08:00
|
|
|
return (cc / 2) * ((t -= 2) * t * t + 2) + b;
|
2016-09-13 17:10:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
function noop() {}
|
2016-10-21 18:02:37 +08:00
|
|
|
|
|
|
|
function getDefaultTarget() {
|
2017-07-24 09:58:56 +08:00
|
|
|
return window;
|
2016-10-21 18:02:37 +08:00
|
|
|
}
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface BackTopProps {
|
2016-07-14 13:29:50 +08:00
|
|
|
visibilityHeight?: number;
|
2019-06-16 20:51:47 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
2016-08-22 17:18:26 +08:00
|
|
|
target?: () => HTMLElement | Window;
|
2016-07-14 13:29:50 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2016-09-19 10:17:07 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-12-05 19:12:18 +08:00
|
|
|
visible?: boolean; // Only for test. Don't use it.
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2016-07-14 13:29:50 +08:00
|
|
|
export default class BackTop extends React.Component<BackTopProps, any> {
|
2016-06-23 23:12:18 +08:00
|
|
|
static defaultProps = {
|
|
|
|
visibilityHeight: 400,
|
2016-07-14 13:29:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
scrollEvent: any;
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2017-07-14 15:37:44 +08:00
|
|
|
constructor(props: BackTopProps) {
|
2016-06-23 23:12:18 +08:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2016-10-14 15:26:22 +08:00
|
|
|
visible: false,
|
2016-06-23 23:12:18 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:20:06 +08:00
|
|
|
getCurrentScrollTop = () => {
|
2017-07-24 09:58:56 +08:00
|
|
|
const getTarget = this.props.target || getDefaultTarget;
|
|
|
|
const targetNode = getTarget();
|
2017-03-30 14:20:06 +08:00
|
|
|
if (targetNode === window) {
|
2018-09-30 16:07:01 +08:00
|
|
|
return window.pageYOffset || document.body.scrollTop || document.documentElement!.scrollTop;
|
2017-03-30 14:20:06 +08:00
|
|
|
}
|
|
|
|
return (targetNode as HTMLElement).scrollTop;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-03-30 14:20:06 +08:00
|
|
|
|
2017-07-14 15:37:44 +08:00
|
|
|
scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
|
2017-03-30 14:20:06 +08:00
|
|
|
const scrollTop = this.getCurrentScrollTop();
|
2016-09-13 17:10:08 +08:00
|
|
|
const startTime = Date.now();
|
|
|
|
const frameFunc = () => {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
const time = timestamp - startTime;
|
|
|
|
this.setScrollTop(easeInOutCubic(time, scrollTop, 0, 450));
|
|
|
|
if (time < 450) {
|
2018-05-22 13:01:28 +08:00
|
|
|
raf(frameFunc);
|
2018-08-11 12:07:14 +08:00
|
|
|
} else {
|
|
|
|
this.setScrollTop(0);
|
2016-09-13 17:10:08 +08:00
|
|
|
}
|
|
|
|
};
|
2018-05-22 13:01:28 +08:00
|
|
|
raf(frameFunc);
|
2016-10-21 18:02:37 +08:00
|
|
|
(this.props.onClick || noop)(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2017-07-14 15:37:44 +08:00
|
|
|
setScrollTop(value: number) {
|
2017-07-24 09:58:56 +08:00
|
|
|
const getTarget = this.props.target || getDefaultTarget;
|
|
|
|
const targetNode = getTarget();
|
2016-08-18 11:28:00 +08:00
|
|
|
if (targetNode === window) {
|
|
|
|
document.body.scrollTop = value;
|
2018-09-30 16:07:01 +08:00
|
|
|
document.documentElement!.scrollTop = value;
|
2016-08-18 11:28:00 +08:00
|
|
|
} else {
|
2016-08-22 17:18:26 +08:00
|
|
|
(targetNode as HTMLElement).scrollTop = value;
|
2016-08-18 11:28:00 +08:00
|
|
|
}
|
2016-06-23 23:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleScroll = () => {
|
2016-10-21 18:02:37 +08:00
|
|
|
const { visibilityHeight, target = getDefaultTarget } = this.props;
|
2016-08-18 11:28:00 +08:00
|
|
|
const scrollTop = getScroll(target(), true);
|
2016-06-23 23:12:18 +08:00
|
|
|
this.setState({
|
2017-03-06 15:48:03 +08:00
|
|
|
visible: scrollTop > (visibilityHeight as number),
|
2016-06-23 23:12:18 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-06-23 23:12:18 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2017-07-24 09:58:56 +08:00
|
|
|
const getTarget = this.props.target || getDefaultTarget;
|
|
|
|
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll);
|
2016-10-14 15:26:22 +08:00
|
|
|
this.handleScroll();
|
2016-06-23 23:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderBackTop = ({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls, className = '', children } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
2016-11-30 10:20:23 +08:00
|
|
|
const classString = classNames(prefixCls, className);
|
2016-06-23 23:12:18 +08:00
|
|
|
|
|
|
|
const defaultElement = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
2017-10-18 17:16:14 +08:00
|
|
|
<div className={`${prefixCls}-icon`} />
|
2016-06-23 23:12:18 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2016-07-08 10:23:20 +08:00
|
|
|
// fix https://fb.me/react-unknown-prop
|
2016-07-14 13:29:50 +08:00
|
|
|
const divProps = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'className',
|
|
|
|
'children',
|
2016-07-08 10:23:20 +08:00
|
|
|
'visibilityHeight',
|
2018-01-06 23:55:15 +08:00
|
|
|
'target',
|
2018-12-05 19:12:18 +08:00
|
|
|
'visible',
|
2016-07-08 10:23:20 +08:00
|
|
|
]);
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const visible = 'visible' in this.props ? this.props.visible : this.state.visible;
|
|
|
|
|
|
|
|
const backTopBtn = visible ? (
|
2016-11-25 12:03:39 +08:00
|
|
|
<div {...divProps} className={classString} onClick={this.scrollToTop}>
|
|
|
|
{children || defaultElement}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
2016-06-23 23:12:18 +08:00
|
|
|
return (
|
2016-07-01 17:54:01 +08:00
|
|
|
<Animate component="" transitionName="fade">
|
2016-11-25 12:03:39 +08:00
|
|
|
{backTopBtn}
|
2016-06-23 23:12:18 +08:00
|
|
|
</Animate>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderBackTop}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-06-23 23:12:18 +08:00
|
|
|
}
|