Incorrect handling of restyled components with CSS selectors with nested components
See original GitHub issueCurrent behavior:
If you style already existing styled component, it will lose all styles that base component received via CSS selectors with nested components.
To reproduce:
import styled from '@emotion/styled';
const Link = styled.a`
color: red;
`;
const LinkContainer = styled.div`
${Link} {
color: blue;
}
`;
const StyledLink = styled(Link)`
border: 1px grey solid;
`;
ReactDOM.render(
<LinkContainer>
<StyledLink>I must be blue</StyledLink>
</LinkContainer>,
document.getElementById('root')
);
Expected behaviour:
In this example StyledLink
should be blue
, but appears to be red
.
styled-components
handles this case properly: https://codesandbox.io/s/styled-components-playground-forked-v3829?file=/src/index.js
Environment information:
react
version: 16.13.1@emotion/react
version: 11.1.4
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
[css-nesting] How to resolve nested CSS with pseudo ... - GitHub
Making nesting inside pseudo-elements invalid will block many use cases, and will make it harder to teach and understand nesting.
Read more >CSS selectors - CSS: Cascading Style Sheets - MDN Web Docs
CSS selectors define the pattern to select elements to which a set of CSS rules are then applied.
Read more >Native CSS nesting: What you need to know - LogRocket Blog
With CSS nesting, any styles should be applied before the nested selectors. This is one difference between CSS nesting and nesting with other ......
Read more >CSS Nesting Module - W3C
This module describes support for nesting a style rule within another style rule, allowing the inner rule's selector to reference the elements ......
Read more >Why We're Breaking Up with CSS-in-JS - DEV Community
Naturally, you set className="row" on these elements. Now the new component's rows have an unsightly border and you have no idea why! While...
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 FreeTop 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
Top GitHub Comments
This sounds good. My general thoughts on
styled(SomeStyledComponent)
are that it should be equivalent tostyled(forwardRef((props, ref) => <SomeStyledComponent {...props} ref={ref} />))
and here we’re doing the wrong thing so we should fix it.There aren’t really performance concerns with this, it’s just adding some extra class names.
@penx this is a different issue - you are referring to
&
being based on the hashed styles rather than based on the component identity. This is by design and won’t change.@iChenLei the issue can be seen here: https://codesandbox.io/s/recursing-bassi-s6809?file=/src/index.js so it’s still there.