UNPKG

9.31 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _typeof = require("@babel/runtime/helpers/typeof");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports["default"] = void 0;
11
12var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
14var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
16var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
17
18var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
19
20var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
21
22var React = _interopRequireWildcard(require("react"));
23
24var _dynamicCSS = require("rc-util/lib/Dom/dynamicCSS");
25
26var _ref2 = require("rc-util/lib/ref");
27
28var _raf = _interopRequireDefault(require("./raf"));
29
30var _configProvider = require("../config-provider");
31
32var _reactNode = require("./reactNode");
33
34function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
35
36function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
37
38var styleForPseudo; // Where el is the DOM element you'd like to test for visibility
39
40function isHidden(element) {
41 if (process.env.NODE_ENV === 'test') {
42 return false;
43 }
44
45 return !element || element.offsetParent === null || element.hidden;
46}
47
48function isNotGrey(color) {
49 // eslint-disable-next-line no-useless-escape
50 var match = (color || '').match(/rgba?\((\d*), (\d*), (\d*)(, [\d.]*)?\)/);
51
52 if (match && match[1] && match[2] && match[3]) {
53 return !(match[1] === match[2] && match[2] === match[3]);
54 }
55
56 return true;
57}
58
59var Wave = /*#__PURE__*/function (_React$Component) {
60 (0, _inherits2["default"])(Wave, _React$Component);
61
62 var _super = (0, _createSuper2["default"])(Wave);
63
64 function Wave() {
65 var _this;
66
67 (0, _classCallCheck2["default"])(this, Wave);
68 _this = _super.apply(this, arguments);
69 _this.containerRef = /*#__PURE__*/React.createRef();
70 _this.animationStart = false;
71 _this.destroyed = false;
72
73 _this.onClick = function (node, waveColor) {
74 var _a, _b;
75
76 if (!node || isHidden(node) || node.className.indexOf('-leave') >= 0) {
77 return;
78 }
79
80 var insertExtraNode = _this.props.insertExtraNode;
81 _this.extraNode = document.createElement('div');
82
83 var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this),
84 extraNode = _assertThisInitialize.extraNode;
85
86 var getPrefixCls = _this.context.getPrefixCls;
87 extraNode.className = "".concat(getPrefixCls(''), "-click-animating-node");
88
89 var attributeName = _this.getAttributeName();
90
91 node.setAttribute(attributeName, 'true'); // Not white or transparent or grey
92
93 if (waveColor && waveColor !== '#ffffff' && waveColor !== 'rgb(255, 255, 255)' && isNotGrey(waveColor) && !/rgba\((?:\d*, ){3}0\)/.test(waveColor) && // any transparent rgba color
94 waveColor !== 'transparent') {
95 extraNode.style.borderColor = waveColor;
96 var nodeRoot = ((_a = node.getRootNode) === null || _a === void 0 ? void 0 : _a.call(node)) || node.ownerDocument;
97 var nodeBody = nodeRoot instanceof Document ? nodeRoot.body : (_b = nodeRoot.firstChild) !== null && _b !== void 0 ? _b : nodeRoot;
98 styleForPseudo = (0, _dynamicCSS.updateCSS)("\n [".concat(getPrefixCls(''), "-click-animating-without-extra-node='true']::after, .").concat(getPrefixCls(''), "-click-animating-node {\n --antd-wave-shadow-color: ").concat(waveColor, ";\n }"), 'antd-wave', {
99 csp: _this.csp,
100 attachTo: nodeBody
101 });
102 }
103
104 if (insertExtraNode) {
105 node.appendChild(extraNode);
106 }
107
108 ['transition', 'animation'].forEach(function (name) {
109 node.addEventListener("".concat(name, "start"), _this.onTransitionStart);
110 node.addEventListener("".concat(name, "end"), _this.onTransitionEnd);
111 });
112 };
113
114 _this.onTransitionStart = function (e) {
115 if (_this.destroyed) {
116 return;
117 }
118
119 var node = _this.containerRef.current;
120
121 if (!e || e.target !== node || _this.animationStart) {
122 return;
123 }
124
125 _this.resetEffect(node);
126 };
127
128 _this.onTransitionEnd = function (e) {
129 if (!e || e.animationName !== 'fadeEffect') {
130 return;
131 }
132
133 _this.resetEffect(e.target);
134 };
135
136 _this.bindAnimationEvent = function (node) {
137 if (!node || !node.getAttribute || node.getAttribute('disabled') || node.className.indexOf('disabled') >= 0) {
138 return;
139 }
140
141 var onClick = function onClick(e) {
142 // Fix radio button click twice
143 if (e.target.tagName === 'INPUT' || isHidden(e.target)) {
144 return;
145 }
146
147 _this.resetEffect(node); // Get wave color from target
148
149
150 var waveColor = getComputedStyle(node).getPropertyValue('border-top-color') || // Firefox Compatible
151 getComputedStyle(node).getPropertyValue('border-color') || getComputedStyle(node).getPropertyValue('background-color');
152 _this.clickWaveTimeoutId = window.setTimeout(function () {
153 return _this.onClick(node, waveColor);
154 }, 0);
155
156 _raf["default"].cancel(_this.animationStartId);
157
158 _this.animationStart = true; // Render to trigger transition event cost 3 frames. Let's delay 10 frames to reset this.
159
160 _this.animationStartId = (0, _raf["default"])(function () {
161 _this.animationStart = false;
162 }, 10);
163 };
164
165 node.addEventListener('click', onClick, true);
166 return {
167 cancel: function cancel() {
168 node.removeEventListener('click', onClick, true);
169 }
170 };
171 };
172
173 _this.renderWave = function (_ref) {
174 var csp = _ref.csp;
175 var children = _this.props.children;
176 _this.csp = csp;
177 if (! /*#__PURE__*/React.isValidElement(children)) return children;
178 var ref = _this.containerRef;
179
180 if ((0, _ref2.supportRef)(children)) {
181 ref = (0, _ref2.composeRef)(children.ref, _this.containerRef);
182 }
183
184 return (0, _reactNode.cloneElement)(children, {
185 ref: ref
186 });
187 };
188
189 return _this;
190 }
191
192 (0, _createClass2["default"])(Wave, [{
193 key: "componentDidMount",
194 value: function componentDidMount() {
195 var node = this.containerRef.current;
196
197 if (!node || node.nodeType !== 1) {
198 return;
199 }
200
201 this.instance = this.bindAnimationEvent(node);
202 }
203 }, {
204 key: "componentWillUnmount",
205 value: function componentWillUnmount() {
206 if (this.instance) {
207 this.instance.cancel();
208 }
209
210 if (this.clickWaveTimeoutId) {
211 clearTimeout(this.clickWaveTimeoutId);
212 }
213
214 this.destroyed = true;
215 }
216 }, {
217 key: "getAttributeName",
218 value: function getAttributeName() {
219 var getPrefixCls = this.context.getPrefixCls;
220 var insertExtraNode = this.props.insertExtraNode;
221 return insertExtraNode ? "".concat(getPrefixCls(''), "-click-animating") : "".concat(getPrefixCls(''), "-click-animating-without-extra-node");
222 }
223 }, {
224 key: "resetEffect",
225 value: function resetEffect(node) {
226 var _this2 = this;
227
228 if (!node || node === this.extraNode || !(node instanceof Element)) {
229 return;
230 }
231
232 var insertExtraNode = this.props.insertExtraNode;
233 var attributeName = this.getAttributeName();
234 node.setAttribute(attributeName, 'false'); // edge has bug on `removeAttribute` #14466
235
236 if (styleForPseudo) {
237 styleForPseudo.innerHTML = '';
238 }
239
240 if (insertExtraNode && this.extraNode && node.contains(this.extraNode)) {
241 node.removeChild(this.extraNode);
242 }
243
244 ['transition', 'animation'].forEach(function (name) {
245 node.removeEventListener("".concat(name, "start"), _this2.onTransitionStart);
246 node.removeEventListener("".concat(name, "end"), _this2.onTransitionEnd);
247 });
248 }
249 }, {
250 key: "render",
251 value: function render() {
252 return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, this.renderWave);
253 }
254 }]);
255 return Wave;
256}(React.Component);
257
258exports["default"] = Wave;
259Wave.contextType = _configProvider.ConfigContext;
\No newline at end of file