Comments (13)
Woo, found a manual escape hatch:
const Vertical = 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 👍
from styled-components.
The above demo works now because of #29 but this one doesn't:
import React from 'react'
import styled from 'styled-components'
const Outer = styled.div`
padding: 1rem;
p {
color: red;
}
`
const Inner = styled.div`
p {
color: blue;
}
`
const AlternateInner = styled(Inner)`
p {
color: green;
}
`
export default () => (
<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:
export default () => (
<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>
)
This is... going to be a pain to fix.
from styled-components.
Simpler example not using descendant tag selectors since those are going to hurt no matter what:
const Outer = styled.div`
padding: 1rem;
color: red;
`
const Inner = styled.div`
color: blue;
`
const AlternateInner = styled(Inner)`
color: green;
`
export default () => (
<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.
from styled-components.
Ah, I think componentDidUpdate
will fire in the right order. So maybe we have to defer injection of the CSS until that point.
from styled-components.
So maybe we have to defer injection of the CSS until [
componentDidUpdate
]
Hmm, sounds like that might introduce flashes of unstyled content?
from styled-components.
I just implemented it, it doesn't seem to. I think we're good.
from styled-components.
The idea being that if you called styled(X)
then X
s styles are injected before your own. That's the same guarantee composes
gives you in CSS Modules.
from styled-components.
Wouldn't that break in this case?
const Comp = styled.something`
styles: here;
`;
const SomeOtherComp = styled(Comp)`
styles: overridden;
`;
// …
<SomeOtherComp>
<SomeComp />
</SomeOtherComp>
from styled-components.
Nope, that should be fine.
from styled-components.
Interesting, by fixing the more complex case we break the original case :/
Test cases:
const SVG = styled.svg`
line {
stroke-width: 4px;
}
`
const Vertical = styled.g`
line {
stroke-width: 1px;
}
`
export default () => (
<SVG>
<line x1={10} y1={10} x2={90} y2={10}/>
<Vertical>
<line x1={10} y1={10} x2={90} y2={90}/>
</Vertical>
</SVG>
)
const Outer = styled.div`
padding: 1rem;
color: red;
`
const Inner = styled.p`
color: blue;
`
const AlternateInner = styled(Inner)`
color: green;
`
export default () => (
<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 😱😱😱
from styled-components.
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.
from styled-components.
&&
≈≈ aphrodite/important
– I like it!
from styled-components.
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.
from styled-components.
Related Issues (20)
- Antd dark theme does not work HOT 1
- Fix plain NodeJS usage
- RNW styles applied twice when using nextjs app directory with react-native-web with SSR HOT 1
- styled-components injects styles for non loaded components on react-native-web
- Simple Styled Component giving error about extending not existing component in Next 14
- Jest test fail on upgrading the styled-components lib version from 5.3.1 to 6.1.8
- correct work in nextjs app route with plugins
- TypeError: __vite_ssr_import_5__.default.div is not a function [fixed]
- `css` props are not applied within the server component.
- Failed to parse `&` multiple times in `:is() :where() :has()` pseudo-class selector
- Theme prop (not styled) is not being passed through HOT 1
- Feature request for v5 to ease migration to v6 of large ecosystems
- Interplay of `shouldForwardProp` clarifying question
- Export the `toStyleSheet` function HOT 3
- createGlobalStyle not working version >= 5.3.1 HOT 1
- [Docs] Lint for pseudo selectors - Styled Components update from v5 to v6 HOT 1
- Tailwind selectors do not work the expected (from me) way
- React SSR Streaming Bug
- Error while building project after upgrading from 5.3.11 to 6.0.0 HOT 2
- styled-components is ~50% larger than it could be due to IE11 support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from styled-components.