2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
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';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
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';
|
2022-11-19 13:45:56 +08:00
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-07-15 12:57:03 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-04-20 19:36:59 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2016-09-19 10:17:07 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-06-29 11:08:39 +08:00
|
|
|
duration?: number;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2022-11-19 13:45:56 +08:00
|
|
|
const BackTop: React.FC<BackTopProps> = (props) => {
|
2022-11-24 14:59:17 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
className,
|
|
|
|
rootClassName,
|
2022-11-24 14:59:17 +08:00
|
|
|
visibilityHeight = 400,
|
|
|
|
target,
|
|
|
|
onClick,
|
|
|
|
duration = 450,
|
|
|
|
} = props;
|
|
|
|
const [visible, setVisible] = React.useState<boolean>(visibilityHeight === 0);
|
|
|
|
|
|
|
|
const ref = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const getDefaultTarget = (): HTMLElement | Document | Window =>
|
2024-06-22 21:59:12 +08:00
|
|
|
ref.current?.ownerDocument || window;
|
2020-04-01 17:38:21 +08:00
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
const handleScroll = throttleByAnimationFrame(
|
2022-11-24 14:59:17 +08:00
|
|
|
(e: React.UIEvent<HTMLElement, UIEvent> | { target: any }) => {
|
2024-06-15 15:39:01 +08:00
|
|
|
const scrollTop = getScroll(e.target);
|
2022-11-24 14:59:17 +08:00
|
|
|
setVisible(scrollTop >= visibilityHeight);
|
2020-05-16 13:06:11 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-09-19 09:55:11 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('BackTop');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(false, 'BackTop', 'FloatButton.BackTop');
|
2022-09-19 09:55:11 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
React.useEffect(() => {
|
2022-12-30 17:15:54 +08:00
|
|
|
const getTarget = target || getDefaultTarget;
|
|
|
|
const container = getTarget();
|
|
|
|
handleScroll({ target: container });
|
|
|
|
container?.addEventListener('scroll', handleScroll);
|
2020-05-16 13:06:11 +08:00
|
|
|
return () => {
|
2022-09-06 08:55:38 +08:00
|
|
|
handleScroll.cancel();
|
2022-12-30 17:15:54 +08:00
|
|
|
container?.removeEventListener('scroll', handleScroll);
|
2020-05-16 13:06:11 +08:00
|
|
|
};
|
2022-11-24 14:59:17 +08:00
|
|
|
}, [target]);
|
2020-05-16 13:06:11 +08:00
|
|
|
|
|
|
|
const scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
|
2022-11-24 14:59:17 +08:00
|
|
|
scrollTo(0, { getContainer: target || getDefaultTarget, duration });
|
|
|
|
onClick?.(e);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2016-06-23 23:12:18 +08:00
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
|
2020-05-16 13:06:11 +08:00
|
|
|
const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
2023-11-14 10:39:49 +08:00
|
|
|
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2023-11-14 10:39:49 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-04-20 19:36:59 +08:00
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const classString = classNames(
|
2022-04-20 19:36:59 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2020-09-06 13:07:39 +08:00
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2020-05-16 13:06:11 +08:00
|
|
|
|
|
|
|
// fix https://fb.me/react-unknown-prop
|
|
|
|
const divProps = omit(props, [
|
|
|
|
'prefixCls',
|
|
|
|
'className',
|
2023-01-20 11:03:50 +08:00
|
|
|
'rootClassName',
|
2020-05-16 13:06:11 +08:00
|
|
|
'children',
|
|
|
|
'visibilityHeight',
|
|
|
|
'target',
|
|
|
|
]);
|
|
|
|
|
2022-11-24 14:59:17 +08:00
|
|
|
const defaultElement = (
|
|
|
|
<div className={`${prefixCls}-content`}>
|
|
|
|
<div className={`${prefixCls}-icon`}>
|
|
|
|
<VerticalAlignTopOutlined />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2023-11-14 10:39:49 +08:00
|
|
|
return wrapCSSVar(
|
2020-05-16 13:06:11 +08:00
|
|
|
<div {...divProps} className={classString} onClick={scrollToTop} ref={ref}>
|
2022-11-24 14:59:17 +08:00
|
|
|
<CSSMotion visible={visible} motionName={`${rootPrefixCls}-fade`}>
|
|
|
|
{({ className: motionClassName }) =>
|
|
|
|
cloneElement(props.children || defaultElement, ({ className: cloneCls }) => ({
|
|
|
|
className: classNames(motionClassName, cloneCls),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
</CSSMotion>
|
2022-04-20 19:36:59 +08:00
|
|
|
</div>,
|
2020-05-16 13:06:11 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
BackTop.displayName = 'BackTop';
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
export default BackTop;
|