-
-
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
[styles] Generate global class names #15140
[styles] Generate global class names #15140
Conversation
1e3bc96
to
227b29b
Compare
@material-ui/core: parsed: +0.10% , gzip: +0.16% Details of bundle changes.Comparing: 1155be4...35e8f3f
|
This comment has been minimized.
This comment has been minimized.
A big part of the discussion is why a CSS-in-JSS was chosen previously. We can't just discuss these issues in isolation while ignoring history. Otherwise we'll be going in circles. |
You can find one part of the history in https://github.com/oliviertassinari/a-journey-toward-better-style: the #Interesting dimensions. I don't see how it's a regression with the considered dimensions. If I had to redo it today, I would consider the performance and overrides potentiel dimensions as well. |
cb6dac3
to
fa07c10
Compare
9a1fdbe
to
92f280e
Compare
25cc354
to
9cc4b99
Compare
2574943
to
4c237d9
Compare
@oliviertassinari The inexactness of the So with this approach, is it true that the only way to explicitly control the seed for nested themes is to call |
A leading dash is added before the index counter. It can be used to discriminate the style rules, well spotted.
Yes, it's correct. |
@oliviertassinari I do have one concern related to my previous comment. I think the following change causes the prefix selector approach to be unreliable:
Now if I want to target |
I think that you are correct one more time. If the class names were Disabling the root shorhand would work too. I like that better actually. |
@oliviertassinari Are you meaning you would only disable the root shorthand when in a nested theme? The goal would be to allow customizations to be done in a way that would work regardless of nesting. When supporting nesting, it would be cleaner to be able to use
which would work, but is a lot uglier looking. I remember seeing discussion somewhere recently regarding the shorthand for "root", but I don't remember where or how strongly people felt about this. To me, I think the resulting classes match up more consistently with the component CSS documentation without the shorthand (i.e. leaving "-root" as part of the class name) and it would seem confusing for it to be inconsistent between nested and non-nested scenarios. |
Co-Authored-By: oliviertassinari <[email protected]>
Co-Authored-By: oliviertassinari <[email protected]>
Co-Authored-By: oliviertassinari <[email protected]>
Co-Authored-By: oliviertassinari <[email protected]>
Co-Authored-By: oliviertassinari <[email protected]>
Co-Authored-By: oliviertassinari <[email protected]>
2bc4ba1
to
e925dc2
Compare
e925dc2
to
35e8f3f
Compare
@ryancogswell I have moved the seed to prefix the generated class name, I have removed the |
Just to give you some feedback on this topic. createGenerateClassName({
disableGlobal: true
}) Reasons:
Since this is a pure developer happiness change, at the expense of performance please keep at least the option to reenable the old behaviour ( |
@HaNdTriX Thank you for the feedback! Performance should be pretty close between the two versions. We will definitely keep the |
Just to add some stats: We had an initial payload increase by Source:
|
Thanks for sharing these stats! |
@oliviertassinari I do uncounter the same challenge when using overriding with global class names when using seeds. My styles with elements targeted such as this |
Closes #15002.
Breaking change
Remove the
dangerouslyUseGlobalCSS
options (makes it the default behavior).This change is almost like we were providing default values to the
classes
API. There is one difference though. We are also using these classes to style the components. The advantage is that you can easily find the class name applying a specific CSS property. Once you know the class name to target, you can override it. It's similar to the strategy Ant Design uses. I have heard good feedback about this proposed change for overriding styles with styled components and SCSS. The disadvantage is that Material-UI has to automatically opt-out the global class names when theme nesting is used. I believe it's better than providing true default values to the classes API (that we wouldn't use to style the component). You can look at Sancho-UI as a library implementing this pattern. It's not ideal.This is an important structural change. We want to make sure we are going in the right direction. Changing it can have large impacts on our users (once they use this new API). I hope the beta phase will be enough to validate and properly test it.
I have created a new TextField customization example. For complex DOM tree structures, it's simpler, the usage of the classes API can be a mess:
or with styled components:
The RFC
It's a continuation of #14999. I would like to get people feedback on the following change.
Summary
We have many people that find the overrides of our components hard. This is an attempt to improve the situation. We had made this topic one of our priorities in the roadmap. I'm proposing a new global CSS API. I hope it can serve the 20% of the people using plain CSS and the 20% using styled-components.
I have implemented a POC, you can browse the documentation live: https://deploy-preview-15140--material-ui.netlify.com/.
Basic example
https://codesandbox.io/s/zxmxjlx34.
https://codesandbox.io/s/yjrxrj06n9
https://codesandbox.io/s/xl7w73nq2w
Motivation
Overrides
I hope that by using global CSS, we will make the overrides of the library more accessible.
When first designing the style API of the library, it's something we wanted to avoid doing. We have put the approach under a
dangerouslyUseGlobalCSS
flag. It's documented as a quick escape hatch for prototyping. I'm proposing to make it the default behavior.Right now, if you want to customize the components, your best strategy is to:
classes
APIclasses
APIThis proposal removes the need for step n°3 and n°4, and helps with n°5 with a shorthand notation for the pseudo class like .disabled, .selected, .checked that requires a specificity of 2 instead of 1.
It should solve #15002 at the same time.
Inspiration
It's simpler to take inspiration of another website when you can revert engineer it's structure. Global class names help.
Detailed design
You can think of this change as providing meaningful default values to the
classes
API.I like the strategy used by https://github.com/moroshko/react-autosuggest#theme-optional.
I think that it works better than what react-select tried: #14999 (comment).
The generated class names are close to the BEM methodology. We can take the button as an example:
Drawbacks
Alternatives
Instead of making it the default, we could make people opt-in. It's what we do with
dangerouslyUseGlobalCSS
. In practice, we would make it safe instead of unsafe.Adoption strategy
This is only a breaking change for people using
dangerouslyUseGlobalCSS
. We can update the documentation usage one step at the time. I think that we can keep demonstrating the classes API usage without the default values where it's simple enough.How we teach this
With the documentation. We can document the default class names values in the /api/.
Unresolved questions
?