Show Menu
Cheatography

Polymer 2.0 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

build custom element

Polymer 1.x
Polyme­r({})
Polymer 2.0
class MyElement extends Polyme­r.E­lement {}

API

this.s­had­owRoot
get shadow DOM
this.$.co­ntainer
access identifier #container
<te­mplate is="­dom­-bi­nd">
use Polymer bindings without defining a new custom element
$$(sel­ector)
Returns the first node in this element's local DOM that matches selector

Instance Methods

fire(type, [detail], [options])
Fires a custom event
async(­method, [wait])
Calls method asynch­ron­ously
deboun­ce(­job­Name, callback, [wait])
Call debounce to collapse multiple requests for a named task into one invocation
toggle­Att­rib­ute­(name, bool, [node])
toggles the named boolean attribute
this.t­ran­sfo­rm(­'ro­tat­eX(­90d­eg)', this.$.myDiv)
Applies a CSS transform to the specified node, or host element if no node is specified
resolv­eUr­l(url)
returns a path relative to the current document

property list for polymer base

Polyme­r.T­emp­latizer behavior

// Get a template from somewhere, e.g. light DOM
var template = Polymer.dom(this).querySelector('template');
// Prepare the template
this.templatize(template);
// Instance the template with an initial data model
var instance = this.stamp({myProp: 'initial'});
// Insert the instance's DOM somewhere, e.g. light DOM
Polymer.dom(this).appendChild(instance.root);
// Changing a property on the instance will propagate to bindings
// in the template
instance.myProp = 'new value';
The Polyme­r.T­emp­latizer behavior adds methods to generate instances of templates that are each managed by an anonymous Polyme­r.Base instance.

https:­//w­ww.p­ol­yme­r-p­roj­ect.or­g/1.0/­doc­s/a­pi/­Pol­yme­r.T­emp­latizer
 

Lifecycle

created
ready
attached
detached
attrib­ute­Changed

Property name to attribute name mapping

Attribute names are converted to lowercase property names
Attribute names with dashes are converted to camelCase property names

Custom CSS properties

.title {
    color: var(--my-toolbar-title-color);
 }
value of the property will inherit down to the toolbar where it is used if defined

https:­//w­ww.p­ol­yme­r-p­roj­ect.or­g/1.0/­doc­s/d­evg­uid­e/s­tyling

Declared properties

computed
The value is interp­reted as a method name and argument list
observer
The value is interp­reted as a method name to be invoked when the property value changes

Config­uring default property values

Polymer({

  is: 'x-custom',

  properties: {

    mode: {
      type: String,
      value: 'auto'
    },

    data: {
      type: Object,
      notify: true,
      value: function() { return {}; }
    }

  }

});

Style

:host ::content div
must have a selector to the left of the ::content pseudo­-el­ement
--my-t­ool­bar­-ti­tle­-color
These custom properties can be defined and will propogate down the composed DOM tree
color: var(--­my-­too­lba­r-t­itl­e-c­olor, blue)
include a default value in the var() function

Custom CSS mixins

// example
<dom-module id="my-toolbar">

  <template>

    <style>
      :host {
        padding: 4px;
        background-color: gray;
        / apply a mixin /
        @apply(--my-toolbar-theme);
      }
      .title {
        @apply(--my-toolbar-title-theme);
      }
    </style>

    <span class="title">{{title}}</span>

  </template>

  ...

</dom-module>

// example usage 
<dom-module id="my-element">

  <template>

    <style>
      / Apply custom theme to toolbars /
      :host {
        --my-toolbar-theme: {
          background-color: green;
          border-radius: 4px;
          border: 1px solid gray;
        };
        --my-toolbar-title-theme: {
          color: green;
        };
      }

      / Make only toolbars with the .warning class red and bold /
      .warning {
        --my-toolbar-title-theme: {
          color: red;
          font-weight: bold;
        };
      }
    </style>

    <my-toolbar title="This one is green."></my-toolbar>
    <my-toolbar title="This one is green too."></my-toolbar>

    <my-toolbar class="warning" title="This one is red."></my-toolbar>

  </template>

  <script>
    Polymer({ is: 'my-element'});
  </script>

</dom-module>