1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
3 | var __rest = this && this.__rest || function (s, e) {
|
4 | var t = {};
|
5 | for (var p in s) {
|
6 | if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
7 | }
|
8 | if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
9 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
10 | }
|
11 | return t;
|
12 | };
|
13 | import VerticalAlignTopOutlined from "@ant-design/icons/es/icons/VerticalAlignTopOutlined";
|
14 | import classNames from 'classnames';
|
15 | import CSSMotion from 'rc-motion';
|
16 | import addEventListener from "rc-util/es/Dom/addEventListener";
|
17 | import useMergedState from "rc-util/es/hooks/useMergedState";
|
18 | import React, { memo, useContext, useEffect, useRef } from 'react';
|
19 | import FloatButton, { floatButtonPrefixCls } from './FloatButton';
|
20 | import { ConfigContext } from '../config-provider';
|
21 | import getScroll from '../_util/getScroll';
|
22 | import scrollTo from '../_util/scrollTo';
|
23 | import { throttleByAnimationFrame } from '../_util/throttleByAnimationFrame';
|
24 | import FloatButtonGroupContext from './context';
|
25 | import useStyle from './style';
|
26 | var BackTop = function BackTop(props) {
|
27 | var customizePrefixCls = props.prefixCls,
|
28 | _props$className = props.className,
|
29 | className = _props$className === void 0 ? '' : _props$className,
|
30 | _props$type = props.type,
|
31 | type = _props$type === void 0 ? 'default' : _props$type,
|
32 | _props$shape = props.shape,
|
33 | shape = _props$shape === void 0 ? 'circle' : _props$shape,
|
34 | _props$visibilityHeig = props.visibilityHeight,
|
35 | visibilityHeight = _props$visibilityHeig === void 0 ? 400 : _props$visibilityHeig,
|
36 | _props$icon = props.icon,
|
37 | icon = _props$icon === void 0 ? React.createElement(VerticalAlignTopOutlined, null) : _props$icon,
|
38 | target = props.target,
|
39 | onClick = props.onClick,
|
40 | _props$duration = props.duration,
|
41 | duration = _props$duration === void 0 ? 450 : _props$duration,
|
42 | restProps = __rest(props, ["prefixCls", "className", "type", "shape", "visibilityHeight", "icon", "target", "onClick", "duration"]);
|
43 | var _useMergedState = useMergedState(false, {
|
44 | value: props.visible
|
45 | }),
|
46 | _useMergedState2 = _slicedToArray(_useMergedState, 2),
|
47 | visible = _useMergedState2[0],
|
48 | setVisible = _useMergedState2[1];
|
49 | var ref = useRef(null);
|
50 | var scrollEvent = useRef(null);
|
51 | var getDefaultTarget = function getDefaultTarget() {
|
52 | return ref.current && ref.current.ownerDocument ? ref.current.ownerDocument : window;
|
53 | };
|
54 | var handleScroll = throttleByAnimationFrame(function (e) {
|
55 | var scrollTop = getScroll(e.target, true);
|
56 | setVisible(scrollTop > visibilityHeight);
|
57 | });
|
58 | var bindScrollEvent = function bindScrollEvent() {
|
59 | var getTarget = target || getDefaultTarget;
|
60 | var container = getTarget();
|
61 | scrollEvent.current = addEventListener(container, 'scroll', function (e) {
|
62 | handleScroll(e);
|
63 | });
|
64 | handleScroll({
|
65 | target: container
|
66 | });
|
67 | };
|
68 | useEffect(function () {
|
69 | bindScrollEvent();
|
70 | return function () {
|
71 | if (scrollEvent.current) {
|
72 | scrollEvent.current.remove();
|
73 | }
|
74 | handleScroll.cancel();
|
75 | };
|
76 | }, [target]);
|
77 | var scrollToTop = function scrollToTop(e) {
|
78 | scrollTo(0, {
|
79 | getContainer: target || getDefaultTarget,
|
80 | duration: duration
|
81 | });
|
82 | if (typeof onClick === 'function') {
|
83 | onClick(e);
|
84 | }
|
85 | };
|
86 | var _useContext = useContext(ConfigContext),
|
87 | getPrefixCls = _useContext.getPrefixCls;
|
88 | var prefixCls = getPrefixCls(floatButtonPrefixCls, customizePrefixCls);
|
89 | var rootPrefixCls = getPrefixCls();
|
90 | var _useStyle = useStyle(prefixCls),
|
91 | _useStyle2 = _slicedToArray(_useStyle, 1),
|
92 | wrapSSR = _useStyle2[0];
|
93 | var groupShape = useContext(FloatButtonGroupContext);
|
94 | var mergeShape = groupShape || shape;
|
95 | var contentProps = _extends({
|
96 | prefixCls: prefixCls,
|
97 | icon: icon,
|
98 | type: type,
|
99 | shape: mergeShape
|
100 | }, restProps);
|
101 | return wrapSSR( React.createElement(CSSMotion, {
|
102 | visible: visible,
|
103 | motionName: rootPrefixCls + "-fade"
|
104 | }, function (_ref) {
|
105 | var motionClassName = _ref.className;
|
106 | return React.createElement(FloatButton, _extends({
|
107 | ref: ref
|
108 | }, contentProps, {
|
109 | onClick: scrollToTop,
|
110 | className: classNames(className, motionClassName)
|
111 | }));
|
112 | }));
|
113 | };
|
114 | export default memo(BackTop); |
\ | No newline at end of file |