UNPKG

3.49 kBJavaScriptView Raw
1"use client";
2
3import * as React from 'react';
4import VerticalAlignTopOutlined from "@ant-design/icons/es/icons/VerticalAlignTopOutlined";
5import classNames from 'classnames';
6import CSSMotion from 'rc-motion';
7import omit from "rc-util/es/omit";
8import getScroll from '../_util/getScroll';
9import { cloneElement } from '../_util/reactNode';
10import scrollTo from '../_util/scrollTo';
11import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
12import { devUseWarning } from '../_util/warning';
13import { ConfigContext } from '../config-provider';
14import useStyle from './style';
15const 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 // fix https://fb.me/react-unknown-prop
69 const divProps = omit(props, ['prefixCls', 'className', 'rootClassName', 'children', 'visibilityHeight', 'target']);
70 const defaultElement = /*#__PURE__*/React.createElement("div", {
71 className: `${prefixCls}-content`
72 }, /*#__PURE__*/React.createElement("div", {
73 className: `${prefixCls}-icon`
74 }, /*#__PURE__*/React.createElement(VerticalAlignTopOutlined, null)));
75 return wrapCSSVar(/*#__PURE__*/React.createElement("div", Object.assign({}, divProps, {
76 className: classString,
77 onClick: scrollToTop,
78 ref: ref
79 }), /*#__PURE__*/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};
96if (process.env.NODE_ENV !== 'production') {
97 BackTop.displayName = 'BackTop';
98}
99export default BackTop;
\No newline at end of file