-
-
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
[v5] CSS injection order is wrong #24109
Comments
@goffioul now that the Typography, Button, etc components are migrated to emotion, you need to use the This is only required for the time of the migration (v5-alpha phase). Once we do no longer depend on JSS, the order should be correct. Here is a codesandbox with a working example - https://codesandbox.io/s/devtools-material-demo-forked-ethyg?file=/index.js You will notice that the import * as React from "react";
import ReactDOM from "react-dom";
import StylesEngineProvider from "@material-ui/core/StylesEngineProvider";
import Demo from "./demo";
ReactDOM.render(
<StyledEngineProvider injectFirst>
<Demo />
</StyledEngineProvider>,
document.querySelector("#root")
); |
I was aware of |
It's actually a bit more complicated than that, and I actually need to use the 2 styles providers, like the following: import { render } from 'react-dom';
import { StylesProvider, jssPreset } from '@material-ui/core/styles';
import StyledEngineProvider from '@material-ui/core/StyledEngineProvider';
import { create } from 'jss';
import rtl from 'jss-rtl';
const jss = create({ plugins: [...jssPreset().plugins, rtl()] });
render(
<StyledEngineProvider injectFirst>
<StylesProvider jss={jss}>
<App />
</StylesProvider>
</StyledEngineProvider>,
document.getElementById('app')
); |
@goffioul Thanks for raising it. We are still looking for a proper solution to this problem. In v5, we have changed the default import. What if the best answer was? import { StylesProvider, jssPreset } from '@material-ui/styles';
import StyledEngineProvider from '@material-ui/core/StyledEngineProvider'; Notice how:
|
As a developer using material-ui (not developing it), initially I totally missed the different import. Using the same name obviously adds to the confusion, because old code is already importing If the target is to have a drop-in replacement for the v4 styling engine, I'd say using the same name is a good option. The problem I'm having is just a transient situation in alpha-grade code, I can live with that. Maybe a warning when using the old StylesProvider would help detect such scenario. If a drop-in replacement is not on the table, then I'd go with a different name. |
The 5.0.0 alpha seems to awash with duplicate imports. I was using StyleProvider via @material-ui/styled-engine. Is that the correct one? Ideally, the import of the StyleProvider should be consistent; possibly as a wrapper around both JSS and emotion StyleProviders. That way, legacy components still using JSS are still supported, while the newer implementations using Emotion will work seamlessly. Once JSS is fully removed, it would cause any breaking changes. |
@mainfraame See my proposal in #24109 (comment). We need two different style providers as they are two different components with different APIs, they might even be required at the same time when both JSS and emotion/sc are used at the same time in the page. |
@oliviertassinari thanks for your reply. I do understand that for the time being both providers will need to be supported. I agree with your point about distinct naming and think that would be the best option for reducing complexity for devs. It seems to me like just using Can anyone comment on which styled should be used? I see the legacy styled is using jss still and experimental styled is using emotion. Would you recommend just using experimental styled for all override styling, or would u suggest to continue to use the jss implementation? |
@mainfraame Prefer using modules coming from |
The solution in #24109 (comment) worked for me. For others who might be lacking the |
@oliviertassinari any plans to add more to the docs regarding the styled/experimentalStyled hooks? |
Because emotion styles now have to be injected before JSS styles, emotion's css prop can no longer be used to override styles of components that are still using JSS internally unless |
I'd be happy to share a recording provided it can be privatly |
@mnajdova can I share privately? Where should I send it? (note: as above, this won't be a minimal reproduction unfortunately) |
As an exception - I can help you if you send me a replay of the problem privately and describe the issue you are facing in detail (both what happens and what is your expected result) |
Found the issue was on my end. Apparently, the codemod missed some of the |
Wouldn't be possible without a tip from @mnajdova - if you see styles coming from a "static" class name (like |
Correct me if I'm wrong, but with const jss = create({
...jssPreset(),
insertionPoint: 'jss'
}); And my SPA <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base href="<%= htmlWebpackPlugin.options.baseHref %>" target="_blank">
<meta name="viewport" content="initial-scale=1, maximum-scale=5">
<meta http-equiv="X-UA-compatible" content="IE=Edge">
<title><%= htmlWebpackPlugin.options.title %></title>
<!-- jss -->
</head>
<body>
<div id="app"></div>
</body>
</html> So now I am migrating a toy project and I have to use Chrome is showing 713 DOM nodes inside |
if you're using |
Why is use of Starting a new app and wanted do things the "right" way instead of using deprecated JSS. However while using v5 with emotion we had to come here to learn about Once we added in this secret super power Component in _app.js our styles were finally applying correctly. UPDATE: Didn't realize it had changed from @material-ui to @mui. Double checked my deps in package.json and saw entries for @material-ui and @mui. Reinstalled with just @mui and updated import paths properly fixed this issue. |
Tried my best to isolate the CSS injection order issue. You can re-create the issue by clicking the "Go to the about page" link, and hit the refresh icon. Link: https://codesandbox.io/s/flamboyant-hooks-hdq1b?file=/pages/about.tsx /pages/about.tsx const CustomButton = styled(Button)`
background: pink;
color: black;
`; /pages/_app.tsx export default function MyApp(props: AppProps) {
const { Component, pageProps } = props;
return (
<React.Fragment>
...
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</StyledEngineProvider>
</React.Fragment>
);
} |
@paullaros I'm unsure why this happens - I'm not familiar enough with Styled Components. It seems though that the final component has more than a single SC-based class name and, in fact, the injection order is different in both situations - leading to a different visual appearance. I've verified that the "pink classname" is being passed here within Just a note - this situation would not happen with Emotion. We have explicit logic to decode "registered class names" from the received |
There hasn't been any activity on the issue for over a month and a fix was provided for the initial issue. I am closing it. |
FYI if anyone else runs into an issue with styling, injection order got messed up by my import of So, peer dep issue, it uses MUI v4 (at a maximum). I do not know the exact reason why this occurs, but my theory w/ very little knowledge of CSS engines is that the loading of its module may perform some side effect as the import chain is traversed and an insertion to the styles in |
doesn't work well with SSR if you are having issues with SSR and the emotion cache giving nothing (which may not be obvious) it's because of the StyledEngineProvider. On the other hand without StyledEngineProvider then non-SSR mechanism don't work well. There was nothing wrong with JSS, but emotion and JSS "DO NOT" play nice with each other. I've even had situations where the render changes unexpectedly as if hitting some race conditions. For anyone considering migrating to v5 if you are happy with v4 it's not worth it. It's been 2 weeks for me, and I think I have to rewrite everything to drop JSS because emotion and JSS refuse to work together, I've tried every trick in the book, it's not reliable. |
@onzag do you have a reproduction that you can share? Otherwise we cannot act on a statement without any prove/reproduction.
Yes they do, you just have to configure them properly. We used them on our documentation while migration all components, and we didn't have issues.
On the contrary, this is required for making sure that the styles coming from JSS comes after the styles generated from emotion.
Again, create a small reproduction and you are likely going to find the issue, or we can take a look into it. It's a bit misleading to say upgrading is not worth if you haven't actually use v5 :) The migration will take some time, and we've tried to create codemod for most of the things we thought we could automate, but we believe it is indeed worth migrating. |
I can confirm this. If you have SSR and add |
@pkaufi we have several examples to showcase how SSR should be configured, for example using nextjs, remix etc. You can find more on this here - https://mui.com/getting-started/example-projects/#official-examples |
Here is how our docs is configured on using both emotion and JSS - https://github.com/mui/material-ui/blob/master/docs/pages/_document.js |
@mnajdova Thank you for your response. I actually used one of your examples and created a reproduction repository of what I was talking about above: https://github.com/pkaufi/ssr-reproduction As you can see, |
Ah ok, I see the confusion now. The examples use custom emotion cache, which means that it is enough toa add Reference: https://mui.com/guides/interoperability/#main-content If you want to use both emotion and JSS, you should configure and add the styles in the correct order for SSR, this is how we do it for our documentation: https://github.com/mui/material-ui/blob/master/docs/pages/_document.js |
Thank you very much for the explanation, this helped a lot. I think I got confused with all the possibilities and namings. It's much more clear now :) |
Glad that we resolved the confusion @pkaufi :) |
I'm using the following pattern in my code, but it doesn't work anymore in v5 (tested with 5.0.0-alpha.20). My custom styles get overwritten by other styles (likely coming from the new styling mechanism):
The text was updated successfully, but these errors were encountered: