w3c / w3c/csswg-drafts

[css-cascade-6] Named scopes proposal

Open
#9,742 6 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

css-cascade-6
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:

  1. 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>.
  2. 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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.