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

Let's play with Polymer

Let's play with Polymer

A quick overview of Web Components and Polymer library with basic examples. Also, you can find information about available tools for the Polymer development.

Presented at GDG DevFest Spring 2016 in Kosice, Slovakia.

Oleh Zasadnyy

May 07, 2016
Tweet

More Decks by Oleh Zasadnyy

Other Decks in Programming

Transcript

  1. <div class="tabs">
 <ul>
 <li><a href="#tab-1"><span>Tab 1</span></a></li>
 <li><a href="#tab-2"><span>Tab 2</span></a></li>
 <li><a

    href="#tab-3"><span>Tab 3</span></a></li>
 <div id="tab-1">
 <h2>Tab 1 header</h2>
 <p>Lorem Ipsum is simply dummy text of the industry.</p>
 </div>
 <div id="tab-2">
 <h2>Tab 2 header</h2>
 <p>Lorem Ipsum is simply dummy text of the industry.</p>
 </div>
 <div id="tab-3">
 <h2>Tab 3 header</h2>
 <p>Lorem Ipsum is simply dummy text of the industry.</p>
 </div>
 </ul>
 </div>
  2. Less markup. Less JS. Less confusion. <paper-tabs>
 <paper-tab>ITEM ONE</paper-tab>
 <paper-tab>ITEM

    TWO</paper-tab>
 <paper-tab>ITEM THREE</paper-tab>
 </paper-tabs>
  3. Templates Native templating in the browser Shadow DOM Scoped CSS!!!

    + encapsulated markup Custom Elements Create new HTML elements and extend existing ones (native only) 
 HTML Imports Load custom element definitions and resources
  4. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>
 .amazing

    {
 color: navy;
 }
 </style>
 <div class="amazing">Wow, so easy</div>
 </template>
 </dom-module>
  5. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>...</style>
 <div

    class="amazing">
 <content></content>
 </div>
 </template>
 </dom-module>
  6. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>...</style> 


    <h3 class="name">Oleh</h3>
 <div class="country">Ukraine</div>
 </template>
 </dom-module>
  7. Polymer({
 is: 'badge-element', 
 properties: {
 name: String, 
 country:

    String, 
 age: {
 type: Number, 
 value: 42
 }
 }
 });
  8. Polymer({
 is: 'badge-element',
 properties: {
 name: String, 
 country: String,

    
 age: {
 type: Number, 
 value: 42, 
 observer: '_ageChanged'
 }
 }
 });
  9. Polymer({
 is: 'badge-element',
 properties: {
 name: String, 
 country: String,

    
 age: {
 type: Number, 
 value: 42, 
 observer: '_ageChanged', 
 notify: true
 }
 }
 });
  10. Polymer({
 is: 'badge-element',
 properties: {
 first: String, 
 last: String,

    
 fullname: {
 type: String, 
 computed: '_computeFullName(first, last)'
 }
 }
 });
  11. Polymer({
 is: 'badge-element',
 properties: {
 name: String, 
 country: String,

    
 showCountry: {
 type: Boolean, 
 value: false
 }
 }
 });
  12. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>...</style>
 <h3

    class="name">{{name}}</h3>
 <div class="country" 
 hidden$=“{{showCountry}}">
 {{country}}
 </div>
 </template>
 </dom-module>
  13. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>
 .name

    { color: grey; } 
 .country {color: lightgray; } 
 </style> 
 <h3 class=“name”>{{name}}</h3>
 <div class=“country”>{{country}}</div>
 </template>
 </dom-module>
  14. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>
 .name

    { color: var(--name-font-color); }
 .country { ... }
 </style> 
 <h3 class="name">{{name}}</h3>
 <div class="country">{{country}}</div>
 </template>
 </dom-module>
  15. <!-- my-theme.html -->
 
 
 <style is="custom-style"> 
 badge-element {

    
 --name-font-color: blue; 
 --country-font-color: yellow;
 }
 </style>
  16. <!-- badge-element.html -->
 
 
 <dom-module id="badge-element">
 <template>
 <style>
 :host

    { @apply(--badge-theme); }
 .name { ... }
 .country { ... }
 </style>
 <h3 class="name">{{name}}</h3>
 <div class="country">{{country}}</div>
 </template>
 </dom-module>
  17. <!-- my-theme.html -->
 
 
 <style is="custom-style">
 badge-element {
 --badge-theme:

    { 
 padding: 20px;
 background: lightskyblue; 
 border: 2px solid lightgray; 
 border-radius: 5px;
 };
 --name-font-color: blue;
 --country-font-color: yellow;
 }
 </style>
  18. $ npm install -g polyserve $ cd seed‐element $ polyserve

    Starting Polyserve on port 8000 Serving components from bower_components
  19. <html>
 <head>
 <meta charset="utf-8">
 <script src="../../webcomponents-lite.js"></ script>
 <script src=“../../web-component-tester/ browser.js"></script>


    </head>
 <body>
 <script>
 // Load and run all tests (.html, .js):
 WCT.loadSuites([
 'basic-test.html'
 ]);
 </script>
 </body>
  20. $ vulcanize ‐-inline-css -‐inline‐scripts ‐‐strip‐ comments index.html | polyclean |

    crisper -‐html index.build.html -‐-js index.build.js $ npm install ‐g polybuild $ polybuild index.html
  21. Polymer Starter Kit A starting point for building web applications

    with Polymer developers.google.com/web/tools/polymer-starter-kit
  22. first-class components end-to-end build process application theming responsive app layout

    offline-first (optional) recipes for ES2015 support Polymer performance
  23. Components for nearly any app, out of the box Complete

    build chain for bringing your app to production Flexiable app theming using custom properties Responsive app layout boilerplate & routing Material Design ready