You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constSVG=styled.svg` line { stroke-width: 1px; }`constHorizontal=styled.g` line { shape-rendering:crispEdges; }`constVertical=styled.g` line { stroke-width: 1.5px; }`exportdefault()=>(<SVG><Horizontal><linex1={0}y1={0}x2={width}y2={0}/></Horizontal><Vertical><linex1={0}y1={0}x2={width}y2={height}/></Vertical></SVG>)
Because Horizontal and Vertical get rendered before SVG, and I'm only injecting the styles on render, ._compile_from_Vertical line {} appears before._compiled_from_SVG line {}. They have the same specificity of course and so SVG wins.
Injected source should be in the same order it is written, not in the order it's rendered.
The above demo works now because of #29 but this one doesn't:
importReactfrom'react'importstyledfrom'styled-components'constOuter=styled.div` padding: 1rem; p { color: red; }`constInner=styled.div` p { color: blue; }`constAlternateInner=styled(Inner)` p { color: green; }`exportdefault()=>(<div>{/* This causes AlternateInner to be rendered, then Inner */}<AlternateInner/>{/* Then outer gets rendered, overwriting both of them */}<Outer><p>This should be red</p><Inner><p>This should be blue</p></Inner><AlternateInner><p>This should be green</p></AlternateInner></Outer></div>)
If you drop the initial <AlternateInner/> it works:
exportdefault()=>(<div><Outer><p>This should be red</p><Inner><p>This should be blue</p></Inner><AlternateInner><p>This should be green</p></AlternateInner></Outer></div>)
Simpler example not using descendant tag selectors since those are going to hurt no matter what:
constOuter=styled.div` padding: 1rem; color: red;`constInner=styled.div` color: blue;`constAlternateInner=styled(Inner)` color: green;`exportdefault()=>(<div><AlternateInner/><Outer>
This should be red
<Inner>
This should be blue
</Inner><AlternateInner>
This should be green
</AlternateInner></Outer></div>)
Again, commenting out <AlternateInner/> makes it work.
Interesting, by fixing the more complex case we break the original case :/
Test cases:
constSVG=styled.svg` line { stroke-width: 4px; }`constVertical=styled.g` line { stroke-width: 1px; }`exportdefault()=>(<SVG><linex1={10}y1={10}x2={90}y2={10}/><Vertical><linex1={10}y1={10}x2={90}y2={90}/></Vertical></SVG>)
constOuter=styled.div` padding: 1rem; color: red;`constInner=styled.p` color: blue;`constAlternateInner=styled(Inner)` color: green;`exportdefault()=>(<div><AlternateInner/><Outer>
This should be red
<Inner>
This should be blue
</Inner><AlternateInner>
This should be green
</AlternateInner></Outer></div>)
If we inject the CSS during componentWillReceiveProps we get CSS injected as Components get rendered:
If we wait until componentDidUpdate we get:
So... I think we need to actually go off the source order of the style.tagName calls then inject the CSS into that spot. Having a separate style tag for each one would work, but 😱😱😱
Mind you, the only reason that SVG example breaks is because there are two descendant tag selectors (which is not good practice), so I'm going to ship the version that works for const AlternateInner = styled(Inner) (i.e. componentDidUpdate).
I did just confirm that if the styles have the right specificity then the picture is right for both examples:
I did that by repeating the class selectors N times where N is the order of their definition. But we can't do that in production, so we have to insert rules out-of-order. Glamor can't do that at the moment, and I'm a bit worried about the pef implications (one rule injected at the top could invalidate A LOT of styles). But if I can figure out some other approach I will.
constVertical=styled.g` && line { stroke-width: 1px; }`
This generates .xxx.xxx line which lifts its specificity above .yyy line (from SVG above), so tbh I think this is "good enough" to consider this option closed.
In fact you can actually use && { some: thing; } to target the element itself (not just descendants). In case you have some greedy rules outside (like html p) which would override a styled.p with a single class name. This is great, since we don't have to design good JS to cover bad CSS, we can just give folks a way to generate even worse CSS 👍
Reacted by Max Stoiber, Marcin Zielinski, Rahul Gaba and Stewart Duffy
Activity
geelen commentedon Sep 26, 2016
The above demo works now because of #29 but this one doesn't:
If you drop the initial
<AlternateInner/>it works:This is... going to be a pain to fix.
geelen commentedon Sep 26, 2016
Simpler example not using descendant tag selectors since those are going to hurt no matter what:
Again, commenting out
<AlternateInner/>makes it work.geelen commentedon Sep 26, 2016
Ah, I think
componentDidUpdatewill fire in the right order. So maybe we have to defer injection of the CSS until that point.mxstbr commentedon Sep 26, 2016
Hmm, sounds like that might introduce flashes of unstyled content?
geelen commentedon Sep 26, 2016
I just implemented it, it doesn't seem to. I think we're good.
geelen commentedon Sep 26, 2016
The idea being that if you called
styled(X)thenXs styles are injected before your own. That's the same guaranteecomposesgives you in CSS Modules.mxstbr commentedon Sep 26, 2016
Wouldn't that break in this case?
geelen commentedon Sep 26, 2016
Nope, that should be fine.
geelen commentedon Sep 26, 2016
Interesting, by fixing the more complex case we break the original case :/
Test cases:
If we inject the CSS during
componentWillReceivePropswe get CSS injected as Components get rendered:If we wait until
componentDidUpdatewe get:So... I think we need to actually go off the source order of the
style.tagNamecalls then inject the CSS into that spot. Having a separatestyletag for each one would work, but 😱😱😱geelen commentedon Sep 26, 2016
Mind you, the only reason that SVG example breaks is because there are two descendant tag selectors (which is not good practice), so I'm going to ship the version that works for
const AlternateInner = styled(Inner)(i.e.componentDidUpdate).I did just confirm that if the styles have the right specificity then the picture is right for both examples:
I did that by repeating the class selectors N times where N is the order of their definition. But we can't do that in production, so we have to insert rules out-of-order. Glamor can't do that at the moment, and I'm a bit worried about the pef implications (one rule injected at the top could invalidate A LOT of styles). But if I can figure out some other approach I will.
geelen commentedon Sep 26, 2016
Woo, found a manual escape hatch:
This generates
.xxx.xxx linewhich lifts its specificity above.yyy line(fromSVGabove), so tbh I think this is "good enough" to consider this option closed.In fact you can actually use
&& { some: thing; }to target the element itself (not just descendants). In case you have some greedy rules outside (likehtml p) which would override astyled.pwith a single class name. This is great, since we don't have to design good JS to cover bad CSS, we can just give folks a way to generate even worse CSS 👍mxstbr commentedon Sep 26, 2016
&&≈≈aphrodite/important– I like it!geelen commentedon Sep 28, 2016
So this didn't stay solved for long. I found that server-side rendering wasn't firing the
componentDidMount, so we can't use that to inject.Merge pull request #1 from siddharthkp/no-sodu
StyleSheet now proxies all the methods we needed from Glamor (#1)
Support different import names