index.js 3.3 KB

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