Custom Elements with Polymer Web Components #econfpsu16

John Riviello
John RivielloEngineering Fellow at Comcast
Custom Elements with
Polymer Web Components
John Riviello
@JohnRiv
Distinguished Engineer, Comcast Interactive Media
Elements: The Web Conference at Penn State – June 14, 2016
Polymer &
Web Components
”250/365 - Bricks" by Kenny Louie is licensed under CC BY 2.0
Why? What? How?
Communicating & shipping
web design & functionality
updates across a large
organization is HARD WORK
Communicating & Sharing Web Updates Across Your Organization
1. Living Style Guides
2. Tiny Bootstraps
3. AngularJS Directives
4. React Components
5. Web Standards?
John Riviello – Custom Elements with Polymer Web Components4
Potential Technical Solutions:
YES!
Web Standards!
Web Components!
What are
Web Components?
What are Web Components?
4 Specs
John Riviello – Custom Elements with Polymer Web Components7
What are Web Components?
Custom Elements
John Riviello – Custom Elements with Polymer Web Components8
What are Web Components?
Custom Elements
John Riviello – Custom Elements with Polymer Web Components9
•Provides a way for authors to build their own
fully-featured DOM elements.
- <xc-tab>Your Wifi</xc-tab>
•Can extend existing HTML elements
- <button is="xc-button">Edit</button>
What are Web Components?
HTML Imports
John Riviello – Custom Elements with Polymer Web Components10
What are Web Components?
HTML Imports
John Riviello – Custom Elements with Polymer Web Components11
• Means to import custom elements
- <link rel="import" href="../xc-tab/xc-tab.html">
• Can link to external resources
- <link rel="import"
href="http://polygit.org/components/paper-
toast/paper-toast.html">
What are Web Components?
Templates
John Riviello – Custom Elements with Polymer Web Components12
What are Web Components?
Templates
John Riviello – Custom Elements with Polymer Web Components13
• 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
What are Web Components?
Shadow DOM
John Riviello – Custom Elements with Polymer Web Components14
What are Web Components?
Shadow DOM
John Riviello – Custom Elements with Polymer Web Components15
•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">
Can we even use
these things?
Source: http://jonrimmer.github.io/are-we-componentized-yet/
Are We Componentized Yet?
There’s hope.
What’s better than
hope?
POLYFILLS!
*as long as there is still hope*
Web Components Polyfills
webcomponents.js
John Riviello – Custom Elements with Polymer Web Components20
•Custom Elements
•HTML Imports
•Templates
•Shadow DOM
•ES6 WeakMap
•Mutation Observers
Browser Support
• IE10 (max polyfilling)
• IE11+/Edge
• Chrome (latest)
• Firefox (latest)
• Safari 7+
• Chrome Android (latest)
• Mobile Safari (latest)
Size: 33.3kB gzipped
Web Components Polyfills
webcomponents-lite.js
John Riviello – Custom Elements with Polymer Web Components21
•Custom Elements
•HTML Imports
•Templates
•Shadow DOM
•ES6 WeakMap
•Mutation Observers
Browser Support
• IE10 (flaky)
• IE11+/Edge
• Chrome (latest)
• Firefox (latest)
• Safari 7+
• Chrome Android (latest)
• Mobile Safari (latest)
Size: 33.3kB 12.0kB gzipped
Lightweight
Web Component
Libraries
Lightweight Web Component Libraries
X-Tag 5.0kB
John Riviello – Custom Elements with Polymer Web Components23
• IE9+/Edge
• Chrome (all)
• Firefox (all)
• Safari ”Mac” (5.1+?)
• Chrome & Android 4.0+
• Mobile Safari 5+
• Opera 11+
• IE10+/Edge
• Chrome 35+
• Firefox 35+
• Safari 7+
• Chrome Android (latest?)
• Mobile Safari (latest?)
• Opera (latest)
• IE11+/Edge
• Chrome (latest)
• Firefox (latest)
• Safari 7+
• Chrome Android (latest)
• Mobile Safari 7.1+
• Opera (latest)
Bosonic 9.5kB Polymer 37.2kB
Google Polymer
What is Polymer?
1. Material Design
2. A framework
3. Required to use Web Components
John Riviello – Custom Elements with Polymer Web Components25
Polymer is NOT:
What is Polymer?
<script>
var proto = Object.create(HTMLElement.prototype),
protoElement;
proto.createdCallback = function () {
this.textContent = "I'm a proto-element.
Check out my prototype!"
};
protoElement = document.registerElement('proto-element', {
prototype: proto
});
</script>
John Riviello – Custom Elements with Polymer Web Components26
Creating a Custom Element Natively
What is Polymer?
<link rel="import"
href="bower_components/polymer/polymer.html">
<script>
Polymer({
is: "proto-element",
created: function() {
this.textContent = "I'm a proto-element.
Check out my prototype!"
}
});
</script>
John Riviello – Custom Elements with Polymer Web Components27
Creating a Polymer Custom Element
What is Polymer?
<!DOCTYPE html>
<html>
<head>
<script src="webcomponents-lite.js"></script>
<link rel="import" href="proto-element.html">
</head>
<body>
<proto-element></proto-element>
</body>
</html>
RESULT:
I'm a proto-element. Check out my prototype!
John Riviello – Custom Elements with Polymer Web Components28
Using Our Custom Element
What is Polymer?
Polymer({
is: 'my-namecard',
properties: {
myName: {
type: String
}
},
ready: function() {
this.textContent = 'Hi! My name is ' + this.myName;
}
});
<my-namecard my-name="John"></my-namecard>
RESULT:
Hi! My name is John
John Riviello – Custom Elements with Polymer Web Components29
Configuring Properties
What is Polymer?
<dom-module id="my-namecard">
<template>
<div>Hi! My name is <span>{{myName}}</span></div>
</template>
<script>
Polymer({
is: 'my-namecard',
properties: {
myName: {
type: String
}
}
});
</script>
</dom-module>
John Riviello – Custom Elements with Polymer Web Components30
Data Binding
<my-namecard my-name="John">
</my-namecard>
RESULT:
Hi! My name is John
What is Polymer?
<dom-module id="my-namecard">
<template>
<div>Hi! My name is <span>{{myName}}</span></div>
</template>
<style>
span {
font-weight: bold;
}
</style>
<script>
Polymer({
is: 'my-namecard',
…
John Riviello – Custom Elements with Polymer Web Components31
Encapsulated Styles
<my-namecard my-name="John">
</my-namecard>
<p><span>Span Text</span></p>
RESULT:
Hi! My name is John
Span Text
What is Polymer?
xc-styles/xc-styles.html:
<style is="custom-style">
:root { --si-blue-sky: #2B9CD8; }
</style>
xc-custom-element/xc-custom-element.html:
<link rel="import" href="../xc-styles/xc-styles.html">
<dom-module id="xc-custom-element">
<template>
<style>
:host { border: 1px solid var(--si-blue-sky); }
</style>
</template>
</dom-module>
John Riviello – Custom Elements with Polymer Web Components32
CSS Variables for Sharing Properties
What is Polymer?
xc-fancy-element/xc-fancy-element.html:
<style>
:host {
color: var(--xc-fancy-element-color, red);
}
</style>
xc-custom-element/xc-custom-element.html:
<link rel="import" href="../xc-fancy-element/xc-fancy-element.html">
<style is="custom-style">
xc-fancy-element {
--xc-fancy-element-color: blue;
}
</style>
<template><xc-fancy-element></xc-fancy-element></template>
John Riviello – Custom Elements with Polymer Web Components33
CSS Variables for Custom Styles
What is Polymer?
xc-fancy-element/xc-fancy-element.html:
<style>
:host {
@apply(--xc-fancy-element);
}
</style>
xc-custom-element/xc-custom-element.html:
<link rel="import" href="../xc-fancy-element/xc-fancy-element.html">
<style is="custom-style">
--xc-fancy-element: {
color: blue;
margin: 0 auto;
};
</style>
<template><xc-fancy-element></xc-fancy-element></template>
John Riviello – Custom Elements with Polymer Web Components34
Mixins for Custom Styles
What is Polymer?
• Behaviors (shared functionality)
• Events
• Gestures (up, down, tap, track)
• CLI Tools
• Built-in Test Framework
• Generated Documentation Pages
John Riviello – Custom Elements with Polymer Web Components35
Other Features Polymer Provides
Building with
Polymer
Building with Polymer
• Use someone else’s element(s)
• Build your own element(s)
• Build an entire app with Polymer
John Riviello – Custom Elements with Polymer Web Components37
How to Get Started
Using Open Source
Polymer Elements
$ bower install GITHUB/ELEMENT --save
$ bower install PolymerElements/app-route --save
https://elements.polymer-project.org
https://customelements.io
Building Your Own
Polymer Element
Custom Elements with Polymer Web Components #econfpsu16
$ polymer help
Available Commands
build Builds an application-style project
help Shows this help message,
or help for a specific command
init Initializes a Polymer project
lint Lints the project
serve Runs the polyserve development server
test Runs web-component-tester
$ polymer init
? Which starter template would you like to use?
› element: A blank element template
application: A blank application template
shop: The "Shop" Progressive Web App demo
app-drawer-template: A starter application…
Custom Elements with Polymer Web Components #econfpsu16
Custom Elements with Polymer Web Components #econfpsu16
Custom Elements with Polymer Web Components #econfpsu16
Building with Polymer
• A component should do 1 thing
• Break up into smaller components
• Component doesn’t have to be visual
• Syndicating outside of primary use
John Riviello – Custom Elements with Polymer Web Components49
Things to Consider
Building a
Polymer App
Custom Elements with Polymer Web Components #econfpsu16
Building with Polymer
$ npm install -g polymer-cli
$ polymer init
? Which starter template would you like to use?
element: A blank element template
› application: A blank application template
shop: The "Shop" Progressive Web App demo
app-drawer-template: A starter application…
John Riviello – Custom Elements with Polymer Web Components52
Building a Polymer App
Building with Polymer
? Application name cli-demo
? Main element name cli-demo-app
? Brief description of the application App Demo
John Riviello – Custom Elements with Polymer Web Components53
Building a Polymer App
Building with Polymer
create bower.json
create index.html
create manifest.json
create README.md
create src/cli-demo-app/cli-demo-app.html
create test/cli-demo-app/cli-demo-app_test.html
John Riviello – Custom Elements with Polymer Web Components54
Building a Polymer App
Building with Polymer
Project generated!
Installing dependencies...
$ polymer serve
• Load up http://localhost:8080
John Riviello – Custom Elements with Polymer Web Components55
Building a Polymer App
Hello cli-demo-app
• View README.md for info on building & running tests
• Create additional elements
• Pull in external elements
Deploying with
Polymer
Deploying with Polymer
• HTML Import external components
• Bundle internal components
- vulcanize (gulp, grunt or standalone)
- Polymer CLI:
$ polymer build
John Riviello – Custom Elements with Polymer Web Components57
External vs. Internal
Custom Elements with Polymer Web Components
John Riviello – Custom Elements with Polymer Web Components58
Learning More
• polymer-project.org
• webcomponents.org
• Polycasts with Rob Dodson on YouTube
• 2016 Google I/O Polymer videos on YouTube
• Polymer Summit videos on YouTube
• Polymer Slack: polymer-slack.herokuapp.com
For Further Info & Feedback:
Twitter: @JohnRiv
1 of 58

Recommended

The Truth About Your Web App's Performance by
The Truth About Your Web App's PerformanceThe Truth About Your Web App's Performance
The Truth About Your Web App's PerformanceJohn Riviello
1.6K views106 slides
Build Reusable Web Components using HTML5 Web cComponents by
Build Reusable Web Components using HTML5 Web cComponentsBuild Reusable Web Components using HTML5 Web cComponents
Build Reusable Web Components using HTML5 Web cComponentsGil Fink
2.8K views32 slides
Polymer / WebComponents by
Polymer / WebComponentsPolymer / WebComponents
Polymer / WebComponentsArnaud Kervern
1.2K views52 slides
Booting up with polymer by
Booting up with polymerBooting up with polymer
Booting up with polymerMarcus Hellberg
690 views51 slides
Polymer and web component by
Polymer and web componentPolymer and web component
Polymer and web componentImam Raza
1.4K views25 slides
Google Developer Group(GDG) DevFest Event 2012 Android talk by
Google Developer Group(GDG) DevFest Event 2012 Android talkGoogle Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talkImam Raza
1.4K views70 slides

More Related Content

What's hot

Web Components by
Web ComponentsWeb Components
Web ComponentsFITC
2.1K views36 slides
Polymer - Welcome to the Future @ PyGrunn 08/07/2014 by
Polymer - Welcome to the Future @ PyGrunn 08/07/2014Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Polymer - Welcome to the Future @ PyGrunn 08/07/2014Spyros Ioakeimidis
81.6K views52 slides
Introduction to Web Components by
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web ComponentsRich Bradshaw
855 views46 slides
Web components by
Web componentsWeb components
Web componentsGil Fink
517 views45 slides
Web Components by
Web ComponentsWeb Components
Web ComponentsNikolaus Graf
2.7K views28 slides
Polymer & the web components revolution 6:25:14 by
Polymer & the web components revolution 6:25:14Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14mattsmcnulty
4.2K views98 slides

What's hot(20)

Web Components by FITC
Web ComponentsWeb Components
Web Components
FITC2.1K views
Polymer - Welcome to the Future @ PyGrunn 08/07/2014 by Spyros Ioakeimidis
Polymer - Welcome to the Future @ PyGrunn 08/07/2014Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Spyros Ioakeimidis81.6K views
Introduction to Web Components by Rich Bradshaw
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web Components
Rich Bradshaw855 views
Web components by Gil Fink
Web componentsWeb components
Web components
Gil Fink517 views
Polymer & the web components revolution 6:25:14 by mattsmcnulty
Polymer & the web components revolution 6:25:14Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
mattsmcnulty4.2K views
Polymer by jskvara
Polymer Polymer
Polymer
jskvara4.8K views
Introduction to Web Components by Fu Cheng
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web Components
Fu Cheng7.2K views
Web Components with Polymer (extra Polymer 2.0) by Dhyego Fernando
Web Components with Polymer (extra Polymer 2.0)Web Components with Polymer (extra Polymer 2.0)
Web Components with Polymer (extra Polymer 2.0)
Dhyego Fernando259 views
Web components the future is here by Gil Fink
Web components   the future is hereWeb components   the future is here
Web components the future is here
Gil Fink5.4K views
Web Components Everywhere by Ilia Idakiev
Web Components EverywhereWeb Components Everywhere
Web Components Everywhere
Ilia Idakiev1.3K views
Polymer, A Web Component Polyfill Library by naohito maeda
Polymer, A Web Component Polyfill LibraryPolymer, A Web Component Polyfill Library
Polymer, A Web Component Polyfill Library
naohito maeda832 views
Polymer presentation in Google HQ by Harshit Pandey
Polymer presentation in Google HQPolymer presentation in Google HQ
Polymer presentation in Google HQ
Harshit Pandey2.6K views
Web Components by FITC
Web ComponentsWeb Components
Web Components
FITC1.4K views
Razor into the Razor'verse by Ed Charbeneau
Razor into the Razor'verseRazor into the Razor'verse
Razor into the Razor'verse
Ed Charbeneau729 views
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17 by GreeceJS
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
The rise of Polymer and Web Components (Kostas Karolemeas) - GreeceJS #17
GreeceJS452 views
Web Components + Backbone: a Game-Changing Combination by Andrew Rota
Web Components + Backbone: a Game-Changing CombinationWeb Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
Andrew Rota30.3K views

Viewers also liked

Case study - Google's Polymer web components let us develop tomorrow's digita... by
Case study - Google's Polymer web components let us develop tomorrow's digita...Case study - Google's Polymer web components let us develop tomorrow's digita...
Case study - Google's Polymer web components let us develop tomorrow's digita...Henry D Amm
2.3K views23 slides
End to-end apps with type script by
End to-end apps with type scriptEnd to-end apps with type script
End to-end apps with type scriptGil Fink
317 views23 slides
Web components by
Web componentsWeb components
Web componentsRocío Muñoz
963 views33 slides
Principios de Diseño de Componentes Web by
Principios de Diseño de Componentes WebPrincipios de Diseño de Componentes Web
Principios de Diseño de Componentes WebJavier Vélez Reyes
5.1K views170 slides
Dc Motors by
Dc MotorsDc Motors
Dc Motorsstooty s
2.6K views18 slides
Summer trainingvlsi design-2011 by
Summer trainingvlsi design-2011Summer trainingvlsi design-2011
Summer trainingvlsi design-2011dkhari
1K views10 slides

Viewers also liked(20)

Case study - Google's Polymer web components let us develop tomorrow's digita... by Henry D Amm
Case study - Google's Polymer web components let us develop tomorrow's digita...Case study - Google's Polymer web components let us develop tomorrow's digita...
Case study - Google's Polymer web components let us develop tomorrow's digita...
Henry D Amm2.3K views
End to-end apps with type script by Gil Fink
End to-end apps with type scriptEnd to-end apps with type script
End to-end apps with type script
Gil Fink317 views
Dc Motors by stooty s
Dc MotorsDc Motors
Dc Motors
stooty s2.6K views
Summer trainingvlsi design-2011 by dkhari
Summer trainingvlsi design-2011Summer trainingvlsi design-2011
Summer trainingvlsi design-2011
dkhari1K views
Summer trainingsoftware 2011 by dkhari
Summer trainingsoftware 2011Summer trainingsoftware 2011
Summer trainingsoftware 2011
dkhari690 views
Sportconomy Tutorial by Sportconomy
Sportconomy TutorialSportconomy Tutorial
Sportconomy Tutorial
Sportconomy2K views
CéGtaláLó éS CéGbank by Julcsilany
CéGtaláLó éS CéGbankCéGtaláLó éS CéGbank
CéGtaláLó éS CéGbank
Julcsilany464 views
The Truth About Your Web App's Performance by John Riviello
The Truth About Your Web App's PerformanceThe Truth About Your Web App's Performance
The Truth About Your Web App's Performance
John Riviello1.7K views
Robert Milich\'s Sample Portfolio by rmilich
Robert Milich\'s Sample PortfolioRobert Milich\'s Sample Portfolio
Robert Milich\'s Sample Portfolio
rmilich379 views
Как запустить стартап Free by Nata Isaevich
Как запустить стартап FreeКак запустить стартап Free
Как запустить стартап Free
Nata Isaevich683 views
3.dev meetup2 visualforce_sites_a_pruzan by Nata Isaevich
3.dev meetup2 visualforce_sites_a_pruzan3.dev meetup2 visualforce_sites_a_pruzan
3.dev meetup2 visualforce_sites_a_pruzan
Nata Isaevich518 views
Chicora Introduction by chicora
Chicora IntroductionChicora Introduction
Chicora Introduction
chicora333 views
Desde el cielo by deliaa
Desde el cieloDesde el cielo
Desde el cielo
deliaa331 views
Visual Resume of Marc Campman by Marc Campman
Visual Resume of Marc CampmanVisual Resume of Marc Campman
Visual Resume of Marc Campman
Marc Campman617 views

Similar to Custom Elements with Polymer Web Components #econfpsu16

Introduction to Web Components & Polymer Workshop - JS Interactive by
Introduction to Web Components & Polymer Workshop - JS InteractiveIntroduction to Web Components & Polymer Workshop - JS Interactive
Introduction to Web Components & Polymer Workshop - JS InteractiveJohn Riviello
194 views50 slides
Web Components: The Future of Web Development is Here by
Web Components: The Future of Web Development is HereWeb Components: The Future of Web Development is Here
Web Components: The Future of Web Development is HereJohn Riviello
719 views122 slides
Web Components: The Future of Web Development is Here by
Web Components: The Future of Web Development is HereWeb Components: The Future of Web Development is Here
Web Components: The Future of Web Development is HereJohn Riviello
677 views120 slides
Introduction to Web Components & Polymer Workshop - U of I WebCon by
Introduction to Web Components & Polymer Workshop - U of I WebConIntroduction to Web Components & Polymer Workshop - U of I WebCon
Introduction to Web Components & Polymer Workshop - U of I WebConJohn Riviello
254 views62 slides
Workshop: Introduction to Web Components & Polymer by
Workshop: Introduction to Web Components & Polymer Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer John Riviello
640 views50 slides
Intro to Web Components, Polymer & Vaadin Elements by
Intro to Web Components, Polymer & Vaadin ElementsIntro to Web Components, Polymer & Vaadin Elements
Intro to Web Components, Polymer & Vaadin ElementsManuel Carrasco Moñino
2.2K views35 slides

Similar to Custom Elements with Polymer Web Components #econfpsu16(20)

Introduction to Web Components & Polymer Workshop - JS Interactive by John Riviello
Introduction to Web Components & Polymer Workshop - JS InteractiveIntroduction to Web Components & Polymer Workshop - JS Interactive
Introduction to Web Components & Polymer Workshop - JS Interactive
John Riviello194 views
Web Components: The Future of Web Development is Here by John Riviello
Web Components: The Future of Web Development is HereWeb Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
John Riviello719 views
Web Components: The Future of Web Development is Here by John Riviello
Web Components: The Future of Web Development is HereWeb Components: The Future of Web Development is Here
Web Components: The Future of Web Development is Here
John Riviello677 views
Introduction to Web Components & Polymer Workshop - U of I WebCon by John Riviello
Introduction to Web Components & Polymer Workshop - U of I WebConIntroduction to Web Components & Polymer Workshop - U of I WebCon
Introduction to Web Components & Polymer Workshop - U of I WebCon
John Riviello254 views
Workshop: Introduction to Web Components & Polymer by John Riviello
Workshop: Introduction to Web Components & Polymer Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer
John Riviello640 views
Ensuring Design Standards with Web Components by John Riviello
Ensuring Design Standards with Web ComponentsEnsuring Design Standards with Web Components
Ensuring Design Standards with Web Components
John Riviello553 views
Polymer-Powered Design Systems - DevFest Florida by John Riviello
Polymer-Powered Design Systems - DevFest FloridaPolymer-Powered Design Systems - DevFest Florida
Polymer-Powered Design Systems - DevFest Florida
John Riviello701 views
Components Approach to building Web Apps by Vinci Rufus
Components Approach to building Web AppsComponents Approach to building Web Apps
Components Approach to building Web Apps
Vinci Rufus443 views
Open Apereo - Web components workshop by btopro
Open Apereo - Web components workshopOpen Apereo - Web components workshop
Open Apereo - Web components workshop
btopro224 views
Web Components at Scale, HTML5DevConf 2014-10-21 by Chris Danford
Web Components at Scale, HTML5DevConf 2014-10-21Web Components at Scale, HTML5DevConf 2014-10-21
Web Components at Scale, HTML5DevConf 2014-10-21
Chris Danford1K views
Meteor - Codemotion Rome 2015 by Codemotion
Meteor - Codemotion Rome 2015Meteor - Codemotion Rome 2015
Meteor - Codemotion Rome 2015
Codemotion955 views
Meteor + Polymer by wolf4ood
Meteor + PolymerMeteor + Polymer
Meteor + Polymer
wolf4ood5.1K views
Dart Past Your Competition by Getting Your Digital Experience into Market Fas... by Perficient, Inc.
Dart Past Your Competition by Getting Your Digital Experience into Market Fas...Dart Past Your Competition by Getting Your Digital Experience into Market Fas...
Dart Past Your Competition by Getting Your Digital Experience into Market Fas...
Perficient, Inc.542 views
Intro to MontageJS by Ryan Paul
Intro to MontageJSIntro to MontageJS
Intro to MontageJS
Ryan Paul875 views
Angular elements - embed your angular components EVERYWHERE by Nadav Mary
Angular elements - embed your angular components EVERYWHEREAngular elements - embed your angular components EVERYWHERE
Angular elements - embed your angular components EVERYWHERE
Nadav Mary150 views
JEE Conf 2015: Less JS! by _Dewy_
JEE Conf 2015: Less JS!JEE Conf 2015: Less JS!
JEE Conf 2015: Less JS!
_Dewy_1.6K views
Intro to polymer-Devfest Yaoundé 2013 by gdgyaounde
Intro to polymer-Devfest Yaoundé 2013Intro to polymer-Devfest Yaoundé 2013
Intro to polymer-Devfest Yaoundé 2013
gdgyaounde1.2K views

Recently uploaded

Mini-Track: AI and ML in Network Operations Applications by
Mini-Track: AI and ML in Network Operations ApplicationsMini-Track: AI and ML in Network Operations Applications
Mini-Track: AI and ML in Network Operations ApplicationsNetwork Automation Forum
10 views24 slides
PRODUCT PRESENTATION.pptx by
PRODUCT PRESENTATION.pptxPRODUCT PRESENTATION.pptx
PRODUCT PRESENTATION.pptxangelicacueva6
15 views1 slide
Ransomware is Knocking your Door_Final.pdf by
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdfSecurity Bootcamp
59 views46 slides
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...Bernd Ruecker
40 views69 slides
PRODUCT LISTING.pptx by
PRODUCT LISTING.pptxPRODUCT LISTING.pptx
PRODUCT LISTING.pptxangelicacueva6
14 views1 slide
Data Integrity for Banking and Financial Services by
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial ServicesPrecisely
25 views26 slides

Recently uploaded(20)

iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker40 views
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely25 views
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana16 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab21 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views

Custom Elements with Polymer Web Components #econfpsu16

  • 1. Custom Elements with Polymer Web Components John Riviello @JohnRiv Distinguished Engineer, Comcast Interactive Media Elements: The Web Conference at Penn State – June 14, 2016
  • 2. Polymer & Web Components ”250/365 - Bricks" by Kenny Louie is licensed under CC BY 2.0 Why? What? How?
  • 3. Communicating & shipping web design & functionality updates across a large organization is HARD WORK
  • 4. Communicating & Sharing Web Updates Across Your Organization 1. Living Style Guides 2. Tiny Bootstraps 3. AngularJS Directives 4. React Components 5. Web Standards? John Riviello – Custom Elements with Polymer Web Components4 Potential Technical Solutions:
  • 7. What are Web Components? 4 Specs John Riviello – Custom Elements with Polymer Web Components7
  • 8. What are Web Components? Custom Elements John Riviello – Custom Elements with Polymer Web Components8
  • 9. What are Web Components? Custom Elements John Riviello – Custom Elements with Polymer Web Components9 •Provides a way for authors to build their own fully-featured DOM elements. - <xc-tab>Your Wifi</xc-tab> •Can extend existing HTML elements - <button is="xc-button">Edit</button>
  • 10. What are Web Components? HTML Imports John Riviello – Custom Elements with Polymer Web Components10
  • 11. What are Web Components? HTML Imports John Riviello – Custom Elements with Polymer Web Components11 • Means to import custom elements - <link rel="import" href="../xc-tab/xc-tab.html"> • Can link to external resources - <link rel="import" href="http://polygit.org/components/paper- toast/paper-toast.html">
  • 12. What are Web Components? Templates John Riviello – Custom Elements with Polymer Web Components12
  • 13. What are Web Components? Templates John Riviello – Custom Elements with Polymer Web Components13 • 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
  • 14. What are Web Components? Shadow DOM John Riviello – Custom Elements with Polymer Web Components14
  • 15. What are Web Components? Shadow DOM John Riviello – Custom Elements with Polymer Web Components15 •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">
  • 16. Can we even use these things?
  • 19. POLYFILLS! *as long as there is still hope*
  • 20. Web Components Polyfills webcomponents.js John Riviello – Custom Elements with Polymer Web Components20 •Custom Elements •HTML Imports •Templates •Shadow DOM •ES6 WeakMap •Mutation Observers Browser Support • IE10 (max polyfilling) • IE11+/Edge • Chrome (latest) • Firefox (latest) • Safari 7+ • Chrome Android (latest) • Mobile Safari (latest) Size: 33.3kB gzipped
  • 21. Web Components Polyfills webcomponents-lite.js John Riviello – Custom Elements with Polymer Web Components21 •Custom Elements •HTML Imports •Templates •Shadow DOM •ES6 WeakMap •Mutation Observers Browser Support • IE10 (flaky) • IE11+/Edge • Chrome (latest) • Firefox (latest) • Safari 7+ • Chrome Android (latest) • Mobile Safari (latest) Size: 33.3kB 12.0kB gzipped
  • 23. Lightweight Web Component Libraries X-Tag 5.0kB John Riviello – Custom Elements with Polymer Web Components23 • IE9+/Edge • Chrome (all) • Firefox (all) • Safari ”Mac” (5.1+?) • Chrome & Android 4.0+ • Mobile Safari 5+ • Opera 11+ • IE10+/Edge • Chrome 35+ • Firefox 35+ • Safari 7+ • Chrome Android (latest?) • Mobile Safari (latest?) • Opera (latest) • IE11+/Edge • Chrome (latest) • Firefox (latest) • Safari 7+ • Chrome Android (latest) • Mobile Safari 7.1+ • Opera (latest) Bosonic 9.5kB Polymer 37.2kB
  • 25. What is Polymer? 1. Material Design 2. A framework 3. Required to use Web Components John Riviello – Custom Elements with Polymer Web Components25 Polymer is NOT:
  • 26. What is Polymer? <script> var proto = Object.create(HTMLElement.prototype), protoElement; proto.createdCallback = function () { this.textContent = "I'm a proto-element. Check out my prototype!" }; protoElement = document.registerElement('proto-element', { prototype: proto }); </script> John Riviello – Custom Elements with Polymer Web Components26 Creating a Custom Element Natively
  • 27. What is Polymer? <link rel="import" href="bower_components/polymer/polymer.html"> <script> Polymer({ is: "proto-element", created: function() { this.textContent = "I'm a proto-element. Check out my prototype!" } }); </script> John Riviello – Custom Elements with Polymer Web Components27 Creating a Polymer Custom Element
  • 28. What is Polymer? <!DOCTYPE html> <html> <head> <script src="webcomponents-lite.js"></script> <link rel="import" href="proto-element.html"> </head> <body> <proto-element></proto-element> </body> </html> RESULT: I'm a proto-element. Check out my prototype! John Riviello – Custom Elements with Polymer Web Components28 Using Our Custom Element
  • 29. What is Polymer? Polymer({ is: 'my-namecard', properties: { myName: { type: String } }, ready: function() { this.textContent = 'Hi! My name is ' + this.myName; } }); <my-namecard my-name="John"></my-namecard> RESULT: Hi! My name is John John Riviello – Custom Elements with Polymer Web Components29 Configuring Properties
  • 30. What is Polymer? <dom-module id="my-namecard"> <template> <div>Hi! My name is <span>{{myName}}</span></div> </template> <script> Polymer({ is: 'my-namecard', properties: { myName: { type: String } } }); </script> </dom-module> John Riviello – Custom Elements with Polymer Web Components30 Data Binding <my-namecard my-name="John"> </my-namecard> RESULT: Hi! My name is John
  • 31. What is Polymer? <dom-module id="my-namecard"> <template> <div>Hi! My name is <span>{{myName}}</span></div> </template> <style> span { font-weight: bold; } </style> <script> Polymer({ is: 'my-namecard', … John Riviello – Custom Elements with Polymer Web Components31 Encapsulated Styles <my-namecard my-name="John"> </my-namecard> <p><span>Span Text</span></p> RESULT: Hi! My name is John Span Text
  • 32. What is Polymer? xc-styles/xc-styles.html: <style is="custom-style"> :root { --si-blue-sky: #2B9CD8; } </style> xc-custom-element/xc-custom-element.html: <link rel="import" href="../xc-styles/xc-styles.html"> <dom-module id="xc-custom-element"> <template> <style> :host { border: 1px solid var(--si-blue-sky); } </style> </template> </dom-module> John Riviello – Custom Elements with Polymer Web Components32 CSS Variables for Sharing Properties
  • 33. What is Polymer? xc-fancy-element/xc-fancy-element.html: <style> :host { color: var(--xc-fancy-element-color, red); } </style> xc-custom-element/xc-custom-element.html: <link rel="import" href="../xc-fancy-element/xc-fancy-element.html"> <style is="custom-style"> xc-fancy-element { --xc-fancy-element-color: blue; } </style> <template><xc-fancy-element></xc-fancy-element></template> John Riviello – Custom Elements with Polymer Web Components33 CSS Variables for Custom Styles
  • 34. What is Polymer? xc-fancy-element/xc-fancy-element.html: <style> :host { @apply(--xc-fancy-element); } </style> xc-custom-element/xc-custom-element.html: <link rel="import" href="../xc-fancy-element/xc-fancy-element.html"> <style is="custom-style"> --xc-fancy-element: { color: blue; margin: 0 auto; }; </style> <template><xc-fancy-element></xc-fancy-element></template> John Riviello – Custom Elements with Polymer Web Components34 Mixins for Custom Styles
  • 35. What is Polymer? • Behaviors (shared functionality) • Events • Gestures (up, down, tap, track) • CLI Tools • Built-in Test Framework • Generated Documentation Pages John Riviello – Custom Elements with Polymer Web Components35 Other Features Polymer Provides
  • 37. Building with Polymer • Use someone else’s element(s) • Build your own element(s) • Build an entire app with Polymer John Riviello – Custom Elements with Polymer Web Components37 How to Get Started
  • 39. $ bower install GITHUB/ELEMENT --save $ bower install PolymerElements/app-route --save
  • 44. $ polymer help Available Commands build Builds an application-style project help Shows this help message, or help for a specific command init Initializes a Polymer project lint Lints the project serve Runs the polyserve development server test Runs web-component-tester
  • 45. $ polymer init ? Which starter template would you like to use? › element: A blank element template application: A blank application template shop: The "Shop" Progressive Web App demo app-drawer-template: A starter application…
  • 49. Building with Polymer • A component should do 1 thing • Break up into smaller components • Component doesn’t have to be visual • Syndicating outside of primary use John Riviello – Custom Elements with Polymer Web Components49 Things to Consider
  • 52. Building with Polymer $ npm install -g polymer-cli $ polymer init ? Which starter template would you like to use? element: A blank element template › application: A blank application template shop: The "Shop" Progressive Web App demo app-drawer-template: A starter application… John Riviello – Custom Elements with Polymer Web Components52 Building a Polymer App
  • 53. Building with Polymer ? Application name cli-demo ? Main element name cli-demo-app ? Brief description of the application App Demo John Riviello – Custom Elements with Polymer Web Components53 Building a Polymer App
  • 54. Building with Polymer create bower.json create index.html create manifest.json create README.md create src/cli-demo-app/cli-demo-app.html create test/cli-demo-app/cli-demo-app_test.html John Riviello – Custom Elements with Polymer Web Components54 Building a Polymer App
  • 55. Building with Polymer Project generated! Installing dependencies... $ polymer serve • Load up http://localhost:8080 John Riviello – Custom Elements with Polymer Web Components55 Building a Polymer App Hello cli-demo-app • View README.md for info on building & running tests • Create additional elements • Pull in external elements
  • 57. Deploying with Polymer • HTML Import external components • Bundle internal components - vulcanize (gulp, grunt or standalone) - Polymer CLI: $ polymer build John Riviello – Custom Elements with Polymer Web Components57 External vs. Internal
  • 58. Custom Elements with Polymer Web Components John Riviello – Custom Elements with Polymer Web Components58 Learning More • polymer-project.org • webcomponents.org • Polycasts with Rob Dodson on YouTube • 2016 Google I/O Polymer videos on YouTube • Polymer Summit videos on YouTube • Polymer Slack: polymer-slack.herokuapp.com For Further Info & Feedback: Twitter: @JohnRiv