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

Extending the outer theme with a nested ThemeProvider throws an error if css variables are active #44429

Open
mattstobbs opened this issue Nov 16, 2024 · 2 comments
Assignees
Labels
customization: theme Centered around the theming features status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@mattstobbs
Copy link

mattstobbs commented Nov 16, 2024

Steps to reproduce

Steps:

  1. Open this link to live example: (required) https://codesandbox.io/p/sandbox/exciting-rhodes-zk9kwk
  2. Enable css variables in the outer ThemeProvider
  3. Nest a ThemeProvider within the outer ThemeProvider
  4. Attempt to extend the outer theme following the guidance in Theming (the codesandbox is adapted straight from that example)
  5. When createTheme is called with the outer theme spread into it, an error is thrown. This seems to be because the outer theme has a vars field, which is not allowed as an input to createTheme.

Current behavior

The following error is thrown:

MUI: `vars` is a private field used for CSS variables support.
Please use another name.

Expected behavior

An error should not be thrown and the outer theme should be able to be extended even if CSS variables are enabled.

Context

We're trying to extend the outer theme with additional styles, as described in the docs.

I'm happy to raise a PR to address this issue, once a fix has been agreed.

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: Extending theme

@mattstobbs mattstobbs added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 16, 2024
@zannager zannager added the customization: theme Centered around the theming features label Nov 18, 2024
@siriwatknp
Copy link
Member

siriwatknp commented Nov 18, 2024

The goal of having CSS variables is to have a single instance of createTheme for your app. If you want to customize some part of the app with different values, you can override some tokens to a specific scope:

<ThemeProvider theme={theme}>

  ….
  
      <div style={{ '--mui-palette-primary-main':  }}></div>
      
      
</ThemeProvider>

create nested themes would create a huge size of stylesheet with duplicate tokens.

However, if you really want to do it, you can get away with this:

<ThemeProvider
  theme={(theme: Theme) => {
    const { vars, …rest } = theme;
    return createTheme({
      ...rest,
      palette: {
        ...rest.palette,
        primary: {
          main: green[500],
        },
      },
    })
  }}
>
  <Checkbox defaultChecked />
  <Checkbox defaultChecked color="secondary" />
</ThemeProvider>

@siriwatknp siriwatknp added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 18, 2024
@mattstobbs
Copy link
Author

@siriwatknp Thanks for your reply!

Your response answers the case in the attached sandbox. For additional context for the situation we're in, we're not looking to override the tokens within the inner theme provider, we mostly override the components (e.g. set the default variant for text fields to "outlined"). Destructuring vars from the theme works well in that case though.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 18, 2024
@DiegoAndai DiegoAndai moved this to Backlog in Material UI Nov 21, 2024
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 status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
Status: Backlog
Development

No branches or pull requests

3 participants