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.

Styling another component with styled()

See original GitHub issue
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Is there a way to style another component with styled() without using classes but targeting the styled component? Being able to do something like this:

const Child = styled('div')({
  color: 'red'
});

const Parent = styled('div')({
  [`& ${Child}`]: {
    color: 'green'
  }
});

render (
    <Parent>
        <Child>Green because I am inside a Parent</Child>
    </Parent>
    <Child>Red because I am not inside a Parent</Child>
)

If it’s already possible, could it be added to the documentation? Because I wasn’t able to find such information.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Nov 19, 2021

I don’t think this would even be possible. In the MUI’s code the import has to resolve to a value usable at runtime. All macros are “pseudo” imports - they are not working on the runtime level, they were not designed for this. It’s a signal for the babel-plugin-macros to handle this, using what is contained in the imported file, and ellide the import from the file (well, there is keepImports option, but to use we change the importee path, so it changes from @emotion/styled/macro to just @emotion/styled).

Just take a look at the content of the macro file: https://unpkg.com/@emotion/styled@11.6.0/macro.js

This file imports our Babel plugin and thus it imports a lot of Babel stuff - you don’t want for that to end up in your bundles when not using Babel macros. And it doesn’t even export anything styled-related so without babel-plugin-macros such an import in your dependency (in this case - MUI) would fail hard.

1reaction
mnajdovacommented, Oct 19, 2021

You need to add @emotion/babel-plugin but it doesn’t work. See #29078

@bordeo you say on the issue that you linked that it is related to using next.js and confirmed that it worked previously on https://github.com/mui-org/material-ui/issues/26366#issuecomment-942435579

Please don’t leave misleading comments.

It is duplicate of https://github.com/mui-org/material-ui/issues/26366 @MariaSolOs please check if the comments provided there will solve your issue, if not provide a reproduction that we can take a look at.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
styled -components generates an actual stylesheet with classes, and attaches those classes to the DOM nodes of styled components via the className prop....
Read more >
Advanced ways to write styled-components - Medium
Referring to another component ... In this example, you will create the item styled component and then the unordered list styled component. Within...
Read more >
Override component style inside a new ... - Stack Overflow
You need to pass the component name, not the instance to styled() function. Instead of const CustomA = styled(<A />) where <A />...
Read more >
How To Use Styled-Components In React - Smashing Magazine
In React's own words, styled components are “visual primitives for components”, and their goal is to give us a flexible way to style...
Read more >
How To Use React Styled Components Efficiently - Copycat
You may choose to retain all your styles within the same file as the other React component that utilizes them, or you can...
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