Top Banner
Web Components A revolution ? @cbalit 0
45

Devoxx 2014-webComponents

Jul 17, 2015

Download

Internet

Cyril Balit
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Devoxx 2014-webComponents

Web ComponentsA revolution ?

@cbalit0

Page 2: Devoxx 2014-webComponents

Why components ?This is the way we already do itFor widgets ? applications ? pluggins ?ReusableEncapsulatedExtendable

Page 3: Devoxx 2014-webComponents

What is it ?4 specifications

1. Html Import2. Shadow Dom3. Template4. Custom Element

Page 4: Devoxx 2014-webComponents

Html Import

Page 5: Devoxx 2014-webComponents

What is it ?Include an HTML element inside another one

Page 6: Devoxx 2014-webComponents

How ?<link rel="import" href="/path/myfile.html">

CORSLoad onceDon't block page parsing

Page 7: Devoxx 2014-webComponents

Get the contentHTML and CSS are just loaded but not included (available) We

retrieve the content using the import property<link id="my-import" rel="import" href="myfile.html"><script>

</script>

var link = document.querySelector('#my-import'); var content = link.import; //I can now acces to my content var el = content.querySelector('.mySelector');

document.body.appendChild(el.cloneNode(true));

Page 8: Devoxx 2014-webComponents

And for Javascript ...Run in the page contextCan access to its DOM ...... and the one from the page

<script>

</script>

var importDoc = document.currentScript.ownerDocument; var mainDoc = document;

Page 9: Devoxx 2014-webComponents

EventsLoad and Error Event

<script async="">

</script>

<link rel="import" href="file.html" onload="handleLoad(event)" onerror="handleError(event)"

function handleLoad(e) { console.log('Loaded import: ' + e.target.href); } function handleError(e) { console.log('Error loading import: ' + e.target.href); }

Page 10: Devoxx 2014-webComponents

Support

Page 11: Devoxx 2014-webComponents

Shadow Dom

Page 12: Devoxx 2014-webComponents

Old !!!!Browsers already use it

<input type="range"><input type="date"><input type="hour">

jj/mm/aaaa

Page 13: Devoxx 2014-webComponents

What is it ?It's all about Encapsulation

Page 14: Devoxx 2014-webComponents

What does this meanIsolated containerNew kind of nodeshadow rootshadow host

Page 15: Devoxx 2014-webComponents

With JavascriptcreateShadowRootelement.shadowRoot

<button>Normal button!</button><button id="myBtn">Hello, world!</button><script>

</script>

var host = document.querySelector('#myBtn'); var root = host.createShadowRoot(); root.textContent = 'こんにちは、影の世界!';

Normal button! こんにちは、影の世界!

Page 16: Devoxx 2014-webComponents

Shadow DOM versus LightDOM

<my-element> <span>Hello</span></my-element>

visible sub-tree (childNodes, children, innerHTML ...)internal nodecomposed DOM: what the browser see and render

Page 17: Devoxx 2014-webComponents

Insertions pointsDefine render area in the shadow domNo logical link

<div id="host"> <span class="title">Hello too</span> <span>Bla Bla Bla</span></div><script> var shadow = document.querySelector('#host').createShadowRoot(); shadow.innerHTML = '<h1>In Shadow</h1>' + '<h2><content select=".title"></content></h2>' + '<section><content select="*"></content></section>';</script>

Page 18: Devoxx 2014-webComponents

Support

Page 19: Devoxx 2014-webComponents

Template

Page 20: Devoxx 2014-webComponents

What is it ?Reusable DOM template

Page 21: Devoxx 2014-webComponents

Not in the documentNo side effect

DOM not renderedScript not parsedImage not loaded

Page 22: Devoxx 2014-webComponents

Usage1. Get the template with a selector2. acces to the content with the content property3. clone: he's alive4. insert the clone element in the page

Page 23: Devoxx 2014-webComponents

Exemple<template id="mytemplate"> <img src="img/templates_64x64.png" alt="great image"> <div id="comment">My new comment</div></template><script>

</script>

var t = document.querySelector('#mytemplate'); // Populate the src at runtime. t.content.querySelector('img').src = 'img/templates_64x64.png'; t.content.querySelector('#comment').innerHTML = 'My new comment';

var clone = document.importNode(t.content, true); document.body.appendChild(clone);

My new comment

Page 24: Devoxx 2014-webComponents

Support

Page 25: Devoxx 2014-webComponents

Custom element

Page 26: Devoxx 2014-webComponents

What is it ?Define new HTML elementExtend existing elements

Page 27: Devoxx 2014-webComponents

How ?registerElementa name (with a -)a prototype (HTMLElement by default)

<script>

</script>

<my-elt></my-elt>

var myElt = document.registerElement('my-elt',HTMLElement.prototype);

Page 28: Devoxx 2014-webComponents

Extend existing elements

<script>

</script>

<button is="”my-button”"></button>

var myButton = document.registerElement('my-button', { prototype: Object.create(HTMLButtonElement.prototype), extends: 'button' });

Page 29: Devoxx 2014-webComponents

LifecycleDeclaration vs register

Seen as unresolved

pseudo-selector :unresolved

<style>

</style><my-element>register</my-element><button onclick="document.registerElement('my-element')">Register</button>

*:unresolved { color: red; }

i'm unresolved Register

Page 30: Devoxx 2014-webComponents

CallbackcreatedCallbackattachedCallbackdetachedCallbackattributeChangedCallback

var myElemtProto = Object.create(HTMLElement.prototype);

myElemtProto.createdCallback = function() {};

var myElemt = document.registerElement('my-element', myElemtProto);

Page 31: Devoxx 2014-webComponents

Add content

Page 32: Devoxx 2014-webComponents

innerHTML

myEltProto.createdCallback = function() {this.innerHTML = "<b>un peu de contenu!</b>";};

Page 33: Devoxx 2014-webComponents

shadowDom

myEltProto.createdCallback = function() {var shadow = this.createShadowRoot();shadow.innerHTML = "<b>un peu de contenu!</b>";};

Page 34: Devoxx 2014-webComponents

Template

<template id="sdtemplate">

</template>

myEltProto.createdCallback = function() {var t = document.querySelector('#sdtemplate');var clone = document.importNode(t.content, true);this.createShadowRoot().appendChild(clone);};

Page 35: Devoxx 2014-webComponents

Add code

Page 36: Devoxx 2014-webComponents

The prototypemyEltProto.myFctn=function(){...}Object.defineProperty(myEltProto, "bar", {value: 5});

Page 37: Devoxx 2014-webComponents

Support

Page 38: Devoxx 2014-webComponents

And so what ?

Page 39: Devoxx 2014-webComponents

Real good specificationCan be used alone

But a weak support

So what can we do ?

Page 40: Devoxx 2014-webComponents

POLYMER

polyfills (platform.js)components (core-elements, paper-elements)sugaring (polymer.js)

Page 41: Devoxx 2014-webComponents

X-TAG

Web Components Polyfills (custom element et HTMLImports)X-Tag Custom ElementsX-Tag Core Library

Page 42: Devoxx 2014-webComponents

BOSONIC

polyfills (platform.js)components

Page 44: Devoxx 2014-webComponents

Thank you !!!cbalit

Page 45: Devoxx 2014-webComponents

My new comment