I am not going to tell you that you should jump into Web Components and that it is the new cool shit that will replace your framework.
I am trying to resolve the questions about the following topics as efficient as possible to get your head around it:
A Web Component is anything that is defined via customElements.define
Interface.
It does not matter if you use Shadow DOM or whatever. If you use that API for a custom tag you are using a Web Component.
<your-tag></your-tag>
<script>
// most simple example of a Web Component:
customElements.define('your-tag', class extends HTMLElement {
connectedCallback() {
this.innerHTML = 'Hello world!';
}
}
</script>
They are not self-closing so do not do it!
You have 4 of 5 methods that you might want to use in your Web Component commonly:
constructor()
Use for data pre-fetching etc.connectedCallback()
tells you when it is mounted in the DOMdisconnectedCallback()
tells you when it is unmounted from the DOMattributeChangedCallback(...)
tells you when an attribute on your element has changed
static get observedAttributes() {return ['foo', 'bar']; }
which tells which attributes to observe and call the callback oncustomElements.define('your-tag', class extends HTMLElement {
connectedCallback() {
this.innerHTML = 'Hello Light';
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = 'Hello Darkness';
}
}
Shadow DOM first of all is a DOM Node as every other node.
The Shadow DOM Node however cannot be found via querySelector
nor can everything inside of it be found via querySelector
from the outside.
Also the outside CSS does not apply.
Some quick clarifications:
Shadow DOM…
videoplayer
lib on your document they will not find any DOM inside your Shadow DOM nor put any styling on anything withinsay
in my document with a method hello()
can I call it from inside the Shadow DOM?Yes! See: https://codesandbox.io/s/say-hello-qswww?file=/src/index.js You define the Web Component in your normal JS scope.
I can see exactly 2 answers here:
Light DOM != Normal DOM. Light DOM is referred as the DOM in a ShadowDOM-Web-Component that is NOT in the Shadow DOM.
If you check my live example you will see that you do not see the Light DOM content (“Hello Light”) because the Shadow DOM content takes over the Web Component once attached. The Light DOM then only can be referenced via Slots (next section).
If you put <slot><slot>
anywhere inside of Shadow DOM and you have content in the Light DOM then the content in the Light DOM that “got missing” in section 3.1. will show up where the <slot>
sits.
It is literally a placeholder that acts as if the slotted content would be copied to that exact point where the slot is. So in your head just think of: Where the <slot>
tag is everything from outside the Shadow DOM will be “copied” (virtually) to.
https://codesandbox.io/s/light-dom-vs-shadow-dom-cmyh9?file=/src/index.js
Most probably you will need the following:
::slotted()
:host()
:host-context()
I created a Sandbox which hopefully helps to understand how it works (post in the comments if you want further text explanation here):
https://codesandbox.io/s/gracious-saha-928yt?file=/index.html .