-
Notifications
You must be signed in to change notification settings - Fork 669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[extend-rule] #1855
Comments
The sass version of extend has a simple definition: Given an expression In essence, this is a way of assigning HTML attributes with specific values to elements that do not have those traits such that selectors will match against them anyway for the purposes of styling them. The placeholder selector has the semantics of the class attribute but for an attribute that does not exist in HTML. Because this "style placeholder" attribute can never be assigned from the document, it gives a CSS-only domain within which styles can be re-used without ever touching the html or templates of an application -- this creates a strong separation of concerns that is very much in the spirit of CSS's original design goal. A placeholder selector, like a class, can be used in any part of a complex selector, but those selectors never match an element unless they are extended, because there's no equivalent attribute in html for it. The Sass implementation is problematic because it can only emulate this semantic through selector rewriting. In doing so, the specificity of the rewritten selector changes causing the selectors to match in a new overall cascade that is not the same as the original. But an in-browser implementation would not have this issue. As such, there's no reason to "instead treat the extend rule as plain simple substitution." It's is incorrect to even conceptualize Of note: The legal syntax for the extended compound selector |
Thanks for sharing your thoughts and explaining things so well, @chriseppstein. There is one thing I would like to understand better.
Would you help me conceptualize this with an example? For instance, based on the following CSS, I would expect strong {
font-weight: bolder;
}
.foo {
font-weight: lighter;
}
.bar {
@extend strong;
} I would expect I chose that example because it contrasts with what I expect Sass and Tab’s original spec to do. In Sass, it would be lighter because of how the cascade is re-ordered (citation: Sassmeister). In Tab’s original spec, it would also be lighter because the styles being inherited from Please accept my apologies if you already answered this and I misunderstood you. |
@jonathantneal This is a not uncommon misconception about But the behavior of extend that I (and @tabatkins) describe follows naturally from the syntactic choice to use selectors as the abstraction upon which the .article strong {
font-weight: bolder;
}
.article em {
font-weight: lighter;
}
section .important {
@extend strong;
}
section em {
font-weight: 100;
}
strong {
font-weight: bold;
}
em {
font-weight: 200;
}
section strong {
font-weight: normal;
}
.important {
@extend em;
} Now consider the following document: <div id="id1" class="important">unscoped</div>
<div class="article">
<span id="id2" class="important">important article text</span>
<section>
<span id="id3" class="important">important section text</span>
</section>
</div>
<section>
<p id="id4" class="important">important section paragraph</span>
</section> Given those styles and this document what do you expect the If the specificity and document order of the selector If every document element can only be "extended" once, then the document order and specificity of the Early in the design of Sass's Ultimately, it seems like what you're asking for is the ability to assign a unique css-specific id to a single ruleset and then use it like a mixin at the call site. That's a fine feature, but it's not |
To clarify what i meant when I said:
Consider the following CSS with the hypothetical .article .important { font-weight: bold; color: black; }
.article .muted { color: gray; }
#a-particular-article h2 { @extend .important; } Sass rewrites this to: .article .important, .article #a-particular-article h2, #a-particular-article .article h2 {
font-weight: bold;
color: black; }
.article .muted {
color: gray; } (note: sass should also generate If there is an element Specifically, |
You don't have to extend to reuse, you just need |
@Array23 that requires duplication at many existing markup locations to enforce a style concern that can be expressed in on place in the stylesheet (that two classes must always be used together - by design). And the use of strong here is simply for exposition. For cases where you want something to look similar have have different markup semantics -- the same reason the font weight can be changed independently of the markup. |
That's mistake in templating. Duplication is always error if not meant for caching purposes. |
Right. Well, you do you. I think the use cases for this feature have been proven through 8+ years of it being widely used in the open source community. |
What's the status of the draft today? |
As a CSS author, I want to re-use styles from other rules. I don’t want to (and often can’t) do this in HTML by stringing classnames together (the Atomic CSS strategy).
I caught a glimpse of a solution to this problem behind a flag in Chrome with the
@apply
feature. Its author, @tabatkins has rightly pointed out that this solution is problematic, and it has since been abandoned.In its place, I propose we bring in a simplified version of the Sass
@extend
rule.The
@extend
rule allows authors to declare that certain elements, such as everything matching.serious-error
, must act as if they had the necessary features to match another selector, such as.error
. So that:Is equivalent to:
A straight-forward thing about the Sass
@extend
rule is that styles pulled in from thestrong
selector are now weighted by the.serious-error
selector. This means I don’t have to think about whether some other, higher-weighted selector is actually blocking the styles I am trying to pull in.Unfortunately, the Sass version of
@extend
has sometimes been discouraged because it re-orders rules in order to group any extended selectors together and reduce the final output code. I recommend we drop that part of Sass extend, and instead treat the extend rule as plain simple substitution.If possible, I would also like to see us bring in the Sass companion to the extend rule; the placeholder selector. These are simple selectors which do not match elements. So that:
is equivalent to:
Here is a link to a specification which covers this, originally by Tab Atkins: https://jonathantneal.github.io/specs/css-extend-rule/
The text was updated successfully, but these errors were encountered: