ant-design/components/back-top/index.tsx

136 lines
3.5 KiB
TypeScript
Raw Normal View History

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';
import omit from 'omit.js';
import getScroll from '../_util/getScroll';
import getRequestAnimationFrame from '../_util/getRequestAnimationFrame';
2016-06-23 23:12:18 +08:00
const reqAnimFrame = getRequestAnimationFrame();
2017-07-14 15:37:44 +08:00
const easeInOutCubic = (t: number, b: number, c: number, d: number) => {
const 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;
}
};
2017-07-14 15:37:44 +08:00
function noop() { }
2016-10-21 18:02:37 +08:00
function getDefaultTarget() {
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;
2016-10-19 17:51:33 +08:00
onClick?: React.MouseEventHandler<any>;
2016-08-22 17:18:26 +08:00
target?: () => HTMLElement | Window;
2016-07-14 13:29:50 +08:00
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
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 = {
visible: false,
2016-06-23 23:12:18 +08:00
};
}
getCurrentScrollTop = () => {
const getTarget = this.props.target || getDefaultTarget;
const targetNode = getTarget();
if (targetNode === window) {
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
}
return (targetNode as HTMLElement).scrollTop;
}
2017-07-14 15:37:44 +08:00
scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
const scrollTop = this.getCurrentScrollTop();
const startTime = Date.now();
const frameFunc = () => {
const timestamp = Date.now();
const time = timestamp - startTime;
this.setScrollTop(easeInOutCubic(time, scrollTop, 0, 450));
if (time < 450) {
reqAnimFrame(frameFunc);
}
};
reqAnimFrame(frameFunc);
2016-10-21 18:02:37 +08:00
(this.props.onClick || noop)(e);
2016-06-23 23:12:18 +08:00
}
2017-07-14 15:37:44 +08:00
setScrollTop(value: number) {
const getTarget = this.props.target || getDefaultTarget;
const targetNode = getTarget();
if (targetNode === window) {
document.body.scrollTop = value;
document.documentElement.scrollTop = value;
} else {
2016-08-22 17:18:26 +08:00
(targetNode as HTMLElement).scrollTop = value;
}
2016-06-23 23:12:18 +08:00
}
handleScroll = () => {
2016-10-21 18:02:37 +08:00
const { visibilityHeight, target = getDefaultTarget } = this.props;
const scrollTop = getScroll(target(), true);
2016-06-23 23:12:18 +08:00
this.setState({
visible: scrollTop > (visibilityHeight as number),
2016-06-23 23:12:18 +08:00
});
}
componentDidMount() {
const getTarget = this.props.target || getDefaultTarget;
this.scrollEvent = addEventListener(getTarget(), 'scroll', this.handleScroll);
this.handleScroll();
2016-06-23 23:12:18 +08:00
}
componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
}
render() {
2016-10-21 18:02:37 +08:00
const { prefixCls = 'ant-back-top', className = '', children } = this.props;
const classString = classNames(prefixCls, className);
2016-06-23 23:12:18 +08:00
const defaultElement = (
<div className={`${prefixCls}-content`}>
<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',
]);
const backTopBtn = this.state.visible ? (
<div {...divProps} className={classString} onClick={this.scrollToTop}>
{children || defaultElement}
</div>
) : null;
2016-06-23 23:12:18 +08:00
return (
<Animate component="" transitionName="fade">
{backTopBtn}
2016-06-23 23:12:18 +08:00
</Animate>
);
}
}