Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/hooks/useStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ export default function useStatus(
}
};

const eventHandlers = React.useMemo<{
const eventHandlers: {
[STEP_PREPARE]?: MotionPrepareEventHandler;
[STEP_START]?: MotionEventHandler;
[STEP_ACTIVE]?: MotionEventHandler;
}>(() => getEventHandlers(currentStatus), [currentStatus]);
} = getEventHandlers(currentStatus);

const [startStep, step] = useStepQueue(
currentStatus,
Expand Down Expand Up @@ -318,8 +318,8 @@ export default function useStatus(
motionAppear
? 'NONE'
: // Enter or Leave check
step === STEP_START || step === STEP_ACTIVE
? styleStep === step
: true,
step === STEP_START || step === STEP_ACTIVE
? styleStep === step
: true,
];
}
39 changes: 39 additions & 0 deletions tests/CSSMotion.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ describe('CSSMotion', () => {
},
);

it('uses the latest active handler during a motion', () => {
const firstActive = jest.fn(() => ({ opacity: 0.1 }));
const latestActive = jest.fn(() => ({ opacity: 0.9 }));
const Demo = ({
visible,
onEnterActive,
}: {
visible: boolean;
onEnterActive: CSSMotionProps['onEnterActive'];
}) => (
<CSSMotion
motionName="transition"
motionAppear={false}
visible={visible}
onEnterActive={onEnterActive}
>
{({ style, className }) => (
<div className={clsx('motion-box', className)} style={style} />
)}
</CSSMotion>
);

const { container, rerender } = render(
<Demo visible={false} onEnterActive={firstActive} />,
);
rerender(<Demo visible onEnterActive={firstActive} />);
rerender(<Demo visible onEnterActive={latestActive} />);

act(() => {
jest.runAllTimers();
});

expect(firstActive).not.toHaveBeenCalled();
expect(latestActive).toHaveBeenCalledTimes(1);
expect(container.querySelector('.motion-box')).toHaveStyle({
opacity: '0.9',
});
});

it('leaveClassName should add to dom', () => {
const genMotion = props => {
const { visible, leavedClassName } = props;
Expand Down