-
Notifications
You must be signed in to change notification settings - Fork 756
Description
CSS evolved extensively in the last four years and gained a lot of features such as :has(), @container, @layer, @scope, CSS Anchor Positioning and @property(), to name a few.
These new CSS features change the concept of how we can write composable and high-distributable HTML with Web Components without always resolving to the highest power by utilizing ShadowDOM.
In the rise of design systems, there is currently no way to combine the powers of both ModernCSS and <slot>. This proposal aims to solve this problem.
Shadow Boundaries vs ModernCSS
Using ShadowDOM leads to a lot of new CSS features being rendered useless; to provide an incomplete list of problems identified so far:
- No containment queries (
@container) across shadow boundaries of nested ShadowDOM Web components (was resolved via [css-contain] CQ vs shadow boundaries #5984 in spec but needs to be implemented across vendors). CSS Anchor Positioningdoes not work cross shadow boundaries.- No use of
:has()to style a parent depending on his slotted children (there is a proposal for a:has-slottedselector) - No import of global
@layer(). Each shadow-root recreates the semantics of the layers. - No 100% CSS encapsulation as some styles pierce the shadow-boundary when slotting content into a shadow root.
- Using complex selectors like
::partthat are limited in styling ShadowDOM content from the outside. - Using CSS Custom Properties as an API to control styles in ShadowDOM contents is limiting (compared to global CSS).
- Additional: Constructable Stylesheets cannot use
@import() - Additional: Adopted StyleSheets have a bug in chromium
Solutions in the wild
- StencilJS has experimental support for slots outside the ShadowDOM → https://ionic.io/blog/enhanced-support-for-slots-outside-of-shadow-dom
- Lightning Design System allows users to create their components in multiple DOM flavors → https://developer.salesforce.com/docs/platform/lwc/guide/create-dom-intro.html
- Keith Grant wrote a SlottedElement to mimic in light DOM LitElements
There are articles in the web community that express the same sentiment about slots being the only-missing ShadowDOM feature.
Proposed solution
Since <slot>s are native to the ShadowDOM and cannot be polyfilled in the regular DOM (and style encapsulation can be achieved natively via modern CSS) the proposed solution is to let users use <slot> with LightDOM Web Components.
A simplified example with slots inside a <template> without attaching a shadowRoot
class MyCard extends HTMLElement {
constructor() {
super();
let template = document.getElementById("card-template");
let templateContent = template.content;
/**
* Append the template content and somehow
* reconstruct the flat-tree to allow slotting
* and live content projection.
* @TODO: This needs to be fleshed out.
*/
this.appendChild(templateContent.cloneNode(true);
}
}
customElements.define("my-card", MyCard);<!-- The card template -->
<template id="card-template">
<div class="card">
<div class="card-header">
<slot name="card-header"></slot>
</div>
<div class="card-content">
<slot></slot>
</div>
</div>
</template>
<!-- Using the web component -->
<my-card>
<h2 slot="card-header">My Card Header</h2>
This is my regular card content.
</my-card>- No changes needed to the existing ShadowDOM specification (e.g. adding mechanisms to let global styles pierce the shadow boundary with
open-stylablevia [css-cascade-5] Add an adoptStyles capability in ShadowRoots (WICG 909: open-stylable Shadow Roots) #10176). - Developers can use all the new features such as
@layerand@scopeto provide a finer-grained CSS encapsulation. - Developers can keep their mental model writing CSS (as in trusting the cascade), and not use tightly coupled selectors
:host(parent-component) ::slotted(child-component) {…}to establish connections between nested ShadowDOM Web Components. - Developers can hide the implementation details of their Custom Element and slot the content into the places needed without ShadowDOM.
- Live content projection as with ShadowDOM.
- Usage of
slotchangeevent (optional).
Open questions
How can we signal that a LightDOM Web Component is using <slot>s without attaching another root node and appending the template to it?
A path forward
- Involve more people and gather more use-cases.
- Gain deeper insights from the working group and verify the case.
- Fleshing out how a flat-tree could look and be constructed.
Addendum
This is my first proposal that formed up after great talks with @bramus, @una, @fantasai, @andruud and @mirisuzanne at the CSSDay 2024.
Happy for any comments/corrections/recommendations, cheers!