Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web components - The component model for the Web

Web components - The component model for the Web

Presentation about Web Components used during the DOT 2014. You can find information about Templates, Custom Elements, Shadow DOM and HTML imports the technologies behind the Web Components.

Nicola Sanitate

September 20, 2014
Tweet

More Decks by Nicola Sanitate

Other Decks in Programming

Transcript

  1. Elementi$ CONFIGURABILI Small Medium Large X-Large XX-Large <select id="size" size="6"

    multiple> <option disabled>Small</option> <option disabled>Medium</option> <option selected>Large</option> <option>X-Large</option> <option>XX-Large</option> </select>
  2. Elementi$ COMPONIBILI Small <select> <optgroup label="Small"> <option>Small</option> </optgroup> <optgroup label="Medium">

    <option>Medium</option> </optgroup> <optgroup label="Large"> <option>Large</option> <option>X-Large</option> <option>XX-Large</option> </optgroup> </select>
  3. Carousel$/$Slideshow Carousel$/$Slideshow <head> <link href="styles/bootstrap.min.css" rel="stylesheet"> <_script_ src="lib/bootstrap/carousel.js"></_script_> </head> <body>

    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel" <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active" <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner"> <div class="item active"> <img src="..." alt="..."> </div> <div class="item"> <img src="..." alt="..."> </div> <div class="item"> <img src="..." alt="..."> </div> </div>
  4. Carousel$/$Slideshow Carousel$/$Slideshow <head> <link href="lib/bootstrap/carousel.html" rel="import"> </head> <body> <carousel> <img

    src="..." alt="..."> <img src="..." alt="..."> <img src="..." alt="..."> </carousel> </body>
  5. Come$si$usa Come$si$usa var t = document.querySelector('#my-template'); var clone = document.importNode(t.content,

    true); // Populate the src at runtime. clone.querySelector('img').src = 'logo.png'; document.body.appendChild(clone);
  6. Esempio Esempio <button onclick="useIt()">Cliccami</button> <div id="container"></div> <template id="my-template"> <div>Provengo da

    una &lt;template&gt;.</div> <_script_>alert('Grazie!')</_script_> </template> function useIt() { var t = document.querySelector('#my-template'); var clone = document.importNode(t.content, true); document.querySelector('#container').appendChild(clone); }
  7. Come$si$definisce Come$si$definisce var XFoo = document.registerElement('x-foo'); oppure var XFoo =

    document.registerElement('mega-button', { prototype: Object.create(HTMLButtonElement.prototype), extends: 'button' });
  8. Metodi$di$lifecycle Metodi$di$lifecycle var MyElement = document.registerElement('my-element', { prototype: Object.create(HTMLElement.prototype), //

    createdCallback, attachedCallback, detachedCallback attributeChangedCallback: { value: function(attr, oldVal, newVal) { ... } } });
  9. Custom9Elements $+$ Template <my-tag></my-tag> <template id="my-template"> <p>Sono in &lt;my-tag&gt; e

    provengo da una &lt;template&gt;.</p> </template> var proto = Object.create(HTMLElement.prototype, { createdCallback: { value: function() { var t = document.querySelector('#my-template'); var clone = document.importNode(t.content, true); this.appendChild(clone); } } }); document.registerElement('my-tag', { prototype: proto });
  10. E$il$contenuto$dellVhost? E$il$contenuto$dellVhost? <button>Nicola</button> var host = document.querySelector('button'); var root =

    host.createShadowRoot(); root.innerHTML = 'Ciao <content></content>. Sei il benvenuto!';
  11. Shadow9DOM $+$ Custom9Elements $+$ Template <p>Non sono nello Shadow DOM</p>

    <my-tag>Nicola</my-tag> <template id="my-template"> <p>Ciao <content></content>. Sono nello Shadow DOM di &lt;my-tag&gt; e provengo da una & </template> var proto = Object.create(HTMLElement.prototype, { createdCallback: { value: function() { var t = document.querySelector('#my-template'); var clone = document.importNode(t.content, true); this.createShadowRoot().appendChild(clone); } } }); document.registerElement('my-tag', { prototype: proto }); p { color: orange; }
  12. All9together9now <template id="my-template"> <p>Ciao <content></content>. Sono nello Shadow DOM di

    &lt;my-tag&gt; e provengo da una & </template> var importDoc = document.currentScript.ownerDocument; var proto = Object.create(HTMLElement.prototype, { createdCallback: { value: function() { var t = importDoc.querySelector('#my-template'); var clone = document.importNode(t.content, true); this.createShadowRoot().appendChild(clone); } } }); document.registerElement('my-tag', { prototype: proto }); <head> <link rel="import" href="shadow-dom-for-import.html"> </head> <body> <my-tag>Nicola</my-tag> </body>