Open
Description
This issue is a follow-up to the previous issue: #9741
In that issue, I explored the way :scope
works in a nested context:
@scope (.outer) {
@scope (.inner) {
:scope.inner .test {
background: lightgreen;
}
/* This should never match */
:scope.outer .test.never {
background: tomato;
}
}
}
My proposal consists of two parts:
- Augment the syntax of
@scope
(https://drafts.csswg.org/css-cascade-6/#scope-syntax), allowing specifying the<scope-name>
as a<custom-ident>
(or<dashed-ident>
?), in a way similar (syntax-wise, not behavior-wise though) how we can specify a name for containers (https://drafts.csswg.org/css-contain-3/#container-rule) with a<container-name>
. - Introduce a functional form of a
:scope
pseudo-class, which could be used with the scope name inside, like:scope(my-scope)
(or:scope(--my-scope)
if we'd decide on<dashed-ident>
).
This way, we could do something like this:
@scope outer (.outer) {
@scope inner (.inner) {
:scope(inner) .test {
background: lightgreen;
}
:scope(outer) .test {
background: lime;
}
}
}
Basically, allowing specifying any specific scope defined around our selector.
Some things to think about:
- Can the same name be used multiple times? I'd say that yes, in which case, similar to anonymous scopes, the innermost one would win.
- Do we need some way to get the equivalent
&
, where we'd retain the scope root's selector's specificity? I'm not sure of the use-cases for that, probably could be a separate proposal in itself.
Use cases:
- Using a named scope for the root one, enabling something similar to the
@at-root
in Sass, or root reference in Stylus. This could work already with the:scope
, but only unless a nested scope is introduced, in which case we will lose the access to the root scope. - “Naming” scopes to be reusable inside, as a way to not repeat yourself. This way, scopes could fully replace something like BEM, where we could define a scope for the “block”, and then define sub-scopes for every “element”. Then, we retain an ability to mention “modifiers” on the block and elements separately when specifying various states and cases.
Out of this proposal's scope (sorry): naming scopes as a way to reuse their selectors: this is probably a job for CSS-extensions (label, spec; I did not find a good single issue to mention).