question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v5] CSS injection order is wrong

See original GitHub issue

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):

const styles = theme => ({
    title: {
        [theme.breakpoints.down('sm')]: theme.typography.body1
    }
})

function App({ classes }) {
    return <Typography variant="h5" classes={{h5:classes.title}}>Hello</Typography>;
}

const StyledApp = withStyles(styles)(App);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:22
  • Comments:59 (35 by maintainers)

github_iconTop GitHub Comments

14reactions
goffioulcommented, Dec 24, 2020

I was aware of injectFirst attribute, but it didn’t work in my app. After some testing with the codesandbox, it appears the problem come from importing StylesProvider from @material-ui/core/styles (v4 code, which I’m currently migrating) instead of @material-ui/core. You can verify that in your codesandbox.

11reactions
mnajdovacommented, Jan 28, 2021

@goffioul now that the Typography, Button, etc components are migrated to emotion, you need to use the StylesProvider exported from ‘@material-ui/core’ with the injectFirst option, in order for the CSS injection order to be correct between emotion and JSS. It is explained here: https://next.material-ui.com/guides/interoperability/#css-injection-order.

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 <Demo /> is wrapped with the StylesProvider component.

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")
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Style tag seems to be in the wrong order when updating to ...
I found something in the doc of material-ui about css injection order but seems like it doesn't relate to this kind of situation....
Read more >
Material UI v5 CSS injection order - CodeSandbox
VS Code's tsserver was deleted by another application such as a misbehaving virus detection tool. Please reinstall VS Code. Manage Extension.
Read more >
Releases - styled-components
Escape every CSS ident character necessary when converting component ... Backport fix where classnames are composed in the wrong order if custom class...
Read more >
Style library interoperability - Material UI - MUI
Note: Most CSS-in-JS solutions inject their styles at the bottom of the HTML ... In order for the injection order to still be...
Read more >
JSS integration with React
Critical CSS extraction - only CSS from rendered components gets extracted. ... Custom setup; Multi-tree setup; Injection order; Usage with TypeScript ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found