Web Components 
What? Why? Polymer? 
Rich Bradshaw - Focal Strategy - @richbradshaw
Why do I care about this? 
• You are a HTML “user” - this will help you do more advanced things easily 
• You are a front end dev - you want to reuse components across sites/pages 
• You build single page apps (SPAs) and want a better way to organise things 
• You are interested in the direction that the web platform is heading
• What are the conventions of HTML? Plan 
• How do we build ‘components’ at the moment? 
• What are the limitations of the current approach? 
Questions/short break 
• What’s in the Web Components spec? 
• What components exist already? 
Questions/short break 
• What’s Polymer? 
• Let’s build an app using Polymer
Conventions in HTML
Elements can fire events, and have public properties
Elements are configured using attributes. 
Elements can be nested, and can only make sense 
if they are nested.
Some elements are not visual. 
Elements can have methods.
Some elements can provide complex behaviour.
Some elements have surprisingly complete 
APIs, that been there since, well, forever.
Summary 
Elements: 
• Are configured by attributes 
• Can fire events 
• Can have their own methods and properties 
• Are not always visible (e.g. meta, track, media) 
• Can be required to be within another element 
Check out: https://developer.mozilla.org/en-US/docs/Web/API
How do we build ‘components’ 
at the moment?
Component? 
• Self contained part of a page 
• Google Map, Youtube Video, 
Carousel, Accordion, Nav Bar………
+ some CSS in ‘global’ scope 
+ JS with public and private 
variables/methods 
jQuery Cycle
Google Maps
Advanced YouTube Embed
Problems 
• Not that user friendly ( jQuery isn’t bad) 
• Not easy to ensure assets aren’t loaded multiple times 
• Hard to link together 
• Hard for plugins to hide their CSS from the rest of the page
Inconsistency 
HTML Current Plugins 
Config Attributes Javascript 
Events Listen to element Listen to framework events 
Methods/Props On element From framework 
Interop Any framework Only same framework
Questions so far?
Web Components
Custom Elements HTML Imports 
Templates Shadow DOM 
BTW, there’s a polyfill for each - IE10+ and current version of all others
Shadow DOM 
Encapsulate the content of the DOM. Private JS/CSS for HTML.
A number input: Same ID in every instance 
Shadow DOM
Shadow DOM
Shadow DOM 
+ 
=
Browser support = 42.2% http://caniuse.com/#feat=shadowdom 
Shadow DOM
Templates 
Inert bits of HTML/CSS/JS that can be stamped out as needed. 
Like a native version of Handlebars/Mustache/Hogan.
Templates 
Broken image won’t cause 
error as this is inert 
Fill it in, stamp it out
+ 
You could now swap the template out without changing the page markup… 
Templates
Templates 
CSS Selectors
Browser support = 54.4% http://caniuse.com/#feat=template 
Templates
HTML Imports 
You can import CSS, JS, images, video and audio already - why 
not HTML?
How to load HTML 
• iframe = limited 
• ajax = OK but needs JS 
• pretending via <script type=‘x-template’>
Browser dedupes for you. Can be from another domain. 
Can be changed/created by JS. 
Content accessible via JS, ideal for templates. 
HTML Imports
Browser support = 35.3% http://caniuse.com/#feat=imports 
HTML Imports
Custom Elements 
Put all that together…
Custom Elements
Custom elements 
• Name must contain a dash 
• Default is to inherit from HTMLElement 
• Can extend native elements! 
• Can have methods, properties, events as native elements do 
• Can target unresolved elements to hide with CSS to avoid FOUC
Browser support = 38.4% http://caniuse.com/#feat=custom-elements 
Custom Elements
Existing Components
Really good list at: http://customelements.io/
Questions so far? 
http://webcomponents.org/ 
http://customelements.io/ 
http://caniuse.com/
Polymer
Polymer 
• Everything is an element 
• Comes with the basics (e.g. menus, buttons etc) 
• Two way data binding (on everything…) 
• Includes platform.js, polyfill for all we’ve talked about, 
object.observe, new web animation spec, pointer events and more.
Demo Time!
Find out more… 
• http://updates.html5rocks.com/tag/web-components 
• http://w3c.github.io/webcomponents/explainer/ 
• Spec: @coonsta, @dglazkov 
• Tools, articles: @ebidel, @addyosmani 
• Components Registry: http://customelements.io/ by @zenorocha 
• http://www.polymer-project.org/ 
• http://www.x-tags.org/, http://mozilla.github.io/brick/ 
• Freenode #webcomponents

Introduction to Web Components

  • 1.
    Web Components What?Why? Polymer? Rich Bradshaw - Focal Strategy - @richbradshaw
  • 2.
    Why do Icare about this? • You are a HTML “user” - this will help you do more advanced things easily • You are a front end dev - you want to reuse components across sites/pages • You build single page apps (SPAs) and want a better way to organise things • You are interested in the direction that the web platform is heading
  • 3.
    • What arethe conventions of HTML? Plan • How do we build ‘components’ at the moment? • What are the limitations of the current approach? Questions/short break • What’s in the Web Components spec? • What components exist already? Questions/short break • What’s Polymer? • Let’s build an app using Polymer
  • 4.
  • 5.
    Elements can fireevents, and have public properties
  • 6.
    Elements are configuredusing attributes. Elements can be nested, and can only make sense if they are nested.
  • 7.
    Some elements arenot visual. Elements can have methods.
  • 8.
    Some elements canprovide complex behaviour.
  • 9.
    Some elements havesurprisingly complete APIs, that been there since, well, forever.
  • 10.
    Summary Elements: •Are configured by attributes • Can fire events • Can have their own methods and properties • Are not always visible (e.g. meta, track, media) • Can be required to be within another element Check out: https://developer.mozilla.org/en-US/docs/Web/API
  • 11.
    How do webuild ‘components’ at the moment?
  • 12.
    Component? • Selfcontained part of a page • Google Map, Youtube Video, Carousel, Accordion, Nav Bar………
  • 13.
    + some CSSin ‘global’ scope + JS with public and private variables/methods jQuery Cycle
  • 14.
  • 15.
  • 16.
    Problems • Notthat user friendly ( jQuery isn’t bad) • Not easy to ensure assets aren’t loaded multiple times • Hard to link together • Hard for plugins to hide their CSS from the rest of the page
  • 17.
    Inconsistency HTML CurrentPlugins Config Attributes Javascript Events Listen to element Listen to framework events Methods/Props On element From framework Interop Any framework Only same framework
  • 18.
  • 19.
  • 20.
    Custom Elements HTMLImports Templates Shadow DOM BTW, there’s a polyfill for each - IE10+ and current version of all others
  • 21.
    Shadow DOM Encapsulatethe content of the DOM. Private JS/CSS for HTML.
  • 22.
    A number input:Same ID in every instance Shadow DOM
  • 23.
  • 24.
  • 25.
    Browser support =42.2% http://caniuse.com/#feat=shadowdom Shadow DOM
  • 26.
    Templates Inert bitsof HTML/CSS/JS that can be stamped out as needed. Like a native version of Handlebars/Mustache/Hogan.
  • 27.
    Templates Broken imagewon’t cause error as this is inert Fill it in, stamp it out
  • 28.
    + You couldnow swap the template out without changing the page markup… Templates
  • 29.
  • 30.
    Browser support =54.4% http://caniuse.com/#feat=template Templates
  • 31.
    HTML Imports Youcan import CSS, JS, images, video and audio already - why not HTML?
  • 32.
    How to loadHTML • iframe = limited • ajax = OK but needs JS • pretending via <script type=‘x-template’>
  • 33.
    Browser dedupes foryou. Can be from another domain. Can be changed/created by JS. Content accessible via JS, ideal for templates. HTML Imports
  • 34.
    Browser support =35.3% http://caniuse.com/#feat=imports HTML Imports
  • 35.
    Custom Elements Putall that together…
  • 36.
  • 37.
    Custom elements •Name must contain a dash • Default is to inherit from HTMLElement • Can extend native elements! • Can have methods, properties, events as native elements do • Can target unresolved elements to hide with CSS to avoid FOUC
  • 38.
    Browser support =38.4% http://caniuse.com/#feat=custom-elements Custom Elements
  • 39.
  • 40.
    Really good listat: http://customelements.io/
  • 41.
    Questions so far? http://webcomponents.org/ http://customelements.io/ http://caniuse.com/
  • 42.
  • 43.
    Polymer • Everythingis an element • Comes with the basics (e.g. menus, buttons etc) • Two way data binding (on everything…) • Includes platform.js, polyfill for all we’ve talked about, object.observe, new web animation spec, pointer events and more.
  • 45.
  • 46.
    Find out more… • http://updates.html5rocks.com/tag/web-components • http://w3c.github.io/webcomponents/explainer/ • Spec: @coonsta, @dglazkov • Tools, articles: @ebidel, @addyosmani • Components Registry: http://customelements.io/ by @zenorocha • http://www.polymer-project.org/ • http://www.x-tags.org/, http://mozilla.github.io/brick/ • Freenode #webcomponents