-
Notifications
You must be signed in to change notification settings - Fork 688
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] Pseudo-class to indicate when a slot has content #6867
Comments
I have also needed this on several occasions. At the moment, you need to (at the minimum) inspect A typical use case for me is to hide ( So being able to target a slot with assigned content is the most pressing use-case. Though I think it's also worth considering whether there should be separate pseudo-class for each of these states:
|
I'd like to point out that a
I'm quite keen myself to set a |
Perhaps inline with
A good partner to this spec would be #6620 |
This seems like a related request in the WICG space: WICG/webcomponents#936 |
The CSS Working Group just discussed
The full IRC log of that discussion<emeyer> fantasai: I wanted to know if this is something we want to pursue. plinss was asked whether or not we can check to see if something has slotted content. Is this something we want to do?<emeyer> TabAtkins: The use case makes sense and the argument is reasonable. The fact you can’t tell if text content has been slotted in leaves a whole. I say was call it ‘:slottted’ <fantasai> s/slottted/has-slotted/ <emeyer> PaulG: Is the example intended to be a slot name? <emeyer> TabAtkins: ‘::slotted’ will select anything slotted into that slot. <emeyer> fantasai: Tab and I should draft a proposal. <emeyer> Rossen: Would this be level 4? <emeyer> fantasai: No, 5 <fantasai> ACTION: Tab + fantasai to draft into Selectors 5 <emeyer> RESOLVED: Take this up for Selectors 5, fantasai and tabatkins will come back with proposed text |
in the example the |
Can you elaborate on what you're thinking of? The As I said in the minutes above, I think |
@tabatkins where are you seeing the Would be we able to do something here along the lines of Is there any benefit in being forward looking to the discussion in #7922 and make it a combinator? Maybe that doesn't make sense in this case... |
An interesting edge case to consider will be slots with default content as well. Does default content count as
|
I'd say no, because that But perhaps there may be more use cases. |
Great nuance in:
That I'm not quite sure how you could combine |
Now that I think of it, it's not just slot:not(:has(*)):not(:has-slotted(*)) {
display: none;
} ( <slot name="title">${this.cardTitle}</slot> I wasn't aware that default slot content is returned by |
It's on the
I'd say no. It's not slotted content, and in particular, it's not useful to detect slotted content in this case if this is true. That is, if |
We have a number of SSR users who really need this selector. There a few reasons this is critical in some SSR implementations:
It seems like the idea of a new selector is fairly well received... can this idea be added to an upcoming csswg meeting? |
In the case of Shoelace, we're using imperative DOM code to do slot detection which, as @justinfagnani mentioned, doesn't work on the server. We can probably work around some of these checks by changing structure/display so empty slots collapse, but this is very uncomfortable and won't solve all of our use cases. As it stands, without such a selector, we can't get SSR fully functional. This has, unfortunately, been a pretty big hurdle for adopters who want to use our custom elements with metaframeworks + SSR. |
I'm using the same imperative code mechanism as Shoelace above to detect slots having content. A css selector would be amazing. |
Tested The behavior is good for simple cases but not useful for nested composition. Perhaps this can be solved via #10771.
|
I'm going to
Agenda+
as I'd like to capture a resolution that the current behaviour of not matching the flat tree is okay, and perhaps to resolve to investigating a way to match the flat tree (though perhaps this is If web developers could please provide signal that the current behaviour as prototyped in Firefox and Chrome (that |
I really like having anything available in this area (is there any way that I can further explain the high level of important the web component developing community places on this API?). I am super excited about the implementation currently available in Chrome/Firefox (so many things we could never do have already been opened up to us with this API). However, not being able to select against the flattened tree will be a hard blocker on this API; maybe not today, maybe not tomorrow, but it will come up (much like questions around white space being "slotted") that would be better to be answered today, if only directionally. Personally, I can certainly be appeased with the selector matching <div><template shadowrootmode="open">
<div>
<template shadowrootmode="open">
<style>
::slotted(input) {
color: red;
}
</style>
<slot></slot>
</template>
<slot><input value="not styled" /></slot>
</div>
</template></div> Practically, we do need to support the spread. Questions:Does that mean multiple selectors? Does Functionally, will component authors be doomed/required to "know" the slotted content depth? <div>
<template shadowrootmode="open">
<div>
<template shadowrootmode="open">
<div>
<template shadowrootmode="open">
<style>
p {
color: red;
}
slot:has-slotted(slot:has-slotted(slot:has-slotted(p))) + p {
color: green;
}
</style>
<slot></slot>
<p>Styled content</p>
</template>
<slot></slot>
</div>
</template>
</div>
</template>
<p>Slotted content.</p>
</div> Does a combinator approach mean that Could any of these practically address selecting when content is delivered via the hole in Many thanks to all who are investing time into making this a high-quality and powerful additions to the web platform! 🙇🏼♂️ |
Based on the 3rd row of the table here, it's my opinion that: The bare form of Note, it's slightly confusing to call this the flattened tree because The basic use case for this:
There are 2 separate issues which I think should be considered orthogonal to
|
The CSS Working Group just discussed
The full IRC log of that discussion<kbabbitt> keithamus0: tldr from thread is chrome has :slotted, it can match on a few different things<kbabbitt> keithamus0: concept of flattened vs non-flattened tree <kbabbitt> ... flagged in firefox and chrome as non-flattened tree <kbabbitt> ... so it can match direct descendant of a slot <kbabbitt> ... some contention around this <kbabbitt> ... setting flattened to true would mean it would make child slots transparent effectively <kbabbitt> ... so if you have a slot slotted by another slot then that slot [missed] <kbabbitt> ... my opinion is we should keep behavior as-is <kbabbitt> ... that's the only way you can match slotted content <kbabbitt> ... even if it's just a text node as a descendant of a slot element <kbabbitt> ... web components cg is asking for commitment that this is acceptable <emilio> q+ <kbabbitt> ... as long as we define the alternatives to that <astearns> ack rachelandrew <kbabbitt> ... chief use cases are: has my slot been populated? what has it been populated with? <astearns> ack emilio <kbabbitt> emilio: flattened true and flattened false - only effective difference is nested slots right? <kbabbitt> keithamus0: that's true, has-slotted will always match populated text nodes <kbabbitt> ... nested slots become transparent <kbabbitt> [missed some context] <kbabbitt> keithamus0: how do you determine nested slots is the short summary <kbabbitt> emilio: to me, flattened tree behavior is what I'd expect as an author <kbabbitt> ... components that use nested slots... that becomes useless <kbabbitt> ... a bit confused about when you'd want to differentiate between useful slotted content and empty slot <kbabbitt> ... why would you want flattened behavior? <kbabbitt> keithamus0: other way around makes more sense for that use case, am I displaying default slotted content <kbabbitt> emilio: right but if you have an empty slot you could do that? <kbabbitt> keithamus0: I don't think so? <kbabbitt> ... will have to test <kbabbitt> emilio: my understanding is that you'd display default content of nested slot <dbaron> (that would be my assumption ^) <kbabbitt> ... flattened behavior is easier but I'm surprised that's most useful <kbabbitt> ... would not object but might be worth checking <kbabbitt> ...ideally has-slotted should reflect fallback content <kbabbitt> keithamus0: agree, that's my understanding of what flattened false would do <kbabbitt> emilio: assuming that's the case flattened false seems reasonable <kbabbitt> ... if you want we can resolve that we make has-slotted match fallback content <keithamus0> PROPOSED RESOLUTION: `:has-slotted` should match when the fallback content is not being displayed <kbabbitt> astearns: doesn't mention flat tree deliberately <kbabbitt> ... but if it turns out flat tree is desired behavior, [?] <kbabbitt> keithamus0: need to confirm <kbabbitt> ... whether a slot would display fallback content <kbabbitt> ... but if that's the case it shouldn't use the flat tree, but if it isn't then it should <kbabbitt> RESOLVED: `:has-slotted` should match when the fallback content is not being displayed |
A quick test tells me the current functionality is correct; slotting a Test case:
Therefore the existing WPTs and existing spec prose are sufficient. |
I like that there is a resolution here, being able to select on this content is going to be a massive improvement for shadow root users at large. Many thanks for trudging through this! 👏🏼 👏🏼 👏🏼 However, there's one last thing that I think is highly important to consider with this feature... The fact that many developers have difficulty grasping the nuances of styling shadow DOM and leveraging ExampleBeyond the simple inability to address text nodes with <my-button>
<template shadowrootmode="open">
<slot name=icon>Fallback content</slot>
<slot></slot>
</template>
</my-button> And it is leveraged within a larger pattern as follows: <larger-pattern>
<template shadowrootmode="open">
<my-button>
<slot name=icon slot=icon></slot>
This is a button
</my-button>
</template>
</larger-pattern> The Note: while "slotted text" is central to the OP here, knowing when an actual thing is slotted is a much wider pain that becomes harder to solve if we step on the language that should be used to do that work. Alternatives
|
Unless I'm mistaken, <padded-with-icon>
not padded
</padded-with-icon>
<padded-with-icon>
padded <i-con slot="icon"></i-con>
</padded-with-icon> But this has-a composition won't work because the icon slot inside <has-a-padded-with-icon>
<template shadowrootmode="open">
<padded-with-icon>
<slot></slot>
<slot name="icon" slot="icon"></slot>
</padded-with-icon>
</template>
oops, this is padded
<has-a-padded-with-icon> |
This resolution does not fit with the general consensus in this issue that The need here is to know if renderable content has been supplied to the slot. Unfortunately, this is not the same as if it will show fallback content. This is because a The ability to select if fallback content is showing, if specific elements have been slotted, or if slots themselves have been slotted should be handled as follow-on cases since they are (1) less needed and more complex, and (2) could be covered by a functional form and/or a combinator if/when that is possible. Proposal
|
I strongly agree with @sorvell here. I think it'll be confusing to have this divergence from |
The CSS Working Group just discussed
The full IRC log of that discussion<bramus> keithamus: last week we resolved to match when the fallback content is not being displayed<bramus> … whoich means using non flat tree <bramus> … think this is a mistake <bramus> … some people form the WC community reached out <lea> q? <lea> q+ <bramus> … was maybe overindexing on maybe wehter the fallback tree is displayed or <missed> <bramus> … would like to change the resolution to use the flattened tree <astearns> ack lea <bramus> lea: do we stay consistent with JS API or do we do a better thing? Both are valid. Proably should follow JS appraoch for consistency <bramus> … am wondering if we can have two pseudo classes <bramus> keithamus: JS api offer the optionality through a flattened flag <bramus> … has more to do with compat with ::slotted <bramus> … cannot do slotted-slot <bramus> … also composition <bramus> … authors of components are more likely to want to know if th ething has been slotted. <keithamus> s/fallback tree is displayed or <missed>/fallback content is displayed vs the flattened slots/ <bramus> … if that is many layers deep seems irrelevant to them <bramus> lea: agree <bramus> keithamus: proposed resolution is to use the flattened tree <bramus> … to create new selector that uses non-flat tree I have no opinion <lea> q? <bramus> … polyfills: wont affect us because JS has optionality for both <bramus> lea: I understand the sentiment that we need to ship this ASAP. really needed <bramus> keithamus: I have i2s for chrome and I believe we are working towards one for firefox as well <lea> s/I understand the sentiment /I completely agree with the sentiment / <bramus> … need to resolve on this <keithamus> PROPOSED RESOLUTION: :has-slotted should use the flattened tree to resolve if content is slotted or not. <lea> +1 <bramus> astearns: with a future issue on adding a param or new pseudo to not use the flat tree <bramus> … that’s a different, later, discussion <bramus> keithamus: sgtm <bramus> chrishtr: do we need resived resolution? <bramus> astearns: no, wanted to point out other issue is out of scope <lea> PROPOSED RESOLUTION: :has-slotted should use the flattened tree to resolve if content is slotted or not. We could later discuss a different pseudo-class for the other behavior. <bramus> chrishtr: so someone should raise a new issue <bramus> keithamus: i'll try and do that tomorrow <keithamus> RESOLVED: :has-slotted should use the flattened tree to resolve if content is slotted or not. We could later discuss a different pseudo-class for the other behavior. <bramus> keithamus: thanks everyone for your time |
I'm developing several web components and I'm finding a need to tell when a slot has content or not. (e.g. to apply margins or not so empty slots don't impact layout, but layout well when they do, etc.)
The :empty pseudo-class applies when the slot doesn't have direct content, even when it has slotted content, so doesn't work. Using slot:has(::slotted(*)) wouldn't detect text-only content.
Once possibility would be to redefine :empty to detect slotted content, but it may be useful to keep the current behavior to differentiate empty slots from slots with default content (and the change may break exiting content). So a new pseudo-class that only detects slotted content may be useful.
The text was updated successfully, but these errors were encountered: