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.

Confusing behavior with ampersand selector

See original GitHub issue

Hi,

I’m having confusing behavior with the plugin. I’m not quite sure this is a bug or expected behavior but I couldn’t find any answer for this online neither docs.

Steps to reproduce

Create a styled component

const StyledComponent = styled(Component)`
.parent {
  & .hede {
    color:black
  }
}`

Expected Behavior / Output

.parent .hede{color:black;}

Actual Behavior / Output

.iQAcRJ .parent .hede{color:black;}

It includes the unique class of the component when I use ampersand sign to get parent selector. Is it normal? If yes, is there any way to get parent selector like SASS?

Cheers

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thysultancommented, Dec 17, 2017

The id in the example is a placeholder to demonstrate what s-c generates, so this.

styled.div`
.parent {
  &__kido + &__kido1 {
    color: red
  }
}

Is treated like sass would treat the following.

div {
  .parent {
    &__kido + &__kido1 {
      color: red
    }
  }
}

That is to say, it uses the same parent > child name-spacing* mechanism found in sass.

With this behavior in some cases(like mine) its impossible to use the ampersand.

Not sure i see the problem, the only difference between the expected vs actual is the added specificity.

0reactions
kittencommented, Dec 17, 2017

@mrtkrcm It’d be helpful to understand what you’re actually trying to achieve apart from your current approach 😉 I hope that I can help you nonetheless for now.

&__kido + &__kido1

I understand that you’re trying to apply some kind of BEM modifiers as sibling identifiers here? This is actually not something we’d recommend, but to detail what & does,

The parent selector, or ampersand, or simply &, resolves to the component class. This is not the dynamic class that is generated depending on dynamic props. Thus & is going to resolve to the “wider” component’s id.

At the same time you’re trying to achieve something that doesn’t behave like the CSS nesting dictates it to behave.

So maybe you can detail your use case and we can help you to clear up the confusion

Read more comments on GitHub >

github_iconTop Results From Across the Web

LESS: Secrets of the Ampersand - SLaks.Blog
One of the less-documented features of the LESS language is the ampersand selector, which refers to the parent selector inside a nested selector ......
Read more >
sass - Target immediate parent with ampersand (SCSS)
I'm experiencing what I think is odd behavior in scss. I have a class nested 2 levels deep and I'm trying to target...
Read more >
The Sass Ampersand
You can qualify a selector by putting the & on the right. Meaning, select the button class only when a child of a...
Read more >
Confusing CSS behaviors that actually make sense - YouTube
CSS can be infuriating at times. There are some really strange things that it does... BUT a lot of those infuriating things happen...
Read more >
Documentation
extend({ props: { checkpoints: { type: 'array', default: function () { return []; } } } });. NOTE: Both array and object have...
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