-
Notifications
You must be signed in to change notification settings - Fork 1
Upgrade Guide for Polymer Elements from 1.x to 2.0
This guide does not replace the Polymer Upgrade Guide but tries to extend it or highlight some parts. It is based on my experiences with upgrading the Polymer-based webcomponents flexible-rating and mammooc-rating-widget.
Some dependencies have the same version number 2.0 for Polymer 2.0, but others have different ones (especially the web-component-tester
or the iron-component-page
). Check these!
Before:
<style is="custom-stlye">
<!-- ... -->
</style>
After:
<custom-style>
<style>
<!-- ... -->
</style>
</custom-style>
Before:
<script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
After:
<script src="../../webcomponentsjs/webcomponents-loader.js"></script>
If you use additional components, such as <dom-repeat>
, <dom-if>
, <dom-module>
, <dom-bind>
or <custom-style>
, you must include these separatly.
Before:
<link rel="import" href="../../polymer/polymer.html">
After:
<link rel="import" href="../polymer/polymer-element.html">
<!-- Additional imports -->
<link rel="import" href="../polymer/lib/elements/dom-repeat.html">
Before:
:host:focus {
outline-color: red;
}
After:
:host(:focus) {
outline-color: red;
}
Before:
:host:not(:disabled) .star-wrapper:hover ::content > span {
cursor: pointer;
}
After:
:host(:not([disabled])) .star-wrapper:hover > span {
cursor: pointer;
}
Before:
@apply(--paper-font-headline);
After:
@apply --paper-font-headline;
After:
<link rel="import" href="../shadycss/apply-shim.html">
Before:
<script>
<!-- ... -->
</script>
After:
<script>
"use strict";
<!-- ... -->
</script>
Before:
is: 'flexible-rating',
After:
static get is() {
return 'flexible-rating';
}
The behaviors are now included at the very begining, when the class is declared. Choose a custom name for your class (ususally simiar to is()
, but with CamelCase)!
Before:
behaviors:
[
Polymer.IronFormElementBehavior,
Polymer.IronValidatableBehavior,
Polymer.IronA11yKeysBehavior,
Polymer.IronControlState
],
After:
class FlexibleRating extends Polymer.mixinBehaviors([
Polymer.IronFormElementBehavior,
Polymer.IronValidatableBehavior,
Polymer.IronA11yKeysBehavior,
Polymer.IronControlState
], Polymer.Element) {
Before:
properties: {
/**
* The maximum number of stars possible.
*/
max: {
type: Number,
value: 5,
notify: true
}
}
After:
static get properties() {
return {
/**
* The maximum number of stars possible.
*/
max: {
type: Number,
value: 5,
notify: true
}
}
}
Before:
observers: [
'_updateAria(value, max)'
]
After:
static get observers() {
return [
'_updateAria(value, max)'
]
}
Before:
keyBindings: {
'left down pagedown home': '_decrementKey',
'right up pageup end': '_incrementKey'
}
After:
static get keyBindings() {
return {
'left down pagedown home': '_decrementKey',
'right up pageup end': '_incrementKey'
}
}
Before:
_increment: function() {
this._updateValue(Math.floor(this.value) + 1);
},
After:
_increment() {
this._updateValue(Math.floor(this.value) + 1);
}
Before:
hostAttributes: {
role: 'slider',
tabindex: 0,
'aria-hidden': 'true'
},
After:
ready() {
this._ensureAttribute('role', 'slider');
this._ensureAttribute('tabindex', 0);
this._ensureAttribute('area-hidden', 'true');
super.ready();
}
Before:
listeners: {
'api-load': '_apiLoaded',
'post-sent': '_postSent'
},
After:
ready() {
this.addEventListener('api-load', this._apiLoaded.bind(this));
this.addEventListener('post-sent', this._postSent.bind(this));
super.ready()
}
This is added to the class
definition. It could be empty or call super:
After:
ready() {
super.ready();
}
This registration is done outside of the class definition because functions of the class are used.
After:
<!-- ... -->
// Register custom element definition using standard platform API
customElements.define(FlexibleRating.is, FlexibleRating);
</script>
Before:
Polymer.dom(editableFixture.root).querySelectorAll('.star-wrapper > span');
After:
editableFixture.root.querySelectorAll('.star-wrapper > span');
Before:
MammoocLocalizeBehaviorImpl = {
properties: {
language: {
// ...
}
}
}
MammoocLocalizeBehavior = [Polymer.AppLocalizeBehavior, MammoocLocalizeBehaviorImpl];
After:
class MammoocLocalizeBehaviorImpl {
static get properties() {
return {
language: {
// ...
}
}
}
}
MammoocLocalizeBehavior = [Polymer.AppLocalizeBehavior, MammoocLocalizeBehaviorImpl];