Skip to content

Commit

Permalink
fix: appeared again after closing and disappeared very quickly (#39)
Browse files Browse the repository at this point in the history
* fix: appeared again after closing and disappeared very quickly

* fix: add test case

* fix: add test case

* fix: 修复!removeOnLeave 因为没有 leavedCLassName 而走到最后的 motionChildren = null 分支
  • Loading branch information
heiyu4585 committed Jan 9, 2023
1 parent d164f7f commit ca02040
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CSSMotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ export function genCSSMotion(
// Stable children
if (mergedVisible) {
motionChildren = children({ ...mergedProps }, setNodeRef);
} else if (!removeOnLeave && renderedRef.current) {
} else if (!removeOnLeave && renderedRef.current && leavedClassName) {
motionChildren = children(
{ ...mergedProps, className: leavedClassName },
setNodeRef,
);
} else if (forceRender) {
} else if ( forceRender || (!removeOnLeave && !leavedClassName)) {
motionChildren = children(
{ ...mergedProps, style: { display: 'none' } },
setNodeRef,
Expand Down
53 changes: 53 additions & 0 deletions tests/CSSMotion.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,59 @@ describe('CSSMotion', () => {
},
);

it('leaveClassName should add to dom', () => {
const genMotion = (props) => {
const {visible,leavedClassName} = props
return (
<CSSMotion
motionName="transition"
visible={visible}
removeOnLeave={false}
leavedClassName={leavedClassName}
>
{({ style, className }) => {
return (
<div
style={style}
className={classNames('motion-box', className)}
/>
);
}}
</CSSMotion>
);
};
const { container, rerender } = render(genMotion({visible:false}));

rerender(genMotion({ visible: true }));

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

expect(container.querySelector('.motion-box')).toBeTruthy();
rerender(genMotion({ visible: false,leavedClassName:'removed'}));
act(() => {
jest.runAllTimers();
});

fireEvent.transitionEnd(container.querySelector('.motion-box'));

expect(container.querySelector('.motion-box')).toHaveClass('removed');

rerender(genMotion({ visible: true }));
act(() => {
jest.runAllTimers();
});
rerender(genMotion({ visible: false }));
act(() => {
jest.runAllTimers();
});

fireEvent.transitionEnd(container.querySelector('.motion-box'));
expect(container.querySelector('.motion-box')?.classList.contains('removed')).toBeFalsy();

});

it('stop transition if config motion to false', () => {
const genMotion = (props?: CSSMotionProps) => (
<CSSMotion motionName="transition" visible {...props}>
Expand Down

1 comment on commit ca02040

@vercel
Copy link

@vercel vercel bot commented on ca02040 Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.