-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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
[styles] Theme Provider is not working correctly when nested themes are used #15914
Comments
@naman13924 You need a top-level theme provider: https://codesandbox.io/s/material-demo-29fxp. |
So basically multiple theme roots are not allowed on a page. There has to be a single provider root. Wouldn't that make multiple react trees impossible on a page? |
@eps1lon I have demonstrated the simplest workaround in the codesandbox: one ThemeProvider at the top of the tree. |
Pretty sure that's what I said
I would guess this is a pretty basic use case when using multiple react roots. That shouldn't be hidden behind nested API docs. |
@eps1lon Yes, I have repeated it for people that might have the problem and that skip read. It's a really important point. I do agree that there is close to no documentation it about and that we should address the problem. I wasn't sure it would impact many people. I suspect few people have this use case, but I don't have enough data to defend this claim. I will work on it as more people report it (hence gain priority over the other issues) |
Am I right in thinking that this is what has broken support of our v0.X theme nesting? |
Also wondering that @Ajaay. We have a v0 theme nested in a now v4 theme and are running into a ton of issues that seem to be theme related. |
@ztoben It's still supported (but not encouraged). Do you have a minimale reproduction? |
If it makes any difference I didn’t have problems with v4.0.0. Only when I updated to v4.0.1 did they start
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Olivier Tassinari <notifications@github.com>
Sent: Wednesday, May 29, 2019 2:44:15 PM
To: mui-org/material-ui
Cc: Ajaay; Mention
Subject: Re: [mui-org/material-ui] Theme Provider is not working correctly when nested themes are used (#15914)
@ztoben<https://github.com/ztoben> It's still supported. Do you have a minimale reproduction?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#15914>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ABO3LP66YW7LHLRA6I4BG3TPX2CC7ANCNFSM4HQCI2HQ>.
|
@oliviertassinari, it's in a private repo that's pretty complex. We're working on getting a reproduction case set up. |
@oliviertassinari, @eps1lon - can you please clarify what you mean by "Multiple theme roots are not allowed on a page. There has to be a single provider root"? In my sandbox example, I have a single provider root in |
With root I mean a provider which has no parent provider: <React.Fragment>
<ThemeProvider /> /* root */
<ThemeProvider /> /* root */
<ThemeProvider /> /* root */
</React.Fragment> <React.Fragment>
<ThemeProvider> /* root */
<ThemeProvider /> /* child */
<ThemeProvider /> /* child */
</ThemeProvider>
</React.Fragment> Edit: |
@eps1lon is right, in order to achieve this, you need a light theme provider that wraps your whole tree, a dark theme provider that wraps the side nav and a |
Thanks for your responses @eps1lon & @oliviertassinari. Now I am clear that in my example, I have one light theme provider at the root of the page and one dark theme provider as a child that wraps the sidebar - which is essentially a The issue is, as @eps1lon mentioned, the dark theme relies on global styles. The Given this approach, I don't think nested themes will automatically switch between light and dark themes. However, I came up with a simple trick based on @oliviertassinari's hint that seems to work. I have introduced a Thanks again for your help. |
Looks good to me. If your side nav stays as simple, I would try not to nest two themes. |
@oliviertassinari, the side nav will stay that simple. I was relying on |
You could apply the dark background color and color present in the theme. So yes, with a simple div. |
Just tried a simple div with dark background and white color - it does not work nicely. Here's an example. While the list text works because it uses |
Makes sense. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sure I understand, but guess my question boils down to a solution to reduce the number of |
An update, this issue is being resolved in v5 thanks to #22342. So far, we have migrated the Slider in the lab, where this can be tested. It no longer generates global class names, class names are hashed. However, the components have global class names that can be targeted. |
I'm affected by this issue, we have multiple react roots on the page and because of this the classnames that are generated aren't deterministic, causing components to re-render unnecessarily. |
Ditto on this issue - I'm working on a single-spa app as @pkhodaveissi mentioned, and having multiple React apps on the page that each use a (Note: This is only happening on pages with separately webpack-aged apps, if you know what I mean.) I confirmed that upgrading to MUI 5 would indeed resolve the issue for us, but seeing as MUI 5 isn't ready yet and we have quite a lot of migration tasks to do from 4 -> 5, I'm looking for an interim solution in the meantime. const generateClassName = createGenerateClassName({
disableGlobal: true, // or seed: 'something_unique' ?
});
...
const App = () => {
...
return (
<StylesProvider generateClassName={generateClassName}>
<MuiThemeProvider theme={theme}>
....
</MuiThemeProvider>
</StylesProvider>
);
}; I couldn't quite figure out if StylesProvider is meant to replace or supplement MuiThemeProvider from the documentation. |
@ahayes91 If you have multiple react roots, I'm closing as this is fixed in v5. |
To be brutally honest, details about the ThemeProvider's technology should not even matter here, not even a little bit, or at least from the user's point of view. Concerns about a feature's design are almost always of greater significance than the technology that enables said design. And with this said, regarding the fact that multiple ThemeProviders must have a parent ThemeProvider element, there is an unasked yet obvious question that warrants asking: where is the user expected to learn about this somewhat esoteric yet potentially code-breaking ThemeProvider fact? In the issues section of the mui Github? No. tldr: this should not be a design feature to begin with, but since it is and hasn't been changed yet, the docs should be extremely clear about it, which they aren't. |
@Johnrobmiller It's a v4 only issue, a version we don't actively support anymore. This issue is gone in v5. We gave up on styling with global class names. This structural change harms the DX of customizations but yields other interesting features that make it a significant net positive. |
@oliviertassinari Sorry to bring this up again but we are in a middle of a migration from v4 to v5 and i would like to minimize the amount of research and surprises i'm going to face. For example, given this style: root: {
'& .MuiBadge-badge': {
// something
}
} The seed won't be chained to the The reason i'm bringing this is up, is to ask if the solution that was introduced in v5 bypass or address this issue in any way? |
Earlier themes were working fine before the 4.0 release but now the nested themes are overriding the themes which are outside its scope
for example in your documentation example https://codesandbox.io/s/material-demo-hdy1n
replace the demo.js code with the attached code in demo.txt file
demo.txt
The theme of components outside the nested themes are getting overridden by the nested one.
I am facing this challenge in my whole project as I have many nested themes and the ui has changed unexpectedly due to this,
earlier it was working fine with v3.9
The text was updated successfully, but these errors were encountered: