UNPKG

2.78 kBJavaScriptView Raw
1import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4import { useEffect, useRef } from 'react';
5import raf from "rc-util/es/raf";
6import useState from "rc-util/es/hooks/useState";
7/**
8 * Popup should follow the steps for each component work correctly:
9 * measure - check for the current stretch size
10 * align - let component align the position
11 * aligned - re-align again in case additional className changed the size
12 * afterAlign - choice next step is trigger motion or finished
13 * beforeMotion - should reset motion to invisible so that CSSMotion can do normal motion
14 * motion - play the motion
15 * stable - everything is done
16 */
17
18var StatusQueue = ['measure', 'alignPre', 'align', null, 'motion'];
19export default (function (visible, doMeasure) {
20 var _useState = useState(null),
21 _useState2 = _slicedToArray(_useState, 2),
22 status = _useState2[0],
23 setInternalStatus = _useState2[1];
24
25 var rafRef = useRef();
26
27 function setStatus(nextStatus) {
28 setInternalStatus(nextStatus, true);
29 }
30
31 function cancelRaf() {
32 raf.cancel(rafRef.current);
33 }
34
35 function goNextStatus(callback) {
36 cancelRaf();
37 rafRef.current = raf(function () {
38 // Only align should be manually trigger
39 setStatus(function (prev) {
40 switch (status) {
41 case 'align':
42 return 'motion';
43
44 case 'motion':
45 return 'stable';
46
47 default:
48 }
49
50 return prev;
51 });
52 callback === null || callback === void 0 ? void 0 : callback();
53 });
54 } // Init status
55
56
57 useEffect(function () {
58 setStatus('measure');
59 }, [visible]); // Go next status
60
61 useEffect(function () {
62 switch (status) {
63 case 'measure':
64 doMeasure();
65 break;
66
67 default:
68 }
69
70 if (status) {
71 rafRef.current = raf( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
72 var index, nextStatus;
73 return _regeneratorRuntime().wrap(function _callee$(_context) {
74 while (1) {
75 switch (_context.prev = _context.next) {
76 case 0:
77 index = StatusQueue.indexOf(status);
78 nextStatus = StatusQueue[index + 1];
79
80 if (nextStatus && index !== -1) {
81 setStatus(nextStatus);
82 }
83
84 case 3:
85 case "end":
86 return _context.stop();
87 }
88 }
89 }, _callee);
90 })));
91 }
92 }, [status]);
93 useEffect(function () {
94 return function () {
95 cancelRaf();
96 };
97 }, []);
98 return [status, goNextStatus];
99});
\No newline at end of file