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

[Typography] Apply font properties to typography inherit variant #33621

Merged
merged 10 commits into from
Mar 14, 2023
Merged

[Typography] Apply font properties to typography inherit variant #33621

merged 10 commits into from
Mar 14, 2023

Conversation

oyar99
Copy link
Contributor

@oyar99 oyar99 commented Jul 23, 2022

Ensure that Typography components actually inherit font properties when inherit variant is used.
Since the Link component is built on top of the Typography component, this also fixes #32942.

const theme = createTheme({
  typography: {
    fontFamily: "Inconsolata, Arial"
  }
});

Before:

<ThemeProvider theme={theme}>
    <Typography variant="body1">
      <Link>This is good (it's Inconsolata)</Link>
      <br />
      <Link component="button">This is not good (it's Arial)</Link>
    </Typography>
</ThemeProvider>

Now:

<ThemeProvider theme={theme}>
    <Typography variant="body1">
      <Link>This is good (it's Inconsolata)</Link>
      <br />
      <Link component="button">This is good too (it's Inconsolata)</Link>
    </Typography>
</ThemeProvider>

Fixes #32942

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@michaldudak michaldudak changed the title [typography] apply font properties to typography inherit variant [Typography] Apply font properties to typography inherit variant Jul 28, 2022
@michaldudak michaldudak added the component: Typography The React component. label Jul 28, 2022
@mui-bot
Copy link

mui-bot commented Aug 1, 2022

Netlify deploy preview

https://deploy-preview-33621--material-ui.netlify.app/

@material-ui/core: parsed: +0.10% , gzip: +0.09%
@material-ui/lab: parsed: +0.19% , gzip: +0.16%
@material-ui/system: parsed: +0.54% , gzip: +0.48%
@mui/material-next: parsed: +0.37% , gzip: +0.32%
@mui/joy: parsed: +0.10% , gzip: +0.11%

Bundle size report

Details of bundle changes

Generated by 🚫 dangerJS against 8b6f44a

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

There's a unit test failing. Would you mind taking a look at it?

@michaldudak
Copy link
Member

The visual regression tests detected another issue. The sx prop doesn't override the CSS properties from the variant. Compare https://mui.com/material-ui/react-tree-view/#gmail-clone with https://deploy-preview-33621--material-ui.netlify.app/material-ui/react-tree-view/#gmail-clone.

This has worked fine before, so I wonder if it's related to the latest changes to Emotion support in the System (cc @garronej, @mnajdova).

@mnajdova
Copy link
Member

mnajdova commented Aug 5, 2022

@michaldudak I don't think the problem is with the order of how things are defined, but how this variant is defined. In the demo that you linked, if you change the fontWeight with font-weight it works as expected, see https://codesandbox.io/s/xenodochial-banach-z7sojt. There is something wrong with how the variant is defined or how the value is fetched (haven't looked into too much details, just wanted to confirm it is not related to the changes done by @garronej as that would have a much bigger impact).

@garronej
Copy link
Contributor

garronej commented Aug 5, 2022

Thanks @mnajdova for figuring this out. I am on vacation so I wasn't able to look at it.

@michaldudak
Copy link
Member

@mnajdova Thanks, I'll take a deeper look at this, then.

@ZeeshanTamboli ZeeshanTamboli added the bug 🐛 Something doesn't work label Mar 8, 2023
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

I fixed the issue identified by the visual regression test mentioned in #33621 (comment). As @mnajdova pointed out in #33621 (comment), it was about how the variant was fetched in the sx prop implementation. The issue was that, after adding the missing inherit variant in Typography, the default theme mapping for typography fetched the whole inherit variant object from createTypography when there was any CSS font property of inherit value defined in the sx prop.

I added a fix in 224fdaa along with a test in the sx prop implementation. However, I wonder if there is a better way to fix this. Perhaps there is something we can do in the Typography component level instead of the sx prop. But I also believe this is how we should do it, and it makes sense. (We can't change variant="inherit" to something else, that would be a breaking change.)

I am approving it from my side, but DO NOT MERGE it until approved by @mnajdova or @michaldudak .

@mnajdova
Copy link
Member

@ZeeshanTamboli I changed a bit the fix for the sx prop, by providing the necessary changes to the sx config instead of the styleFunctionSx. The changes are related to the default theme, for e.g. they are not related to Joy UI, so it makes sense for them not to live in the styleFucntionSx intself.

@mnajdova mnajdova merged commit c1a5203 into mui:master Mar 14, 2023
let value =
props.theme.typography?.[
`${prop}${
props[prop] === 'default' || props[prop] === prop
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, what is this props[prop] === prop for? CSS property's value being equal to it's own property?

@ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli I changed a bit the fix for the sx prop, by providing the necessary changes to the sx config instead of the styleFunctionSx. The changes are related to the default theme, for e.g. they are not related to Joy UI, so it makes sense for them not to live in the styleFucntionSx intself.

Nice! Makes sense.

}

if (!value) {
value = propValue;

Choose a reason for hiding this comment

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

You've duplicated if statements here and on line 22

@genepaul
Copy link

It appears that this PR may have broken the sx handling of font props? The documentation says that you can do something like this:

sx={{
    fontWeight: 'fontWeightMedium'
}}

But the code in this PR appears to only look for the prop without the fontWeight:

sx={{
   fontWeight: 'medium'
}}

Here's a codesandbox that reproduces the issue: https://codesandbox.io/s/vibrant-brook-572rnc?file=/src/App.js

You can verify that this is a regression by changing the version of @mui/material that is pulled in to 5.11.12 or earlier, and notice that using fontWeightMedium properly changes the font weight.

Here's the documentation that refers to being able to use fontWeightLight (as an example):

https://mui.com/system/getting-started/the-sx-prop/#typography

@genepaul
Copy link

I created an issue for this: #36542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: Typography The React component.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Link with component=button does not use custom font
9 participants