question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect handling of restyled components with CSS selectors with nested components

See original GitHub issue

Current 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:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
emmatowncommented, Jan 27, 2021

This sounds good. My general thoughts on styled(SomeStyledComponent) are that it should be equivalent to styled(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.

2reactions
Andaristcommented, May 1, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found