by Salvatore Laisa @ BolognaJS - 18/04/2018
"You can think of Web Components as reusable user interface widgets that are created using open Web technology. They are part of the browser, and so they do not need external libraries like jQuery or Dojo."
from Mozilla MDN
After a Chrome-only implementation, referred as "v0", browser vendor worked together for a common specification now called "Web Components v1".
this.shadow.innerHTML = `
<style>
section{
color: red;
}
</style>
<section>
My awesome scoped style!
</section>
`
Can I Use custom-elementsv1? Data on support for the custom-elementsv1 feature across the major browsers from caniuse.com.
Can I Use shadowdomv1? Data on support for the shadowdomv1 feature across the major browsers from caniuse.com.
class MyComponent extends HTMLElement {
constructor(){
super();
console.log('<my-componennt> created');
}
connectedCallback(){
console.log('<my-componennt> added to DOM');
}
}
customElements.define('my-component', MyComponent);
<my-element></my-element>
it works as a regular HTML element.
class UsefulElement extends HTMLElement {
constructor(){
super();
this.shadow = this.attachShadow({mode: 'open'});
this.text = "I'm a super useful element!";
}
connectedCallback(){
this.shadow.innerHTML = `<p>${this.text}</p>$`;
}
}
customElements.define('useful-element', UsefulElement);
The great (Google) herald of Web Components
Not strictly related to Web Components... but what about having pure Javascript competing with Virtual DOM?
function tick(render) {
render`
<div>
<h2>It is ${new Date().toLocaleTimeString()}.</h2>
</div>
`;
}
setInterval(tick, 1000,
hyperHTML.bind(document.body)
);
... and many examples and comparisons out there!
In a quite funny twist Google developed a similar library a few months ago...
Started as a React inspired library but based on Web Components..
basically just import che component in your page and add it in your application template
but not always it's so easy 😡
Luckily there is...
AKA: did you noticed my gamepad remote?
Reveal.initialize();
// Listening to custom events from gamepad-api element
const gamepad = document.querySelector('gamepad-api');
gamepad.addEventListener('button', evt => {
evt.detail.button === 'prev' ?
Reveal.prev() : Reveal.next();
});
keep on playin!
Keep consistency across frameworks