Web Components: The Future of Web Development is Here

John Riviello
John RivielloEngineering Fellow at Comcast
Drupaldelphia - April 27, 2018
John Riviello
@JohnRiv
Chris Lorenzo
@Chiefcll
Web Components:
The Future of Web Development is Here
© Comcast
© Comcast
© Comcast
© Comcast
© Comcast
© Comcast
Don’t all of today’s web
frameworks/libraries
provide “components”?
React
React is all about building reusable components.
In fact, with React the only thing you do is build
components. Since they're so encapsulated,
components make code reuse, testing, and
separation of concerns easy.
Source: https://facebook.github.io/react/docs/why-react.html
© Comcast
© Comcast
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll13
How do we create a
component without
the framework?
#UseThePlatform
- Polymer Motto
Web Components
#UseThePlatform
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll17
4 Specs
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll18
Custom Elements
What Are Web Components?
19
Custom Elements
•Provides a way for authors to build their own
fully-featured DOM elements.
<paper-tabs selected="0" scrollable>
<paper-tab>The first tab</paper-tab>
<paper-tab>Tab two</paper-tab>
<paper-tab>The third tab</paper-tab>
<paper-tab>Fourth tab</paper-tab>
</paper-tabs>
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
What Are Web Components?
20 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll21
HTML Imports
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll22
HTML Imports
• Means to import custom elements
- <link rel="import" href="../paper-tabs/paper-tabs.html">
• Built-in deduplication
• Componetize the HTML, CSS & JavaScript
• Will be replaced by ES6 Modules
- import "../paper-tabs/paper-tabs.js"
What Are Web Components?
23 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll24
Templates
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll25
• Used to declare fragments of HTML
- <template id="tab">
<div class="tab-content"></div>
</template>
• The element itself renders nothing
• Can be cloned and inserted in the document via
JavaScript, which will render the content
Templates
What Are Web Components?
26 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll27
Shadow DOM
What Are Web Components?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll28
•Allows you to take a DOM subtree and
hide it from the document scope
•Hides CSS styles as well
•Common examples from HTML5 include:
<select>, <video>, & <input type="date">
Shadow DOM
What Are Web Components?
29 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
Can we even use
these APIs?
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll31
Source: https://lists.w3.org/Archives/Public/public-webapps/2011JulSep/0975.html
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll32
Sources: https://developers.google.com/web/fundamentals/getting-started/primers/customelements
https://developers.google.com/web/fundamentals/getting-started/primers/shadowdom
33 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
34
Source: https://developer.microsoft.com/en-us/microsoft-edge/platform/status/
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
35
Source: https://developer.microsoft.com/en-us/microsoft-edge/platform/status/
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
Source: http://gs.statcounter.com/browser-version-partially-combined-market-share/all/united-states-of-america/#weekly-201812-201817-bar
84.5%
"Samsung Galaxy S7" by Kārlis Dambrāns is licensed under CC BY 2.0
Mobile Safari 10.1Chrome for
Android 54
= nearly
100%
of mobile
How do we use a
component with
a framework?
© Comcast
© Comcast
Source:
https://www.github.com/Comcast/polaris
© Comcast
<script href="https://polaris.xfinity.com/polaris.js"></script>
<xc-header
tab="myaccount"
is-authed="[[isAuthed]]"
login-url="/login"
sign-out-url="/logout"
first-name="[[openidData.given_name]]"
user-name="[[openidData.preferred_username]]">
</xc-header>
<xc-footer></xc-footer>
Existing Frameworks
Applications
Web Platform
Web Components built with Polymer (or not)
Rob Dodson:
Custom Elements
Everywhere
https://custom-elements-everywhere.com
How do we use a
component with
Drupal?
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
How do I use
Web Components?
“The Way of the Web” - Ben Issa, ING
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll49
Source: https://youtu.be/8ZTFEhPBJEE?list=PLNYkxOF6rcICc687SxHQRuo9TVNOJelSZ
“Assemblers First, Craftspeople
Second”
- Ben Issa, 2016 Polymer Summit
Using Web Components
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll50
"Puzzle Pieces Jigsaw Piece Concept" is licensed under CC0 1.0 / Color adjusted from original
"Blacksmith Forging Smith Forger" is licensed under CC0 1.0 / Color adjusted from original
Web Components: The Future of Web Development is Here
$ bower install ELEMENT_NAME --save
$ bower install iron-pages --save
$ bower install ELEMENT_NAME --save
$ bower install iron-pages --save
Using Web Components
• Import the component
- <link rel="import" href="../iron-pages/iron-pages.html">
- import "../iron-pages/iron-pages.js"
• Use your custom element as a normal HTML tag
- <iron-pages>
<div>Page 1 content</div>
<div>Page 2 content</div>
</iron-pages>
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll54
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
Google Polymer Project
Using Web Components
Polymer is NOT
• Material Design
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll59
Source: https://ebidel.github.io/material-playground/
Using Web Components
Polymer is NOT
• Material Design
• Required to use Web Components
• A framework
• Heavy
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll61
Intro to Web Components & Polymer
Framework/Library Sizes (with GZIP compression)
62
Source: http://minime.stephan-brumme.com
React’s size is react + react-dom & gzipped via http://cnvyr.io/online
Polymer’s range is v2.6.0 with just polymer-element.html, to polymer.html without the legacy imports. Bundled, minified & gzipped.
113.7kb 57.2kb 29.2kb30.4kb 12.8kb -
21.6kb
33.3kb
v3.0.0 v1.6.9 v3.3.1v2.5.16 v2.6.0v16.3.0
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
Why should I use
Polymer?
Primitives are
designed so we can
build libraries on
top of them.
Vanilla JS
class MyButton extends HTMLElement {
connectedCallback() {
if (super.connectedCallback)
super.connectedCallback();
this.addEventListener('click', this._click);
}
_click() {
alert('Hello');
}
}
customElements.define('my-button', MyButton);
Polymer
class MyButton extends Polymer.Element {
static get is() { return 'my-button'; }
ready() {
super.ready();
this.addEventListener('click', e => {this._click(e)});
}
_click() {
alert('Hello');
}
}
customElements.define(MyButton.is, MyButton);
Vanilla JS
connectedCallback() {
this.innerHTML = `
<button id='kickMe'>
Kick Me
</button>
`;
this.querySelector('#kickMe')
.addEventListener('click', this.handleClick.bind(this));
}
Polymer
<dom-module id="my-button">
<template>
<button on-click="handleClick">
Kick Me
</button>
</template>
</dom-module>
Vanilla JS
- Only a few components
Polymer
- Lots of components or a site
FYI - Other Libraries
Skate JS
X-Tag
< >XX-Tag
Web Components: The Future of Web Development is Here
Source: https://github.com/webcomponents/gold-standard/wiki
How do I build a
Polymer Component?
Source: https://www.polymer-
project.org/2.0/docs/tools/polymer-cli
$ polymer help
Available Commands
analyze Writes analysis metadata in JSON format to standard out
build Builds an application-style project
help Shows this help message, or help for a specific command
init Initializes a Polymer project
install installs Bower dependencies,
optionally installing "variants"
lint Identifies potential errors in your code.
serve Runs the polyserve development server
test Runs web-component-tester
$ polymer help
Available Commands
analyze Writes analysis metadata in JSON format to standard out
build Builds an application-style project
help Shows this help message, or help for a specific command
init Initializes a Polymer project
install installs Bower dependencies,
optionally installing "variants"
lint Identifies potential errors in your code.
serve Runs the polyserve development server
test Runs web-component-tester
$ polymer init
? Which starter template would you like to use? (Use arrow keys)
❯ polymer-1-element - A simple Polymer 1.0 element template
polymer-2-element - A simple Polymer 2.0 element template
polymer-1-application - A simple Polymer 1.0 application template
polymer-2-application - A simple Polymer 2.0 application
polymer-1-starter-kit - A Polymer 1.x starter application template,
with navigation and "PRPL pattern" loading
polymer-2-starter-kit - A Polymer 2.x starter application template,
with navigation and "PRPL pattern" loading
shop - The "Shop" Progressive Web App demo
$ polymer init
? Which starter template would you like to use? (Use arrow keys)
polymer-1-element - A simple Polymer 1.0 element template
❯ polymer-2-element - A simple Polymer 2.0 element template
polymer-1-application - A simple Polymer 1.0 application template
polymer-2-application - A simple Polymer 2.0 application
polymer-1-starter-kit - A Polymer 1.x starter application template,
with navigation and "PRPL pattern" loading
polymer-2-starter-kit - A Polymer 2.x starter application template,
with navigation and "PRPL pattern" loading
shop - The "Shop" Progressive Web App demo
$ polymer init
? Which starter template would you like to use? polymer-2-element
info: Running template polymer-2-element...
? Element name my-element
? Brief description of the element My sample element
create bower.json
create demo/index.html
create index.html
create polymer.json
create README.md
create my-element.html
create test/my-element_test.html
Project generated!
Installing dependencies...
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
<!--
Material design:
[Tabs](https://www.google.com/design/s
pec/components/tabs.html)
`paper-tabs` makes it easy to explore
and switch between different views or
functional aspects of
an app, or to browse categorized data
sets.
Use `selected` property to get or set
the selected tab.
Example:
<paper-tabs selected="0">
<paper-tab>TAB 1</paper-tab>
<paper-tab>TAB 2</paper-tab>
<paper-tab>TAB 3</paper-tab>
</paper-tabs>
Web Components: The Future of Web Development is Here
<!--
### Styling
The following custom properties and
mixins are available for styling:
Custom property | Description |
Default
----------------|-------------|-------
---
`--paper-tabs-selection-bar-color` |
Color for the selection bar | `--
paper-yellow-a100`
`--paper-tabs-selection-bar` | Mixin
applied to the selection bar | `{}`
`--paper-tabs` | Mixin applied to the
tabs | `{}`
`--paper-tabs-content` | Mixin applied
to the content container of tabs |
`{}`
`--paper-tabs-container` | Mixin
applied to the layout container of
Web Components: The Future of Web Development is Here
/**
* If true, the tabs are aligned
to bottom (the selection bar
appears at the top).
*/
alignBottom: {
type: Boolean,
value: false
},
/**
* If true, tabs are
automatically selected when
focused using the
* keyboard.
*/
autoselect: {
type: Boolean,
value: false
},
Documenting Your Polymer Code
• Document with JSDoc syntax - usejsdoc.org
• Create a page that imports & includes iron-component-page
• Generate the docs and load your page:
$ npm i -g polymer-cli bower
$ polymer analyze > analysis.json
$ polymer serve --open
Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll86
Additional Details: https://github.com/PolymerElements/iron-component-page
Web Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
<test-fixture id="NoLabel">
a11ySuite('NoLabel');
Web Components: The Future of Web Development is Here
Demo Time!!!
What are some
Best Practices?
Sharable ComponentsSharable Components
Web Components: The Future of Web Development is Here
You Need a TeamYou Need a Team
© Comcast
TDD?
DDD!
D
D
D
Demo
Driven
Development
© Comcast
Use CORS
(Cross-Origin Resource Sharing)
Link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
Separate Your
Data from Your UI
Link: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Why?
1.Decouple State / UI
2.Unidirectional Flow
3.Loadable State
4.Community Tools
<xc-app>
index.html
LogicStore {}
component-name-event-name
<xc-app>
index.html
Logic
xc-people-unpause-all-devices
Native Event
Store {}
component-name-event-name
<xc-app>
index.html
Logic
xc-people-unpause-all-devices
Native Event
Store {} What’s
That?
component-name-event-name
<xc-app>
index.html
Logic
xc-people-unpause-all-devices
Native Event
Store {}
component-name-event-name
<xc-app>
index.html
Store {} Logic
xc-people-unpause-all-devices
Native Event
component-name-event-name
<xc-app>
index.html
Store {} Logic
xc-people-unpause-all-devices
Native Event
component-name-event-name
xc-people-unpause-all-devices
Native Event
Smart Parent
+
<xc-app>
index.html
LogicStore {}
component-name-event-name
xc-people-unpause-all-devices
Native Event
<xc-app>
index.html
LogicStore {}
component-name-event-name
xc-people-unpause-all-devices
Native Event
“How to use Redux in Polymer -- Polycasts #61” - Rob Dodson
Web Components: The Future of Web Development is Here114
Source: https://youtu.be/PahsgJn0sgU?list=PLNYkxOF6rcIDdS7HWIC_BYRunV6MHs5xo
Important Concepts to Understand
• Encapsulation
• Composition
• Separation of
Concerns
• Functional
Decomposition
"separated" by hansol is licensed under CC BY 2.0
Simplicity
Matters
-Rich Hickey
"Rocky" by Stefan Ogrisek is licensed under CC BY 2.0 / Color adjusted from original
Simplicity
Creates
Opportunity
Useful Links
• WebComponents.org - webcomponents.org
• Polymer Website - polymer-project.org
• HAX (Headless Authoring eXperience) - haxtheweb.org
• LRNWebComponents - https://www.webcomponents.org/author/LRNWebComponents
• Polymer Slack - polymer-slack.herokuapp.com
• Polymer 2.x Cheat Sheet - https://meowni.ca/posts/polymer-2-cheatsheet/
• PWAs with Polymer: a checklist - https://meowni.ca/posts/polymer-pwa-checklist/
• Custom Elements Everywhere - custom-elements-everywhere.com
• Polycasts on YouTube -
https://www.youtube.com/playlist?list=PLOU2XLYxmsII5c3Mgw6fNYCzaWrsM3sMN
• 2017 Polymer Summit videos on YouTube -
https://www.youtube.com/playlist?list=PLNYkxOF6rcIDP0PqVaJxqNWwIgvoEPzJi
• End-to-End Polymer Apps - 2017 Chrome Dev Summit video - https://youtu.be/Wu2GCRkDecI
• 2017 Google I/O Polymer videos on YouTube -
https://www.youtube.com/playlist?list=PL_c6rbXV248du6m1VJABo32mP7sXWVb4m
118 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
Thank You!
Thank you!
John Riviello
@JohnRiv
Chris Lorenzo
@Chiefcll
1 of 120

More Related Content

Web Components: The Future of Web Development is Here