Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: appeared again after closing and disappeared very quickly #39

Merged
merged 4 commits into from
Jan 9, 2023
Merged
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
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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

!removeOnLeave 的情况因为没有 leavedCLassName 会走到最后的 motionChildren = null 分支, 在forceRender 加上(!removeOnLeave && !leavedClassName) 兜底变成 style 注入,覆盖到 !removeOnleave的情况.

Copy link
Member

Choose a reason for hiding this comment

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

多了空格

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