SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
Introduction to Web Components
HTML Templates, Custom Elements & Shadow DOM
Fu Cheng
@alexcheng1982
2
Agenda
Software Components
HTML Templates
Custom Elements
Shadow DOM
HTML Imports
Polymer Project
3
Web Components is
Component Model of the Web
4
Components
http://www.flickr.com/photos/bdesham/2432400623
5
Software Component
An individual software component is a software package,
a web service, a web resource, or a module that
encapsulates a set of related functions (or data).
Components are modular and cohesive.
Components communicate with each other via interfaces.
A component model is a definition of standards for
component implementation, documentation and
deployment.
– EJB, COM, COBRA
http://en.wikipedia.org/wiki/Software_component
6
Software Component is for
 Reuse
 Sharing
http://www.flickr.com/photos/wheatfields/118700012/
Don't Reinvent the Wheels
7
Components for the Web
We now have different components for the web
– Dojo widgets
– jQuery plugins
– YUI widget
– Sencha components
Different frameworks have their own solutions
No common standards
8
What's the Problem?
Bad encapsulation
http://www.flickr.com/photos/27898848@N06/3174611184
9
In the Web We Have
HTML CSS JavaScript
Structure
Content
Presentation Behaviour
10
After You Add a Component
You may introduce
– Additional DOM nodes
• Statically inlined
• Dynamically created using JavaScript
– CSS rules
– JavaScript objects
These may conflict with your own application
– Duplicated DOM element IDs
– Component's styles mingles with existing styles
– Conflicted global JavaScript object names
11
Add a Twitter Badge to Your Page (1)
Go to https://twitter.com/about/resources/buttons
Select type
You get code like this
12
Add a Twitter Badge to Your Page (2)
You get DOM like this
iframe is the primary tool we have for encapsulation
13
Components Contribute to Current Page
HTML CSS JS
HTML CSS JS HTML CSS JS HTML CSS JS
Widget 1 Widget 2 Widget 3
14
Web Components
Component model standard for the Web
W3C working draft
(http://www.w3.org/standards/techs/components)
A very new standard
15
Web Components
Web Components standards consist of
– HTML Templates (http://www.w3.org/TR/html-templates/)
– Decorators
– Custom Elements (http://www.w3.org/TR/custom-elements/)
– Shadow DOM (http://www.w3.org/TR/shadow-dom/)
– HTML Imports (http://www.w3.org/TR/html-imports/)
16
HTML Templates
Templates = basic structure + place holders
Dynamic web applications use templates to generate
HTML fragments
Current template techniques
– Inline invisible markup
• Set CSS style 'display:none'
– Embed HTML markup in string
• In JavaScript files
17
HTML Templates
Client-side templates are widely used today
– Server-side exposes REST API over JSON
– Combine data with template strings
– Use innerHTML to render
Problems
– Invisible markup
• Resources are still loaded
– Template strings
• XSS attacks
• Not intuitive
18
HTML Templates
<template> element
Declarative way to create HTML fragments
Use content attribute to access the fragment
Content of templates are not part of the document and are
inactive
– Not display
– Cannot query
– Resources not load
Move or clone template to current DOM tree to render
19
Custom Elements
Create your own HTML elements
Use <element> to create
– <element name="x-my-element"><p>My Element</p></element>
Extend existing element
– <element name="x-my-button" extends="button"></element>
How to use in the page
– New element
• <x-my-element></x-my-element>
– Extended element
• <button is="x-my-button>Push</button>
20
Custom Elements
Support lifecyle callbacks
– ready
– inserted
– removed
Use HTML Imports to import custom elements
– <link rel="import" href="x-nice-image.html">
21
Shadow DOM
Shadow DOM is the key of encapsulation within the DOM
tree
 Establish functional boundaries in a document tree
22
Typical Web Application
23
Shadow Tree
Shadow DOM allows elements in the document tree to
host other DOM trees
A set of rules establish encapsulation boundaries
between document tree and shadow trees
24
Document Tree & Shadow Tree
Shadow root is the root of shadow tree
25
Shadow Tree Rendered
When rendered, the shadow tree takes place of the shadow host's content
26
Insertion Point
Insertion point is where shadow host's children are transposed when rendering
27
Insertion Point Rendered
When rendered, shadow host's children are distributed to different
insertion points
28
Encapsulation
All nodes in the shadow tree are NOT accessible in
shadow host's document
Node ids are NOT addressable in shadow host's
document
Style sheets are NOT accessible using shadow host's
document's CSSOM
29
Encapsulation
Styles
– CSS rules declared in an enclosing tree not apply in a shadow tree,
except when the apply-author-styles flag is set for this tree
– CSS selectors cannot cross the shadow boundary
– Use reset-style-inheritance flag to set inheritable CSS properties
to the initial value
Use @host to add styles to shadow host
30
Encapsulation
Events dispatched in the shadow tree may
– Cross the shadow boundary or
– Be terminated at the shadow boundary
Events crossing the shadow boundary are retargetted
– Event's information is adjusted to hide shadow tree
31
Simple Sample
32
Simple Sample
 Use JavaScript to create a shadow
host
 Use <content> to specify insertion
point
 Shadow host's chidren and shadow
tree are composited together when
rendered
 CSS selectors only match wholly
inside or outside of the shadow tree
Style 'color: #f00' not applied to shadow tree
33
HTML Imports
HTML Imports are a way to include and reuse HTML
documents in other HTML documents.
<link rel="import" href="x-person-badge.html">
34
Polymer Project
Polymer is a new type of library for the web, built on top of
Web Components, and designed to leverage the evolving
web platform on modern browsers.
– Created by Google
– http://www.polymer-project.org/
– Announced in Google I/O 2013
Embrace latest standards
Provide polyfills for old browsers
Going to replace Web UI in Dart
35
Polymer Architecture
36
Examples
Web Components examples in my GitHub repository:
https://github.com/alexcheng1982/learn-web-components

Mais conteúdo relacionado

Mais procurados

Introduction to VueJS & Vuex
Introduction to VueJS & VuexIntroduction to VueJS & Vuex
Introduction to VueJS & VuexBernd Alter
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript TutorialDHTMLExtreme
 
Introduction to JavaScript (1).ppt
Introduction to JavaScript (1).pptIntroduction to JavaScript (1).ppt
Introduction to JavaScript (1).pptMuhammadRehan856177
 
Asp.net html server control
Asp.net html  server controlAsp.net html  server control
Asp.net html server controlSireesh K
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTMLAjay Khatri
 
UDA-Guia desarrollo web services
UDA-Guia desarrollo web servicesUDA-Guia desarrollo web services
UDA-Guia desarrollo web servicesAnder Martinez
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS PresentationShawn Calvert
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsBeth Soderberg
 
Graphics & Animation with HTML5
Graphics & Animation with HTML5Graphics & Animation with HTML5
Graphics & Animation with HTML5Knoldus Inc.
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptAndres Baravalle
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginnersSingsys Pte Ltd
 

Mais procurados (20)

Java script
Java scriptJava script
Java script
 
Jquery
JqueryJquery
Jquery
 
Introduction to VueJS & Vuex
Introduction to VueJS & VuexIntroduction to VueJS & Vuex
Introduction to VueJS & Vuex
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
Introduction to JavaScript (1).ppt
Introduction to JavaScript (1).pptIntroduction to JavaScript (1).ppt
Introduction to JavaScript (1).ppt
 
Asp.net html server control
Asp.net html  server controlAsp.net html  server control
Asp.net html server control
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
 
Css Ppt
Css PptCss Ppt
Css Ppt
 
UDA-Guia desarrollo web services
UDA-Guia desarrollo web servicesUDA-Guia desarrollo web services
UDA-Guia desarrollo web services
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation Basics
 
Graphics & Animation with HTML5
Graphics & Animation with HTML5Graphics & Animation with HTML5
Graphics & Animation with HTML5
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Css position
Css positionCss position
Css position
 
Html5 tutorial for beginners
Html5 tutorial for beginnersHtml5 tutorial for beginners
Html5 tutorial for beginners
 
Web Components
Web ComponentsWeb Components
Web Components
 

Semelhante a Introduction to Web Components

Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View EncapsulationJennifer Estrada
 
Lightning web components - Introduction, component Lifecycle, Events, decorat...
Lightning web components - Introduction, component Lifecycle, Events, decorat...Lightning web components - Introduction, component Lifecycle, Events, decorat...
Lightning web components - Introduction, component Lifecycle, Events, decorat...Nidhi Sharma
 
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 & PolymerErik Isaksen
 
Build Reusable Web Components using HTML5 Web cComponents
Build Reusable Web Components using HTML5 Web cComponentsBuild Reusable Web Components using HTML5 Web cComponents
Build Reusable Web Components using HTML5 Web cComponentsGil Fink
 
Web components
Web componentsWeb components
Web componentsMohd Saeed
 
Web Components - The Future is Here
Web Components - The Future is HereWeb Components - The Future is Here
Web Components - The Future is HereGil Fink
 
Web component driven development
Web component driven developmentWeb component driven development
Web component driven developmentGil Fink
 
Developing Branding Solutions for 2013
Developing Branding Solutions for 2013Developing Branding Solutions for 2013
Developing Branding Solutions for 2013Thomas Daly
 
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components Ben Edwards
 
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 ArrivedGil Fink
 
Polymer-Powered Design Systems - DevFest Florida
Polymer-Powered Design Systems - DevFest FloridaPolymer-Powered Design Systems - DevFest Florida
Polymer-Powered Design Systems - DevFest FloridaJohn Riviello
 
Polymer and web component
Polymer and web componentPolymer and web component
Polymer and web componentImam Raza
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsGabriel Walt
 
Web Components: back to the future
Web Components: back to the futureWeb Components: back to the future
Web Components: back to the futureDA-14
 
Tech talk polymer
Tech talk polymerTech talk polymer
Tech talk polymerYanuar W
 

Semelhante a Introduction to Web Components (20)

Angular View Encapsulation
Angular View EncapsulationAngular View Encapsulation
Angular View Encapsulation
 
Angular 2 Component styles
Angular 2 Component stylesAngular 2 Component styles
Angular 2 Component styles
 
Lightning web components - Introduction, component Lifecycle, Events, decorat...
Lightning web components - Introduction, component Lifecycle, Events, decorat...Lightning web components - Introduction, component Lifecycle, Events, decorat...
Lightning web components - Introduction, component Lifecycle, Events, decorat...
 
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
 
Build Reusable Web Components using HTML5 Web cComponents
Build Reusable Web Components using HTML5 Web cComponentsBuild Reusable Web Components using HTML5 Web cComponents
Build Reusable Web Components using HTML5 Web cComponents
 
Web components
Web componentsWeb components
Web components
 
toolkit
toolkittoolkit
toolkit
 
Web Components - The Future is Here
Web Components - The Future is HereWeb Components - The Future is Here
Web Components - The Future is Here
 
Web component driven development
Web component driven developmentWeb component driven development
Web component driven development
 
Developing Branding Solutions for 2013
Developing Branding Solutions for 2013Developing Branding Solutions for 2013
Developing Branding Solutions for 2013
 
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components
Salesforce Auckland Developer Meetup - May 2018 - Lightning Web Components
 
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
 
Manasa
ManasaManasa
Manasa
 
Unit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptxUnit 2 - Data Binding.pptx
Unit 2 - Data Binding.pptx
 
Polymer-Powered Design Systems - DevFest Florida
Polymer-Powered Design Systems - DevFest FloridaPolymer-Powered Design Systems - DevFest Florida
Polymer-Powered Design Systems - DevFest Florida
 
Polymer and web component
Polymer and web componentPolymer and web component
Polymer and web component
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core Components
 
Web Components: back to the future
Web Components: back to the futureWeb Components: back to the future
Web Components: back to the future
 
Web components
Web componentsWeb components
Web components
 
Tech talk polymer
Tech talk polymerTech talk polymer
Tech talk polymer
 

Mais de Fu Cheng

The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of JavaFu Cheng
 
Java SE 7 New Features and Enhancements
Java SE 7 New Features and EnhancementsJava SE 7 New Features and Enhancements
Java SE 7 New Features and EnhancementsFu Cheng
 
Advanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoAdvanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoFu Cheng
 
Java类加载器
Java类加载器Java类加载器
Java类加载器Fu Cheng
 
Ajax应用开发最佳实践
Ajax应用开发最佳实践Ajax应用开发最佳实践
Ajax应用开发最佳实践Fu Cheng
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScriptFu Cheng
 

Mais de Fu Cheng (6)

The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
 
Java SE 7 New Features and Enhancements
Java SE 7 New Features and EnhancementsJava SE 7 New Features and Enhancements
Java SE 7 New Features and Enhancements
 
Advanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojoAdvanced guide to develop ajax applications using dojo
Advanced guide to develop ajax applications using dojo
 
Java类加载器
Java类加载器Java类加载器
Java类加载器
 
Ajax应用开发最佳实践
Ajax应用开发最佳实践Ajax应用开发最佳实践
Ajax应用开发最佳实践
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 

Último

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 WorkerThousandEyes
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Último (20)

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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

Introduction to Web Components

  • 1. Introduction to Web Components HTML Templates, Custom Elements & Shadow DOM Fu Cheng @alexcheng1982
  • 2. 2 Agenda Software Components HTML Templates Custom Elements Shadow DOM HTML Imports Polymer Project
  • 3. 3 Web Components is Component Model of the Web
  • 5. 5 Software Component An individual software component is a software package, a web service, a web resource, or a module that encapsulates a set of related functions (or data). Components are modular and cohesive. Components communicate with each other via interfaces. A component model is a definition of standards for component implementation, documentation and deployment. – EJB, COM, COBRA http://en.wikipedia.org/wiki/Software_component
  • 6. 6 Software Component is for  Reuse  Sharing http://www.flickr.com/photos/wheatfields/118700012/ Don't Reinvent the Wheels
  • 7. 7 Components for the Web We now have different components for the web – Dojo widgets – jQuery plugins – YUI widget – Sencha components Different frameworks have their own solutions No common standards
  • 8. 8 What's the Problem? Bad encapsulation http://www.flickr.com/photos/27898848@N06/3174611184
  • 9. 9 In the Web We Have HTML CSS JavaScript Structure Content Presentation Behaviour
  • 10. 10 After You Add a Component You may introduce – Additional DOM nodes • Statically inlined • Dynamically created using JavaScript – CSS rules – JavaScript objects These may conflict with your own application – Duplicated DOM element IDs – Component's styles mingles with existing styles – Conflicted global JavaScript object names
  • 11. 11 Add a Twitter Badge to Your Page (1) Go to https://twitter.com/about/resources/buttons Select type You get code like this
  • 12. 12 Add a Twitter Badge to Your Page (2) You get DOM like this iframe is the primary tool we have for encapsulation
  • 13. 13 Components Contribute to Current Page HTML CSS JS HTML CSS JS HTML CSS JS HTML CSS JS Widget 1 Widget 2 Widget 3
  • 14. 14 Web Components Component model standard for the Web W3C working draft (http://www.w3.org/standards/techs/components) A very new standard
  • 15. 15 Web Components Web Components standards consist of – HTML Templates (http://www.w3.org/TR/html-templates/) – Decorators – Custom Elements (http://www.w3.org/TR/custom-elements/) – Shadow DOM (http://www.w3.org/TR/shadow-dom/) – HTML Imports (http://www.w3.org/TR/html-imports/)
  • 16. 16 HTML Templates Templates = basic structure + place holders Dynamic web applications use templates to generate HTML fragments Current template techniques – Inline invisible markup • Set CSS style 'display:none' – Embed HTML markup in string • In JavaScript files
  • 17. 17 HTML Templates Client-side templates are widely used today – Server-side exposes REST API over JSON – Combine data with template strings – Use innerHTML to render Problems – Invisible markup • Resources are still loaded – Template strings • XSS attacks • Not intuitive
  • 18. 18 HTML Templates <template> element Declarative way to create HTML fragments Use content attribute to access the fragment Content of templates are not part of the document and are inactive – Not display – Cannot query – Resources not load Move or clone template to current DOM tree to render
  • 19. 19 Custom Elements Create your own HTML elements Use <element> to create – <element name="x-my-element"><p>My Element</p></element> Extend existing element – <element name="x-my-button" extends="button"></element> How to use in the page – New element • <x-my-element></x-my-element> – Extended element • <button is="x-my-button>Push</button>
  • 20. 20 Custom Elements Support lifecyle callbacks – ready – inserted – removed Use HTML Imports to import custom elements – <link rel="import" href="x-nice-image.html">
  • 21. 21 Shadow DOM Shadow DOM is the key of encapsulation within the DOM tree  Establish functional boundaries in a document tree
  • 23. 23 Shadow Tree Shadow DOM allows elements in the document tree to host other DOM trees A set of rules establish encapsulation boundaries between document tree and shadow trees
  • 24. 24 Document Tree & Shadow Tree Shadow root is the root of shadow tree
  • 25. 25 Shadow Tree Rendered When rendered, the shadow tree takes place of the shadow host's content
  • 26. 26 Insertion Point Insertion point is where shadow host's children are transposed when rendering
  • 27. 27 Insertion Point Rendered When rendered, shadow host's children are distributed to different insertion points
  • 28. 28 Encapsulation All nodes in the shadow tree are NOT accessible in shadow host's document Node ids are NOT addressable in shadow host's document Style sheets are NOT accessible using shadow host's document's CSSOM
  • 29. 29 Encapsulation Styles – CSS rules declared in an enclosing tree not apply in a shadow tree, except when the apply-author-styles flag is set for this tree – CSS selectors cannot cross the shadow boundary – Use reset-style-inheritance flag to set inheritable CSS properties to the initial value Use @host to add styles to shadow host
  • 30. 30 Encapsulation Events dispatched in the shadow tree may – Cross the shadow boundary or – Be terminated at the shadow boundary Events crossing the shadow boundary are retargetted – Event's information is adjusted to hide shadow tree
  • 32. 32 Simple Sample  Use JavaScript to create a shadow host  Use <content> to specify insertion point  Shadow host's chidren and shadow tree are composited together when rendered  CSS selectors only match wholly inside or outside of the shadow tree Style 'color: #f00' not applied to shadow tree
  • 33. 33 HTML Imports HTML Imports are a way to include and reuse HTML documents in other HTML documents. <link rel="import" href="x-person-badge.html">
  • 34. 34 Polymer Project Polymer is a new type of library for the web, built on top of Web Components, and designed to leverage the evolving web platform on modern browsers. – Created by Google – http://www.polymer-project.org/ – Announced in Google I/O 2013 Embrace latest standards Provide polyfills for old browsers Going to replace Web UI in Dart
  • 36. 36 Examples Web Components examples in my GitHub repository: https://github.com/alexcheng1982/learn-web-components