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.

Migrate Material UI components to support theme CSS variables

See original GitHub issue

We need your help!

Starting from 5.6.0 we are adding a new experimental API for supporting CSS variables in the Material UI components. You can see more details for this new feature on https://github.com/mui/material-ui/issues/27651 or our docs page.

The changes are not breaking so we can safely apply them in the components during the lifecycle of v5.

Initially, the idea was implemented in one component, the Button, but we would need this to be applied to all components. This issue will help track the progress of the migration.

Contribution guides

  1. Pick a component in the “Ready to be picked up” section
  2. Read the “Patterns” section
  3. Replace the use of these theme tokens to (theme.vars || theme).*.
    • theme.palette.*
    • theme.shape.borderRadius
    • theme.shadows
    • theme.zIndex
  4. Create a new page using the template at docs/pages/experiments/material-ui/css-vars-template.tsx. Ex. if you pick Accordion component, create the page (docs/pages/experiments/material-ui/accordion.tsx) and copy the template. Then, import the Accordion and render it on the page.
  5. Verify that it works
    1. Run yarn docs:dev
    2. go to URL /experiments/material-ui/{component}/ and check the component (it should look the same)
    3. Open the DevTool and toggle the dark mode button at the top of the page, the classNames of the component should not change.
  6. Open a PR and put the URL to the the end of the component below (so that everyone knows it has been taken)
  7. Tag @mui/core to review

If you come across any issue or are unsure of the changes, feel free to open a PR and describe the problem + tag @mui/core for help.

Patterns

Straightforward

- borderRadius: theme.shape.borderRadius
+ borderRadius: (theme.vars || theme).shape.borderRadius

- border: `1px solid ${theme.palette.action.disabledBackground}`,
+ border: `1px solid ${(theme.vars || theme).palette.action.disabledBackground}`,

color manipulation

Some components are using a conditional statement to switch values between modes which causes the flicker issue for SSR application. To fix this, we have provided channel colors to be combined with opacity (thanks to CSS variables, this seems to be the only solution so far).

The channel colors in the theme palettes are mainChannel, lightChannel, darkChannel and contrastTextChannel.

- backgroundColor: alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity),
+ backgroundColor: theme.vars ?
+   `rgba(${theme.vars.palette.primary.mainChannel} / ${theme.vars.palette.action.selectedOpacity})` :
+   alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity),

- backgroundColor: alpha(
-       theme.palette.primary.main,
-       theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
-     ),
+ backgroundColor: theme.vars ?
+       `rgba(${theme.vars.palette.primary.mainChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.focusOpacity}))` :
+       alpha(...)

List of components

Ready to be picked up

✅ First set is done!

Need clarification

These components contain hardcoded values between modes. We will put the guidelines for each of these components and moved them to “Ready to be picked up”.

Proposal: https://github.com/mui/material-ui/issues/32049#issuecomment-1124601805

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:48
  • Comments:40 (29 by maintainers)

github_iconTop GitHub Comments

2reactions
siriwatknpcommented, Apr 29, 2022

Could we suppose that for components that don’t use theme we could mark them as don’t take? For example List component.

I have updated the list.

2reactions
vicasascommented, Apr 28, 2022

Could we suppose that for components that don’t use theme we could mark them as don’t take? For example List component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to CSS theme variables - Material UI - MUI
This is a guide that shows how to migrate an existing Material UI project to CSS theme variables. This migration offers a solution...
Read more >
Theming React Components with CSS Variables | by Eden Ella
Set the custom CSS properties in their corresponding classes. ... Add the default theme class to the App component. ... Create a JS...
Read more >
CSS variables in Material ui - createMuiTheme - Stack Overflow
Actual (21.12.2022). CSS variables are supported now by the MUI as an experimental API, check documentation here. Outdated.
Read more >
تويتر \ تغريدة - Twitter
components! Check out the. @github. issue for details on how to get involved: github.com. Migrate Material UI components to support theme CSS variables...
Read more >
A 55% Performance Improvement Upgrading Material-UI from ...
Styled components wrap another component with a set of styles, and from there you just add the component to your react tree. At...
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