TLDR: As soon as I define()
a Custom Element with Shadow DOM, the Light DOM declared within the same element disappears from the browser viewport.
Why is the Light DOM within the Custom Element no longer displaying?
I am learning / experimenting with Custom Elements
and Shadow DOM
.
I notice that if I have the following Custom Element
without any Shadow DOM
, it works:
<my-company>© 2020, </my-company>
But if I then add custom content to the Custom Element
via the Shadow Dom
...
class myCompany_Element extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({mode: "open"});
}
connectedCallback() {
this.root.textContent += 'Cyberdyne Systems';
}
}
customElements.define('my-company', myCompany_Element);
<my-company>© 2020, </my-company>
the original hard-coded content disappears. (Even though it's still visible in the DOM Inspector.)
I was expecting:
© 2020, Cyberdyne Systems
What's happening here?
Does this mean it's not possible to have
- a
Custom Element
with and withoutShadow DOM
and that I can only have either:
- a
Custom Element
withoutShadow DOM
- a
Custom Element
withShadow DOM
andslots
Or is it possible to have a Custom Element
with and without Shadow DOM
?