Skip to content
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

CSS: Support scoping inner elements of a webc file #5

Open
bglw opened this issue Sep 9, 2022 · 5 comments
Open

CSS: Support scoping inner elements of a webc file #5

bglw opened this issue Sep 9, 2022 · 5 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@bglw
Copy link

bglw commented Sep 9, 2022

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

@zachleat zachleat added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Sep 14, 2022
@zachleat
Copy link
Member

Reclassifying this one as a bug

@zachleat zachleat changed the title Support scoping inner elements of a webc file CSS: Support scoping inner elements of a webc file Oct 14, 2022
@zachleat
Copy link
Member

zachleat commented Nov 1, 2022

I’m waffling back and forth here because I do kinda think this is okay and as a better short term solution I’d like to ship an example of how to override the webc:scoped behavior with any arbitrary css scoping library e.g. css modules maybe? Any opinions on what a good next-level library for this might be?

@zachleat zachleat added the enhancement New feature or request label Nov 1, 2022
@bglw
Copy link
Author

bglw commented Nov 1, 2022

Makes sense. I don't know of any css scoping libraries myself, but that would be a cool thing to find

@mayank99
Copy link

mayank99 commented Dec 16, 2022

lightningcss might be a good library to show how to use css modules with. If not, postcss-modules is another option (i believe this is what vue uses under-the-hood as well). both of these solve the inner-element scoping problem and they give you a json mapping of the original vs transformed class name, which could be substituted against webc's own internal mapping.

also worth mentioning that native scoping is coming and it might be good to start thinking about it. for example, webc could manage a unique hash to pass inside @scope.

@ghost
Copy link

ghost commented Jun 1, 2023

This is still important as long as scoped prefixing occurs before transforming... currently, the CSS being fed to a preprocessor is not what the developer wrote, but the result of applying the prefix transform. This creates unintended side effects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants