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.

How to do nested selectors?

See original GitHub issue

Problem

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

github_iconTop GitHub Comments

7reactions
DouglasGabrcommented, Dec 9, 2019

Other solution is to use emotion styled to use the nested selectors:

import styled from '@emotion/styled'
import { Box } from '@chakra-ui/core'

const MyCustomBox = styled(Box)`
  a {
    color: #0000EE;
    &:visited {
      color: #551A8B;
    }
  }
`

<MyCustomBox>
  These <Link>links</Link> are <Link>colored</Link>!
</MyCustomBox>

If you need to target the Link component in the selector, I think you will need to wrap it in a styled:

import styled from '@emotion/styled'
import { Box, Link } from '@chakra-ui/core'

const StyledLink = styled(Link)``

const MyCustomBox = styled(Box)`
  ${StyledLink} {
    color: #0000EE;
    &:visited {
      color: #551A8B;
    }
  }
`

<MyCustomBox>
  These <StyledLink>links</StyledLink> are <StyledLink>colored</StyledLink>!
</MyCustomBox>

This is a more advanced usage, and I think it’s required to use the emotion babel plugin for it to work

1reaction
kellyrmilligancommented, Oct 17, 2022

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 and styled 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 the a, and could not figure out a way to do this, am I missing something or is styled the way to go?

Read more comments on GitHub >

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

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