UNPKG

16.2 kBJavaScriptView Raw
1function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
7function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
8
9function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
10
11function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
13function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
14
15function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
16
17function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
18
19function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
20
21function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
22
23function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
24
25function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
26
27function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
28
29function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
30
31function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
32
33/* eslint-disable no-param-reassign */
34import React, { Component } from 'react';
35import raf from 'raf';
36import Align from 'rc-align';
37import { composeRef } from "rc-util/es/ref";
38import classNames from 'classnames';
39import RawCSSMotion from "rc-animate/es/CSSMotion";
40import PopupInner from './PopupInner';
41import { getMotion } from './utils/legacyUtil';
42var CSSMotion = RawCSSMotion;
43
44function supportMotion(motion) {
45 return motion && motion.motionName;
46}
47
48var Popup = /*#__PURE__*/function (_Component) {
49 _inherits(Popup, _Component);
50
51 var _super = _createSuper(Popup);
52
53 function Popup() {
54 var _this;
55
56 _classCallCheck(this, Popup);
57
58 _this = _super.apply(this, arguments);
59 _this.state = {
60 targetWidth: undefined,
61 targetHeight: undefined,
62 status: null,
63 prevVisible: null,
64 alignClassName: null
65 };
66 _this.popupRef = React.createRef();
67 _this.alignRef = React.createRef();
68 _this.nextFrameState = null;
69 _this.nextFrameId = null;
70
71 _this.onAlign = function (popupDomNode, align) {
72 var status = _this.state.status;
73 var _this$props = _this.props,
74 getClassNameFromAlign = _this$props.getClassNameFromAlign,
75 onAlign = _this$props.onAlign;
76 var alignClassName = getClassNameFromAlign(align);
77
78 if (status === 'align') {
79 _this.setState({
80 alignClassName: alignClassName,
81 status: 'aligned'
82 }, function () {
83 _this.alignRef.current.forceAlign();
84 });
85 } else if (status === 'aligned') {
86 _this.setState({
87 alignClassName: alignClassName,
88 status: 'afterAlign'
89 });
90
91 onAlign(popupDomNode, align);
92 } else {
93 _this.setState({
94 alignClassName: alignClassName
95 });
96 }
97 };
98
99 _this.onMotionEnd = function () {
100 var visible = _this.props.visible;
101
102 _this.setState({
103 status: visible ? 'AfterMotion' : 'stable'
104 });
105 };
106
107 _this.setStateOnNextFrame = function (state) {
108 _this.cancelFrameState();
109
110 _this.nextFrameState = _objectSpread(_objectSpread({}, _this.nextFrameState), state);
111 _this.nextFrameId = raf(function () {
112 var submitState = _objectSpread({}, _this.nextFrameState);
113
114 _this.nextFrameState = null;
115
116 _this.setState(submitState);
117 });
118 };
119
120 _this.getMotion = function () {
121 return _objectSpread({}, getMotion(_this.props));
122 }; // `target` on `rc-align` can accept as a function to get the bind element or a point.
123 // ref: https://www.npmjs.com/package/rc-align
124
125
126 _this.getAlignTarget = function () {
127 var _this$props2 = _this.props,
128 point = _this$props2.point,
129 getRootDomNode = _this$props2.getRootDomNode;
130
131 if (point) {
132 return point;
133 }
134
135 return getRootDomNode;
136 };
137
138 _this.cancelFrameState = function () {
139 raf.cancel(_this.nextFrameId);
140 };
141
142 _this.renderPopupElement = function () {
143 var _this$state = _this.state,
144 status = _this$state.status,
145 targetHeight = _this$state.targetHeight,
146 targetWidth = _this$state.targetWidth,
147 alignClassName = _this$state.alignClassName;
148 var _this$props3 = _this.props,
149 prefixCls = _this$props3.prefixCls,
150 className = _this$props3.className,
151 style = _this$props3.style,
152 stretch = _this$props3.stretch,
153 visible = _this$props3.visible,
154 align = _this$props3.align,
155 destroyPopupOnHide = _this$props3.destroyPopupOnHide,
156 onMouseEnter = _this$props3.onMouseEnter,
157 onMouseLeave = _this$props3.onMouseLeave,
158 onMouseDown = _this$props3.onMouseDown,
159 onTouchStart = _this$props3.onTouchStart,
160 children = _this$props3.children;
161 var mergedClassName = classNames(prefixCls, className, alignClassName);
162 var hiddenClassName = "".concat(prefixCls, "-hidden"); // ================== Style ==================
163
164 var sizeStyle = {};
165
166 if (stretch) {
167 // Stretch with target
168 if (stretch.indexOf('height') !== -1) {
169 sizeStyle.height = targetHeight;
170 } else if (stretch.indexOf('minHeight') !== -1) {
171 sizeStyle.minHeight = targetHeight;
172 }
173
174 if (stretch.indexOf('width') !== -1) {
175 sizeStyle.width = targetWidth;
176 } else if (stretch.indexOf('minWidth') !== -1) {
177 sizeStyle.minWidth = targetWidth;
178 }
179 }
180
181 var mergedStyle = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, sizeStyle), _this.getZIndexStyle()), style), {}, {
182 opacity: status === 'stable' || !visible ? undefined : 0
183 }); // ================= Motions =================
184
185
186 var mergedMotion = _this.getMotion();
187
188 var mergedMotionVisible = visible;
189
190 if (visible && status !== 'beforeMotion' && status !== 'motion' && status !== 'stable') {
191 mergedMotion.motionAppear = false;
192 mergedMotion.motionEnter = false;
193 mergedMotion.motionLeave = false;
194 }
195
196 if (status === 'afterAlign' || status === 'beforeMotion') {
197 mergedMotionVisible = false;
198 } // ================== Align ==================
199
200
201 var mergedAlignDisabled = !visible || status !== 'align' && status !== 'aligned' && status !== 'stable'; // ================== Popup ==================
202
203 var mergedPopupVisible = true;
204
205 if (status === 'stable') {
206 mergedPopupVisible = visible;
207 } // Only remove popup since mask may still need animation
208
209
210 if (destroyPopupOnHide && !mergedPopupVisible) {
211 return null;
212 }
213
214 return React.createElement(CSSMotion, Object.assign({
215 visible: mergedMotionVisible
216 }, mergedMotion, {
217 removeOnLeave: false,
218 onEnterEnd: _this.onMotionEnd,
219 onLeaveEnd: _this.onMotionEnd
220 }), function (_ref, motionRef) {
221 var motionStyle = _ref.style,
222 motionClassName = _ref.className;
223 return React.createElement(Align, {
224 target: _this.getAlignTarget(),
225 key: "popup",
226 ref: _this.alignRef,
227 monitorWindowResize: true,
228 disabled: mergedAlignDisabled,
229 align: align,
230 onAlign: _this.onAlign
231 }, React.createElement(PopupInner, {
232 prefixCls: prefixCls,
233 visible: mergedPopupVisible,
234 hiddenClassName: hiddenClassName,
235 className: classNames(mergedClassName, motionClassName),
236 ref: composeRef(motionRef, _this.popupRef),
237 onMouseEnter: onMouseEnter,
238 onMouseLeave: onMouseLeave,
239 onMouseDown: onMouseDown,
240 onTouchStart: onTouchStart,
241 style: _objectSpread(_objectSpread({}, mergedStyle), motionStyle)
242 }, children));
243 });
244 };
245
246 _this.renderMaskElement = function () {
247 var _this$props4 = _this.props,
248 mask = _this$props4.mask,
249 maskMotion = _this$props4.maskMotion,
250 maskTransitionName = _this$props4.maskTransitionName,
251 maskAnimation = _this$props4.maskAnimation,
252 prefixCls = _this$props4.prefixCls,
253 visible = _this$props4.visible;
254
255 if (!mask) {
256 return null;
257 }
258
259 var motion = {};
260
261 if (maskMotion && maskMotion.motionName) {
262 motion = _objectSpread({
263 motionAppear: true
264 }, getMotion({
265 motion: maskMotion,
266 prefixCls: prefixCls,
267 transitionName: maskTransitionName,
268 animation: maskAnimation
269 }));
270 }
271
272 return React.createElement(CSSMotion, Object.assign({}, motion, {
273 visible: visible,
274 removeOnLeave: true
275 }), function (_ref2) {
276 var className = _ref2.className;
277 return React.createElement("div", {
278 style: _this.getZIndexStyle(),
279 key: "mask",
280 className: classNames("".concat(prefixCls, "-mask"), className)
281 });
282 });
283 };
284
285 return _this;
286 }
287
288 _createClass(Popup, [{
289 key: "componentDidMount",
290 value: function componentDidMount() {
291 this.componentDidUpdate();
292 }
293 }, {
294 key: "componentDidUpdate",
295 value: function componentDidUpdate() {
296 var status = this.state.status;
297 var _this$props5 = this.props,
298 getRootDomNode = _this$props5.getRootDomNode,
299 visible = _this$props5.visible,
300 stretch = _this$props5.stretch; // If there is a pending state update, cancel it, a new one will be set if necessary
301
302 this.cancelFrameState();
303
304 if (visible && status !== 'stable') {
305 switch (status) {
306 case null:
307 {
308 this.setStateOnNextFrame({
309 status: stretch ? 'measure' : 'align'
310 });
311 break;
312 }
313
314 case 'afterAlign':
315 {
316 this.setStateOnNextFrame({
317 status: supportMotion(this.getMotion()) ? 'beforeMotion' : 'stable'
318 });
319 break;
320 }
321
322 case 'AfterMotion':
323 {
324 this.setStateOnNextFrame({
325 status: 'stable'
326 });
327 break;
328 }
329
330 default:
331 {
332 // Go to next status
333 var queue = ['measure', 'align', null, 'beforeMotion', 'motion'];
334 var index = queue.indexOf(status);
335 var nextStatus = queue[index + 1];
336
337 if (index !== -1 && nextStatus) {
338 this.setStateOnNextFrame({
339 status: nextStatus
340 });
341 }
342 }
343 }
344 } // Measure stretch size
345
346
347 if (status === 'measure') {
348 var $ele = getRootDomNode();
349
350 if ($ele) {
351 this.setStateOnNextFrame({
352 targetHeight: $ele.offsetHeight,
353 targetWidth: $ele.offsetWidth
354 });
355 }
356 }
357 }
358 }, {
359 key: "componentWillUnmount",
360 value: function componentWillUnmount() {
361 this.cancelFrameState();
362 }
363 }, {
364 key: "getZIndexStyle",
365 value: function getZIndexStyle() {
366 var zIndex = this.props.zIndex;
367 return {
368 zIndex: zIndex
369 };
370 }
371 }, {
372 key: "render",
373 value: function render() {
374 return React.createElement("div", null, this.renderMaskElement(), this.renderPopupElement());
375 }
376 }], [{
377 key: "getDerivedStateFromProps",
378 value: function getDerivedStateFromProps(_ref3, _ref4) {
379 var visible = _ref3.visible,
380 props = _objectWithoutProperties(_ref3, ["visible"]);
381
382 var prevVisible = _ref4.prevVisible,
383 status = _ref4.status;
384 var newState = {
385 prevVisible: visible,
386 status: status
387 };
388 var mergedMotion = getMotion(props);
389
390 if (prevVisible === null && visible === false) {
391 // Init render should always be stable
392 newState.status = 'stable';
393 } else if (visible !== prevVisible) {
394 if (visible || supportMotion(mergedMotion) && ['motion', 'AfterMotion', 'stable'].includes(status)) {
395 newState.status = null;
396 } else {
397 newState.status = 'stable';
398 }
399
400 if (visible) {
401 newState.alignClassName = null;
402 }
403 }
404
405 return newState;
406 }
407 }]);
408
409 return Popup;
410}(Component);
411
412export default Popup;
413/* eslint-enable */
\No newline at end of file