-
Notifications
You must be signed in to change notification settings - Fork 668
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
[selectors] Behavior of :root inside Shadow DOM #10492
Comments
Thanks @bramus for following up on this 👍 |
There's already a selector that matches the shadow host - I'm a little confused by the use-case, tho - how would this help |
The use case is to not need a single root element in shadow roots to use |
Nm, I misread the codepen, it duplicates that style in the shadow dom constructor. |
@lukewarlow That's what :host is for, yes. Within the shadow, the host element is treated as the root of the shadow tree, so |
|
Sorry, I corrected my selector via a comment edit. ^_^ (But it still doesn't work, which I think is a bug.) |
I don't think The original problem can be solved using a selector like |
Wouldn't a dedicated Apart for the described use cases, there are most definitely use cases for the e.g.: It would make sense to define "private" custom properties for a web component in the root of the shadow DOM rather in than in the |
The suggestion was to have (Admittedly authors should write
If
I don’t think we should force authors to rewrite their stylesheets when using them inside a shadow root or not. I would expect
That would also make the original use-case work, but would require authors to write |
That's the thing, I would not expect this to work.
Ironically, with this change, you would be forcing authors who rely on the current I think it's worth focusing on @tabatkins and @lukewarlow's discussion for a second.
For example, this works in Firefox and Safari today (but not in Chrome?): <div>
<template shadowrootmode="open">
<style>
:host(:has(p)) { color: red; }
</style>
<slot></slot>
</template>
<p>Red in the light DOM</p>
</div> @Th3S4mur41 I should point out that a |
The short version
Using
:root
in Shadow DOM currently does not match anything. Sparked by https://x.com/Th3S4mur41/status/1805198579910238380 I think it would make sense to have that match the Shadow Root.One wouldn’t be able to use it to style the shadow with it, but it would enable use-cases such as
:has(:popover-open)
because that selector would then also match:root:has(:popover-open)
– similar to how it behaves in Light DOM.It would fix situations such as this one.
The long version
In https://x.com/Th3S4mur41/status/1805198579910238380 an author wondered why the following worked in Light DOM but not in Shadow DOM as seen in this demo:
Note that in the Shadow DOM variant the popover itself is also part of the Shadow DOM:
Reducing the code it’s the
:has(:popover-open)
that is not working as the author expected. This because with an open popover,:has(:popover-open)
can only match the#shadow-root
, which itself is not selectable.This gave me the idea to have
:root
match the#shadow-root
. One wouldn’t be able to style anything with it, but it would make the use-case above work because:has(:popover-open)
– which is*:has(:popover-open)
– would then be able to match the shadow-root./cc @keithamus and @lukewarlow who participated in the discussion on X.
The text was updated successfully, but these errors were encountered: