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';
|
2020-04-01 17:38:21 +08:00
|
|
|
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
|
|
|
import getScroll from '../_util/getScroll';
|
2019-07-24 22:56:20 +08:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
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>;
|
2020-04-01 17:38:21 +08:00
|
|
|
target?: () => HTMLElement | Window | Document;
|
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
|
|
|
};
|
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
state = {
|
|
|
|
visible: false,
|
|
|
|
};
|
|
|
|
|
2016-07-14 13:29:50 +08:00
|
|
|
scrollEvent: any;
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
node: HTMLDivElement;
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
componentDidMount() {
|
2020-04-01 17:38:21 +08:00
|
|
|
this.bindScrollEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: BackTopProps) {
|
|
|
|
const { target } = this.props;
|
|
|
|
if (prevProps.target !== target) {
|
|
|
|
this.bindScrollEvent();
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
}
|
2020-04-01 17:38:21 +08:00
|
|
|
(this.handleScroll as any).cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindScrollEvent() {
|
|
|
|
if (this.scrollEvent) {
|
|
|
|
this.scrollEvent.remove();
|
|
|
|
}
|
|
|
|
const { target } = this.props;
|
|
|
|
const getTarget = target || this.getDefaultTarget;
|
|
|
|
const container = getTarget();
|
|
|
|
this.scrollEvent = addEventListener(container, 'scroll', (e: React.UIEvent<HTMLElement>) => {
|
|
|
|
this.handleScroll(e);
|
|
|
|
});
|
|
|
|
this.handleScroll({
|
|
|
|
target: container,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getVisible() {
|
|
|
|
if ('visible' in this.props) {
|
|
|
|
return this.props.visible;
|
|
|
|
}
|
|
|
|
return this.state.visible;
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
getDefaultTarget = () => {
|
|
|
|
return this.node && this.node.ownerDocument ? this.node.ownerDocument : window;
|
|
|
|
};
|
|
|
|
|
|
|
|
saveDivRef = (node: HTMLDivElement) => {
|
|
|
|
this.node = node;
|
|
|
|
};
|
|
|
|
|
2017-07-14 15:37:44 +08:00
|
|
|
scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
|
2020-04-01 17:38:21 +08:00
|
|
|
const { onClick, target } = this.props;
|
2019-07-30 12:34:12 +08:00
|
|
|
scrollTo(0, {
|
2020-04-01 17:38:21 +08:00
|
|
|
getContainer: target || this.getDefaultTarget,
|
2019-07-24 22:56:20 +08:00
|
|
|
});
|
2019-08-22 18:04:10 +08:00
|
|
|
if (typeof onClick === 'function') {
|
|
|
|
onClick(e);
|
2019-07-24 22:56:20 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
@throttleByAnimationFrameDecorator()
|
|
|
|
handleScroll(e: React.UIEvent<HTMLElement> | { target: any }) {
|
|
|
|
const { visibilityHeight = 0 } = this.props;
|
|
|
|
const scrollTop = getScroll(e.target, true);
|
2016-06-23 23:12:18 +08:00
|
|
|
this.setState({
|
2020-04-01 17:38:21 +08:00
|
|
|
visible: scrollTop > visibilityHeight,
|
2020-01-02 19:10:16 +08:00
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
}
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2020-04-01 17:38:21 +08:00
|
|
|
renderChildren({ prefixCls }: { prefixCls: string }) {
|
|
|
|
const { children } = this.props;
|
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>
|
|
|
|
);
|
2020-04-01 17:38:21 +08:00
|
|
|
return (
|
|
|
|
<Animate component="" transitionName="fade">
|
|
|
|
{this.getVisible() ? <div>{children || defaultElement}</div> : null}
|
|
|
|
</Animate>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderBackTop = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
|
|
|
const { prefixCls: customizePrefixCls, className = '' } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
|
|
|
const classString = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2016-06-23 23:12:18 +08:00
|
|
|
|
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
|
|
|
]);
|
|
|
|
|
2016-06-23 23:12:18 +08:00
|
|
|
return (
|
2020-04-01 17:38:21 +08:00
|
|
|
<div {...divProps} className={classString} onClick={this.scrollToTop} ref={this.saveDivRef}>
|
|
|
|
{this.renderChildren({ prefixCls })}
|
|
|
|
</div>
|
2016-06-23 23:12:18 +08:00
|
|
|
);
|
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
|
|
|
}
|