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

131 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 VerticalAlignTopOutlined from '@ant-design/icons/VerticalAlignTopOutlined';
import { throttleByAnimationFrame } from '../_util/throttleByAnimationFrame';
import { ConfigContext } from '../config-provider';
import getScroll from '../_util/getScroll';
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;
onClick?: React.MouseEventHandler<HTMLElement>;
target?: () => HTMLElement | Window | Document;
2016-07-14 13:29:50 +08:00
prefixCls?: string;
2020-05-18 14:00:19 +08:00
children?: React.ReactNode;
2016-07-14 13:29:50 +08:00
className?: string;
style?: React.CSSProperties;
2020-06-29 11:08:39 +08:00
duration?: number;
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
const BackTop: React.FC<BackTopProps> = props => {
const [visible, setVisible] = React.useState(false);
const ref = React.createRef<HTMLDivElement>();
const scrollEvent = React.useRef<any>();
2019-08-05 18:38:10 +08:00
const getDefaultTarget = () => {
return ref.current && ref.current.ownerDocument ? ref.current.ownerDocument : window;
};
const handleScroll = throttleByAnimationFrame(
(e: React.UIEvent<HTMLElement> | { target: any }) => {
const { visibilityHeight } = props;
const scrollTop = getScroll(e.target, true);
setVisible(scrollTop > visibilityHeight!);
},
);
const bindScrollEvent = () => {
const { target } = props;
const getTarget = target || getDefaultTarget;
const container = getTarget();
scrollEvent.current = addEventListener(container, 'scroll', (e: React.UIEvent<HTMLElement>) => {
handleScroll(e);
});
handleScroll({
target: container,
});
};
React.useEffect(() => {
bindScrollEvent();
return () => {
if (scrollEvent.current) {
scrollEvent.current.remove();
}
(handleScroll as any).cancel();
};
}, [props.target]);
const getVisible = () => {
if ('visible' in props) {
return props.visible;
}
return visible;
};
const scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
2020-06-29 11:08:39 +08:00
const { onClick, target, duration = 450 } = props;
2019-07-30 12:34:12 +08:00
scrollTo(0, {
getContainer: target || getDefaultTarget,
2020-06-29 11:08:39 +08:00
duration,
});
if (typeof onClick === 'function') {
onClick(e);
}
2018-12-07 20:02:01 +08:00
};
2016-06-23 23:12:18 +08:00
const renderChildren = ({ prefixCls }: { prefixCls: string }) => {
const { children } = props;
2016-06-23 23:12:18 +08:00
const defaultElement = (
<div className={`${prefixCls}-content`}>
<div className={`${prefixCls}-icon`}>
<VerticalAlignTopOutlined />
</div>
2016-06-23 23:12:18 +08:00
</div>
);
return (
<Animate component="" transitionName="fade">
{getVisible() ? <div>{children || defaultElement}</div> : null}
</Animate>
);
2018-12-07 20:02:01 +08:00
};
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const { prefixCls: customizePrefixCls, className = '' } = props;
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
const classString = classNames(
prefixCls,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
// fix https://fb.me/react-unknown-prop
const divProps = omit(props, [
'prefixCls',
'className',
'children',
'visibilityHeight',
'target',
'visible',
]);
return (
<div {...divProps} className={classString} onClick={scrollToTop} ref={ref}>
{renderChildren({ prefixCls })}
</div>
);
};
BackTop.defaultProps = {
visibilityHeight: 400,
};
export default React.memo(BackTop);