-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Comments
The goal of having CSS variables is to have a single instance of <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 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 |
@mattstobbs yeah, you can override component props in nested theme providers as shown in this demo: https://codesandbox.io/p/sandbox/clever-ives-f42vn3?workspaceId=d6fa470f-2079-42a1-aa65-469b49bbde6c @siriwatknp I wonder if we could improve the logic to avoid throwing an error. I'd expect to be able to spread one theme object into another. I wonder if ignoring |
@aarongarciah In this case for @mattstobbs, it's better to use https://codesandbox.io/p/sandbox/peaceful-bhabha-l2m634?file=%2Fsrc%2FDemo.tsx%3A32%2C17 What we missed is docs about |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @mattstobbs How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
We should have an API for nested CSS variables injection. We can detect if a nested ThemeProvider is used, and only add the CSS vars that are overriden. We shouldn't recommend to people to use different API if they want nested themes in my opinion. |
Steps to reproduce
Steps:
createTheme
is called with the outer theme spread into it, an error is thrown. This seems to be because the outer theme has avars
field, which is not allowed as an input tocreateTheme
.Current behavior
The following error is thrown:
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
Search keywords: Extending theme
The text was updated successfully, but these errors were encountered: