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:
- Created 2 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
Top 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 >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
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 iskeepImports
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.@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-942435579Please 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.