-
Notifications
You must be signed in to change notification settings - Fork 375
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 selector to style slots with slotted elements #936
Comments
IMHO, I'm currently prototyping the |
I don't think we should do that. |
Maybe |
I'd guess that :empty can't change in this regard, but I think it's not quite the same question anyway. At least in my experience when we do this we're toggling a style on "does the slot have assignees" - but the slot might actually have default content, so it might never be empty in the flat tree. I once wrote |
Oh, I misunderstood the intent of this issue. Sorry for the confusion.
|
Sorry that I mentioned I do think we'd need a new pseudo-class for this like |
I've encountered the need for this many times and I think this would be useful. However, sometimes, knowing whether the slot is empty or not isn't quite good enough because we want to know if the slot contains visible elements. If only I could have The visibility bit aside, I think this should work for the case where there are nested slots. Ex. outer-component
--shadowDOM
<inner-component>
<slot name="outer" slot="inner"></slot>
</inner-component>
<slot></slot>
...
inner-component
--shadowDOM
<slot name="inner"></slot>
... Given such components, usually I would be interested in the flattened assigned nodes. That is, I would usually consider the <outer-component>
<div slot="outer">...</div>
...
</outer-component> But I would usually consider the <outer-component></outer-component> |
I would think that |
To show an example where A component that wants to style based on the presence of slotted nodes will compute a class based on a JS utility: return html`
<button
part="base"
class=${classMap({
'button--has-label': this.hasSlotController.test('[default]'),
'button--has-prefix': this.hasSlotController.test('prefix'),
'button--has-suffix': this.hasSlotController.test('suffix')
.button--has-label.button--small .button__label {
padding: 0 var(--sl-spacing-small);
} and there's a whole I think this whole mechanism and render cycle could be replaced with this CSS if we had the right selector: button:has(slot:not([name])::hasSlotted(*)) {
padding: 0 var(--sl-spacing-small);
} |
So, so much this! At Adobe we currently leverage a mixin for this https://github.com/adobe/spectrum-web-components/blob/main/packages/shared/src/observe-slot-presence.ts and the related has assigned text functionality https://github.com/adobe/spectrum-web-components/blob/main/packages/shared/src/observe-slot-text.ts having this functionality by default without keeping track of slot changes would be amazing. It’s too bad there was initial pushback on In the bike shed, I’d love for this to better match the JS API of Would be very excited to bring to the Web Components Community Group for further feedback! |
Is the use case here involve differentiating assigned nodes vs. default content? Or is it about having any content at all? It would be useful to have a list of concrete use cases for CSS WG folks to study & come up with an appropriate abstraction / name for this feature. |
In the cases that I've come across in our library, we would want to select if there is either assigned or default content. |
I found ten places where this occurs in our codebase where assigned vs unassigned is the condition. Most of them don’t have default content, but two do.
I’m working this out backwards a bit, so not a high-level use case, but since there are some very clear patterns here it may still be useful info: Most of these are toggling |
Interesting. Perhaps there is a grid specific solution that we can make here as well. Not that we'd not want this feature separately but depending on what you're trying to do with grid, adding new capability to grid to resolve the particular use case might make sense. |
Related: #889 |
|
Is this in line with this CSS issue? w3c/csswg-drafts#6867 |
WCCG had their spring F2F in which this was discussed. You can read the full notes of the discussion (#978 (comment)), heading entitled "Styling empty slots". In the meeting it was pointed out that - as already mentioned above - this may be solved by w3c/csswg-drafts#6867. |
We've seen a number of elements that style slots based on whether the slot has slotted contents.
Right now these elements use the
slotchange
event to look at assigned elements and set some state that triggers rendering of the class. This approach can lead to extra rendering work and complicate server-side rendering.It would nice nice to skip JS completely for this styling use case and be able to use some selector, possibly a pseudo-class, to style the slot:
Maybe it's possible that
:has()
will be useful for this case, but I'm not sure. Does this selector even make sense with mixed scopes:#label-slot:has(::slotted(*))
?The text was updated successfully, but these errors were encountered: