In the below examples, foo
is the parent component, and bar
is the child that fits into foo
's slot.
As you can see below I can slot an element if I place it in the HTML directly, but when I try slot an element via JavaScript, it is not slotted, and is not styled by the ::slotted(*)
selector. How does one append an element to a web component's slot?
class ComponentFoo extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(TEMPLATE_FOO.content.cloneNode(true));
}
connectedCallback() {
const slotEl = this.shadowRoot.getElementById("SLOT");
const bar = document.createElement("wc-bar");
bar.textContent = "Bar created via Javascript"
slotEl.appendChild(bar) // <--- Does not work
console.log(bar)
}
};
window.customElements.define("wc-foo", ComponentFoo);
class ComponentBar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(TEMPLATE_BAR.content.cloneNode(true));
}
};
window.customElements.define("wc-bar", ComponentBar);
<template id="TEMPLATE_FOO">
<div>foo</div>
<slot id="SLOT"></slot>
<style>
::slotted(*) {
color: red;
border: 1px solid lime;
}
</style>
</template>
<template id="TEMPLATE_BAR">
<div>bar</div>
</template>
<wc-foo>
<div>bar created via HTML</div>
</wc-foo>