-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathuseStatus.ts
More file actions
323 lines (278 loc) · 8.44 KB
/
useStatus.ts
File metadata and controls
323 lines (278 loc) · 8.44 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { useEvent } from '@rc-component/util';
import useSyncState from '@rc-component/util/lib/hooks/useSyncState';
import * as React from 'react';
import { useEffect, useRef } from 'react';
import type { CSSMotionProps } from '../CSSMotion';
import type {
MotionEvent,
MotionEventHandler,
MotionPrepareEventHandler,
MotionStatus,
StepStatus,
} from '../interface';
import {
STATUS_APPEAR,
STATUS_ENTER,
STATUS_LEAVE,
STATUS_NONE,
STEP_ACTIVE,
STEP_PREPARE,
STEP_PREPARED,
STEP_START,
} from '../interface';
import useDomMotionEvents from './useDomMotionEvents';
import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
import useStepQueue, { DoStep, isActive, SkipStep } from './useStepQueue';
export default function useStatus(
supportMotion: boolean,
visible: boolean,
getElement: () => HTMLElement,
{
motionEnter = true,
motionAppear = true,
motionLeave = true,
motionDeadline,
motionLeaveImmediately,
onAppearPrepare,
onEnterPrepare,
onLeavePrepare,
onAppearStart,
onEnterStart,
onLeaveStart,
onAppearActive,
onEnterActive,
onLeaveActive,
onAppearEnd,
onEnterEnd,
onLeaveEnd,
onVisibleChanged,
}: CSSMotionProps,
): [
status: () => MotionStatus,
stepStatus: StepStatus,
style: React.CSSProperties,
visible: boolean,
styleReady: 'NONE' | boolean,
] {
// Used for outer render usage to avoid `visible: false & status: none` to render nothing
const [asyncVisible, setAsyncVisible] = React.useState<boolean>();
const [getStatus, setStatus] = useSyncState<MotionStatus>(STATUS_NONE);
const [style, setStyle] = React.useState<
[style: React.CSSProperties | undefined, step: StepStatus]
>([null, null]);
const currentStatus = getStatus();
const mountedRef = useRef(false);
const deadlineRef = useRef(null);
// =========================== Dom Node ===========================
function getDomElement() {
return getElement();
}
// ========================== Motion End ==========================
const activeRef = useRef(false);
/**
* Clean up status & style
*/
function updateMotionEndStatus() {
setStatus(STATUS_NONE);
setStyle([null, null]);
}
const onInternalMotionEnd = useEvent((event: MotionEvent) => {
const status = getStatus();
// Do nothing since not in any transition status.
// This may happen when `motionDeadline` trigger.
if (status === STATUS_NONE) {
return;
}
const element = getDomElement();
if (event && !event.deadline && event.target !== element) {
// event exists
// not initiated by deadline
// transitionEnd not fired by inner elements
return;
}
const currentActive = activeRef.current;
let canEnd: boolean | void;
if (status === STATUS_APPEAR && currentActive) {
canEnd = onAppearEnd?.(element, event);
} else if (status === STATUS_ENTER && currentActive) {
canEnd = onEnterEnd?.(element, event);
} else if (status === STATUS_LEAVE && currentActive) {
canEnd = onLeaveEnd?.(element, event);
}
// Only update status when `canEnd` and not destroyed
if (currentActive && canEnd !== false) {
updateMotionEndStatus();
}
});
const [patchMotionEvents] = useDomMotionEvents(onInternalMotionEnd);
// ============================= Step =============================
const getEventHandlers = (targetStatus: MotionStatus) => {
switch (targetStatus) {
case STATUS_APPEAR:
return {
[STEP_PREPARE]: onAppearPrepare,
[STEP_START]: onAppearStart,
[STEP_ACTIVE]: onAppearActive,
};
case STATUS_ENTER:
return {
[STEP_PREPARE]: onEnterPrepare,
[STEP_START]: onEnterStart,
[STEP_ACTIVE]: onEnterActive,
};
case STATUS_LEAVE:
return {
[STEP_PREPARE]: onLeavePrepare,
[STEP_START]: onLeaveStart,
[STEP_ACTIVE]: onLeaveActive,
};
default:
return {};
}
};
const eventHandlers = React.useMemo<{
[STEP_PREPARE]?: MotionPrepareEventHandler;
[STEP_START]?: MotionEventHandler;
[STEP_ACTIVE]?: MotionEventHandler;
}>(() => getEventHandlers(currentStatus), [currentStatus]);
const [startStep, step] = useStepQueue(
currentStatus,
!supportMotion,
newStep => {
// Only prepare step can be skip
if (newStep === STEP_PREPARE) {
const onPrepare = eventHandlers[STEP_PREPARE];
if (!onPrepare) {
return SkipStep;
}
return onPrepare(getDomElement());
}
// Rest step is sync update
if (newStep in eventHandlers) {
setStyle([
eventHandlers[newStep]?.(getDomElement(), null) || null,
newStep,
]);
}
if (newStep === STEP_ACTIVE && currentStatus !== STATUS_NONE) {
// Patch events when motion needed
patchMotionEvents(getDomElement());
if (motionDeadline > 0) {
clearTimeout(deadlineRef.current);
deadlineRef.current = setTimeout(() => {
onInternalMotionEnd({
deadline: true,
} as MotionEvent);
}, motionDeadline);
}
}
if (newStep === STEP_PREPARED) {
updateMotionEndStatus();
}
return DoStep;
},
);
const active = isActive(step);
activeRef.current = active;
// ============================ Status ============================
const visibleRef = useRef<boolean | null>(null);
// Update with new status
useIsomorphicLayoutEffect(() => {
// When use Suspense, the `visible` will repeat trigger,
// But not real change of the `visible`, we need to skip it.
// https://github.com/ant-design/ant-design/issues/44379
if (mountedRef.current && visibleRef.current === visible) {
return;
}
setAsyncVisible(visible);
const isMounted = mountedRef.current;
mountedRef.current = true;
// if (!supportMotion) {
// return;
// }
let nextStatus: MotionStatus;
// Appear
if (!isMounted && visible && motionAppear) {
nextStatus = STATUS_APPEAR;
}
// Enter
if (isMounted && visible && motionEnter) {
nextStatus = STATUS_ENTER;
}
// Leave
if (
(isMounted && !visible && motionLeave) ||
(!isMounted && motionLeaveImmediately && !visible && motionLeave)
) {
nextStatus = STATUS_LEAVE;
}
const nextEventHandlers = getEventHandlers(nextStatus);
// Update to next status
if (nextStatus && (supportMotion || nextEventHandlers[STEP_PREPARE])) {
setStatus(nextStatus);
startStep();
} else {
// Set back in case no motion but prev status has prepare step
setStatus(STATUS_NONE);
}
visibleRef.current = visible;
}, [visible]);
// ============================ Effect ============================
// Reset when motion changed
useEffect(() => {
if (
// Cancel appear
(currentStatus === STATUS_APPEAR && !motionAppear) ||
// Cancel enter
(currentStatus === STATUS_ENTER && !motionEnter) ||
// Cancel leave
(currentStatus === STATUS_LEAVE && !motionLeave)
) {
setStatus(STATUS_NONE);
}
}, [motionAppear, motionEnter, motionLeave]);
useEffect(
() => () => {
mountedRef.current = false;
clearTimeout(deadlineRef.current);
},
[],
);
// Trigger `onVisibleChanged`
const firstMountChangeRef = React.useRef(false);
useEffect(() => {
// [visible & motion not end] => [!visible & motion end] still need trigger onVisibleChanged
if (asyncVisible) {
firstMountChangeRef.current = true;
}
if (asyncVisible !== undefined && currentStatus === STATUS_NONE) {
// Skip first render is invisible since it's nothing changed
if (firstMountChangeRef.current || asyncVisible) {
onVisibleChanged?.(asyncVisible);
}
firstMountChangeRef.current = true;
}
}, [asyncVisible, currentStatus]);
// ============================ Styles ============================
let mergedStyle = style[0];
if (eventHandlers[STEP_PREPARE] && step === STEP_START) {
mergedStyle = {
transition: 'none',
...mergedStyle,
};
}
const styleStep = style[1];
return [
getStatus,
step,
mergedStyle,
asyncVisible ?? visible,
!mountedRef.current && currentStatus === STATUS_NONE
? // Appear
'NONE'
: // Enter or Leave
step === STEP_START || step === STEP_ACTIVE
? styleStep === step
: true,
];
}