What defines a reusable UI component
Why and when we should encapsulate a Component from its surroundings
How to create Components with a well-defined interface
Design patterns: Composition vs. Configuration
22. Custom Components
UI Components, SimilarTalk 2016 22
class MyComp extends HTMLElement {
constructor() {
// always call super
super();
// add class
this.classList.add(...");
// add click listener
this.addEventListener('click', e => { ... });
}
// Called when the element is inserted into a document,
connectedCallback() {}
// Called when the element is removed from a document
disconnectedCallback() {}
}
window.customElements.define(my-comp', MyComp);
24. HTML Imports
UI Components, SimilarTalk 2016 24
<html lang="en-US">
<head>
<title>Human Being</title>
<link rel="import" href="/imports/heart.html">
</head>
<body>
<p>What is a body without a heart?</p>
</body>
</html>
25. Shadow DOM
UI Components, SimilarTalk 2016 25
<div id="host"></div>
<script>
var host = document.querySelector('#host');
var root = host.createShadowRoot(); // Create a Shadow Root
var div = document.createElement('div');
div.textContent = 'This is Shadow DOM';
root.appendChild(div); // Append elements to the Shadow Root
</script>
http://codepen.io/daniran/pres/woBNoN
36. straight-forward, but requires in-depth binding
know-how even for a small component
UI Components, SimilarTalk 2016 36
straight-forward, simple and fast
great standard, but the solution has not yet matured
into a complete offering
Summary