[RFC] Strategy for adopting CSS variables in @mui/material
#30485
Replies: 6 comments 10 replies
-
@siriwatknp Since |
Beta Was this translation helpful? Give feedback.
-
Trying to workaround the lack of css properties, I came around an issue:
@siriwatknp Did you already work something out? |
Beta Was this translation helpful? Give feedback.
-
Currently, it's possible to make this work for some components (like the Select component), but not for others (like the Button component):
The reason I'm saying this is: why would just simply allowing CSS variables inside the theme be a bad idea ? |
Beta Was this translation helpful? Give feedback.
-
Maybe we should still export the palette with the specific values as part of the theme, and allow developers to use the JS transformations. We should not make the DX worse than before. Apart from colors, I don't really see another part of the theme that would be necessary to be available as a plan JS object. |
Beta Was this translation helpful? Give feedback.
-
Off-topic: Isn't it possible even now for people that want to try this to use the |
Beta Was this translation helpful? Give feedback.
-
The API is available in Material UI and Joy UI, so I'm closing this. |
Beta Was this translation helpful? Give feedback.
-
Since most checklists in #27651 are done and browser support is also promising (96%), we are in a good stage to move forward to bring CSS variables to
@mui/material
. We believe that it will open a lot of opportunities described in #27651.Overall, CSS variables can be applied to at least 2 levels.
theme level
: Turn theme (JS object) into CSS variables that all components inside the tree can use.component level
: A specific component can have its own meaningful variables that communicate within the component.At the first stage, we want to achieve only the
theme level
because this has obvious benefits such asAlso, we need some time to validate if CSS variables is the real deal, then we can move forward with the
component level
which requires more effort and care.Goal
The intention of this RFC is to share the strategy and iterate with the community on the best approach of adopting CSS variables in
@mui/material
(both for existing and new applications). However, we don't expect to spend any effort on the implementation/migration in Q1 2022.The proposed strategy aims to go with NO breaking change but it might change as the discussion goes.
Duration
I think we can keep this RFC at least for a quarter (Q1 2022) and see the feedback from everyone about the final strategy.
Expecting DX
These are the things that the final strategy should aim for:
@mui/material
should be considered as a feature, meaning that developers can decide not to use it if they don't want to.Proposed strategy
The final strategy should answer all of these questions:
The proposed strategy below aims toward NO breaking change. Please review and ask questions as much as possible so that we have the best way to proceed with.
How to generate CSS variables from the theme?
Expose a provider that produces CSS variables from the theme input and attaches them globally.
CssVarsProvider
will read the theme, create variables, and attach them globally viaGlobalStyles
. Here is an example of some variables from the theme:Which theme tokens will be generated as CSS variables?
According to the current default theme structure, these are the estimated tokens that will be generated as CSS variables.
palette.*
(excludingpalette.mode
,palette.contrastThreshold
,palette.tonalOffset
because they are for JS usage)zIndex.*
shadows.*
typography
htmlFontSize
fontFamily
fontSize
fontWeightLight
fontWeightRegular
fontWeightMedium
fontWeightBold
How does the component access CSS variables inside the theme?
The safest way is to add a new field in the theme to reference CSS variables. The
vars
key contains all references to the generated CSS variables.Then a component can use it like this:
The benefits of using new key (
vars
) are:theme.vars
. If there is notheme.vars
, fallback to existing behavior.The downsides
How to deal with the current color manipulation at runtime?
There are a lot of components that are using javascript to manipulate color from the theme at runtime. For example:
There is no way to convert color using only CSS at the moment of writing (it is possible in CSS color level 5 but that's a long way to go).
The only workaround with CSS variables is to create a channel variable with a combination of an opacity variable:
With this approach, the implementation will be like this:
What needs to be done for existing apps to adopt CSS variables?
1. Replace
ThemeProvider
withCssVarsProvider
2. Move palette inside
colorSchemes
This is quite different from
ThemeProvider
approach. It changes from switching between themes to preparing all the themes beforehand. This step is required if you have a custom theme to make the flicker issue for SSR goes away.You don't need to worry about this process, we will create a thorough docs for this part again.
3. Update toggle UI to use
useColorScheme
hookThe
CssVarsProvider
handles the local storage sync for you, so you can remove that part from the codebase. (We will provide an option to use custom storage if you'd like)cc @mui-org/core @mui-org/x @mui-org/contributors
Beta Was this translation helpful? Give feedback.
All reactions