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

Componentize your app with Polymer

Componentize your app with Polymer

Building components that can stand the test of time is tough! Frameworks change, new fads emerge, and often we're left with an aging codebase that needs to be rewritten if we want to stay current. Polymer and Web Components aim to change this cycle by building on new platform primitives which will allow us to finally create interoperable components that work in any environment. In this session we'll look at the current Web Component ecosystem, and I'll show you how to start to integrate these tools into your projects.

Rob Dodson

June 26, 2015
Tweet

More Decks by Rob Dodson

Other Decks in Technology

Transcript

  1. when presented with an abundance of choice, you can feel

    increasingly unsure of whether yours is the right one - Addy Osmani
  2. <dom-module id=“quick-alert"> <style> .alert { background: green; } </style> <template>

    <div class=“alert”>Alert!!</div> </template> </dom-module>
  3. Polymer({ is: ‘name-tag’, properties: { first: String, last: String, age:

    { type: Number, value: 42, observer: ‘_ageChanged' } } }); Observer function
  4. Polymer({ is: ‘name-tag’, properties: { first: String, last: String, age:

    { type: Number, value: 42, observer: ‘_ageChanged’, notify: true } } }); Notify event / bindings
  5. Polymer({ is: ‘name-tag’, properties: { first: String, last: String, fullname:

    { type: String, computed: ‘_computeFullName(first, last)' } } }); Computed property
  6. <dom-module id=“quick-alert"> <style> .alert { background: green; } </style> <template>

    <div class=“alert”>Alert!!</div> </template> </dom-module>
  7. <dom-module id=“quick-alert"> <style> .alert { background: var(--quick-alert-background); } </style> <template>

    <div class=“alert”>Alert!!</div> </template> </dom-module> A custom property
  8. <dom-module id=“quick-alert"> <style> .alert { background: var(--quick-alert-background, green); } </style>

    <template> <div class=“alert”>Alert!!</div> </template> </dom-module>
  9. <dom-module id=“quick-alert"> <style> .alert { background: green; @apply(--quick-alert-theme) } </style>

    <template> <div class=“alert”>Alert!!</div> </template> </dom-module>
  10. <!-- my-theme.html --> <style is="custom-style"> quick-alert { --quick-alert-theme: { background:

    linear-gradient(135deg, #3D7EAA, #FFE47A); font-size: 24px; text-shadow: 0px 3px 0px #909090, 0px 5px 10px rgba(0, 0, 0, 0.6); }; } </style>
  11. App-wide Theming #303f9f CSS custom properties --dark-primary-color --light-primary-color --accent-color --primary-text-color

    #303f9f --dark-primary-color --light-primary-color --accent-color --primary-text-color
  12. // While there is only one cache in this example,

    the same logic will handle the case where // there are multiple versioned caches. var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) { return CURRENT_CACHES[key]; }); event.waitUntil( caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.map(function(cacheName) { if (expectedCacheNames.indexOf(cacheName) == -1) { // If this cache name isn't present in the array of "expected" cache names, then delete it. console.log('Deleting out of date cache:', cacheName); return caches.delete(cacheName); } }) ); }) ); }); // This sample illustrates an aggressive approach to caching, in which every valid response is
  13. Service Worker caching Take your app offline with ease <link

    rel="import" href="platinum-sw-elements.html"> <platinum-sw> <platinum-sw-cache default-cache-strategy="networkFirst" precache='["image.jpg", "results.json", "page.html"]'> </platinum-sw-cache> </platinum-sw>
  14. Push Notifications Stay informed of messages from your app <platinum-push-messaging

    title="You have new stories" message="4 RSS feeds have new stories" icon-url="rss.png" click-url=“feeds/new/“> </platinum-push-messaging>
  15. index.html <body> … <template is="dom-bind" id="app"> <paper-drawer-panel id="paperDrawerPanel"> <div drawer>

    <!-- Drawer Toolbar --> <paper-toolbar id="drawerToolbar"> <span class="paper-font-title">Menu</span> </paper-toolbar> … </template> Delete this element and move the content to your own Angular template
  16. my-app.ts @Component({ selector: 'my-app' }) @View({ templateUrl: 'views/my-app/my-app.html' }) //

    Component controller export class MyAppComponent { } bootstrap(MyAppComponent);
  17. my-app.html <paper-drawer-panel id="paperDrawerPanel"> <div drawer> <!-- Drawer Toolbar --> <paper-toolbar

    id="drawerToolbar"> <span class="paper-font-title">Menu</span> </paper-toolbar> <!-- Drawer Content --> <paper-menu class="list"> <a data-route="home" href="/"> <iron-icon icon="home"></iron-icon> <span>Home</span> </a> …
  18. my-app.ts // Component controller export class MyAppComponent { page('/', ()

    => { this.route = 'home'; }); page('/users', () => { this.route = 'users'; }); page('/contact', () => { this.route = 'contact'; }); // add #! before urls page({ hashbang: true }); }
  19. index.html <head> ... <script src=“bower_components/webcomponentsjs/webcomponents.js"> </script> <script> // Tell Polymer

    to use Shadow DOM var Polymer = Polymer || {}; Polymer.dom = 'shadow'; </script> ... </head>
  20. my-app.ts import {Component, View, bootstrap, ElementRef} from 'angular2/angular2'; constructor(elementRef: ElementRef)

    { // Access DOM with elementRef.domElement } Access the DOM for this component
  21. my-app.ts // Show toast when Service Worker registration completes var

    el = elementRef.domElement; var sw = el.querySelector('platinum-sw-register'); var toast = el.querySelector('#caching-complete'); sw.addEventListener('service-worker-installed', function() { toast.show(); });
  22. Components for nearly any app, out of the box. Complete

    build chain for bringing your app to production.
  23. Components for nearly any app, out of the box. Complete

    build chain for bringing your app to production. Flexible app theming using custom properties
  24. Components for nearly any app, out of the box. Complete

    build chain for bringing your app to production. Flexible app theming using custom properties Responsive app layout boilerplate & routing
  25. Components for nearly any app, out of the box. Complete

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