SlideShare a Scribd company logo
1 of 46
Download to read offline
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Web Components - The Future is
Here
Gil Fink
CEO and Senior Consultant, sparXys
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
The Pyramid of Doom
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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)
Agenda
• The problems we face
• Web Components APIs
o Templates
o Imports
o Shadow DOM
o Custom Elements
• Libraries to the rescue
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
1. Undescriptive Markup
Markup Example
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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!
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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?
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
The Web Components
Standards
•Reusable DOM fragmentsTemplates
•Load HTML declarativelyImports
•DOM encapsulationShadow DOM
•Create your own elements
Custom
Elements
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Setting The Environment
• Browsers have only partial support for Web
Components
o So we use the webcomponents.js Polyfill for Web Components
• Download:
o https://github.com/webcomponents/webcomponentsjs/
o Or install using your favorite package manager (Bower, Nuget)
• Make sure the Polyfill script runs first
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Demo
Setting the environment
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Let’s Drill Down
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Templates
• A new HTML element – template
• Can be used to instantiate document fragments
• Can wrap HTML, style tags and script tags
• No data binding support 
• To use the template you need some JavaScript
magic
<template id=”myTemplate”>
<div>
…
</div>
</template>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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);
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Templates
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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”>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Imports and Bundling
• Enable to bundle a full component into one HTML
file
o The HTML can include scripts and CSS styles
• The whole bundle can be retrieved in a single call
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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;
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Imports
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Import Additional Notes
• Scripts running inside the import can reference the
containing document by calling
document.currentScript.ownerDocument
• CORS constraints apply to documents imported
from other domains
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
The Problems Shadow
DOM Tries to Solve
• Encapsulation of components/widgets
• Style leakage
o Leaks from one component to another
o Leaks from imported 3th party library/framework
• Global DOM
o id or class attributes all over the place
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Shadow DOM in The
Browser
<video src="media/myVideo.mp4" controls></video>
<input type="date">
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Show Shadow DOM
Elements in Chrome
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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>’;
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Styling Shadow DOM
• :host and :host() pseudo-class
• ::content pseudo-element
<div name="myElement">
<style>
:host {
border: 1px solid lightgray;
}
</style>
<template>...</template>
</div>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Shadow DOM
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
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’
});
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Custom Elements –
Naming
• You can create named elements (almost) any way
you want:
o Same naming rules as other HTML tags
o There must be a dash (“-”) in the name
• To future-proof the name against the HTML standard
• To avoid naming collisions
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Custom Elements – Usage
• Use the element in your DOM:
or use the createElement function:
<my-input></my-input>
var elm = document.createElement(‘my-input’);
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Custom Element
Callbacks
• Custom elements have life cycle events:
• createdCallback
o Called when an instance is created
• attachedCallback
o Called when an instance is added to DOM subtree
• detachedCallback
o Called when an instance is removed from a DOM subtree
• attributeChangedCallback
o Called after an attribute value changes
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Custom Elements
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
The Current State of Web
Components
• Still W3C Working Drafts
• Browser support:
http://caniuse.com/#search=web%20components
• Main libraries:
Polymer X-Tag
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Polymer
• Components library that is written and maintained
by Google
• Includes
o Ability to build your own custom components
o Ready to use web components
• Polymer leverages the web components standard
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Polymer Catalog
• Full catalog that includes various ready to use
components
• You can look at the component categories here:
https://elements.polymer-project.org/
• Support to Google material design and Google API
components included
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Creating Custom
Elements in Polymer
<dom-module id="contact-card">
<link rel="import" type="css" href="contact-card.css">
<template>
<content></content>
<iron-icon icon="star" hidden$="{{!starred}}"></iron-icon>
</template>
<script>
Polymer({
is: 'contact-card',
properties: {
starred: Boolean
}
});
</script>
</dom-module>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Using The Custom
Elements in Polymer
<contact-card starred>
<img src="profile.jpg" alt="Gil's photo">
<span>Gil Fink</span>
</contact-card>
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Polymer
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
X-Tag
• Components library that is supported by Microsoft
• Includes only the ability to write your own custom
components
o Ready to use web components can be found in the Brick library
• X-Tag leverages the web components standard
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Creating Web
Components in X-Tag
xtag.register('x-myelement', {
lifecycle: {
created: function(){
// do something when the element is created
}
},
events: {
focus: function(){
// do something when the element in focus
}
},
methods: {
someMethod: function(){
// implementation
} }
});
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
X-Tag
Demo
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Questions?
Summary
• Web Components are emerging standards that
enables:
• Encapsulation
• Separation of Concerns
• Element portability
• And more
• They are still in development
• Taking the web one step forward!
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Resources
• Download the slide deck:
http://bit.ly/1OCOnbL
• http://webcomponents.org/
https://www.polymer-project.org/1.0/
http://www.x-tags.org/
• My Blog – http://www.gilfink.net
• Follow me on Twitter – @gilfink
Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
Thank You!

More Related Content

What's hot

Google Developer Group(GDG) DevFest Event 2012 Android talk
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 talk
Imam Raza
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPC
Mayflower GmbH
 
Monster JavaScript Course - 50+ projects and applications
Monster JavaScript Course - 50+ projects and applicationsMonster JavaScript Course - 50+ projects and applications
Monster JavaScript Course - 50+ projects and applications
Laurence Svekis ✔
 

What's hot (20)

Google Developer Group(GDG) DevFest Event 2012 Android talk
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 talk
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSS
 
Top java script frameworks ppt
Top java script frameworks pptTop java script frameworks ppt
Top java script frameworks ppt
 
The Future of the Web: HTML5
The Future of the Web: HTML5The Future of the Web: HTML5
The Future of the Web: HTML5
 
The Truth About Your Web App's Performance
The Truth About Your Web App's PerformanceThe Truth About Your Web App's Performance
The Truth About Your Web App's Performance
 
Razor into the Razor'verse
Razor into the Razor'verseRazor into the Razor'verse
Razor into the Razor'verse
 
Polymer / WebComponents
Polymer / WebComponentsPolymer / WebComponents
Polymer / WebComponents
 
Polymer - Welcome to the Future @ PyGrunn 08/07/2014
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
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPC
 
Polymer presentation in Google HQ
Polymer presentation in Google HQPolymer presentation in Google HQ
Polymer presentation in Google HQ
 
HTML5 & Friends
HTML5 & FriendsHTML5 & Friends
HTML5 & Friends
 
Introduction to HTML5
Introduction to HTML5Introduction to HTML5
Introduction to HTML5
 
Web components - The Future is Here
Web components - The Future is HereWeb components - The Future is Here
Web components - The Future is Here
 
JavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive CodeJavaScript DOM - Dynamic interactive Code
JavaScript DOM - Dynamic interactive Code
 
HTML5 - Introduction
HTML5 - IntroductionHTML5 - Introduction
HTML5 - Introduction
 
Monster JavaScript Course - 50+ projects and applications
Monster JavaScript Course - 50+ projects and applicationsMonster JavaScript Course - 50+ projects and applications
Monster JavaScript Course - 50+ projects and applications
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
Rails + Webpack
Rails + WebpackRails + Webpack
Rails + Webpack
 
How to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScriptHow to Build ToDo App with Vue 3 + TypeScript
How to Build ToDo App with Vue 3 + TypeScript
 
JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1JavaScript Missing Manual, Ch. 1
JavaScript Missing Manual, Ch. 1
 

Viewers also liked

Viewers also liked (9)

Levent-Gurses' Introduction to Web Components & Polymer
Levent-Gurses' Introduction to Web Components & PolymerLevent-Gurses' Introduction to Web Components & Polymer
Levent-Gurses' Introduction to Web Components & Polymer
 
Microservices y la era Post Industrial de la Web
Microservices y la era Post Industrial de la WebMicroservices y la era Post Industrial de la Web
Microservices y la era Post Industrial de la Web
 
High Performance Web Components
High Performance Web ComponentsHigh Performance Web Components
High Performance Web Components
 
O'Reilly Fluent, Web Components Enterprise
O'Reilly Fluent, Web Components EnterpriseO'Reilly Fluent, Web Components Enterprise
O'Reilly Fluent, Web Components Enterprise
 
HTML5 Web Components
HTML5 Web ComponentsHTML5 Web Components
HTML5 Web Components
 
Polymer and web component
Polymer and web componentPolymer and web component
Polymer and web component
 
Microservices + Oracle: A Bright Future
Microservices + Oracle: A Bright FutureMicroservices + Oracle: A Bright Future
Microservices + Oracle: A Bright Future
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
 
Dockercon State of the Art in Microservices
Dockercon State of the Art in MicroservicesDockercon State of the Art in Microservices
Dockercon State of the Art in Microservices
 

Similar to Web components the future is here

Freelancer Weapons of mass productivity
Freelancer Weapons of mass productivityFreelancer Weapons of mass productivity
Freelancer Weapons of mass productivity
Gregg Coppen
 

Similar to Web components the future is here (20)

Web component driven development
Web component driven developmentWeb component driven development
Web component driven development
 
The Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has ArrivedThe Time for Vanilla Web Components has Arrived
The Time for Vanilla Web Components has Arrived
 
Front End Development for Back End Developers - Devoxx UK 2017
 Front End Development for Back End Developers - Devoxx UK 2017 Front End Development for Back End Developers - Devoxx UK 2017
Front End Development for Back End Developers - Devoxx UK 2017
 
Web Components - The Future is Here
Web Components - The Future is HereWeb Components - The Future is Here
Web Components - The Future is Here
 
Working with Shortcodes in WordPress
Working with Shortcodes in WordPressWorking with Shortcodes in WordPress
Working with Shortcodes in WordPress
 
Freelancer Weapons of mass productivity
Freelancer Weapons of mass productivityFreelancer Weapons of mass productivity
Freelancer Weapons of mass productivity
 
Wordpress beyond blogging
Wordpress beyond bloggingWordpress beyond blogging
Wordpress beyond blogging
 
X tag with web components - joe ssekono
X tag with web components - joe ssekonoX tag with web components - joe ssekono
X tag with web components - joe ssekono
 
Theming websites effortlessly with Deliverance (CMSExpo 2010)
Theming websites effortlessly with Deliverance (CMSExpo 2010)Theming websites effortlessly with Deliverance (CMSExpo 2010)
Theming websites effortlessly with Deliverance (CMSExpo 2010)
 
Rapid WordPress theme development
Rapid WordPress theme developmentRapid WordPress theme development
Rapid WordPress theme development
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
Designing True Cross-Platform Apps
Designing True Cross-Platform AppsDesigning True Cross-Platform Apps
Designing True Cross-Platform Apps
 
Twig in the Wild
Twig in the WildTwig in the Wild
Twig in the Wild
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
HTML5, just another presentation :)
HTML5, just another presentation :)HTML5, just another presentation :)
HTML5, just another presentation :)
 
Azure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusabilityAzure Resource Manager templates: Improve deployment time and reusability
Azure Resource Manager templates: Improve deployment time and reusability
 
Dojo javascript toolkit
Dojo javascript toolkit Dojo javascript toolkit
Dojo javascript toolkit
 
Fundamentals of HTML5
Fundamentals of HTML5Fundamentals of HTML5
Fundamentals of HTML5
 
Training presentation.pptx
Training presentation.pptxTraining presentation.pptx
Training presentation.pptx
 
How to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy stepsHow to create a local Android open source project mirror in 6 easy steps
How to create a local Android open source project mirror in 6 easy steps
 

More from Gil Fink

More from Gil Fink (20)

Becoming a Tech Speaker
Becoming a Tech SpeakerBecoming a Tech Speaker
Becoming a Tech Speaker
 
Web animation on steroids web animation api
Web animation on steroids web animation api Web animation on steroids web animation api
Web animation on steroids web animation api
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
Stencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has ArrivedStencil: The Time for Vanilla Web Components has Arrived
Stencil: The Time for Vanilla Web Components has Arrived
 
Being a tech speaker
Being a tech speakerBeing a tech speaker
Being a tech speaker
 
Working with Data in Service Workers
Working with Data in Service WorkersWorking with Data in Service Workers
Working with Data in Service Workers
 
Demystifying Angular Animations
Demystifying Angular AnimationsDemystifying Angular Animations
Demystifying Angular Animations
 
Redux data flow with angular
Redux data flow with angularRedux data flow with angular
Redux data flow with angular
 
Redux data flow with angular
Redux data flow with angularRedux data flow with angular
Redux data flow with angular
 
Who's afraid of front end databases?
Who's afraid of front end databases?Who's afraid of front end databases?
Who's afraid of front end databases?
 
One language to rule them all type script
One language to rule them all type scriptOne language to rule them all type script
One language to rule them all type script
 
End to-end apps with type script
End to-end apps with type scriptEnd to-end apps with type script
End to-end apps with type script
 
Redux data flow with angular 2
Redux data flow with angular 2Redux data flow with angular 2
Redux data flow with angular 2
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJSBiological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
 
Who's afraid of front end databases
Who's afraid of front end databasesWho's afraid of front end databases
Who's afraid of front end databases
 
Biological modeling, powered by angular js
Biological modeling, powered by angular jsBiological modeling, powered by angular js
Biological modeling, powered by angular js
 
Whos afraid of front end databases?
Whos afraid of front end databases?Whos afraid of front end databases?
Whos afraid of front end databases?
 
One language to rule them all type script
One language to rule them all type scriptOne language to rule them all type script
One language to rule them all type script
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 
Biological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJSBiological Modeling, Powered by AngularJS
Biological Modeling, Powered by AngularJS
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Web components the future is here

  • 1. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Web Components - The Future is Here Gil Fink CEO and Senior Consultant, sparXys
  • 2. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek The Pyramid of Doom
  • 3. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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)
  • 4. Agenda • The problems we face • Web Components APIs o Templates o Imports o Shadow DOM o Custom Elements • Libraries to the rescue Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
  • 5. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 1. Undescriptive Markup Markup Example
  • 6. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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!
  • 7. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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>
  • 8. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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?
  • 9. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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
  • 10. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek The Web Components Standards •Reusable DOM fragmentsTemplates •Load HTML declarativelyImports •DOM encapsulationShadow DOM •Create your own elements Custom Elements
  • 11. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Setting The Environment • Browsers have only partial support for Web Components o So we use the webcomponents.js Polyfill for Web Components • Download: o https://github.com/webcomponents/webcomponentsjs/ o Or install using your favorite package manager (Bower, Nuget) • Make sure the Polyfill script runs first
  • 12. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Demo Setting the environment
  • 13. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Let’s Drill Down
  • 14. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Templates • A new HTML element – template • Can be used to instantiate document fragments • Can wrap HTML, style tags and script tags • No data binding support  • To use the template you need some JavaScript magic <template id=”myTemplate”> <div> … </div> </template>
  • 15. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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);
  • 16. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Templates Demo
  • 17. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Imports and Bundling • Enable to bundle a full component into one HTML file o The HTML can include scripts and CSS styles • The whole bundle can be retrieved in a single call
  • 19. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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;
  • 20. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Imports Demo
  • 21. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Import Additional Notes • Scripts running inside the import can reference the containing document by calling document.currentScript.ownerDocument • CORS constraints apply to documents imported from other domains
  • 22. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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
  • 23. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek The Problems Shadow DOM Tries to Solve • Encapsulation of components/widgets • Style leakage o Leaks from one component to another o Leaks from imported 3th party library/framework • Global DOM o id or class attributes all over the place
  • 24. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Shadow DOM in The Browser <video src="media/myVideo.mp4" controls></video> <input type="date">
  • 25. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Show Shadow DOM Elements in Chrome Demo
  • 26. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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>’;
  • 27. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Styling Shadow DOM • :host and :host() pseudo-class • ::content pseudo-element <div name="myElement"> <style> :host { border: 1px solid lightgray; } </style> <template>...</template> </div>
  • 28. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Shadow DOM Demo
  • 29. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek 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’ });
  • 30. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Custom Elements – Naming • You can create named elements (almost) any way you want: o Same naming rules as other HTML tags o There must be a dash (“-”) in the name • To future-proof the name against the HTML standard • To avoid naming collisions
  • 31. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Custom Elements – Usage • Use the element in your DOM: or use the createElement function: <my-input></my-input> var elm = document.createElement(‘my-input’);
  • 32. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Custom Element Callbacks • Custom elements have life cycle events: • createdCallback o Called when an instance is created • attachedCallback o Called when an instance is added to DOM subtree • detachedCallback o Called when an instance is removed from a DOM subtree • attributeChangedCallback o Called after an attribute value changes
  • 33. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Custom Elements Demo
  • 34. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek The Current State of Web Components • Still W3C Working Drafts • Browser support: http://caniuse.com/#search=web%20components • Main libraries: Polymer X-Tag
  • 35. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Polymer • Components library that is written and maintained by Google • Includes o Ability to build your own custom components o Ready to use web components • Polymer leverages the web components standard
  • 36. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Polymer Catalog • Full catalog that includes various ready to use components • You can look at the component categories here: https://elements.polymer-project.org/ • Support to Google material design and Google API components included
  • 37. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Creating Custom Elements in Polymer <dom-module id="contact-card"> <link rel="import" type="css" href="contact-card.css"> <template> <content></content> <iron-icon icon="star" hidden$="{{!starred}}"></iron-icon> </template> <script> Polymer({ is: 'contact-card', properties: { starred: Boolean } }); </script> </dom-module>
  • 38. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Using The Custom Elements in Polymer <contact-card starred> <img src="profile.jpg" alt="Gil's photo"> <span>Gil Fink</span> </contact-card>
  • 39. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Polymer Demo
  • 40. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek X-Tag • Components library that is supported by Microsoft • Includes only the ability to write your own custom components o Ready to use web components can be found in the Brick library • X-Tag leverages the web components standard
  • 41. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Creating Web Components in X-Tag xtag.register('x-myelement', { lifecycle: { created: function(){ // do something when the element is created } }, events: { focus: function(){ // do something when the element in focus } }, methods: { someMethod: function(){ // implementation } } });
  • 42. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek X-Tag Demo
  • 43. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Questions?
  • 44. Summary • Web Components are emerging standards that enables: • Encapsulation • Separation of Concerns • Element portability • And more • They are still in development • Taking the web one step forward! Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek
  • 45. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Resources • Download the slide deck: http://bit.ly/1OCOnbL • http://webcomponents.org/ https://www.polymer-project.org/1.0/ http://www.x-tags.org/ • My Blog – http://www.gilfink.net • Follow me on Twitter – @gilfink
  • 46. Join the conversation on Twitter: @DevWeek // #DW2016 // #DevWeek Thank You!