Skip to content

Commit 270eae8

Browse files
committed
fix: Update motion to stable with next frame
1 parent af404fd commit 270eae8

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

src/Popup/PopupInner.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import { useRef, useState } from 'react';
33
import Align from 'rc-align';
44
import { RefAlign } from 'rc-align/lib/Align';
5-
import CSSMotion, { CSSMotionProps } from 'rc-motion';
5+
import CSSMotion, { CSSMotionProps, MotionEndEventHandler } from 'rc-motion';
66
import classNames from 'classnames';
77
import {
88
Point,
@@ -133,29 +133,41 @@ const PopupInner = React.forwardRef<PopupInnerRef, PopupInnerProps>(
133133
}
134134

135135
// ======================== Motion ========================
136-
const motion = getMotion(props);
136+
const motion = { ...getMotion(props) };
137+
['onAppearEnd', 'onEnterEnd', 'onLeaveEnd'].forEach(eventName => {
138+
const originHandler: MotionEndEventHandler = motion[eventName];
139+
motion[eventName] = (element, event) => {
140+
goNextStatus();
141+
return originHandler?.(element, event);
142+
};
143+
});
137144

138145
function onShowPrepare() {
139146
return new Promise(resolve => {
140147
prepareResolveRef.current = resolve;
141148
});
142149
}
143150

151+
// Go to stable directly when motion not provided
152+
React.useEffect(() => {
153+
if (!motion.motionName && status === 'motion') {
154+
goNextStatus();
155+
}
156+
}, [motion.motionName, status]);
157+
144158
// ========================= Refs =========================
145159
React.useImperativeHandle(ref, () => ({
146160
forceAlign,
147161
getElement: () => elementRef.current,
148162
}));
149163

150164
// ======================== Render ========================
151-
const interactiveReady = status === 'stable' || !visible;
152-
153165
const mergedStyle: React.CSSProperties = {
154166
...stretchStyle,
155167
zIndex,
156168
...style,
157-
opacity: interactiveReady ? undefined : 0,
158-
pointerEvents: interactiveReady ? undefined : 'none',
169+
opacity: status === 'motion' || status === 'stable' ? undefined : 0,
170+
pointerEvents: status === 'stable' ? undefined : 'none',
159171
};
160172

161173
// Align status

src/Popup/useVisibleStatus.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ type PopupStatus = null | 'measure' | 'align' | 'aligned' | 'motion' | 'stable';
1515

1616
type Func = () => void;
1717

18-
const StatusQueue: PopupStatus[] = [
19-
'measure',
20-
'align',
21-
null,
22-
'motion',
23-
'stable',
24-
];
18+
const StatusQueue: PopupStatus[] = ['measure', 'align', null, 'motion'];
2519

2620
export default (
2721
visible: boolean,
2822
doMeasure: Func,
29-
): [PopupStatus, (callback: () => void) => void] => {
23+
): [PopupStatus, (callback?: () => void) => void] => {
3024
const [status, setStatus] = useState<PopupStatus>(null);
3125
const rafRef = useRef<number>();
3226

@@ -35,21 +29,23 @@ export default (
3529
}
3630

3731
function goNextStatus(callback?: () => void) {
38-
// Only align should be manually trigger
39-
setStatus(prev => {
40-
switch (status) {
41-
case 'align':
42-
return 'motion';
43-
}
32+
cancelRaf();
33+
rafRef.current = raf(() => {
34+
// Only align should be manually trigger
35+
setStatus(prev => {
36+
switch (status) {
37+
case 'align':
38+
return 'motion';
4439

45-
return prev;
46-
});
40+
case 'motion':
41+
return 'stable';
42+
}
4743

48-
// Trigger callback in another frame
49-
if (callback) {
50-
cancelRaf();
51-
rafRef.current = raf(callback);
52-
}
44+
return prev;
45+
});
46+
47+
callback?.();
48+
});
5349
}
5450

5551
// Init status
@@ -67,8 +63,9 @@ export default (
6763

6864
if (status) {
6965
rafRef.current = raf(async () => {
70-
const nextStatus = StatusQueue[StatusQueue.indexOf(status) + 1];
71-
if (nextStatus) {
66+
const index = StatusQueue.indexOf(status);
67+
const nextStatus = StatusQueue[index + 1];
68+
if (nextStatus && index !== -1) {
7269
setStatus(nextStatus);
7370
}
7471
});

0 commit comments

Comments
 (0)