11. To define your own HTML tag
Custom Element
<body>
...
<script>
var XFoo = document.registerElement('x-foo', {
prototype: Object.create(HTMLElement.prototype)
});
</script>
<x-foo></x-foo>
</body>
12. To encapsulate subtree and style in an element
Shadow DOM
<button>Hello, world!</button>
<script>
var host = document.querySelector('button');
var root = host.createShadowRoot();
root.textContent = 'こんにちは、影の世界 !';
</script>
13. To include an html page in another one
HTML Imports
<link rel="import" href="warnings.html">
<script>
var link = document.querySelector('link[rel="import"]');
var content = link.import;
// Grab DOM from warning.html's document.
var el = content.querySelector('.warning');
document.body.appendChild(el.cloneNode(true));
</script>
14. To have clonable document template
Template
<template id="mytemplate">
<img src="" alt="great image">
<div class="comment"></div>
</template>
var t = document.querySelector('#mytemplate');
// Populate the src at runtime.
t.content.querySelector('img').src = 'logo.png';
var clone = document.importNode(t.content, true);
document.body.appendChild(clone);
22. A simple container with a header
section and a content section
<paper-header-panel>
<paper-header-panel flex>
<paper-toolbar>
<paper-icon-button icon=“menu">
</paper-icon-button>
<div>MY APP</div>
</paper-toolbar>
<div>…</div>
</paper-header-panel>
MY APP
31. Coming soon ...
● ES6
○ optional
● Custom element V1
○ lifecycle changes
● Shadow Dom V1
○ slot
● Closer to the standard
○ no more polymer.fire….
● January 2017
40. ● dispatch with fire
● can send datas
Polymer({
is: 'x-custom',
handleClick: function() {
this.fire('kick', {kicked: true});
}
});
<x-custom></x-custom>
<script>
document.querySelector('x-custom').addEventListener
('kick', function (e) {
console.log(e.detail.kicked); // true
})
</script>
Events
41. Get and use Polymer({
is: 'x-custom',
doSomething: function() {
...
}
});
<x-custom></x-custom>
<script>
var elmt=document.querySelector('x-custom');
elmt.doSomething();
</script>
Method
43. ● [[]] : one-way bindings OR host to child
● {{}} : automatic binding (one way or two way according to the configuration)
Data Binding : 2 way
<dom-module id="host-element">
<template>
<child-element name="{{myName}}"></child-element>
</template>
</dom-module>
44. ● iterate on a list
● filter/sort feature
<dom-module id="employee-list">
<template>
<template is="dom-repeat" items="{{employees}}">
<div># <span>{{index}}</span></div>
<div>First name: <span>{{item.first}}</span></div>
</template>
</template>
…
</dom-module>
Helpers : dom-repeat
45. ● Conditional Template
<dom-module id="user-page">
<template>
<template>
All users will see this:
<div>{{user.name}}</div>
<template is="dom-if" if="{{user.isAdmin}}">
Only admins will see this.
<div>{{user.secretAdminStuff}}</div>
</template>
</template>
…
</dom-module>
Helpers : dom-if
48. with shadow dom, new selectors
● :host
● ::content
● Cross scope Styling
○ ::shadow et /deep/
source :http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/
CSS et Shadow Dom
51. An implementation of the
google-recaptcha component
<re-captcha>
<re-captcha
sitekey="yoursitekey"></re-captcha>
source: https://github.com/cbalit/re-captcha