Skip to content
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

CSS Variables Being Overridden After Page Load #43482

Closed
jamesga1 opened this issue Aug 27, 2024 · 5 comments · Fixed by #43632
Closed

CSS Variables Being Overridden After Page Load #43482

jamesga1 opened this issue Aug 27, 2024 · 5 comments · Fixed by #43632
Assignees
Labels
bug 🐛 Something doesn't work customization: theme Centered around the theming features support: question Community support but can be turned into an improvement v6.x

Comments

@jamesga1
Copy link

jamesga1 commented Aug 27, 2024

Sorry if there's a better location to ask this and feel free to move it wherever.

We've recently updated from v5.16.7 up to 6.0.0 and would like to use css-variables, but ideally without having to populate createThemes with a light and dark mode palette.

theme.tsx

export const theme = createTheme({
  cssVariables: {
    colorSchemeSelector: '.theme-%s',
  },
  colorSchemes: { light: true, dark: true },
  breakpoints: {
    values: {
      xSmall: 0,
      small: 600,
      medium: 960,
      large: 1280,
      xLarge: 1440,
      xxLarge: 1920,

      // generic device sizes
      mobile: 425,
      tablet: 640,
      laptop: 1024,
      desktop: 1280,
    },
  },
  typography: {
    fontFamily: 'var(--font-family)',
  },
});

styles.scss

.theme-light {
  --mui-palette-primary-main: red;
  --mui-palette-primary-dark: maroon;
}

.theme-dark {
  --mui-palette-primary-main: olive;
  --mui-palette-primary-dark: green;
}

If we update the styles.scss with a new value and HMR updates the page, we are able to see the correct color, but as soon as we refresh the page, the colors will default back to their stock values:

<Button variant="contained" color="primary" type="submit"  disabled={isSubmitting}>
  Submit
</Button>

chrome_2kcm2Arex4

Is there a way to be able to accomplish this, or will the mui generated css variables always override those in styles.scss.

Search keywords:

@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 27, 2024
@siriwatknp
Copy link
Member

siriwatknp commented Aug 28, 2024

Thanks for asking. The root cause might be the insertion point. Try this https://mui.com/material-ui/migration/v5-style-changes/#style-library

I managed to find a workaround by wrapping the CSS theme variables with@layer:

const sheets = theme.generateStyleSheets();
const newSheets = sheets.map((sheet) => {
  return {
    "@layer tokens": sheet,
  };
});
theme.generateStyleSheets = () => newSheets;

function App() {
  return <ThemeProvider theme={theme}></ThemeProvider>
}

https://github.com/siriwatknp/material-ui-v6-vite-scss

@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information customization: theme Centered around the theming features and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 28, 2024
@siriwatknp siriwatknp added enhancement This is not a bug, nor a new feature v6.x support: question Community support but can be turned into an improvement and removed status: waiting for author Issue with insufficient information labels Aug 28, 2024
@fetters5
Copy link

When using NX we have noticed that the emotion global style are inserted last and thus override anything in styles.css even if I add <StyledEngineProvider injectFirst > above the theme provider.

If I make the change the hot reload moves the styles related css to the bottom

image

Not sure if this is the intention but will try the work around

@siriwatknp
Copy link
Member

When using NX we have noticed that the emotion global style are inserted last and thus override anything in styles.css even if I add <StyledEngineProvider injectFirst > above the theme provider.

This is a known issue for Emotion emotion-js/emotion#2790. It should be fixed from the Emotion side.

@siriwatknp siriwatknp added the external dependency Blocked by external dependency, we can’t do anything about it label Aug 29, 2024
@siriwatknp siriwatknp added bug 🐛 Something doesn't work and removed external dependency Blocked by external dependency, we can’t do anything about it enhancement This is not a bug, nor a new feature labels Sep 6, 2024
@siriwatknp
Copy link
Member

I mark this as a bug from Material UI user perspective. The <StyledEngineProvider injectFirst> should work but it's not. I have a PR to fix this issue.

Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

We value your feedback @jamesga1! How was your experience with our support team?
If you could spare a moment, we'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work customization: theme Centered around the theming features support: question Community support but can be turned into an improvement v6.x
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants