mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 22:48:29 +08:00
7ab95b7eae
- typescript upgrade to 2.2.1
131 lines
3.2 KiB
TypeScript
Executable File
131 lines
3.2 KiB
TypeScript
Executable File
import React from 'react';
|
|
import Animate from 'rc-animate';
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
|
import classNames from 'classnames';
|
|
import omit from 'omit.js';
|
|
import Icon from '../icon';
|
|
import getScroll from '../_util/getScroll';
|
|
import getRequestAnimationFrame from '../_util/getRequestAnimationFrame';
|
|
|
|
const reqAnimFrame = getRequestAnimationFrame();
|
|
|
|
const currentScrollTop = () => {
|
|
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
|
|
};
|
|
|
|
const easeInOutCubic = (t, b, c, d) => {
|
|
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;
|
|
}
|
|
};
|
|
|
|
function noop() {}
|
|
|
|
function getDefaultTarget() {
|
|
return typeof window !== 'undefined' ?
|
|
window : null;
|
|
}
|
|
|
|
export interface BackTopProps {
|
|
visibilityHeight?: number;
|
|
onClick?: React.MouseEventHandler<any>;
|
|
target?: () => HTMLElement | Window;
|
|
prefixCls?: string;
|
|
className?: string;
|
|
style?: React.CSSProperties;
|
|
}
|
|
|
|
export default class BackTop extends React.Component<BackTopProps, any> {
|
|
static defaultProps = {
|
|
visibilityHeight: 400,
|
|
};
|
|
|
|
scrollEvent: any;
|
|
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = {
|
|
visible: false,
|
|
};
|
|
}
|
|
|
|
scrollToTop = (e) => {
|
|
const scrollTop = currentScrollTop();
|
|
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);
|
|
(this.props.onClick || noop)(e);
|
|
}
|
|
|
|
setScrollTop(value) {
|
|
const targetNode = (this.props.target || getDefaultTarget)();
|
|
if (targetNode === window) {
|
|
document.body.scrollTop = value;
|
|
document.documentElement.scrollTop = value;
|
|
} else {
|
|
(targetNode as HTMLElement).scrollTop = value;
|
|
}
|
|
}
|
|
|
|
handleScroll = () => {
|
|
const { visibilityHeight, target = getDefaultTarget } = this.props;
|
|
const scrollTop = getScroll(target(), true);
|
|
this.setState({
|
|
visible: scrollTop > (visibilityHeight as number),
|
|
});
|
|
}
|
|
|
|
componentDidMount() {
|
|
this.handleScroll();
|
|
this.scrollEvent = addEventListener((this.props.target || getDefaultTarget)(), 'scroll', this.handleScroll);
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
if (this.scrollEvent) {
|
|
this.scrollEvent.remove();
|
|
}
|
|
}
|
|
|
|
render() {
|
|
const { prefixCls = 'ant-back-top', className = '', children } = this.props;
|
|
const classString = classNames(prefixCls, className);
|
|
|
|
const defaultElement = (
|
|
<div className={`${prefixCls}-content`}>
|
|
<Icon className={`${prefixCls}-icon`} type="to-top" />
|
|
</div>
|
|
);
|
|
|
|
// fix https://fb.me/react-unknown-prop
|
|
const divProps = omit(this.props, [
|
|
'prefixCls',
|
|
'className',
|
|
'children',
|
|
'visibilityHeight',
|
|
]);
|
|
|
|
const backTopBtn = this.state.visible ? (
|
|
<div {...divProps} className={classString} onClick={this.scrollToTop}>
|
|
{children || defaultElement}
|
|
</div>
|
|
) : null;
|
|
|
|
return (
|
|
<Animate component="" transitionName="fade">
|
|
{backTopBtn}
|
|
</Animate>
|
|
);
|
|
}
|
|
}
|