Skip to content

Commit 6734bf5

Browse files
authored
fix: reset when prepare not exist (#44)
* chore: skip prepare if no need * chore: fix logic * chore: comment it
1 parent 21e6c6d commit 6734bf5

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/hooks/useStatus.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,8 @@ export default function useStatus(
101101
const [patchMotionEvents] = useDomMotionEvents(onInternalMotionEnd);
102102

103103
// ============================= Step =============================
104-
const eventHandlers = React.useMemo<{
105-
[STEP_PREPARE]?: MotionPrepareEventHandler;
106-
[STEP_START]?: MotionEventHandler;
107-
[STEP_ACTIVE]?: MotionEventHandler;
108-
}>(() => {
109-
switch (status) {
104+
const getEventHandlers = (targetStatus: MotionStatus) => {
105+
switch (targetStatus) {
110106
case STATUS_APPEAR:
111107
return {
112108
[STEP_PREPARE]: onAppearPrepare,
@@ -131,7 +127,13 @@ export default function useStatus(
131127
default:
132128
return {};
133129
}
134-
}, [status]);
130+
};
131+
132+
const eventHandlers = React.useMemo<{
133+
[STEP_PREPARE]?: MotionPrepareEventHandler;
134+
[STEP_START]?: MotionEventHandler;
135+
[STEP_ACTIVE]?: MotionEventHandler;
136+
}>(() => getEventHandlers(status), [status]);
135137

136138
const [startStep, step] = useStepQueue(status, !supportMotion, newStep => {
137139
// Only prepare step can be skip
@@ -205,10 +207,15 @@ export default function useStatus(
205207
nextStatus = STATUS_LEAVE;
206208
}
207209

210+
const nextEventHandlers = getEventHandlers(nextStatus);
211+
208212
// Update to next status
209-
if (nextStatus) {
213+
if (nextStatus && (supportMotion || nextEventHandlers[STEP_PREPARE])) {
210214
setStatus(nextStatus);
211215
startStep();
216+
} else {
217+
// Set back in case no motion but prev status has prepare step
218+
setStatus(STATUS_NONE);
212219
}
213220
}, [visible]);
214221

tests/CSSMotion.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ describe('CSSMotion', () => {
547547

548548
// hide immediately since motion is disabled
549549
rerender(<Demo motion={false} visible={false} />);
550+
550551
expect(container.querySelector('.hidden')).toBeTruthy();
551552
});
552553

0 commit comments

Comments
 (0)