-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
composeClasses should append keys from classes, not prepend them before the result of getUtilityClass #27945
Comments
Thanks for the feedback. Could you provide a concrete example of customization that is no longer working as expected in 5.x? To be clear: The order of classnames in the |
One more thing. Did you follow the Interoperability guide in case if you are using plain CSS for customization? |
Yes, I checked that and also read through all migration guides. |
Here I made a quick example hat shows different ways of styling using classes which all fail:
I would disagree. Especially with @emotion/react being used internally in MUI and in custom styles, with both sharing the same global cache, that seems to be the only thing relevant for specificity if I'm not missing something? However it also seems to be the case outside of that ecosystem e.g. when using plain CSS classes. |
Preparing a PR now. |
@janus-reith can you prepare codesandbox without tss-react? If the problem is the integration with it, we could propagate the issue there. Also, for nextjs, we already have a template example that could be use as a starting point: https://github.com/mui-org/material-ui/tree/next/examples/nextjs-with-typescript |
There's nothing to disagree. classname order not affecting specificity is a fact. If browsers do consider that for specificity then that's a browser bug since the CSS spec does not consider classname order.
-- https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity#how_is_specificity_calculated |
In my example above I included different examples, tss-react being one of them. |
Great! My point was, nextjs requires some configuring of emotion and you have direct access to where you inject new style tags (you can add them after emotion). This should fix at least the plain CSS |
Sorry, could it be that we put a different meaning to the term "specificity" here? I'm not referring to the specificity of some css selector here -> Sure, fully agree, if one is more specific than another one, the order of classname would not change that. From your quote:
That exactly is my point. Scoped to that element, both have the same specificity, they are classnames which dont have any other selector logic involved. |
Sure, will create an example using the default example, that might rule out any configuration differences in _document/_app for next.js |
@mnajdova Here is a new demo, based on the default next-with-typescript example: |
I've fixed the plain CSS and CSS modules: https://stackblitz.com/edit/node-vfbg4t?file=pages%2F_document.tsx The changes required were:
For the CSS modules, there were no styles, so I've just added the class there to set the background color :) For the Global styles, I would say it is expected, as usually Global styles are used for setting some defaults, not for overriding. If you strongly think this should not work like that, you can change the order of the style tags created by the |
The spec references the order of declarations in the document e.g. <style>
.foo {}
/* this one will override .firstClassName since it comes later*/
.bar {}
</style> it does not refer to the order of classnames in the attribute. To reword: Both of the following elements will have the same styles in any spec-compliant browsers. I'm not aware of any user agent that generates different styles for these: The only thing that would change the styles (of both elements) is re-ordering the declaration of the <style>
.foo {}
/* this one will override .firstClassName since it comes later*/
.bar {}
</style> to <style>
.bar {}
/* this one will override .firstClassName since it comes later*/
.foo {}
</style>
I would strongly advise against defining new terminology in Material-UI. CSS is hard enough to learn. Diverging from what you're used to, would be highly confusing. |
Agree 100% |
But you don't have to believe me. You can verify it yourself: https://codesandbox.io/s/order-of-classes-in-attributes-does-not-matter-dyt1s |
Thank you @mnajdova!
Hm, I am sure I used this before in a different example and it did not have an affect, will need to go though some tests again.
I see, interesting, so in that case the class actually gets prioritized although it is not the last in the list and both reference a classname, I didn't get that.
Sorry, my bad, appareantly StackBlitz didn't copy something here. Yes, thank you, that's the same I had there.
Got it, thx. No I agree, I wouldn't want to change that order, it makes sense that the global styles come first. But this still would leave me with the issue when using classes from the same emotion cache, that comes up when using tss-react. They are injected on the same level like the MUI ones if I'm not mistaking something, and in that case, the custom ones should be prioritized, that's what my PR is trying to solve. |
Thanks for taking the time to write this up @eps1lon! Yes it looks like I was under the wrong impression on that one. |
This is exactly @eps1lon's point, it works now because the For tss-react, I am not sure what the custom nextjs helpers do, I would suggest start looking there. |
Hm, I had just used these helpers in the sandbox before to make a simple example based on the default example they had there. In my actual project that I'm trying to convert to v5, I base the logic off the example in the material-ui repo, with no specific logic for |
Ok, going to close this one then. Feel free to link this issue if you open a new one there so that people will have context. |
Yup, already done earlier: garronej/tss-react#17 |
@mnajdova Everything now works as expected in the latest version of |
Current Behavior 😯
Right now, styling a component by passing
classes
is not possible anymore.This might be a most simple example:
The button background would still have the primary color.
The resulting
className
on the element would be(leaving aside focus and so on): "myClassName MuiButton-root"Expected Behavior 🤔
The custom class would have priority over the internal one:
"MuiButton-root myClassName"
Steps to Reproduce 🕹
Steps:
Context 🔦
This behavior is new in v5 - Passing
classes
is a common pattern in v4 to customize MUI Components.This is a blocker for an easy transition to v5 using tss-react
Solution 🚀
This could be fixed in a two-line change by moving this:
https://github.com/mui-org/material-ui/blob/64c527a818756ae1b0ba88a3236f11b6060a7315/packages/material-ui-unstyled/src/composeClasses/composeClasses.ts#L18
to come before the if-block here:
https://github.com/mui-org/material-ui/blob/64c527a818756ae1b0ba88a3236f11b6060a7315/packages/material-ui-unstyled/src/composeClasses/composeClasses.ts#L15
I don't know if there is any internal use of passing
classes
which might break due to that change since it expects the opposite behavior - should be rather simple to verify though.Your Environment 🌎
`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: