Replies: 3 comments 1 reply
-
Meeting AIs:
|
Beta Was this translation helpful? Give feedback.
-
@e111077 can you write out what using |
Beta Was this translation helpful? Give feedback.
-
Just wanted to +1 this. We've seen this come up very commonly, where component authors use
One thing to add: SSR/hydration is currently hampered not just from the perspective of progressive enhancement or FOUC, but also because you must have an implementation/polyfill of
One small callout: I wonder if it will be a bit confusing for authors that 1) |
Beta Was this translation helpful? Give feedback.
-
Authors: Elliott Marquez (@e111077) & Jared White (@jaredcwhite)
Background
In shadow DOM, projection is the concept of placing an element into the light DOM and rendering it in a custom position via slots in the shadow DOM. This allows for greater control over the layout and styling of elements while also giving the user direct control over the contents and styling since the nodes are in the light DOM.
These are the current mechanisms in which to interact with slotted content:
::slotted()
pseudo element<slot>
can also have childrenDeveloper-facing Problem
Currently there is no CSS-only way to determine whether a
<slot>
has content assigned to it without requiring the consumer of a web component to explicitly state so.Use Case Example
In the web component-based https://github.com/material-components/material-web, there is the Dialog component. The dialog component can have three sections slotted in:
When the dialog’s content is slotted, the dialog must show a divider which is rendered in its shadow DOM. In essence the
<md-divider>
which is a sibling to<slot name=”content”>
must be styled. e.gThe DX would be cleaner if
has-content
could be inferred by the CSS rather than the user having to apply it or JS having to run at runtime.Using
/slotted/
for the sake of example.This removes the need for the user to declare that there is slotted content twice:
div[slot=content]
md-dialog[has-content]
Proposal Discussions
/slotted/
combinator CSS-WG (more general but it could support:has(/slotted/)
)::slotted()
a combinator csswg-drafts#7922:hasSlotted()
WICGWorkarounds
There are two types of workarounds, the comprehensive JS approach and the less-generalized additive spacing approach.
Generalized JS approach
slotchange
event on the slotslotchange
has content or note.g. in Lit for conciseness
Issues with approach
This requires JS which means it is incompatible with SSR without hydration and the user defining the host attribute.
Since the user must define the host attribute correctly, this approach may easily lead to layout shift after element upgrade if the user has not set the correct attribute.
Additive Spacing Approach
This approach works where the slot detection changes styles such that spacing is added or removed to sibling nodes.
For example in the following case where a button with an icon should have
20px
inline margins when there is no icon but16px
and20px
inline start and end margins respectively. This is an example with/slotted/
.With this approach, one would:
/slotted/
styles to the slotted childrenExample of the additive spacing approach.
Issues With Approach
display
to siblingsPolyfill
This is not generally polyfillable without a JavaScript approach.
Browser Positions
No official or concrete positions.
Next Steps
WCCG needs to choose a proposal to champion, refine, and move forward as well as prioritize this issue amongst the few we would like to champion.
Author’s recommended proposal
/slotted/ approach as discussed in CSSWG. It is ambitious but it fixes more issues around slotted styles.
Beta Was this translation helpful? Give feedback.
All reactions