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

[SvgIcon] Fix passing an ownerState to SvgIcon changes the font size #34429

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/material-ui/api/svg-icon.json
Expand Up @@ -40,6 +40,7 @@
"colorDisabled",
"fontSizeInherit",
"fontSizeSmall",
"fontSizeMedium",
"fontSizeLarge"
],
"globalClasses": {},
Expand Down
5 changes: 5 additions & 0 deletions docs/translations/api-docs/svg-icon/svg-icon.json
Expand Up @@ -50,6 +50,11 @@
"nodeName": "the root element",
"conditions": "<code>fontSize=\"small\"</code>"
},
"fontSizeMedium": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>fontSize=\"medium\"</code>"
},
"fontSizeLarge": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
Expand Down
9 changes: 9 additions & 0 deletions packages/mui-joy/src/SvgIcon/SvgIcon.test.js
Expand Up @@ -129,4 +129,13 @@ describe('<SvgIcon />', () => {
expect(container.firstChild).to.have.attribute('viewBox', '-4 -4 24 24');
});
});

it('should not override internal ownerState with the ownerState passed to the icon', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const { container } = render(<SvgIcon ownerState={{ fontSize: 'sm' }}>{path}</SvgIcon>);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a real use case to test this one? I ask this because I plan to change the test file to .tsx and I am pretty sure that the SvgIcon does not support ownerState prop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the icon slot in MUI X Pickers does pass ownerState for CSS customization based on ownerState. Also, you can see it in the before and after CodeSandboxes in the description, so we need it.

You can use // @ts-expect-error comment.

expect(container.firstChild).toHaveComputedStyle({ fontSize: '20px' }); // fontSize: xl -> 1.25rem = 20px
});
});
2 changes: 1 addition & 1 deletion packages/mui-joy/src/SvgIcon/SvgIcon.tsx
Expand Up @@ -89,14 +89,14 @@ const SvgIcon = React.forwardRef(function SvgIcon(inProps, ref) {
<SvgIconRoot
as={component}
className={clsx(classes.root, className)}
ownerState={ownerState}
focusable="false"
color={htmlColor}
aria-hidden={titleAccess ? undefined : true}
role={titleAccess ? 'img' : undefined}
ref={ref}
{...other}
{...(!inheritViewBox && { viewBox })}
ownerState={ownerState}
>
{children}
{titleAccess ? <title>{titleAccess}</title> : null}
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-material/src/SvgIcon/SvgIcon.js
Expand Up @@ -47,7 +47,7 @@ const SvgIconRoot = styled('svg', {
inherit: 'inherit',
small: theme.typography?.pxToRem?.(20) || '1.25rem',
medium: theme.typography?.pxToRem?.(24) || '1.5rem',
large: theme.typography?.pxToRem?.(35) || '2.1875',
large: theme.typography?.pxToRem?.(35) || '2.1875rem',
Copy link
Member Author

Choose a reason for hiding this comment

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

rem unit was missing, not related to the issue.

}[ownerState.fontSize],
// TODO v5 deprecate, v6 remove for sx
color:
Expand Down Expand Up @@ -96,14 +96,14 @@ const SvgIcon = React.forwardRef(function SvgIcon(inProps, ref) {
<SvgIconRoot
as={component}
className={clsx(classes.root, className)}
ownerState={ownerState}
focusable="false"
color={htmlColor}
aria-hidden={titleAccess ? undefined : true}
role={titleAccess ? 'img' : undefined}
ref={ref}
{...more}
{...other}
ownerState={ownerState}
>
{children}
{titleAccess ? <title>{titleAccess}</title> : null}
Expand Down
9 changes: 9 additions & 0 deletions packages/mui-material/src/SvgIcon/SvgIcon.test.js
Expand Up @@ -119,4 +119,13 @@ describe('<SvgIcon />', () => {
expect(container.firstChild).to.have.attribute('viewBox', '-4 -4 24 24');
});
});

it('should not override internal ownerState with the ownerState passed to the icon', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const { container } = render(<SvgIcon ownerState={{ fontSize: 'large' }}>{path}</SvgIcon>);
expect(container.firstChild).toHaveComputedStyle({ fontSize: '24px' }); // fontSize: medium -> 1.5rem = 24px
});
});
2 changes: 2 additions & 0 deletions packages/mui-material/src/SvgIcon/svgIconClasses.ts
Expand Up @@ -17,6 +17,8 @@ export interface SvgIconClasses {
fontSizeInherit: string;
/** Styles applied to the root element if `fontSize="small"`. */
fontSizeSmall: string;
/** Styles applied to the root element if `fontSize="medium"`. */
fontSizeMedium: string;
Comment on lines +20 to +21
Copy link
Member Author

Choose a reason for hiding this comment

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

Not related to the issue, but found this class was missing in types.

/** Styles applied to the root element if `fontSize="large"`. */
fontSizeLarge: string;
}
Expand Down