Skip to content

Commit bd25dec

Browse files
authored
fix: visible -> !visible should also trigger onVisibleChanged (#33)
1 parent de1c9cf commit bd25dec

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/hooks/useStatus.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ export default function useStatus(
226226
// Trigger `onVisibleChanged`
227227
const firstMountChangeRef = React.useRef(false);
228228
useEffect(() => {
229+
// [visible & motion not end] => [!visible & motion end] still need trigger onVisibleChanged
230+
if (asyncVisible) {
231+
firstMountChangeRef.current = true;
232+
}
233+
229234
if (asyncVisible !== undefined && status === STATUS_NONE) {
230235
// Skip first render is invisible since it's nothing changed
231236
if (firstMountChangeRef.current || asyncVisible) {

tests/CSSMotion.spec.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,5 +773,40 @@ describe('CSSMotion', () => {
773773

774774
unmount();
775775
});
776+
777+
it('fast visible to !visible', () => {
778+
const onVisibleChanged = jest.fn();
779+
780+
const Demo = ({ visible }: { visible: boolean }) => (
781+
<CSSMotion
782+
motionName="transition"
783+
motionAppear
784+
motionEnter
785+
motionLeave
786+
visible={visible}
787+
onVisibleChanged={onVisibleChanged}
788+
>
789+
{({ style, className }) => (
790+
<div
791+
style={style}
792+
className={classNames('motion-box', className)}
793+
/>
794+
)}
795+
</CSSMotion>
796+
);
797+
798+
const { unmount, rerender, container } = render(<Demo visible />);
799+
rerender(<Demo visible={false} />);
800+
act(() => {
801+
jest.runAllTimers();
802+
});
803+
804+
fireEvent.animationEnd(container.querySelector('.motion-box'));
805+
806+
expect(onVisibleChanged).toHaveBeenCalledTimes(1);
807+
expect(onVisibleChanged).toHaveBeenCalledWith(false);
808+
809+
unmount();
810+
});
776811
});
777812
});

0 commit comments

Comments
 (0)