mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
775d1800bb
* chore(deps-dev): bump eslint-config-prettier from 6.15.0 to 7.0.0 Bumps [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) from 6.15.0 to 7.0.0. - [Release notes](https://github.com/prettier/eslint-config-prettier/releases) - [Changelog](https://github.com/prettier/eslint-config-prettier/blob/main/CHANGELOG.md) - [Commits](https://github.com/prettier/eslint-config-prettier/compare/v6.15.0...v7.0.0) Signed-off-by: dependabot[bot] <support@github.com> * chore: fix eslint style * chore: prettier code style Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: afc163 <afc163@gmail.com>
136 lines
3.8 KiB
TypeScript
136 lines
3.8 KiB
TypeScript
import * as React from 'react';
|
|
import CSSMotion from 'rc-motion';
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
|
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';
|
|
import { cloneElement } from '../_util/reactNode';
|
|
|
|
export interface BackTopProps {
|
|
visibilityHeight?: number;
|
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
|
target?: () => HTMLElement | Window | Document;
|
|
prefixCls?: string;
|
|
children?: React.ReactNode;
|
|
className?: string;
|
|
style?: React.CSSProperties;
|
|
duration?: number;
|
|
visible?: boolean; // Only for test. Don't use it.
|
|
}
|
|
|
|
const BackTop: React.FC<BackTopProps> = props => {
|
|
const [visible, setVisible] = useMergedState(false, {
|
|
value: props.visible,
|
|
});
|
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
|
const scrollEvent = React.useRef<any>();
|
|
|
|
const getDefaultTarget = () =>
|
|
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 scrollToTop = (e: React.MouseEvent<HTMLDivElement>) => {
|
|
const { onClick, target, duration = 450 } = props;
|
|
scrollTo(0, {
|
|
getContainer: target || getDefaultTarget,
|
|
duration,
|
|
});
|
|
if (typeof onClick === 'function') {
|
|
onClick(e);
|
|
}
|
|
};
|
|
|
|
const renderChildren = ({ prefixCls }: { prefixCls: string }) => {
|
|
const { children } = props;
|
|
const defaultElement = (
|
|
<div className={`${prefixCls}-content`}>
|
|
<div className={`${prefixCls}-icon`}>
|
|
<VerticalAlignTopOutlined />
|
|
</div>
|
|
</div>
|
|
);
|
|
return (
|
|
<CSSMotion visible={visible} motionName="fade" removeOnLeave>
|
|
{({ className: motionClassName }) => {
|
|
const childNode = children || defaultElement;
|
|
return (
|
|
<div>
|
|
{cloneElement(childNode, ({ className }) => ({
|
|
className: classNames(motionClassName, className),
|
|
}))}
|
|
</div>
|
|
);
|
|
}}
|
|
</CSSMotion>
|
|
);
|
|
};
|
|
|
|
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);
|