How to do nested selectors?
See original GitHub issueProblem
Emotion supports nested selectors, and they can sometimes be very useful. However, it does not appear that Chakra supports this feature, even though Chakra is built on Emotion.
Is it possible to use nested selectors with Chakra, as well? I don’t see a way to do so in the documentation. I tried using the css
prop to build on Emotion, but the Typescript annotations do not allow use of this prop in a Typescript project (which is what I’m using).
Proposed Solution
Maybe the Box
component could add a prop called nested
, which accepts a JS object of nested selectors and styles? For example:
<Box
nested={{
"a": { color: "#0000EE" },
"a:visited": { color: "#551A8B;" },
}}
>
These <Link>links</Link> are <Link>colored</Link>!
</Box>
Note that the above example is handy for restoring standard HTML link colors after the CSSReset
component removes them.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
CSS Nesting Module - W3C
To be nest-prefixed , a nesting selector must be the first simple selector in the first compound selector of the selector. If the...
Read more >Native CSS nesting: What you need to know - LogRocket Blog
The @nest rule allows nesting to be more flexible in CSS. So rather than having the nesting selector only at the beginning of...
Read more >CSS Combinators - W3Schools
A combinator is something that explains the relationship between the selectors. A CSS selector can contain more than one simple selector.
Read more >Nesting CSS classes - Stack Overflow
You can use grouping selectors and/or multiple classes on a single element, or you can use a template language and process it with...
Read more >How to Nest Your CSS Selectors for Cleaner Code
Nesting your CSS selectors is one small trick that makes things much easier down the road. You'll write your CSS more quickly, apply...
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
Other solution is to use emotion
styled
to use the nested selectors:If you need to target the
Link
component in the selector, I think you will need to wrap it in a styled:This is a more advanced usage, and I think it’s required to use the emotion babel plugin for it to work
I ended up using the
styled
method to do this as well, however I was a bit sad and it felt like an escape hatch. I really like the single part and multipart theme stuff with defineStyle functions, etc. I was hoping initially that I could do it there somehow, so I settled on a split between the theme andstyled
to do the things I could not do 😭in this case, I had a container
a
, and on hover wanted to apply styles to different things inside thea
, and could not figure out a way to do this, am I missing something or isstyled
the way to go?