web component
DESCRIPTION
Web Component intro at OSDC 2014, #osdctwTRANSCRIPT
![Page 1: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/1.jpg)
Web Componentothree
![Page 2: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/2.jpg)
othree⚫ HappyDesigner, MozTW!
⚫ PhD Candidate @ NTUST, F2E @HTC
https://github.com/othreehttps://blog.othree.net/
![Page 3: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/3.jpg)
othree
![Page 4: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/4.jpg)
Why Web Component
![Page 5: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/5.jpg)
Why
⚫ Rich Internet Applications!
⚫ More and more custom UI!
⚫ Complex → Modularize!
⚫ Reuse
![Page 6: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/6.jpg)
Custom UI
![Page 7: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/7.jpg)
![Page 8: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/8.jpg)
![Page 9: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/9.jpg)
![Page 10: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/10.jpg)
![Page 11: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/11.jpg)
![Page 12: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/12.jpg)
![Page 13: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/13.jpg)
What Developer Want
⚫ Modularize codes, markup and styles!
⚫ Simple and easy to reuse
![Page 14: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/14.jpg)
What Developer Want
⚫ Use <magic-tag> and everything is done
![Page 15: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/15.jpg)
Standards is Not Enough
![Page 16: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/16.jpg)
Standards Changes too Slow
![Page 17: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/17.jpg)
Let Web Extensible
![Page 18: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/18.jpg)
Extensible Web
https://medium.com/the-future-of-the-web/2fcd1c1bb32!
http://extensiblewebmanifesto.org/!
http://www.w3.org/community/nextweb/
![Page 19: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/19.jpg)
Web Component
![Page 20: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/20.jpg)
Not just new markup language
![Page 21: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/21.jpg)
Not one standard
![Page 22: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/22.jpg)
The Standards⚫ Shadow DOM!
⚫ Custom Element!
⚫ HTML Imports!
⚫ Template!
⚫ Scoped Style!
⚫ Mutation Observer … etc
![Page 23: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/23.jpg)
Template
![Page 24: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/24.jpg)
<template>
⚫ Provide reusable DOMNode!
⚫ Parse but not render!
⚫ Used to use <script type="text/template">!
⚫ Not parse at all
http://www.w3.org/TR/html5/scripting-1.html#the-template-element
![Page 25: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/25.jpg)
<template id="sdom">! <header>! <h2>Eric</h2>! </header>! <section>! <div>Digital Jedi</div>! </section>! <footer>! <h4>footer text</h4>! </footer>!</template>
![Page 26: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/26.jpg)
var tpl = document.getElementById('sdom');!!var dom = tpl.content.cloneNode(true);!!shadowRoot.appendChild(dom);
![Page 27: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/27.jpg)
Shadow DOM
![Page 28: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/28.jpg)
Shadow DOM
⚫ Hidden nodes in DOM tree!
⚫ Encapsulation!
⚫ Keep component simple!
⚫ Browser already have this feature
http://w3c.github.io/webcomponents/spec/shadow/
![Page 29: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/29.jpg)
![Page 30: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/30.jpg)
![Page 31: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/31.jpg)
Take a Look
![Page 32: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/32.jpg)
Use
⚫ Create shadow root!
⚫ appendChild to shadow root
![Page 33: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/33.jpg)
var host = document! .querySelector('.custom-component');!!var root = host.createShadowRoot();!!root.innerHTML = html_template_string;
![Page 34: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/34.jpg)
DOM Tree
root
node(host)node
![Page 35: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/35.jpg)
DOM Tree
Shadow Tree
root
node(host)
Shadow root
node
node
![Page 36: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/36.jpg)
Get DOMNodes
⚫ querySelector, querySelectorAll on shadow root!
⚫ DOM API
![Page 37: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/37.jpg)
Style
var html_template_string = !! '<style>div { color: red; }</style>' ! ! + '<div>Click me!</div>';
![Page 38: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/38.jpg)
Style in Shadow DOM
⚫ Scoped by default!
⚫ Possible to import separate css file!
⚫ Path issue
![Page 39: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/39.jpg)
<option>
⚫ How to make custom element like <select>!
⚫ Fill content when using it
![Page 40: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/40.jpg)
<select>
<option>
![Page 41: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/41.jpg)
<magic-tag id="example4">! <h2>Eric</h2>! <h2>Bidelman</h2>! <div>Digital Jedi</div>! <h4>footer text</h4>!</magic-tag>!!<template id="sdom">! <header>! <content select="h2"></content>! </header>! <section>! <content select="div"></content>! </section>! <footer>! <content select="h4:first-of-type"></content>! </footer>!</template>
![Page 42: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/42.jpg)
<content>
⚫ <content> as insertion point!
⚫ CSS selector to select content
![Page 43: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/43.jpg)
![Page 44: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/44.jpg)
Custom Element
![Page 45: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/45.jpg)
Custom Element
⚫ Define your element!
⚫ Use API
http://w3c.github.io/webcomponents/spec/custom/
![Page 46: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/46.jpg)
<element>
⚫ No more in standards
![Page 47: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/47.jpg)
Register Element
registerElement(‘x-button', {prototype: xButtonProto});
element name options
prototype extend
![Page 48: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/48.jpg)
Custom Element Name
⚫ Custom element’s name should have “-”!
⚫ ex: x-button, my-slider!
⚫ With “-”, element don’t know by browser will treat as unresolved element, otherwise it will be unknown element
![Page 49: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/49.jpg)
Unresolved
⚫ :unresolved can use to hide/style unresolved elements!
⚫ Avoid FOUT
![Page 50: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/50.jpg)
Custom Prototype
⚫ Inherit from HTMLElement.prototype!
⚫ Add lifecycle callbacks
![Page 51: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/51.jpg)
Element Lifecycle
⚫ Define in custom element standard!
⚫ created!
⚫ attached!
⚫ detatched!
⚫ attributeChanged
https://www.w3.org/Bugs/Public/show_bug.cgi?id=24314
![Page 52: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/52.jpg)
var doc = document.currentScript.ownerDocument;!!var xButtonProto = Object.create(HTMLElement.prototype);!!!xButtonProto.createdCallback = function () {! var root = this;! var host = this.webkitCreateShadowRoot();! //host blah blah ...!};!!!document.registerElement(‘x-button',! {prototype: xButtonProto}!);
![Page 53: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/53.jpg)
HTML Imports
![Page 54: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/54.jpg)
Imports
⚫ Import more resources for future use!
⚫ Images, Style, Scripts…etc!
⚫ Web Component
http://www.w3.org/TR/html-imports/
![Page 55: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/55.jpg)
<link rel="import" href="path/to/component.html">
![Page 56: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/56.jpg)
<script>
⚫ `document` is importer!
⚫ Easy to work with document!
⚫ How to get its self!
⚫ ex: <template> in importee document
![Page 57: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/57.jpg)
importer importee
document document
document.registerElement!!//register on importer
<link rel="import"! href="…" />
![Page 58: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/58.jpg)
importer importee
document document
<template>! <blah />!</template>
<link rel="import"! href="…" />
![Page 59: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/59.jpg)
importer importee
document document
<template>! <blah />!</template>!!<script>!!// How to get template?!!</script>
<link rel="import"! href="…" />
![Page 60: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/60.jpg)
document.currentScript.ownerDocument;
![Page 61: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/61.jpg)
currentScript
⚫ HTML5 API!
⚫ For 3rd party widgets to locate <script> position
http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-currentscript
![Page 62: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/62.jpg)
importer importee
document document.currentScript
<template>! <blah />!</template>!!<script>!!document.currentScript! .ownerDocument!!</script>
<link rel="import"! href="…" />
![Page 63: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/63.jpg)
All Together
![Page 65: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/65.jpg)
Libs
![Page 67: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/67.jpg)
Brick
⚫ by Mozilla!
⚫ Repository for Custom-UI build by X-Tag
http://mozilla.github.io/brick/
![Page 68: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/68.jpg)
Polymer
⚫ by Google!
⚫ Application build upon Web Component!
⚫ Polyfills!
⚫ Share with X-Tag
http://www.polymer-project.org/
![Page 69: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/69.jpg)
![Page 70: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/70.jpg)
more..
![Page 71: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/71.jpg)
⚫ Semantic!
⚫ Accessibility!
⚫ Internationalization!
⚫ Security!
⚫ Performance!
⚫ OS Native UI…
![Page 72: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/72.jpg)
https://www.youtube.com/watch?v=e29D1daRYrQ
![Page 73: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/73.jpg)
Q&A
![Page 74: Web Component](https://reader033.vdocuments.us/reader033/viewer/2022052504/554954efb4c905f24e8b4e39/html5/thumbnails/74.jpg)
> B