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.

Extending placeholder selector with nested selector with &-suffix doesn't work

See original GitHub issue

When using this placeholder selector (compare with mixin - https://gist.github.com/pepa-linha/43ffbce8e60d96e04c7bbe3d22923169)

%component {
  background: red;
  &-sub {
    background: orange;
  }
}

.Component {
    @extend %component;
}

I get this output

.Component {
    background: red;
}

but I would expect this output

.Component {
    background: red;
}

/* This selector is missing */
.Component-sub {
    background: orange;
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

8reactions
chriseppsteincommented, Apr 5, 2017

@thany First and foremost, you need to understand that the code that handles placeholders is the exact same code that handles all selectors because placeholders are a type of selector. Secondly, we have to consider use cases that are not built around component definitions using well defined naming systems like BEM, because the use of & and @extend are generic. So the feature request isn’t about placeholders, it’s actually about all selectors so what we have to ask is whether we expect the following to work as indicated:

#header {
   &-nav { } // #header-nav
}

#footer {
  @extend #header; // should this produce #footer-nav ? We have decided that it should not.
}

One of the reasons for this decision is that we expect @extend to have the exact same behavior across a nesting refactoring. That is, if you had originally written the code:

#header {}
#header-nav {}
#footer { @extend #header; }

and then later you refactored it to:

#header {
   &-nav { }
}
#footer { @extend #header; }

You should expect the exact same output. This “feature request” would violate this invariant and hence it would actually introduce a bug from another user’s perspective and use case.

Finally, It seems like some people have a mental model that @extend applies before resolving the parent selector, that it is like a search and replace. This is fundamentally a wrong way of thinking about @extend which creates a document level styling relationship between complex css selectors and can only be applied to full selectors and not to just parts of them.

0reactions
chriseppsteincommented, Apr 8, 2017

@pepa-linha the stylus behavior isn’t necessarily worse, it’s just a different concept (using the same syntax 🤔) – as long as the concepts are understood, people will know what to expect and behave accordingly. Their model is a simpler one, it’s easier to implement and arguably it’s easier to understand and know what output it will generate – but it lacks the document level guarantees that Sass makes 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass mixin with nested ampersand prefix - css - Stack Overflow
The solution requires both the use of mixins and placeholder selectors and is a little messy. If we set up our placeholder selector...
Read more >
@extend | Stylus
Extending placeholder selectors ​​ Stylus has a feature similar to the one in Sass — placeholder selectors. Note that if the selector is...
Read more >
Parent Selector - Sass
When a parent selector is used in an inner selector, it's replaced with the corresponding outer selector. This happens instead of the normal...
Read more >
When I Use (and Don't Use) Sass Extend • James Steinbach
Don't mix nesting and extends. Only extend placeholders. Try to use an actual selector instead. Using Using Sass's @extend ...
Read more >
Selectors Level 4 - W3C
Publication as a Working Draft does not imply endorsement by W3C and ... of and extends the set of selectors defined for CSS...
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