diff --git a/src/hooks/useStatus.ts b/src/hooks/useStatus.ts index 32b129b..2556f6f 100644 --- a/src/hooks/useStatus.ts +++ b/src/hooks/useStatus.ts @@ -291,12 +291,13 @@ export default function useStatus( } // Consume each visibility transition once. Activity can reconnect this effect - // without a new transition. Read the live status to avoid notifying an old - // render when a layout effect has already started the next motion. + // without a new transition. Wait for the settled render to commit, and + // check the live status in case an effect has already started another motion. if ( pendingVisibleChangeRef.current && asyncVisible !== undefined && asyncVisible === visibleRef.current && + currentStatus === STATUS_NONE && getStatus() === STATUS_NONE ) { pendingVisibleChangeRef.current = false; diff --git a/tests/CSSMotion.spec.tsx b/tests/CSSMotion.spec.tsx index 8f4a946..0af330a 100644 --- a/tests/CSSMotion.spec.tsx +++ b/tests/CSSMotion.spec.tsx @@ -1059,6 +1059,33 @@ describe('CSSMotion', () => { unmount(); }); + it('removes the node before notifying a new callback when leave is disabled', () => { + const motion = (props: CSSMotionProps) => ( + + {({ className }, ref) => ( +
+ )} + + ); + + const { queryByTestId, rerender } = render(motion({ visible: true })); + rerender(motion({ visible: false })); + act(() => { + jest.runAllTimers(); + }); + expect(queryByTestId('panel')).toHaveClass('transition-leave-active'); + + const onVisibleChanged = jest.fn(() => { + expect(queryByTestId('panel')).toBeNull(); + }); + rerender( + motion({ visible: false, motionLeave: false, onVisibleChanged }), + ); + + expect(onVisibleChanged).toHaveBeenCalledTimes(1); + expect(onVisibleChanged).toHaveBeenCalledWith(false); + }); + it('fast visible to !visible', () => { const onVisibleChanged = jest.fn();