11ty / 11ty/webc

CSS: Support scoping inner elements of a webc file

Open
#5 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug enhancement
Dominant language
JavaScript
Stars
1.4k
Forks
46
Avg merge
10m
Merged PRs (30d)
2

Description

Assumption about the current state:

If I have the file:

<div>
  <h1>Example</h1>
  <p>Content</p>
</div>

<style webc:scoped>
:host h1 {
	color: aliceblue;
}
:host p {
	color: blanchedalmond;
}
</style>

After compilation I would currently see:

{
	html: "<div class=\"wcl2xedjk\"><h1>Example</h1><p>Content</p></div>",
	css: [".wcl2xedjk h1{color:aliceblue}.wcl2xedjk p{color:blanchedalmond}"],
}

Main concern:

For components that wrap another component, this isn't scoped enough to prevent style leakage. For example if our component above had a child component within it, and that child component also contained an h1 element, then .wcl2xedjk h1 would apply and turn it aliceblue.

Desired behaviour:

In Svelte's CSS scoping, I can write the following:

<div>
  <h1>Example</h1>
  <p>Content</p>
</div>

<style>
h1 {
	color: aliceblue;
}
p {
	color: blanchedalmond;
}
</style>

Which would provide me something along the psuedo-lines of

{
	html: "<div><h1 class=\"kziuyb\">Example</h1><p class=\"wgrhia\">Content</p></div>",
	css: ["h1.kziuyb{color:aliceblue}p.wgrhia{color:blanchedalmond}"],
}

Where the styles are scoped directly to the elements they applied to.

Implementation:

In (my) ideal world this would happen automatically when webc:scoped is applied. Alternatively to keep it opt-in it one could utilize something like a :host pseudo-class wrapping styles:

<style webc:scoped>
:host(h1) {
	color: aliceblue;
}
:host(p) {
	color: blanchedalmond;
}
</style>

Ideally slightly more complex selectors would also be handled, so h1 + p:first-child could become h1.qwerty + p.asdfgh:first-child

Contributor guide

No contributing guide indexed for this repository

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 at the handling of <style webc:scoped> and trace how selectors and rendered elements are transformed. Compare the current descendant scoping with the requested per-element scoping, including nested components and selectors such as h1 + p:first-child. Done means styles no longer leak into child components while the shown selectors retain their intended matches.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.