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:
Common Header for the following two examples:
import { styled } from "@material-ui/styles"
const component = (props) => <div {...props} /> // just an example
export const Div = styled(component)({
width: `100%`,
})
export const Div = styled(component)((props) => ({ // IMPORTANT! Note passing the function
width: `100%`,
}))
"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"
}
@o-alexandrov I think that it's a tradeoff you have to accept using the style function approach. I doubt there is any solution to it. Now, this is an issue for JSS, it's outside of our scope.
@kof probably has more context for this. It's not obvious to me what the issue here is. Maybe not being able to change the styles in the chrome dev tools? Maybe it would be more appropriate to speak to the people responsible for the devtools in this case.
@o-alexandrov what is the ACTUAL problem you have?
@oliviertassinari @eps1lon
I ended up solving the expressed issue with Prerendering not in the most elegant way, but unfortunately anything better is not available yet, by applying the same techniques that are expressed in the:
Steps made:
<head>
including cases (for example using styled as a function) when JSS inserts styles directly to the StyleSheet interface instead.@kof
I created a separate issue in JSS to continue there:
@eps1lon
Btw, you are correct:
However, you can change them in the Firefox browser, for example the following:
Still, changing styles doesn't worry me personally at all, as you can apply inline styles to change the behavior, if you are playing with them in the Dev Tools inspecting the element.
I still don't understand the problem ... @eps1lon do you?
@o-alexandrov It's unideal, but part of the tradeoff. In practice, I don't think that it matters, editing the top or bottom section is equivalent, each component instance gets his own style.
While with static style, the style is shared between the component instances, you can edit all of them at the same time.
Some issue with pre-rendering though it isn't explained what the issue is. Maybe the expectation is that renderToStaticMarkup()
would already include the necessary style
tags?
@oliviertassinari
I don't think that it matters, editing the top or bottom section is equivalent, each component instance gets his own style.
Please be informed that I typed the exact same thing above, citing myself:
Still, changing styles doesn't worry me personally at all, as you can apply inline styles to change the behavior, if you are playing with them in the Dev Tools inspecting the element.
However, you brought a new good point on why the inconsistency in the process of inserting rules to the StyleSheet worsens the dev experience:
read-only
, in the browser, you can only affect (preview changes on) a single element.@kof @eps1lon
<head>
, because look at the picture above, the style rules do NOT exist in the HTML, they are applied to the CSSStyleSheet directly, using the CSSStyleSheet.inserRule().Let me even screencast it, so you can see that there are no styles added to the DOM, they are added directly to the CSSStyleSheet instance.
@o-alexandrov the fact that you can't modify the CSS rule inserted via CSSOM API in chrome dev tools is a known tradeo-off and basically a limitation of chrome dev tools and hopefully will be fixed at some point (is there an issue on chrome tracker?)
@kof
After thinking about changing styles for the group of elements that share the same class name, I actually remember the proper way to do that, you can insert the rule to overcome this issue, see the screencast:
<head>
, like the following screencast showcases:what could have been avoided
You seem to know more about rendering CSS than I do.
@kof short summary of the whole discussion:
Because right now JSS does the following:
Common Header for the following two examples:
import { styled } from "@material-ui/styles"
const component = (props) => <div {...props} /> // just an example
export const Div = styled(component)({
width: `100%`,
})
export const Div = styled(component)((props) => ({ // IMPORTANT! Note passing the function
width: `100%`,
}))
@o-alexandrov What do you mean with prerendering? SSR?
@HenriBeck please consider to google terminology before asking. However, since I'm asking the community to solve the issue and don't commit spending my time to propose a solution, I will assist in helping to process the terminology:
ReactDOM.render()
(for example in react, since we are in the Material UI issue)Obviously, all four types are superior to other cases in certain business cases, otherwise, they would have not exist 馃槃
This issue discusses the problem that JSS brings with its inconsistency in:
<head>
in some cases<head>
and applying style rules directly to the CSSStyleSheet instance via insertRule()
in other discussed above cases.This inconsistency not only worsens the Developer experience while working with JSS, but also worsens the ability to prerender the page attempting to inline all styles to the <head>
.
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 reference the original issue there so that anybody searching for a solution here is properly forwarded.
Please be informed that in this post above I actually:
Regarding the:
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
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.
Most helpful comment
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.