web component

74
W eb Component othree

Upload: -

Post on 06-May-2015

873 views

Category:

Technology


0 download

DESCRIPTION

Web Component intro at OSDC 2014, #osdctw

TRANSCRIPT

Page 1: Web Component

Web Componentothree

Page 2: Web Component

othree⚫ HappyDesigner, MozTW!

⚫ PhD Candidate @ NTUST, F2E @HTC

https://github.com/othreehttps://blog.othree.net/

Page 3: Web Component

othree

Page 4: Web Component

Why Web Component

Page 5: Web Component

Why

⚫ Rich Internet Applications!

⚫ More and more custom UI!

⚫ Complex → Modularize!

⚫ Reuse

Page 6: Web Component

Custom UI

Page 7: Web Component
Page 8: Web Component
Page 9: Web Component
Page 10: Web Component
Page 11: Web Component
Page 12: Web Component
Page 13: Web Component

What Developer Want

⚫ Modularize codes, markup and styles!

⚫ Simple and easy to reuse

Page 14: Web Component

What Developer Want

⚫ Use <magic-tag> and everything is done

Page 15: Web Component

Standards is Not Enough

Page 16: Web Component

Standards Changes too Slow

Page 17: Web Component

Let Web Extensible

Page 18: Web Component

Extensible Web

https://medium.com/the-future-of-the-web/2fcd1c1bb32!

http://extensiblewebmanifesto.org/!

http://www.w3.org/community/nextweb/

Page 19: Web Component

Web Component

Page 20: Web Component

Not just new markup language

Page 21: Web Component

Not one standard

Page 22: Web Component

The Standards⚫ Shadow DOM!

⚫ Custom Element!

⚫ HTML Imports!

⚫ Template!

⚫ Scoped Style!

⚫ Mutation Observer … etc

Page 23: Web Component

Template

Page 24: Web Component

<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

<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

var tpl = document.getElementById('sdom');!!var dom = tpl.content.cloneNode(true);!!shadowRoot.appendChild(dom);

Page 27: Web Component

Shadow DOM

Page 28: Web Component

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
Page 30: Web Component
Page 31: Web Component

Take a Look

Page 32: Web Component

Use

⚫ Create shadow root!

⚫ appendChild to shadow root

Page 33: Web Component

var host = document! .querySelector('.custom-component');!!var root = host.createShadowRoot();!!root.innerHTML = html_template_string;

Page 34: Web Component

DOM Tree

root

node(host)node

Page 35: Web Component

DOM Tree

Shadow Tree

root

node(host)

Shadow root

node

node

Page 36: Web Component

Get DOMNodes

⚫ querySelector, querySelectorAll on shadow root!

⚫ DOM API

Page 37: Web Component

Style

var html_template_string = !! '<style>div { color: red; }</style>' ! ! + '<div>Click me!</div>';

Page 38: Web Component

Style in Shadow DOM

⚫ Scoped by default!

⚫ Possible to import separate css file!

⚫ Path issue

Page 39: Web Component

<option>

⚫ How to make custom element like <select>!

⚫ Fill content when using it

Page 40: Web Component

<select>

<option>

Page 41: Web Component

<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

<content>

⚫ <content> as insertion point!

⚫ CSS selector to select content

Page 43: Web Component
Page 44: Web Component

Custom Element

Page 45: Web Component

Custom Element

⚫ Define your element!

⚫ Use API

http://w3c.github.io/webcomponents/spec/custom/

Page 46: Web Component

<element>

⚫ No more in standards

Page 47: Web Component

Register Element

registerElement(‘x-button', {prototype: xButtonProto});

element name options

prototype extend

Page 48: Web Component

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

Unresolved

⚫ :unresolved can use to hide/style unresolved elements!

⚫ Avoid FOUT

Page 50: Web Component

Custom Prototype

⚫ Inherit from HTMLElement.prototype!

⚫ Add lifecycle callbacks

Page 51: Web Component

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

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

HTML Imports

Page 54: Web Component

Imports

⚫ Import more resources for future use!

⚫ Images, Style, Scripts…etc!

⚫ Web Component

http://www.w3.org/TR/html-imports/

Page 55: Web Component

<link rel="import" href="path/to/component.html">

Page 56: Web Component

<script>

⚫ `document` is importer!

⚫ Easy to work with document!

⚫ How to get its self!

⚫ ex: <template> in importee document

Page 57: Web Component

importer importee

document document

document.registerElement!!//register on importer

<link rel="import"! href="…" />

Page 58: Web Component

importer importee

document document

<template>! <blah />!</template>

<link rel="import"! href="…" />

Page 59: Web Component

importer importee

document document

<template>! <blah />!</template>!!<script>!!// How to get template?!!</script>

<link rel="import"! href="…" />

Page 60: Web Component

document.currentScript.ownerDocument;

Page 61: Web Component

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

importer importee

document document.currentScript

<template>! <blah />!</template>!!<script>!!document.currentScript! .ownerDocument!!</script>

<link rel="import"! href="…" />

Page 63: Web Component

All Together

Page 64: Web Component

Demohttps://github.com/othree/web-component-test

Page 65: Web Component

Libs

Page 66: Web Component

X-Tag

⚫ by Mozilla!

⚫ Easy to create custom element

http://www.x-tags.org/

Page 67: Web Component

Brick

⚫ by Mozilla!

⚫ Repository for Custom-UI build by X-Tag

http://mozilla.github.io/brick/

Page 68: Web Component

Polymer

⚫ by Google!

⚫ Application build upon Web Component!

⚫ Polyfills!

⚫ Share with X-Tag

http://www.polymer-project.org/

Page 69: Web Component
Page 70: Web Component

more..

Page 71: Web Component

⚫ Semantic!

⚫ Accessibility!

⚫ Internationalization!

⚫ Security!

⚫ Performance!

⚫ OS Native UI…

Page 73: Web Component

Q&A

Page 74: Web Component

> B