Light DOM vs Shadow DOM: how do I switch a component between them? #1018
-
|
Does WebJs support both light DOM and shadow DOM for components? If so, how do I make a specific component use one or the other? A quick example would help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Yes, both are supported. Every component renders light DOM by default, and you opt a specific component into shadow DOM with one static field: class Card extends WebComponent({ title: String }) {
static shadow = true; // this component now renders shadow DOM
static styles = css`
:host { display: block; }
.body { padding: 1rem; }
`;
render() {
return html`<div class="body"><h3>${this.title}</h3><slot></slot></div>`;
}
}
Card.register('my-card');Drop the static shadow = true line (or set it to false) and the same component renders light DOM instead. That is the whole switch, it is per component, and works either way. Which one to reach for:
Both modes SSR correctly, shadow DOM via Declarative Shadow DOM, so first paint is real HTML in either case. Rule of thumb: stay in light DOM unless you specifically need style encapsulation, then flip that one component to shadow. |
Beta Was this translation helpful? Give feedback.
Yes, both are supported. Every component renders light DOM by default, and you opt a specific component into shadow DOM with one static field:
Drop the static shadow = true line (or set it to false) and the same component renders light DOM instead. That is the whole switch, it is per component, and works either way.
Which one to reach for: