> whoami
...
<an-incdec>
class IncDec extends HTMLElement {
constructor() {
super();
const dummyEl = document.createElement('div');
dummyEl.innerHTML = 'hello';
this.appendChild(dummyEl);
}
connectedCallback() {
}
}
customElements.define('an-incdec', IncDec);
You can now use
<an-incdec> in your
HTML and it will render 'hello'
0
Don't worry about what
:host means just yet. It
will make more sense when we talk about the Shadow
DOM
class IncDec extends HTMLElement {
constructor() {
super();
const template = document.getElementById("incdec-template");
const el = template.content.cloneNode(true);
this.appendChild(el);
}
connectedCallback() {
}
}
customElements.define('an-incdec', IncDec);
The code will render the UI fine, but the styles
will leak out because we are not using the Shadow
DOM. Let's fix that!
class IncDec extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
const template = document.getElementById("#incdec-template");
const el = template.content.cloneNode(true);
this.shadowRoot.append(el);
this._count = 0;
}
get count() {
return this._count;
}
set count(val) {
if (val < 0) {
alert("cannot go less than zero");
return;
}
this._count = val;
this.shadowRoot.querySelector("#count").innerHTML = this.count;
}
connectedCallback() {
this.count = 0;
this.shadowRoot.querySelector("#dec").addEventListener("click", () => {
this.count -= 1;
});
this.shadowRoot.querySelector("#inc").addEventListener("click", () => {
this.count += 1;
});
}
}
customElements.define('an-incdec', IncDec);
Whatever styles you set in the
<style> is scoped to
just your component. It
WILL NOT leak
out
See the Pen NWxONbb by Elton Leander Pinto (@1ntegr8) on CodePen.
import { html, define } from 'hybrids';
export function inc(host) {
host.count += 1;
}
export function dec(host) {
host.count -= 1;
}
export const IncDec = {
count: 0,
render: ({ count }) => html`
${count}
`,
}
define('an-incdec', IncDec);
import React from "react";
class App extends React.Component {
...
render() {
return (
A cool web component!
);
}
}
No! Web Components are compatible with every framework.
In the above example, we are using our component in
React.