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] Nested theme #34278

Open
2 tasks done
mtr1990 opened this issue Sep 11, 2022 · 6 comments
Open
2 tasks done

[CSS variables] Nested theme #34278

mtr1990 opened this issue Sep 11, 2022 · 6 comments
Assignees
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement

Comments

@mtr1990
Copy link

mtr1990 commented Sep 11, 2022

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Before when using nested themes with ThemeProvider and it worked fine.

I recently switched to CSS variables to better support next.js in Darkmode.

  const theme = experimental_extendTheme(deepmerge(defaultTheme, themeOptions));

  return <CssVarsProvider theme={theme}>{children}</CssVarsProvider>;

Above code works fine with following version dependencies:

"@emotion/cache": "^11.9.3",
"@emotion/react": "^11.9.3",
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.9.3",
"@mui/material": "^5.8.6",
"next": "^12.2.0",

But after i upgrade the dependencies get the following errors

"@emotion/cache": "^11.10.3",
"@emotion/react": "^11.10.4",
"@emotion/server": "^11.10.0",
"@emotion/styled": "^11.10.4",
"@mui/material": "^5.10.4",
"next": "^12.3.0",

Screen Shot 2022-09-11 at 14 27 10

Expected behavior 🤔

No error when setting up

Steps to reproduce 🕹

Steps:

Context 🔦

Nested theme:

 <CssVarsProvider theme={outerTheme}>
      <Checkbox defaultChecked color="secondary" />
      < CssVarsProvider theme={theme}
      >
        <Checkbox defaultChecked />
        <Checkbox defaultChecked color="secondary" />
      </CssVarsProvider >
    </CssVarsProvider>

based on CSS variables:
https://mui.com/material-ui/experimental-api/css-variables/#main-content

use CssVarsProvider instead of ThemeProvider

Sanbox : https://codesandbox.io/s/keen-lewin-bkchg9?file=/demo.tsx

Your environment 🌎

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.
@mtr1990 mtr1990 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 11, 2022
@ZeeshanTamboli ZeeshanTamboli added the customization: theme Centered around the theming features label Sep 12, 2022
@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer status: waiting for author Issue with insufficient information labels Sep 13, 2022
@siriwatknp
Copy link
Member

@mtr1990 First of all, thank you so much for opening the issue. I have been waiting for the nesting CSS variables theme.

Before we jump to a conclusion, can you first share why you need a nested CSS variables theme?

@mtr1990
Copy link
Author

mtr1990 commented Sep 13, 2022

Hi @siriwatknp ,

Thanks for your feedback on this issue

Actually the functions change color, or switch light/dark mode… I can work directly on a single theme without nesting.

But in terms of component management, it won't be optimized

Currently the structure I'm using with ThemeProvider looks like this:

    <ThemeProvider>
      <DarkModeProvider>
        <ChangePrimaryColorProvider>{children}</ChangePrimaryColorProvider>
      </DarkModeProvider>
    </ThemeProvider>

https://codesandbox.io/s/awesome-ellis-rt1ol9?file=/theme/palette.ts

You can remove functions like : or

When not in use without affecting the main structure of the theme.

I also want to apply this similar structure when using with

@pinkishincoloragain
Copy link

I have the same needs for the nested cssVarsProvider, and wondering if there will be an update with the feature.

@siriwatknp
Copy link
Member

I have the same needs for the nested cssVarsProvider, and wondering if there will be an update with the feature.

Can you share a codesandbox that I can take a look?

@Yukigamine
Copy link

Can you share a codesandbox that I can take a look?

I ran into the same issue. Here's a codesandbox using the docs example for nested themes.
https://codesandbox.io/p/devbox/flsx8m

On the root / page it's using nested with cssVariables: true and the error pictured above occurs.
If you navigate to /novars, it uses the exact example from the docs without cssVariables in the outer theme and there is no error.

@jcohen14
Copy link

Wanted to bump this. Having the same issue. My app uses the new css variables in v6 but we have needs for certain parts of the app to force light mode or force dark mode.

One of our use cases is a modal containing an iframe to a page that does not support dark mode, so we need to force the surrounding modal into light mode to keep a consistent experience. We also have a different part of the app that we want to always have the inverse color scheme of the system one (i.e. in dark mode this part of the app would be light mode and vice versa).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

6 participants