index.js
5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _rcMotion = _interopRequireDefault(require("rc-motion"));
var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
var _useMergedState3 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));
var _classnames = _interopRequireDefault(require("classnames"));
var _omit = _interopRequireDefault(require("omit.js"));
var _VerticalAlignTopOutlined = _interopRequireDefault(require("@ant-design/icons/VerticalAlignTopOutlined"));
var _throttleByAnimationFrame = require("../_util/throttleByAnimationFrame");
var _configProvider = require("../config-provider");
var _getScroll = _interopRequireDefault(require("../_util/getScroll"));
var _scrollTo = _interopRequireDefault(require("../_util/scrollTo"));
var _reactNode = require("../_util/reactNode");
var BackTop = function BackTop(props) {
var _useMergedState = (0, _useMergedState3["default"])(false, {
value: props.visible
}),
_useMergedState2 = (0, _slicedToArray2["default"])(_useMergedState, 2),
visible = _useMergedState2[0],
setVisible = _useMergedState2[1];
var ref = /*#__PURE__*/React.createRef();
var scrollEvent = React.useRef();
var getDefaultTarget = function getDefaultTarget() {
return ref.current && ref.current.ownerDocument ? ref.current.ownerDocument : window;
};
var handleScroll = (0, _throttleByAnimationFrame.throttleByAnimationFrame)(function (e) {
var visibilityHeight = props.visibilityHeight;
var scrollTop = (0, _getScroll["default"])(e.target, true);
setVisible(scrollTop > visibilityHeight);
});
var bindScrollEvent = function bindScrollEvent() {
var target = props.target;
var getTarget = target || getDefaultTarget;
var container = getTarget();
scrollEvent.current = (0, _addEventListener["default"])(container, 'scroll', function (e) {
handleScroll(e);
});
handleScroll({
target: container
});
};
React.useEffect(function () {
bindScrollEvent();
return function () {
if (scrollEvent.current) {
scrollEvent.current.remove();
}
handleScroll.cancel();
};
}, [props.target]);
var scrollToTop = function scrollToTop(e) {
var onClick = props.onClick,
target = props.target,
_props$duration = props.duration,
duration = _props$duration === void 0 ? 450 : _props$duration;
(0, _scrollTo["default"])(0, {
getContainer: target || getDefaultTarget,
duration: duration
});
if (typeof onClick === 'function') {
onClick(e);
}
};
var renderChildren = function renderChildren(_ref) {
var prefixCls = _ref.prefixCls;
var children = props.children;
var defaultElement = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-icon")
}, /*#__PURE__*/React.createElement(_VerticalAlignTopOutlined["default"], null)));
return /*#__PURE__*/React.createElement(_rcMotion["default"], {
visible: visible,
motionName: "fade",
removeOnLeave: true
}, function (_ref2) {
var motionClassName = _ref2.className;
var childNode = children || defaultElement;
return /*#__PURE__*/React.createElement("div", null, (0, _reactNode.cloneElement)(childNode, function (_ref3) {
var className = _ref3.className;
return {
className: (0, _classnames["default"])(motionClassName, className)
};
}));
});
};
var _React$useContext = React.useContext(_configProvider.ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
direction = _React$useContext.direction;
var customizePrefixCls = props.prefixCls,
_props$className = props.className,
className = _props$className === void 0 ? '' : _props$className;
var prefixCls = getPrefixCls('back-top', customizePrefixCls);
var classString = (0, _classnames["default"])(prefixCls, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-rtl"), direction === 'rtl'), className); // fix https://fb.me/react-unknown-prop
var divProps = (0, _omit["default"])(props, ['prefixCls', 'className', 'children', 'visibilityHeight', 'target', 'visible']);
return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({}, divProps, {
className: classString,
onClick: scrollToTop,
ref: ref
}), renderChildren({
prefixCls: prefixCls
}));
};
BackTop.defaultProps = {
visibilityHeight: 400
};
var _default = /*#__PURE__*/React.memo(BackTop);
exports["default"] = _default;