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

Web Components é um termo guarda chuva para quatro novas especificações: Custom Elements, Shadow DOM, HTML Template e HTML Import. Nessa palestra vamos explorar seus principais recursos e como elas podem mudar a maneira que desenvolvemos componentes para Web.

Jonata Weber

October 04, 2014
Tweet

More Decks by Jonata Weber

Other Decks in Programming

Transcript

  1. 2 ,QPCVC9GDGT Software Engineer • Becker (becker.com/health) • Mederi Saúde

    Domiciliar • Trabalha com PHP desde 2009 • Bacharel em Sistemas de Informação (UNIFACS) • Pós-graduando em Engenharia de Software (UNIFACS) [email protected] github.com/jonataa
  2. Agenda • Manipulando o DOM • Web Components: Overview •

    Polymer • Exemplo • Links Interessantes 3
  3. Date Picker jQuery UI <link rel="stylesheet" href=“/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script

    src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script> <link rel="stylesheet" href="/resources/demos/style.css"> $(function() { $( "#datepicker" ).datepicker(); }); <input type="text" id="datepicker">
  4. 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>
  5. 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5

    </head> 6 <body> 7 8 <h1>Hello, World!</h1> 9 10 </body> 11 </html> Exemplo Manipulando o DOM 29
  6. Custom Elements 35 <gdg-salvador></gdg-salvador> var Proto = Object.create(HTMLElement.prototype); proto.createdCallback =

    function() { this.textContent = 'Google Developers Group'; }; var GDG = document.registerElement(‘gdg-salvador', { prototype: Proto });
  7. Extending a Custom Element var XFooProto = Object.create(HTMLElement.prototype); ... var

    XFooExtended = document.registerElement('x-foo-extended', { prototype: XFooProto, extends: 'x-foo' });
  8. Shadow DOM 40 <h1>My Page</h1> <div></div> var div = document.querySelector('div');

    var shadow = div.createShadowRoot(); var h1 = document.createElement('h1'); h1.innerHTML = 'Hello, GDG!' + '<style> h1 { color: red; } </style>'; shadow.appendChild(h1);
  9. HTML Template 43 <template> <img src="success.jpg" alt=""> </template> <button onclick="exibirFoto()">Exibir

    Foto</button> var t = document.querySelector('template').content; function exibirFoto() { document.body.appendChild(t.cloneNode(true)); }
  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="component.html"> var importDoc = document.querySelector('link[rel="import"').import; var

    style = importDoc.querySelector('style'); var content = importDoc.querySelector('.content'); document.head.appendChild(style.cloneNode(true)); document.body.appendChild(content.cloneNode(true)); index.html <div class="content"> <h1>Olá, GDG Salvador!</h1> <img src="success.jpg" alt=""> </div> <style> h1 { color: red; } </style> component.html
  12. my-element.html Criando um Polymer element 1. Carregar o Polymer core

    (polymer.html) 2. Declarar seu elemento usando <polymer-element> <link rel="import" href="../bower_components/polymer/polymer.html"> <polymer-element name="my-element" noscript> <template> <span>Hello from <b>my-element</b>. This is my Shadow DOM.</span> </template> </polymer-element>
  13. <!-- Polyfill Web Components support for older browsers --> <script

    src="components/platform/platform.js"></script> <!-- Import element --> <link rel="import" href="google-map.html"> <!-- Use element --> <google-map lat="37.790" long="-122.390"></google-map> google-map bit.ly/1v774uw
  14. <!-- Polyfill Web Components support for older browsers --> <script

    src="components/platform/platform.js"></script> <!-- Import element --> <link rel="import" href="google-chart.html"> <!-- Use element --> <google-chart type='pie' height='300px' width='400px' options='{"title": "Distribuição de pessoas em 2014"}' cols='[{"label":"Mês", "type":"string"}, {"label":"Dias", "type":"number"}]' rows='[["Jan", 31],["Fev", 28],["Mar", 31]]'> </google-chart> google-chart bit.ly/1ytx5rV
  15. <!-- Polyfill Web Components support for older browsers --> <script

    src="bower_components/platform/platform.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/platform/platform.js"></script> <!-- Import element --> <link rel="import" href="components/gdg/gdg-salvador.html"> <!-- Use element --> <gdg-salvador size="small" type="full" cliques="10"></gdg-salvador> gdg-salvador github.com/jonataa/custom-elements
  17. <!-- Polyfill Web Components support for older browsers --> <script

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