SlideShare una empresa de Scribd logo
1 de 36
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
Web Components
Web ComponentsWeb Components
Web Components
 
Static and Dynamic webpage
Static and Dynamic webpageStatic and Dynamic webpage
Static and Dynamic webpage
 
Wordpress ppt
Wordpress pptWordpress ppt
Wordpress ppt
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Website design with Wordpress ppt
Website design with Wordpress pptWebsite design with Wordpress ppt
Website design with Wordpress ppt
 
Responsive web design
Responsive web designResponsive web design
Responsive web design
 
Working of web
Working of webWorking of web
Working of web
 
Basic WordPress for Beginner ppt
Basic WordPress for Beginner pptBasic WordPress for Beginner ppt
Basic WordPress for Beginner ppt
 
Meta tags
Meta tagsMeta tags
Meta tags
 
Understanding the Web Page Layout
Understanding the Web Page LayoutUnderstanding the Web Page Layout
Understanding the Web Page Layout
 
Css3
Css3Css3
Css3
 
Complete Lecture on Css presentation
Complete Lecture on Css presentation Complete Lecture on Css presentation
Complete Lecture on Css presentation
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
CMS ( Content Management System ) Digital Marketing
CMS ( Content Management System ) Digital MarketingCMS ( Content Management System ) Digital Marketing
CMS ( Content Management System ) Digital Marketing
 
website planning and creation for beginners
website planning and creation for beginners website planning and creation for beginners
website planning and creation for beginners
 
HTML CSS Basics
HTML CSS BasicsHTML CSS Basics
HTML CSS Basics
 
Server Controls of ASP.Net
Server Controls of ASP.NetServer Controls of ASP.Net
Server Controls of ASP.Net
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
Webservices
WebservicesWebservices
Webservices
 

Similar 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
 

Similar 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
 

Más 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
 

Más 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

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
[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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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...apidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
[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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

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