Skip to content
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
Copy Markdown
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
Copy Markdown
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