Skip to content

Commit f82e9b5

Browse files
committed
fix: wait for DOM removal before notifying leave completion
1 parent 3b40850 commit f82e9b5

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎src/hooks/useStatus.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,13 @@ export default function useStatus(
291291
}
292292

293293
// Consume each visibility transition once. Activity can reconnect this effect
294-
// without a new transition. Read the live status to avoid notifying an old
295-
// render when a layout effect has already started the next motion.
294+
// without a new transition. Wait for the settled render to commit, and
295+
// check the live status in case an effect has already started another motion.
296296
if (
297297
pendingVisibleChangeRef.current &&
298298
asyncVisible !== undefined &&
299299
asyncVisible === visibleRef.current &&
300+
currentStatus === STATUS_NONE &&
300301
getStatus() === STATUS_NONE
301302
) {
302303
pendingVisibleChangeRef.current = false;

‎tests/CSSMotion.spec.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,33 @@ describe('CSSMotion', () => {
10591059
unmount();
10601060
});
10611061

1062+
it('removes the node before notifying a new callback when leave is disabled', () => {
1063+
const motion = (props: CSSMotionProps) => (
1064+
<CSSMotion motionName="transition" motionAppear={false} {...props}>
1065+
{({ className }, ref) => (
1066+
<div className={className} ref={ref} data-testid="panel" />
1067+
)}
1068+
</CSSMotion>
1069+
);
1070+
1071+
const { queryByTestId, rerender } = render(motion({ visible: true }));
1072+
rerender(motion({ visible: false }));
1073+
act(() => {
1074+
jest.runAllTimers();
1075+
});
1076+
expect(queryByTestId('panel')).toHaveClass('transition-leave-active');
1077+
1078+
const onVisibleChanged = jest.fn(() => {
1079+
expect(queryByTestId('panel')).toBeNull();
1080+
});
1081+
rerender(
1082+
motion({ visible: false, motionLeave: false, onVisibleChanged }),
1083+
);
1084+
1085+
expect(onVisibleChanged).toHaveBeenCalledTimes(1);
1086+
expect(onVisibleChanged).toHaveBeenCalledWith(false);
1087+
});
1088+
10621089
it('fast visible to !visible', () => {
10631090
const onVisibleChanged = jest.fn();
10641091

0 commit comments

Comments
 (0)