On Github jedimorten / web-components-presentation
<div class="wrapper"> <div class="header"> <div class="innerheadwrapper"> <div class="innerheader"></div> <div class="navbar"></div> </div> </div> <div class="sidebar"> <div class="innersidebarwrapper"></div> </div> <div class="section"> <div class="contentwrapper"> <div class="content"></div> <div class="content"></div> </div> </div> <div class="footer"></div> </div>
<!DOCTYPE html> <html> <head></head> <body> <header></header> <nav></nav> <section></section> <article></article> <aside></aside> <footer></footer> </body> </html>
Vis hvordan vi bruker HTML5 i slidene
<div class="wrapper"> <header> <div class="innerheadwrapper"> <div class="header"></div> <nav></nav> </div> </header> <aside> <div class="innersidebarwrapper"></div> </aside> <article> <div class="contentwrapper"> <section></section> <section></section> </div> </article> <footer></footer> </div>
Tid: 15min
Dagens workarounds:
Skjult DOM
Overloading script
<div id="mytemplate" style="display: none;"> <img src="logo.png"> <div class="comment"></div> </div>
:) Bruker DOM
:) Ingenting blir rendra
:( Requests
:( Slitsomt å style
<script id="mytemplate" type="text/x-handlebars-template"> <img src="logo.png"/> <div class="comment"></div> </script>
:) Ingenting blir rendra
:) Ingen requests
:( Sikkerhetsproblemer
<template id="mytemplate"> <img src="logo.png"> <div class="comment"></div> </template>
:) Bruker DOM
:) Ingenting blir rendret
:) Ingen requests
:( Dårlig browserstøtte
Skjule støy
Rydde i DOM
Vedlikeholdbart
Vis Chrome shadow DOM
//Hent ut template fra tidligere var template = document.querySelector("#contact-template");
//Hent ut mål-elementet var host = document.querySelector('contact-wrapper');
//Lag en shadow root-node på mål-elementet var root = host.createShadowRoot();
//Klon innholdet og barn-noder, og legg til i shadow DOM var clone = document.importNode(template.content, true); root.appendChild(clone);
<template id="contact-template"> <div class="contact-header"> <content select="h2"></content> <content select="img"></content> </div> <div class="detailed-info"> <content select="ul"></content> </div> </template>
Tid: 35min
Adskilt fra resten av DOM
Styling av host/mål-elementet; :host
Styling av rot-elementet; ::shadow
Tilgang til intern styling; >>>
Shadow DOM er altså tilgjengelig fra utsiden
Kontrollerte forhold
Eksplisitt selecte shadow DOM
:host { opacity: 0.4; } :host(:hover) { opacity: 1; } :host(:active) { opacity: 1; background-color: blue; }
.contact-tile::shadow h2 { color: red; }
Styling av shadow DOM fra utsiden
Velger rot-elementet / shadow root
Velger alle matchende elementer
Kraftigere enn ::shadow
/deep/ forsvinner, erstattes med:
>>> - null nettleserstøtte
.contact-tile /deep/ .detailed-info{ } .contact-tile >>> .detailed-info{ }
/deep/ velger alle elementene i alle shadow DOMs
<button is="unclickable-button">Ikke prøv å klikk på meg</button>Ikke prøv å klikk på meg
Tid: 45min
var proto = Object.create(HTMLButtonElement.prototype); proto.createdCallback = function() { var self = this, t = document.querySelector('#unclickable-template'), shadow = self.createShadowRoot(); shadow.appendChild(document.importNode(t.content, true)); self.style.position = "absolute"; self.addEventListener('mouseover', function(e) { self.style.top = ''; self.style.left = ''; self.style.top = Math.floor((Math.random() * 300) + 1)+'px'; self.style.left = Math.floor((Math.random() * 600) + 1)+'px'; }); }; document.registerElement('unclickable-button', {prototype: proto, extends: 'button'} );
Latest commit: Oops! This browser doesn't support Web Components.
Latest commit: <time is="relative-time" datetime="2015-01-27T16:00:00.000Z"> Oops! This browser doesn't support Web Components. </time>
<link rel="import" href="dist/x-gif.html">
<x-gif src="gifs/dog.gif" speed="5"></x-gif>
Tid: 55min
Ekstern link?
Mappestruktur?
Droppe eksterne avhengigheter?
Mye overhead, ikke deklarativt
Tidlig fase
Dårlig nettleserstøtte
Støtte for web components i "alle" nettlesere!
IE er ikke støttet fullt ut
Applications
Custom elements
Core (Polymer.js)
Foundation (webcomponents.js)
Native
Polyfills for kjente egenskaper:
Applications
Custom elements
Core (Polymer.js)
Foundation (webcomponents.js)
Native
Vanilla vs Polymer
Attributter
Expressions
Touch events
Data binding
Lifecycle callbacks
<template id="contact-template"> <style></style> </template> <script> var proto = Object.create(HTMLElement.prototype), link = document.currentScript.ownerDocument; template = link.querySelector('#contact-template'); proto.createdCallback = function() { //manage stuff }; document.registerElement('contact-tile', {prototype: proto}); </script>
<contact-tile></contact-tile>
<polymer-element name="contact-tile"> <template></template> <script> Polymer({ //manage stuff }); </script> </polymer-element>
Når man deklarerer et polymer-element har polymer reservert noen atributter:
<polymer-element name="contact-tile" attributes="displayDetails favorite"></polymer-element>
Polymer({ displayDetails: false, favorite: false });
Tid: 1t 40min
<polymer-element name="contact-tile"></polymer-element>
Polymer({ publish: { displayDetails: { value: false, reflect: true }, favorite: { value: false, reflect: true } } });
Tid: 1t 40min
Kryssplattform
Mus + touch
Lite overhead
//Standard fra nettleser touch-action: auto; //Ingen touch touch-action: none; //Horizontal scrolling touch-action: pan-x; //Vertical scrolling touch-action: pan-y; //Pinch zoom + scrolling touch-action: manipulation;
Tid: 1t 50min
<div touch-action="auto"></div> <div touch-action="none"></div> <div touch-action="pan-x"></div> <div touch-action="pan-y"></div> <div touch-action="manipulation"></div>
Tid: 1t 50min
<polymer-element name="contact-tile"> <template> {{name}} studerte på {{education}} </template> <script> Polymer('contact-tile', { ready: function() { this.name = 'Yngve'; this.education = 'NTNU'; } }); </script> </polymer-element>
<polymer-element name="contact-tile"> <template> <ul> <template repeat="{{contact in contacts}}"> <li>{{contact.name}}</li> </template> </ul> </template> <script> Polymer('contact-tile', { ready: function() { this.contacts = [ {'name': 'Morten Kjelling', 'position': 'Frontend / .NET'}, {'name': 'Yngve Svalestuen', 'position': 'Omni Capable'}, {'name': 'Bjørn Vegard Thoresen', 'position': '.NET'} ]; } }); </script> </polymer-element>
<polymer-element name="contact-tile"> <template> <ul> <template if="{{!contacts.length}}"> <li>Ingen kontakter!</li> </template> </ul> </template> <script> Polymer('contact-tile', { ready: function() { this.contacts = []; } }); </script> </polymer-element>
<polymer-element name="contact-tile"> <template> <ul> <template repeat="{{contact in contacts}}"> <template if="{{contact.age > 25}}"> <div>{{contact.name}} er eldre enn 25år!</div> </template> </template> </ul> </template> <script> Polymer('contact-tile', { ready: function() { this.contacts = [ {'name': 'Morten Kjelling', 'age': 16}, {'name': 'Yngve Svalestuen', 'age': 24}, {'name': 'Bjørn Vegard Thoresen', 'age': 32}]; } }); </script> </polymer-element>
<a href="{{url}}"></a>
<polymer-element name="contact-tile" attributes="name"> <template> Navn: {{name}} </template> <script> Polymer('name-field', { ready: function() { this.name = 'Ukjent' } }); </script> </polymer-element> <polymer-element name="contact-tile"> <template> <p><name-field name="{{nameValue}}"></name-field></p> <p>Hva heter du? <input value="{{nameValue}}" placeholder="Skriv inn navnet ditt..."></p> </template> </polymer-element> <contact-tile></contact-tile>
<input type="text" value="Verdi som bare settes inn en gang: [[ value ]]">
Blir inaktiv etter første binding
Fordel for ytelsen
Eksperimentell!
Tid: 1t 50min
Gjøre enkle verdi konsepter
Filtrering
Kan ikke legge til HTML ved å bruke expressions
<p>Morten har {{ folders.gif.length }} gifs</p>
<p>Morten har 9001 gifs</p>
Tid: 1t 55min
{{ Hallo Navet | uppercase}}
PolymerExpressions.prototype.uppercase = function(input) { return input.toUpperCase(); };
{{myNumber | toFixed(2)}}
toFixed: function(value, precision) { return Number(value).toFixed(precision); }
Core
Paper
++
Tid: 2t
Tid: 2t 5min
Data-binding
Prototyping
Uferdig dokumentasjon
Fortsatt under utvikling
Feilhåndtering
Tid: 2t 15min
Data-binding
Prototyping
Nina Orucevic / nina.orucevic@ciber.com