2022-06-22 14:57:09 +08:00
|
|
|
import VerticalAlignTopOutlined from '@ant-design/icons/VerticalAlignTopOutlined';
|
|
|
|
import classNames from 'classnames';
|
2020-11-17 23:03:07 +08:00
|
|
|
import CSSMotion from 'rc-motion';
|
2016-06-23 23:12:18 +08:00
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
2020-11-17 23:03:07 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2022-06-22 14:57:09 +08:00
|
|
|
import * as React from 'react';
|
2020-05-16 13:06:11 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-12-05 19:12:18 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
2020-11-17 23:03:07 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-06-22 14:57:09 +08:00
|
|
|
import scrollTo from '../_util/scrollTo';
|
|
|
|
import { throttleByAnimationFrame } from '../_util/throttleByAnimationFrame';
|
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;
|
2020-05-18 14:00:19 +08:00
|
|
|
children?: React.ReactNode;
|
2016-07-14 13:29:50 +08:00
|
|
|
className?: string;
|
2016-09-19 10:17:07 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-06-29 11:08:39 +08:00
|
|
|
duration?: number;
|
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
|
|
|
|
2022-09-06 08:55:38 +08:00
|
|
|
interface ChildrenProps {
|
|
|
|
prefixCls: string;
|
|
|
|
rootPrefixCls: string;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
visible?: boolean; // Only for test. Don't use it.
|
|
|
|
}
|
|
|
|
|
|
|
|
const BackTopContent: React.FC<ChildrenProps> = props => {
|
|
|
|
const { prefixCls, rootPrefixCls, children, visible } = props;
|
|
|
|
const defaultElement = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
|
|
|
<div className={`${prefixCls}-icon`}>
|
|
|
|
<VerticalAlignTopOutlined />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<CSSMotion visible={visible} motionName={`${rootPrefixCls}-fade`}>
|
|
|
|
{({ className: motionClassName }) =>
|
|
|
|
cloneElement(children || defaultElement, ({ className }) => ({
|
|
|
|
className: classNames(motionClassName, className),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
</CSSMotion>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
const BackTop: React.FC<BackTopProps> = props => {
|
2020-11-17 23:03:07 +08:00
|
|
|
const [visible, setVisible] = useMergedState(false, {
|
|
|
|
value: props.visible,
|
|
|
|
});
|
2020-04-01 17:38:21 +08:00
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
2022-11-04 18:36:54 +08:00
|
|
|
const scrollEvent = React.useRef<ReturnType<typeof addEventListener>>(null);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const getDefaultTarget = () =>
|
|
|
|
ref.current && ref.current.ownerDocument ? ref.current.ownerDocument : window;
|
2020-04-01 17:38:21 +08:00
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
const handleScroll = throttleByAnimationFrame(
|
|
|
|
(e: React.UIEvent<HTMLElement> | { target: any }) => {
|
2022-09-23 16:11:52 +08:00
|
|
|
const { visibilityHeight = 400 } = props;
|
2020-05-16 13:06:11 +08:00
|
|
|
const scrollTop = getScroll(e.target, true);
|
2022-09-23 16:11:52 +08:00
|
|
|
setVisible(scrollTop > visibilityHeight);
|
2020-05-16 13:06:11 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const bindScrollEvent = () => {
|
|
|
|
const { target } = props;
|
|
|
|
const getTarget = target || getDefaultTarget;
|
2020-04-01 17:38:21 +08:00
|
|
|
const container = getTarget();
|
2020-05-16 13:06:11 +08:00
|
|
|
scrollEvent.current = addEventListener(container, 'scroll', (e: React.UIEvent<HTMLElement>) => {
|
|
|
|
handleScroll(e);
|
2020-04-01 17:38:21 +08:00
|
|
|
});
|
2022-09-06 08:55:38 +08:00
|
|
|
handleScroll({ target: container });
|
2020-04-01 17:38:21 +08:00
|
|
|
};
|
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
bindScrollEvent();
|
|
|
|
return () => {
|
|
|
|
if (scrollEvent.current) {
|
|
|
|
scrollEvent.current.remove();
|
|
|
|
}
|
2022-09-06 08:55:38 +08:00
|
|
|
handleScroll.cancel();
|
2020-05-16 13:06:11 +08:00
|
|
|
};
|
|
|
|
}, [props.target]);
|
|
|
|
|
|
|
|
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, {
|
2020-05-16 13:06:11 +08:00
|
|
|
getContainer: target || getDefaultTarget,
|
2020-06-29 11:08:39 +08:00
|
|
|
duration,
|
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-05-16 13:06:11 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const { prefixCls: customizePrefixCls, className = '' } = props;
|
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-09-06 13:07:39 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-05-16 13:06:11 +08:00
|
|
|
|
|
|
|
// 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}>
|
2022-09-06 08:55:38 +08:00
|
|
|
<BackTopContent prefixCls={prefixCls} rootPrefixCls={rootPrefixCls} visible={visible}>
|
|
|
|
{props.children}
|
|
|
|
</BackTopContent>
|
2020-05-16 13:06:11 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(BackTop);
|