material-ui: `classes` styles don't override default when using @emotion/react
Abstract
Who it’s affecting
This issues only affect users that uses @emotion/react to write their custom styles.
What is the issue
The styles provided to the classes property of MUI components don’t take priority over the internal styles and there is no way around it.
Original issue
Hi,
Following up with 27945 I think there is (arguably) a unexpected behaviour in the way the classes prop is handled when the user is using emotion and the same cache that material-ui is using.
- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
The default style have the priority hover style provided in classes. (in a specific context described below)
Expected Behavior 🤔
The styles explicitely provided by users in classes should always overwrite the defaults.
Steps to Reproduce 🕹
Steps:
- Remove the
!importantline 21 - Reload and witness that the text is no longer pink.
Context
This issue is not related to SSR and it is not specific to tss-react.
My investigation

Best,
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 19 (18 by maintainers)
Hi @mnajdova,
Let me clarify because I think there is a misunderstanding about what the issue is:
I assert that in the very specific case where:
Then, and only in this very specific configuration, you guys should be able, using
cxinternally, to ensure that the styles provided to mui components usingclassNameorclassestake priority over the default mui styles.<del>Now, you can consider this configuration as being to much of an edge case to bother addressing it.
In this case I’d suggest mentioning in the documentation that this configuration isn’t supported.</del> I kinda disagree with my own statement now since it’s always the case when users uses
@emotion/reactfor custom styles and there is no way around it.PS: This is not something that I need fixed for
tss-react. I just thought you might find this report useful.Yes, let’s start with a PR and see what the final API would look like
Addressed in https://github.com/mui/material-ui/pull/32067
Hi @mnajdova, thanks for your answer,
Precisely!
Yes, I can use
unstable_useEmotionCacheintss-reactto pickup whatever cache is used.The great thing is that we won’t need to change anything beside exposing
cx. Event if nocacheis explicitly provided by the userunstable_useEmotionCachenever returnsnullin the browser sotss-reactalways have a way to pickup whatever cache mui is using.Yes! I am very interested,
@mui/styled-engine-scwould exportclsxasclassNamesand@mui/styled-enginewould export emotion’scxasclassNames.Emotion’s
cxis an implementation of theclassNamesAPI that detects emotion generated class names ensuring styles are overwritten in the correct order. (I am sure you know that already but I document it for others).If you are OK, we can follow the discussion on the basis of a PR?
We would need to export an
cxlike utility from the@mui/styled-engine-scpackage that will simply concat classes, asstyled-componentsdoes not have acxutil. Regarding emotion, can we do this without requiring the apps to be wrapper with a cache provider? As far as I saw, having theunstable_useEmotionCacheshould allow us to do that (we can have default cache, and we can add a similar hook in the@mui/styled-engine-scpackage.If you are interested to give this a go, feel free to, I can help wtih the
@mui/styled-engine-scpackage. The idea is this package and the@mui/styled-enginepackage should expose the same API, so that they can be interchangeable.That’s great to hear.
I am willing to submit a PR if you give me the green light.
For recall, what needs to be done is to internally use
cxto make sure the styles provided by users inclassesoverwrite the internal.I am very motivated because there is something else I forgot to mention. Right now, I need specific instructions to instruct users how to setup SSR.
It’s not a big deal for first-hand TSS users but it is one for users relying on libraries that use TSS internally. mui-datatable for example.
I’ve gone as far as getting a new API into emotion to enable me to pick up the cash but there is still this issue in the way.
Best regards,
I won’t do any changes at this moment and wait to see if there will be more activity on the issue.
Ok, glad we are on the same page 😃 Will report back tomorrow after I spend some more time on this 😃
I am a but confused with this statement. In the codesandbox that you shared, when using
classes.rootthe overrides did not work, but they did work when you usedclassName. This is the opposite of what you’ve just said. Maybe an example to illustrate would help.In v4 as far as I know it doesn’t matter if you provide the classes generated from makeStyles to the
classNameorclasses, it works the same way, as which one will take precedence is based on when the useStyles hook is invoked.Anyway, more detailed example of v4 vs v5 that proves this would be helpful, as I really don’t think using classes generated by makeStyles take presedence based on whether they are passed to
classNamevsclasses.root.Thanks a lot for the clarification @garronej I will anyway spend some more time tomorrow, my biggest worry is that
cxis emotion only API, so I would need to export it as a custom function for the other styled engine in order to make sure that the same interface is exported. Will report back on the issue what I’ve come up with.I have observed this same behaviour. Initially I couldn’t even see where my styles were being applied, but eventually I noticed them merged into one giant rule. I have observed similar behaviour with className too. This is a departure from v4, I believe.
In the sandbox example above, the combined style
.foo-bar-XXXXXX-MuiButtonBase-root-MuiButton-roothas threecolorproperties on it. Given that the last one wins, if this approach to merging is the expected new norm, the custom styles should be appended.I will add, it also makes it harder to debug custom styles, because you can’t easily tell what’s coming from where.