question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with the latest MUIv5 and classes/withStyles overrides of nested elements

See original GitHub issue

Hello,

when I created this PR https://github.com/garronej/tss-react/pull/54 we were still using MUIv4. We are using overrides with classes prop and withStyles of MUI components a lot and since we started migrating to MUIv5, I’ve noticed something strange.

I’ve create a demo here:

I followed the recommended steps from both tss-react and MUI projects to eliminate issues with the setup.

The problem in a nutshell is the following, when you have a component with sub-elements and you’d like to override default styles (let’s say color or background color), one would expect to use what was possible in MUIv4:

const StyledChip = withStyles(Chip, (theme) => ({
  root: {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText,
  },
  deleteIcon: {
    color: theme.palette.primary.contrastText,
    '&:hover': {
      color: `${theme.palette.primary.contrastText}6`,
    },
  },
}));

But it doesn’t work because of CSS selector specificity: image

The only solution I found is with:

const StyledChip = withStyles(Chip, (theme, _props, classes) => ({
  root: {
    backgroundColor: theme.palette.primary.main,
    color: theme.palette.primary.contrastText,
    [`& .${classes.deleteIcon}`]: {
      color: theme.palette.primary.contrastText,
      '&:hover': {
        color: `${theme.palette.primary.contrastText}6`,
      },
    },
  },
}));

Unfortunately it requires a lot of refactoring in our code and it’s also quite hard to reason about, you need to know that you are fighting with specificity here (which I assumed that emotion-based solution should help to avoid).

I’m not 100% sure if it’s a problem in tss-react itself or if it’s more about how MUIv5 handles classes props.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
synaptikocommented, Feb 12, 2022

Got it, I understand it’s not an easy job to support customization, especially with more than one CSS engine. In any case, thanks for you help, feel free to close this issue and let’s see if someone from MUI can help or if we just need to refactor the code to increase specificity.

1reaction
garronejcommented, Feb 12, 2022

OK, thank you for the extra details.
I was making sure there wasn’t anything I can do for you and I am afraid this is effectively the case.

In defense of the MUI team, if they are facing all theses specificity issues it is because they are allowing users to use styled-component instead of @emotion as underlying style engine. ref. It makes their job 10 times harder, I am not sure it’s worth it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with the latest MUIv5 and classes overrides of nested ...
Context. I'd like an easy way to override styles of nested elements/components, without guessing where specificity need to be increased.
Read more >
Breaking changes in v5, part one: styles and themes - MUI
Although your style overrides defined in the theme may partially work, there is an important difference regarding how the nested elements are styled....
Read more >
Cant override styles of nested Material UI components
Basically, the Material UI components accepts a classes prop that can be used to override its styles.
Read more >
Having problems with overriding styles of Material-UI ... - Reddit
As the title says, I have spent a few days trying to override the ... ClassName-root': your styling You canalso go deeper in...
Read more >
How to use styled components with Material UI in a React app
Among the edge cases we will cover are: overriding Material UI's theme, ... you can style components without having to learn a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found