際際滷

際際滷Share a Scribd company logo
DESIGNING REUSABLE
UI COMPONENTS
Danny Rankevich
FEDTech Lead, SimilarWeb
UI Components, SimilarTalk 2016 1
Hi, Im Danny and I love to develop UI
Components
UI Components, SimilarTalk 2016 2
UI Components, SimilarTalk 2016 3
UI Components, SimilarTalk 2016 4
How do we define an Interface for UI
Components?
UI Components, SimilarTalk 2016 5
UI Components, SimilarTalk 2016 6
M
C
B
Model
Configuration
Behavior
UI Components have a Model
input / output
UI Components, SimilarTalk 2016 7
M
UI Component have a Configuration
How it should work
UI Components, SimilarTalk 2016 8
C
UI Components have behavior
What it should do
UI Components, SimilarTalk 2016 9
B
itemList: Array<Item>
selectedItem: Item
UI Components, SimilarTalk 2016 10
M
allowMultiSelect: Boolean
UI Components, SimilarTalk 2016 11
C
onSelect()
emit ItemSelectedEvent
UI Components, SimilarTalk 2016 12
B
tabList: Array<Tab>
selectedTab: Tab
UI Components, SimilarTalk 2016 13
M
direction: Horizontal|Vertical
UI Components, SimilarTalk 2016 14
C
onSelect()
emit TabSelectedEvent
UI Components, SimilarTalk 2016 15
B
What is a UI Component made of?
UI Components, SimilarTalk 2016 16
UI Components, SimilarTalk 2016 17
S
H
J
Presentation (CSS)
Structure (HTML)
Behavior (JS)
How can we author a UI Component?
UI Components, SimilarTalk 2016 18
 angular
UI Components, SimilarTalk 2016 19
 react
 web components
Component Frameworks
 angular
UI Components, SimilarTalk 2016 20
 react
 web components
Component Frameworks
web components
Google standard
UI Components, SimilarTalk 2016 21
Custom Components
Templates
HTML Imports
Shadow DOM
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);
Templates
UI Components, SimilarTalk 2016 23
<template id=my-template">
<style>
.toggle {
cursor: pointer;
border-bottom: 1px solid #d6d6d6;
width: 300px;
display: flex;
flex-direction: row;
}
</style>
<div>
<span>some content</span>
</div>
</template>
// using a template
const t = document.querySelector('#my-template');
const content = t.content.cloneNode(true);
element.appendChild(content);
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>
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
web components implementation
https://customelements.io/PolymerElements
UI Components, SimilarTalk 2016 26
Web components
adoption
UI Components, SimilarTalk 2016 27
Web components
reality
UI Components, SimilarTalk 2016 28
Frameworks Comparison
Using a one test component
https://codepen.io/collection/XedJpy/
UI Components, SimilarTalk 2016 29
Test Component:Toggler
UI Components, SimilarTalk 2016 30
UI Components, SimilarTalk 2016 31
itemList: Array<Item>
selectedItem: ItemM
C
onSelect()B
---
Internal Composition
UI Components, SimilarTalk 2016 32
ToggleItem
Toggle
ToggleItem
ToggleItem
const items = [
{
title: 'one',
index: 1
}, {
title: 'two',
selected: true,
index: 2
}, {
title: 'three',
index: 3
}
];
React
https://codepen.io/daniran/pen/aBzdqd
UI Components, SimilarTalk 2016 33
Angular
https://codepen.io/daniran/pen/xRbrPm
UI Components, SimilarTalk 2016 34
Web components
https://codepen.io/daniran/pen/yVyvZV
UI Components, SimilarTalk 2016 35
 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
ThanksYou!
Questions?
UI Components, SimilarTalk 2016 37

More Related Content

Reusable ui components