SlideShare a Scribd company logo
Build Reusable Web Components
Using HTML5 Web Components
Gil Fink
CEO and Senior Consultant, sparXys
The Pyramid of Doom
Build Reusable Web Components
Using HTML5 Web Components
Gil Fink
CEO and Senior Consultant, sparXys
About Me
• sparXys CEO and Senior consultant
• ASP.NET/IIS Microsoft MVP
• Co-author of Pro Single Page Application
Development (Apress)
• Co-author of 4 Microsoft Official Courses (MOCs)
• Founder of Front-End.IL Meetup and co-organizer of
GDG Rashlatz Meetup
Agenda
• The problems we face
• Web Components APIs
o Templates
o Imports
o Shadow DOM
o Custom Elements
1. Undescriptive Markup
Google+ Example
The Pyramid of Doom is
Back
2. Poor Separation of
Concerns
You want HTML, CSS and
JavaScript to work together
You end up with a mess
The wiring gets in your way!
3. No Native Templates
• Store HTML in hidden DOM element and show it
• Use script tag as a template holder:
<script id=”myTemplate” type=”text/template”>
<div>
…
</div>
</script>
4. No Bundling
• You want to bundle a complex component
The component includes HTML, CSS and JavaScript
how would you do that?
o Use a server side wrapping mechanism?
Web Components to the
Rescue
• A set of standards designed to componentize the
web
• Some general goals:
Code Reuse Encapsulation
Separation of
Concerns
Composition Theming Expressive Semantic
The Web Components
Standards
•Reusable DOM fragmentsTemplates
•Load HTML declarativelyImports
•DOM encapsulationShadow DOM
•Create your own elements
Custom
Elements
Let’s Drill Down
Templates
• A new HTML element – template
• Can be used to instantiate document fragments
• Can wrap HTML, style tags and script tags
• To use the template you need some JavaScript
magic
<template id=”myTemplate”>
<div>
…
</div>
</template>
Cloning a Template
• Select the template and extract its content
o Using its content property
• Use the importNode function to get the cloned
content
• Only when the clone is appended to the DOM
o The style and JavaScript are executed
o Resources like images are retrieved from the server
var template = document.querySelector(‘#myTemplate’);
var clone = document.importNode(template.content, true);
Demo
Templates
Imports
• Load additional HTML documents
o Without Ajax
• A new type of link tag
• Use the rel attribute with the import type:
<link rel=”import” href=”myImport.html”>
Imports and Bundling
• Enable to bundle a full component into a HTML file
o The HTML can include scripts and CSS styles
• The whole bundle can be retrieved in a single call
Imports and The DOM
• Importing a document doesn’t include it into the
DOM
o It will parse it in memory and load all the additional resources
• Use the import property of the link tag:
var content =
document.querySelector(‘link[rel=”import”]’).import;
Demo
Imports
Shadow DOM
• Encapsulate DOM parts
o The browser will know how to present those parts
o The browser won’t show the encapsulated parts in the source code
• Creates a boundary between the component and
its user
Demo
Enabling Chrome to Show Shadow DOM Elements
Shadow DOM – Cont.
• Use the createShadowRoot function to wrap an
element as a shadow DOM:
var host = document.querySelector(‘#shadowDOMHost’);
var root = host.createShadowRoot();
root.innerHTML = ‘<div>Lurking in the shadows</div>’;
Demo
Shadow DOM
Custom Elements
• Enable to extend or to create custom HTML
elements
o The new element must inherit from HTMLElement
• Create a custom element using the registerElement
function:
• Extend an existing element:
var myElement = document.registerElement(‘my-element’);
var myInput = document.registerElement(‘my-input’, {
prototype: Object.create(HTMLInputElement.prototype),
extends: ‘input’
});
Custom Elements – Cont.
• Use the element in your DOM:
or use the createElement function
• Custom elements have life cycle events.
For example:
o createdCallback
o attributeChangedCallback
<my-input></my-input>
Demo
Custom Elements
The Current State of Web
Components
• Still W3C Working Drafts
• Browser support:
http://caniuse.com/#search=web%20components
• Main Polyfills:
Polymer X-Tag
Questions?
Summary
• Web Components are emerging standards that
enables:
• Encapsulation
• Separation of Concerns
• Element portability
• And more
• They are still in working drafts
• Taking the web one step forward!
Resources
• Download the slide deck:
• http://webcomponents.org/
• My Blog – http://www.gilfink.net
• Follow me on Twitter – @gilfink
Thank You!
Ad

Recommended

Web components
Web components
Gil Fink
 
Custom Elements with Polymer Web Components #econfpsu16
Custom Elements with Polymer Web Components #econfpsu16
John Riviello
 
Web Components - The Future is Here
Web Components - The Future is Here
Gil Fink
 
Web Components Everywhere
Web Components Everywhere
Ilia Idakiev
 
Polymer and web component
Polymer and web component
Imam Raza
 
The Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has Arrived
Gil Fink
 
The Truth About Your Web App's Performance
The Truth About Your Web App's Performance
John Riviello
 
Web Components
Web Components
FITC
 
Google Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talk
Imam Raza
 
Web Components
Web Components
Nikolaus Graf
 
Introduction to Web Components
Introduction to Web Components
Rich Bradshaw
 
Introduction to Web Components
Introduction to Web Components
Fu Cheng
 
Web components the future is here
Web components the future is here
Gil Fink
 
Web Components
Web Components
FITC
 
How to build a web application with Polymer
How to build a web application with Polymer
Sami Suo-Heikki
 
Web component driven development
Web component driven development
Gil Fink
 
Web Components and Modular CSS
Web Components and Modular CSS
Andrew Rota
 
Building a Secure App with Google Polymer and Java / Spring
Building a Secure App with Google Polymer and Java / Spring
sdeeg
 
HTML5 - Introduction
HTML5 - Introduction
Davy De Pauw
 
Polymer
Polymer
LearningTech
 
Polymer
Polymer
jskvara
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
Andrew Rota
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
mattsmcnulty
 
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Spyros Ioakeimidis
 
Reaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and Polymer
FITC
 
A brave new web - A talk about Web Components
A brave new web - A talk about Web Components
Michiel De Mey
 
Brownbag on basics of web components
Brownbag on basics of web components
Jason Park
 
Web components, so close!
Web components, so close!
Aleks Zinevych
 
Web components
Web components
Noam Kfir
 

More Related Content

What's hot (20)

Google Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talk
Imam Raza
 
Web Components
Web Components
Nikolaus Graf
 
Introduction to Web Components
Introduction to Web Components
Rich Bradshaw
 
Introduction to Web Components
Introduction to Web Components
Fu Cheng
 
Web components the future is here
Web components the future is here
Gil Fink
 
Web Components
Web Components
FITC
 
How to build a web application with Polymer
How to build a web application with Polymer
Sami Suo-Heikki
 
Web component driven development
Web component driven development
Gil Fink
 
Web Components and Modular CSS
Web Components and Modular CSS
Andrew Rota
 
Building a Secure App with Google Polymer and Java / Spring
Building a Secure App with Google Polymer and Java / Spring
sdeeg
 
HTML5 - Introduction
HTML5 - Introduction
Davy De Pauw
 
Polymer
Polymer
LearningTech
 
Polymer
Polymer
jskvara
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
Andrew Rota
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
mattsmcnulty
 
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Spyros Ioakeimidis
 
Reaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and Polymer
FITC
 
A brave new web - A talk about Web Components
A brave new web - A talk about Web Components
Michiel De Mey
 
Brownbag on basics of web components
Brownbag on basics of web components
Jason Park
 
Google Developer Group(GDG) DevFest Event 2012 Android talk
Google Developer Group(GDG) DevFest Event 2012 Android talk
Imam Raza
 
Introduction to Web Components
Introduction to Web Components
Rich Bradshaw
 
Introduction to Web Components
Introduction to Web Components
Fu Cheng
 
Web components the future is here
Web components the future is here
Gil Fink
 
Web Components
Web Components
FITC
 
How to build a web application with Polymer
How to build a web application with Polymer
Sami Suo-Heikki
 
Web component driven development
Web component driven development
Gil Fink
 
Web Components and Modular CSS
Web Components and Modular CSS
Andrew Rota
 
Building a Secure App with Google Polymer and Java / Spring
Building a Secure App with Google Polymer and Java / Spring
sdeeg
 
HTML5 - Introduction
HTML5 - Introduction
Davy De Pauw
 
Polymer
Polymer
jskvara
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
Andrew Rota
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Polymer & the web components revolution 6:25:14
Polymer & the web components revolution 6:25:14
mattsmcnulty
 
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
Spyros Ioakeimidis
 
Reaching for the Future with Web Components and Polymer
Reaching for the Future with Web Components and Polymer
FITC
 
A brave new web - A talk about Web Components
A brave new web - A talk about Web Components
Michiel De Mey
 
Brownbag on basics of web components
Brownbag on basics of web components
Jason Park
 

Similar to Build Reusable Web Components using HTML5 Web cComponents (20)

Web components, so close!
Web components, so close!
Aleks Zinevych
 
Web components
Web components
Noam Kfir
 
Web components
Web components
Mohd Saeed
 
Webcomponents TLV October 2014
Webcomponents TLV October 2014
Dmitry Bakaleinik
 
Dom date and objects and event handling
Dom date and objects and event handling
smitha273566
 
Web components - The Future is Here
Web components - The Future is Here
Gil Fink
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
karthiksmart21
 
Internet protocol second unit IIPPT.pptx
Internet protocol second unit IIPPT.pptx
ssuser92282c
 
Manasa
Manasa
ManasaRamanarayan
 
Angular View Encapsulation
Angular View Encapsulation
Jennifer Estrada
 
Web components - An Introduction
Web components - An Introduction
cherukumilli2
 
Html,CSS & UI/UX design
Html,CSS & UI/UX design
Karthikeyan Dhanasekaran CUA
 
Dom
Dom
baabtra.com - No. 1 supplier of quality freshers
 
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
Horacio Gonzalez
 
Stencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink
 
Polymer Web Framework - Swecha Boot Camp
Polymer Web Framework - Swecha Boot Camp
Swecha | స్వేచ్ఛ
 
Web technologies-course 09.pptx
Web technologies-course 09.pptx
Stefan Oprea
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Codecamp Romania
 
Agile sites311training
Agile sites311training
Michele Sciabarrà
 
Javascript libraries
Javascript libraries
Dumindu Pahalawatta
 
Web components, so close!
Web components, so close!
Aleks Zinevych
 
Web components
Web components
Noam Kfir
 
Web components
Web components
Mohd Saeed
 
Webcomponents TLV October 2014
Webcomponents TLV October 2014
Dmitry Bakaleinik
 
Dom date and objects and event handling
Dom date and objects and event handling
smitha273566
 
Web components - The Future is Here
Web components - The Future is Here
Gil Fink
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
karthiksmart21
 
Internet protocol second unit IIPPT.pptx
Internet protocol second unit IIPPT.pptx
ssuser92282c
 
Angular View Encapsulation
Angular View Encapsulation
Jennifer Estrada
 
Web components - An Introduction
Web components - An Introduction
cherukumilli2
 
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
ENIB 2015 2016 - CAI Web S02E01- Côté Navigateur 3/3 - Web Components avec Po...
Horacio Gonzalez
 
Stencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has Arrived
Gil Fink
 
Web technologies-course 09.pptx
Web technologies-course 09.pptx
Stefan Oprea
 
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Iasi code camp 12 october 2013 shadow dom - mihai bîrsan
Codecamp Romania
 
Ad

More from Gil Fink (20)

Becoming a Tech Speaker
Becoming a Tech Speaker
Gil Fink
 
Web animation on steroids web animation api
Web animation on steroids web animation api
Gil Fink
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Being a tech speaker
Being a tech speaker
Gil Fink
 
Working with Data in Service Workers
Working with Data in Service Workers
Gil Fink
 
Demystifying Angular Animations
Demystifying Angular Animations
Gil Fink
 
Redux data flow with angular
Redux data flow with angular
Gil Fink
 
Redux data flow with angular
Redux data flow with angular
Gil Fink
 
Who's afraid of front end databases?
Who's afraid of front end databases?
Gil Fink
 
One language to rule them all type script
One language to rule them all type script
Gil Fink
 
End to-end apps with type script
End to-end apps with type script
Gil Fink
 
Redux data flow with angular 2
Redux data flow with angular 2
Gil Fink
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
Gil Fink
 
Who's afraid of front end databases
Who's afraid of front end databases
Gil Fink
 
Biological modeling, powered by angular js
Biological modeling, powered by angular js
Gil Fink
 
Whos afraid of front end databases?
Whos afraid of front end databases?
Gil Fink
 
One language to rule them all type script
One language to rule them all type script
Gil Fink
 
Getting Started with TypeScript
Getting Started with TypeScript
Gil Fink
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
Gil Fink
 
Becoming a Tech Speaker
Becoming a Tech Speaker
Gil Fink
 
Web animation on steroids web animation api
Web animation on steroids web animation api
Gil Fink
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
Being a tech speaker
Being a tech speaker
Gil Fink
 
Working with Data in Service Workers
Working with Data in Service Workers
Gil Fink
 
Demystifying Angular Animations
Demystifying Angular Animations
Gil Fink
 
Redux data flow with angular
Redux data flow with angular
Gil Fink
 
Redux data flow with angular
Redux data flow with angular
Gil Fink
 
Who's afraid of front end databases?
Who's afraid of front end databases?
Gil Fink
 
One language to rule them all type script
One language to rule them all type script
Gil Fink
 
End to-end apps with type script
End to-end apps with type script
Gil Fink
 
Redux data flow with angular 2
Redux data flow with angular 2
Gil Fink
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
Gil Fink
 
Who's afraid of front end databases
Who's afraid of front end databases
Gil Fink
 
Biological modeling, powered by angular js
Biological modeling, powered by angular js
Gil Fink
 
Whos afraid of front end databases?
Whos afraid of front end databases?
Gil Fink
 
One language to rule them all type script
One language to rule them all type script
Gil Fink
 
Getting Started with TypeScript
Getting Started with TypeScript
Gil Fink
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
Gil Fink
 
Ad

Recently uploaded (20)

Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 

Build Reusable Web Components using HTML5 Web cComponents

  • 1. Build Reusable Web Components Using HTML5 Web Components Gil Fink CEO and Senior Consultant, sparXys
  • 3. Build Reusable Web Components Using HTML5 Web Components Gil Fink CEO and Senior Consultant, sparXys
  • 4. About Me • sparXys CEO and Senior consultant • ASP.NET/IIS Microsoft MVP • Co-author of Pro Single Page Application Development (Apress) • Co-author of 4 Microsoft Official Courses (MOCs) • Founder of Front-End.IL Meetup and co-organizer of GDG Rashlatz Meetup
  • 5. Agenda • The problems we face • Web Components APIs o Templates o Imports o Shadow DOM o Custom Elements
  • 7. The Pyramid of Doom is Back
  • 8. 2. Poor Separation of Concerns You want HTML, CSS and JavaScript to work together You end up with a mess The wiring gets in your way!
  • 9. 3. No Native Templates • Store HTML in hidden DOM element and show it • Use script tag as a template holder: <script id=”myTemplate” type=”text/template”> <div> … </div> </script>
  • 10. 4. No Bundling • You want to bundle a complex component The component includes HTML, CSS and JavaScript how would you do that? o Use a server side wrapping mechanism?
  • 11. Web Components to the Rescue • A set of standards designed to componentize the web • Some general goals: Code Reuse Encapsulation Separation of Concerns Composition Theming Expressive Semantic
  • 12. The Web Components Standards •Reusable DOM fragmentsTemplates •Load HTML declarativelyImports •DOM encapsulationShadow DOM •Create your own elements Custom Elements
  • 14. Templates • A new HTML element – template • Can be used to instantiate document fragments • Can wrap HTML, style tags and script tags • To use the template you need some JavaScript magic <template id=”myTemplate”> <div> … </div> </template>
  • 15. Cloning a Template • Select the template and extract its content o Using its content property • Use the importNode function to get the cloned content • Only when the clone is appended to the DOM o The style and JavaScript are executed o Resources like images are retrieved from the server var template = document.querySelector(‘#myTemplate’); var clone = document.importNode(template.content, true);
  • 17. Imports • Load additional HTML documents o Without Ajax • A new type of link tag • Use the rel attribute with the import type: <link rel=”import” href=”myImport.html”>
  • 18. Imports and Bundling • Enable to bundle a full component into a HTML file o The HTML can include scripts and CSS styles • The whole bundle can be retrieved in a single call
  • 19. Imports and The DOM • Importing a document doesn’t include it into the DOM o It will parse it in memory and load all the additional resources • Use the import property of the link tag: var content = document.querySelector(‘link[rel=”import”]’).import;
  • 21. Shadow DOM • Encapsulate DOM parts o The browser will know how to present those parts o The browser won’t show the encapsulated parts in the source code • Creates a boundary between the component and its user
  • 22. Demo Enabling Chrome to Show Shadow DOM Elements
  • 23. Shadow DOM – Cont. • Use the createShadowRoot function to wrap an element as a shadow DOM: var host = document.querySelector(‘#shadowDOMHost’); var root = host.createShadowRoot(); root.innerHTML = ‘<div>Lurking in the shadows</div>’;
  • 25. Custom Elements • Enable to extend or to create custom HTML elements o The new element must inherit from HTMLElement • Create a custom element using the registerElement function: • Extend an existing element: var myElement = document.registerElement(‘my-element’); var myInput = document.registerElement(‘my-input’, { prototype: Object.create(HTMLInputElement.prototype), extends: ‘input’ });
  • 26. Custom Elements – Cont. • Use the element in your DOM: or use the createElement function • Custom elements have life cycle events. For example: o createdCallback o attributeChangedCallback <my-input></my-input>
  • 28. The Current State of Web Components • Still W3C Working Drafts • Browser support: http://caniuse.com/#search=web%20components • Main Polyfills: Polymer X-Tag
  • 30. Summary • Web Components are emerging standards that enables: • Encapsulation • Separation of Concerns • Element portability • And more • They are still in working drafts • Taking the web one step forward!
  • 31. Resources • Download the slide deck: • http://webcomponents.org/ • My Blog – http://www.gilfink.net • Follow me on Twitter – @gilfink