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-scoping][selectors] :root should resolve to :host in shadow trees #11000

Open
LeaVerou opened this issue Oct 4, 2024 · 5 comments
Open

Comments

@LeaVerou
Copy link
Member

LeaVerou commented Oct 4, 2024

https://drafts.csswg.org/selectors/#the-scope-pseudo

Currently, if used in regular CSS but outside an @scope rule, :scope resolves to :root. However, this is not super useful within shadow CSS. :host would be a lot more natural.

This would be a breaking change, but I’d wager the prevalence of :scope outside @scope in shadow CSS is so low that we can still make it.

This means the same CSS can be used within Shadow DOM to style the component, as well as using @import ... scope(...) to style light DOM elements.

@sorvell
Copy link

sorvell commented Oct 4, 2024

This means the same CSS can be used within Shadow DOM to style the component, as well as using @import ... scope(...) to style light DOM elements.

This is indeed an annoying pain point right now when trying to share styles. While you can write :where(:host, :scope, :root), it's cumbersome and I believe this isn't currently supported in Safari (bug or unspec'd?).

Separately and for the same reason could :root resolve to :host in a shadow tree?

@LeaVerou
Copy link
Member Author

LeaVerou commented Oct 4, 2024

Separately and for the same reason could :root resolve to :host in a shadow tree?

YES!

Actually, that’s all we need. Since :scope resolves to :root outside @scope, if :root resolves to :host, by proxy :scope also resolves to :host. I’ll rename the issue.

@LeaVerou LeaVerou changed the title [css-cascade] Outside @scope, :scope should resolve to :host in shadow trees [css-scoping][selectors] :root should resolve to :host in shadow trees Oct 4, 2024
@Loirooriol
Copy link
Contributor

Highlight pseudo-elements inherit custom properties from :root. Does this mean that in shadow dom they will be inherited from the host?

@Loirooriol
Copy link
Contributor

Loirooriol commented Oct 20, 2024

Even if resolving to :host is more natural, right now authors can choose :host vs :root.
If both :root and :scope behave as :host, then authors will lose the ability to target the actual root.
So maybe just changing :scope but not :root would offer more flexibility.

Edit: or maybe just let :root match the host, while in :host-context(:root) it refers to the actual root (or host's host)?

@LeaVerou
Copy link
Member Author

Highlight pseudo-elements inherit custom properties from :root. Does this mean that in shadow dom they will be inherited from the host?

Seems reasonable.

Even if resolving to :host is more natural, right now authors can choose :host vs :root. If both :root and :scope behave as :host, then authors will lose the ability to target the actual root. So maybe just changing :scope but not :root would offer more flexibility.

Not sure that kind of flexibility is something authors need though, given that :root is meaningless in shadow roots.

Edit: or maybe just let :root match the host, while in :host-context(:root) it refers to the actual root (or host's host)?

That seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants