1 | "use client";
|
2 |
|
3 | import * as React from 'react';
|
4 | import VerticalAlignTopOutlined from "@ant-design/icons/es/icons/VerticalAlignTopOutlined";
|
5 | import classNames from 'classnames';
|
6 | import CSSMotion from 'rc-motion';
|
7 | import omit from "rc-util/es/omit";
|
8 | import getScroll from '../_util/getScroll';
|
9 | import { cloneElement } from '../_util/reactNode';
|
10 | import scrollTo from '../_util/scrollTo';
|
11 | import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
12 | import { devUseWarning } from '../_util/warning';
|
13 | import { ConfigContext } from '../config-provider';
|
14 | import useStyle from './style';
|
15 | const BackTop = props => {
|
16 | const {
|
17 | prefixCls: customizePrefixCls,
|
18 | className,
|
19 | rootClassName,
|
20 | visibilityHeight = 400,
|
21 | target,
|
22 | onClick,
|
23 | duration = 450
|
24 | } = props;
|
25 | const [visible, setVisible] = React.useState(visibilityHeight === 0);
|
26 | const ref = React.useRef(null);
|
27 | const getDefaultTarget = () => {
|
28 | var _a;
|
29 | return ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.ownerDocument) || window;
|
30 | };
|
31 | const handleScroll = throttleByAnimationFrame(e => {
|
32 | const scrollTop = getScroll(e.target);
|
33 | setVisible(scrollTop >= visibilityHeight);
|
34 | });
|
35 | if (process.env.NODE_ENV !== 'production') {
|
36 | const warning = devUseWarning('BackTop');
|
37 | warning.deprecated(false, 'BackTop', 'FloatButton.BackTop');
|
38 | }
|
39 | React.useEffect(() => {
|
40 | const getTarget = target || getDefaultTarget;
|
41 | const container = getTarget();
|
42 | handleScroll({
|
43 | target: container
|
44 | });
|
45 | container === null || container === void 0 ? void 0 : container.addEventListener('scroll', handleScroll);
|
46 | return () => {
|
47 | handleScroll.cancel();
|
48 | container === null || container === void 0 ? void 0 : container.removeEventListener('scroll', handleScroll);
|
49 | };
|
50 | }, [target]);
|
51 | const scrollToTop = e => {
|
52 | scrollTo(0, {
|
53 | getContainer: target || getDefaultTarget,
|
54 | duration
|
55 | });
|
56 | onClick === null || onClick === void 0 ? void 0 : onClick(e);
|
57 | };
|
58 | const {
|
59 | getPrefixCls,
|
60 | direction
|
61 | } = React.useContext(ConfigContext);
|
62 | const prefixCls = getPrefixCls('back-top', customizePrefixCls);
|
63 | const rootPrefixCls = getPrefixCls();
|
64 | const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
65 | const classString = classNames(hashId, cssVarCls, prefixCls, {
|
66 | [`${prefixCls}-rtl`]: direction === 'rtl'
|
67 | }, className, rootClassName);
|
68 |
|
69 | const divProps = omit(props, ['prefixCls', 'className', 'rootClassName', 'children', 'visibilityHeight', 'target']);
|
70 | const defaultElement = React.createElement("div", {
|
71 | className: `${prefixCls}-content`
|
72 | }, React.createElement("div", {
|
73 | className: `${prefixCls}-icon`
|
74 | }, React.createElement(VerticalAlignTopOutlined, null)));
|
75 | return wrapCSSVar(React.createElement("div", Object.assign({}, divProps, {
|
76 | className: classString,
|
77 | onClick: scrollToTop,
|
78 | ref: ref
|
79 | }), React.createElement(CSSMotion, {
|
80 | visible: visible,
|
81 | motionName: `${rootPrefixCls}-fade`
|
82 | }, _ref => {
|
83 | let {
|
84 | className: motionClassName
|
85 | } = _ref;
|
86 | return cloneElement(props.children || defaultElement, _ref2 => {
|
87 | let {
|
88 | className: cloneCls
|
89 | } = _ref2;
|
90 | return {
|
91 | className: classNames(motionClassName, cloneCls)
|
92 | };
|
93 | });
|
94 | })));
|
95 | };
|
96 | if (process.env.NODE_ENV !== 'production') {
|
97 | BackTop.displayName = 'BackTop';
|
98 | }
|
99 | export default BackTop; |
\ | No newline at end of file |