[css-cascade-6] Named scopes proposal
Nobody has claimed this yet.
- Dominant language
- Bikeshed
- Stars
- 4.9k
- Forks
- 816
- PR merge metrics
- PR metrics pending
Description
This issue is a follow-up to the previous issue: https://github.com/w3c/csswg-drafts/issues/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
:scopepseudo-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-rootin 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).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the linked issue 9741 and the CSS Cascade 6 scope syntax, then compare the CSS Containment 3 container-name syntax and the CSS Extensions materials mentioned here. Done means reaching agreement on named-scope syntax, functional :scope behavior, repeated-name handling, and the relationship to nested scopes before updating the relevant specification text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- css
- Domain
- documentation, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100