Soooo Polymer is...


A framework?

UI widgets?

It looks like you're building a web page on the internet!


I give my children a default UI!

<select id="schwag">
  <option disabled>Medium</option>
  <option disabled>Large</option>
  <option selected>XX-large</option>

Elements are configured using attributes, not script.

<select size="4" multiple>

Attributes even change how users interact with me

  <optgroup label="German Cars">

Attributes can effect my UI.


I'm picky! I only allow accept certain elements as children.

  <select name="size">
    <option value="s">Small</option>
    <option value="m">Medium</option>
    <option value="l">Large</option>

In other contexts, I take on additional responsibilities.

My DOM exposes properties/methods to JS that don't make sense as markup. I also fire events when interesting things happen.

Where are we today?

Gut check: building a tab component on the web...

Pile on loads of JS

Our markup is atrocious

What a nightmare!

This is how we build web apps!

We're far away from where we started

Markup can be meaningful again

  <hangout-chat from="Paul, Addy" profile="118075919496626375791">
      <hangout-message from="Paul" profile="profile.png" datetime="2013-07-17T12:02">
        <p>Feelin' this Web Components thing.</p>
        <p>Heard of it?</p>
      <hangout-message from="Addy" datetime="2013-07-17T12:12">...</hangout-message>




HTML Imports

Custom Elements

Shadow DOM

What is it?

Polyfill libraries

Shadow DOM, Custom Elements, HTML Imports, Pointer Events...

Sugaring layer

expresses opinionated way to use web component concepts together

UI Components

comprehensive set (in progress)

Philosophy & Goals

Everything as an element

HTML is cool. DOM feels good.

Eliminate boilerplate

Remove tediousness of building web component-based apps

Utilize the modern web platform

Support modern browsers



Everything is an element.

Everything is an element

AJAX...using DOM

<script src="polymer.min.js"></script>
<link rel="import" href="polymer-ajax.html">
<polymer-ajax url="http://gdata.youtube.com/feeds/api/videos/"
var ajax = document.querySelector('polymer-ajax');
ajax.addEventListener('polymer-response', function(e) {
Hit run...

Everything is an element

flexbox...using DOM

<script src="polymer.min.js"></script>
<link rel="import" href="polymer-flex-layout.html">
<polymer-flex-layout vertical iscontainer>
  <div flex>Body</div>

Polymer elements

non-visual utility elements








Services / libs









Behavior / interaction



Polymer UI elements

visual elements




<polymer-ui-card> demo

<polymer-ui-sidebar-menu> demo

<polymer-ui-tabs> demo

<polymer-ui-toggle-button> demo



Reusability...the non-visual polymer-elements are used to implement these!



Eliminate boilerplate.

Polymer features

declarative web components

  • Declarative element registration: <polymer-element>
  • Declarative inheritance: <polymer-element extends="...">
  • Declarative two-way data-binding: <input id="input" value="{{foo}}">
  • Declarative event handlers: <button on-click="{{handleClick}}">
  • Published properties: xFoo.bar = 5 <-> <x-foo bar="5">
  • Property change watchers: barChanged: function() {...}
  • Automatic node finding: this.$.input.value = 5
  • PointerEvents / PointerGestures by default
Be declarative. Write less code.

Custom elements without Polymer :(

<template id="template">
  <style>input { color: orange; }</style>
  <input type="text">

var proto = Object.create(HTMLElement.prototype, {
  createdCallback: {
    value: function() {
      var t = document.querySelector('#template');
      this.createShadowRoot().appendChild(document.importNode(t.content, true));

var MyInput = document.register('my-input', {prototype: proto});

Custom elements with Polymer :)

declarative custom elements

<script src="polymer.min.js"></script>
  1. Create an element definition

    <polymer-element name="my-input" constructor="MyInput" noscript>
      <!-- Note: Polymer creates Shadow DOM from the first <template>. -->
        <style>input { color: orange; }</style>
        <input type="text">

  2. Instantiate - declare it, create DOM, or use new in JS

    // var myInput = document.createElement('my-input');
    // var myInput = new MyInput();

Define an API

complex elements require more juice...

  • Properties/methods are added to prototype
  • this refers to the element itself (e.g. this.localName == "my-input")
  • Can reference external scripts/stylesheets (e.g. CSP friendly)

Publishing properties & data-binding

  1. Inside the element → use data-binding
  2. Outside the element → users configure us using attributes

Features in action

responsive design...using DOM

<script src="polymer.min.js"></script>
<link rel="import" href="polymer-media-query.html">
<polymer-element name="responsive-layout" attributes="responsive">
    <polymer-media-query query="max-width:640px" queryMatches="{{isPhone}}"></...
    <template if="{{isPhone && responsive}}"> <!-- Phone markup -->
    <template if="{{!responsive}}"> <!-- Non-responsive case -->
  <script>Polymer('responsive-layout', {responsive: false});</script>
<responsive-layout responsive>

Polymer is many things!

A web app is a large collection of web components composed of many subelements, known as custom elements.