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

Web Components: Um futuro cada vez mais presente!

Web Components: Um futuro cada vez mais presente!

Apresentação realizada no JS Day na cidade de Feira de Santana/Bahia no dia 06/Junho.

Jonata Weber

June 06, 2015
Tweet

More Decks by Jonata Weber

Other Decks in Programming

Transcript

  1. –Michael Bleigh @ Founder of Divshot “Web Components represent the

    most important advancement in browser technology since AJAX”
  2. var map; function initialize() { var mapOptions = { zoom:

    8, center: new google.maps.LatLng(-34.397, 150.644) }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); } google.maps.event.addDomListener(window, 'load', initialize); Simple Google Map <body> <div id="map-canvas"></div> </body> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
  3. Custom Elements 31 <jsday-fsa></jsday-fsa> var JsDay = Object.create(HTMLElement.prototype); JsDay.createdCallback =

    function() { this.textContent = 'JS Day - Feira de Santana'; }; document.registerElement('jsday-fsa', { prototype: JsDay });
  4. 32 Boas práticas! Custom Elements • Good • x-component •

    x-web-component • Bad • web_components • xelement • XElement
  5. Shadow DOM 38 <h1>My Light DOM</h1> <div id="meu-elemento"></div> var el

    = document.querySelector('#meu-elemento'); var shadow = el.createShadowRoot(); var h1 = document.createElement(‘h1'); var style = document.createElement('style'); h1.textContent = 'Olá, JS DAY!'; style.innerHTML = 'h1 { color: red; }'; shadow.appendChild(h1); shadow.appendChild(style);
  6. Shadow DOM 39 <h1>My Light DOM</h1> <div id="meu-elemento"></div> var el

    = document.querySelector('#meu-elemento'); var shadow = el.createShadowRoot(); var h1 = document.createElement(‘h1'); var style = document.createElement('style'); h1.textContent = 'Olá, JS DAY!'; style.innerHTML = 'h1 { color: red; }'; shadow.appendChild(h1); shadow.appendChild(style);
  7. HTML Template (good) 44 <template> <img src="success.jpg" alt="uma foto qualquer">

    </template> <button onclick="exibirFoto()">Exibir Foto</button> var t = document.querySelector('template').content; function exibirFoto() { document.body.appendChild(t.cloneNode(true)); }
  8. 45 var t = document.querySelector('template').content; function exibirFoto() { document.body.appendChild(t.cloneNode(true)); }

    <template> <img src="success.jpg" alt="uma foto qualquer"> </template> <button onclick="exibirFoto()">Exibir Foto</button> HTML Template (good)
  9. HTML Template nativo não tem data binding 46 // placeholders

    <template bind="{{items}}"></template> // repeaters <template repeat="{{item in items}}"></template> // conditionals <template if="{{item.active}}"></templat
  10. <!DOCTYPE html> <html> <head> <title>PHP Includes</title> </head> <body> <?php include

    'header.php' ?> <div>Meu Conteúdo</div> <?php include 'footer.php' ?> </body> </html> PHP Includes
  11. HTML Imports <link rel="import" href="cabecalho.html"> index.html <div id="template"> <h1>Olá, JS

    DAY!</h1> <h2>Esse é o cabeçalho do site!</h2> </div> <style> h1 { color: red; } </style> cabecalho.html var link = document.querySelector('link[rel=import]'); var importDoc = link.import; var template = importDoc.querySelector('#template'); var style = importDoc.querySelector('style'); document.body.appendChild(template.cloneNode(true)); document.head.appendChild(style.cloneNode(true));
  12. Custom Elements Permite criar novas elementos HTML e extender os

    componentes nativos HTML Templates Possibilita criar fragmento de código manipulável e não visível pelo browser Shadow DOM Encapsulamento e escopo CSS HTML Imports Ajuda a “empacotar" e carrega o componente
  13. Polyfill Implementa a tecnologia que um desenvolvedor espera que o

    navegador forneça nativamente webcomponents.js
  14. Criando um Polymer element 1. Importar o Polymer core (polymer.html)

    2. Declarar seu elemento usando <dom-module> 3. Inicializar o seu elemento usando Polymer({is: "my-element"}); <link rel="import" href="../bower_components/polymer/polymer.html"> <dom-module id="my-element"> <template> <span>Hello from <b>my-element</b>. This is my Shadow DOM.</span> </template> </dom-module> <script> Polymer({is: "my-element"}); </script>
  15. <!-- Polyfill Web Components support for older browsers --> <script

    src="bower_components/webcomponentsjs/ webcomponents.js"></script> <!-- Import element --> <link rel="import" href="meu-contador/meu-contador.html"> <!-- Use element --> <meu-contador pontos="50"></meu-contador> meu-contador github.com/jonataa/custom-elements
  16. <!-- Polyfill Web Components support for older browsers --> <script

    src=“/bower_components/webcomponentsjs/webcomponents.js"></ script> <!-- Import element --> <script src="/bower_components/time-elements/time-elements.js"></ time <time is="time-ago" datetime="2014-07-04T16:30:00-08:00"></time>