[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior when using styled (passing a function as a param for styles)
See original GitHub issue- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
All styles should be internal in DOM, i.e. the actual CSS should be inlined in the <style>
's tags in the <head>
.
Why? Because Prerendering doesn’t work otherwise as the styles, instead of being internal, are directly inserted into the CSSStyleSheet, using insertRule
(the process is so-called the speedy mode). More info:
Current Behavior 😯
Common Header for the following two examples:
import { styled } from "@material-ui/styles"
const component = (props) => <div {...props} /> // just an example
- Works great if using without accessing props, resulting in internal css, rendered in DOM:
export const Div = styled(component)({
width: `100%`,
})
- Uses unwanted CSSStyleSheet.insertRule, not rendering CSS in DOM, directly applying styles to the CSSStyleSheet:
export const Div = styled(component)((props) => ({ // IMPORTANT! Note passing the function
width: `100%`,
}))
Your Environment 🌎
"dependencies": {
"@material-ui/core": "4.0.0-beta.1",
"@material-ui/styles": "4.0.0-beta.1",
"react": "16.8.6",
"react-dom": "16.8.6"
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:18 (9 by maintainers)
Top Results From Across the Web
[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior ...
[4.0.1-beta] Unwanted CSSStyleSheet.insertRule behavior when using styled (passing a function as a param for styles) #15660.
Read more >CSSStyleSheet.insertRule() - Web APIs | MDN
The CSSStyleSheet.insertRule() method inserts a new CSS rule into the current style sheet.
Read more >Passing props to MUI styles - reactjs - Stack Overflow
Solution for how to use both props and theme in material ui : ... Seriously, if you're styling a function component, use makeStyles...
Read more >Advanced Usage - styled-components
This function will receive the parent theme, that is from another <ThemeProvider> higher up the tree. This way themes themselves can be made...
Read more >Demystifying styled-components - Josh W Comeau
What about the styles parameter that we passed to the h1 function? How does it get used? When we render the Title component,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is best discussed in an issue in the Jss repo. It seems like the decision was made consciously. Please reference the original issue there so that anybody searching for a solution here is properly forwarded.
It would be interesting to know how other styling solution solve this.
@o-alexandrov I would suggest you change your tone to be less prescriptive. At least for me it’s not obvious from this issue why the current approach was chosen. You saying that it should not be chosen sounds like you either know better and don’t want to share knowledge or you don’t know the full story and act like you do. Either way isn’t constructive at all.
@eps1lon
Please be informed that in this post above I actually:
I am sad to see such misinterpretation of the discussion, and I am sincerely sorry for the tone that you seem to interpreted as prescriptive. On the other hand, I hope you consider to offer more understanding to those, like me, whose English is not the first language and thus would know, that the tone might simply be something you are not used to growing up in a different culture.