| OLD | NEW |
| (Empty) | |
| 1 # Polymer |
| 2 |
| 3 [](https
://travis-ci.org/Polymer/polymer) |
| 4 |
| 5 Polymer lets you build encapsulated, reusable elements that work just like stand
ard HTML elements, to use in building web applications. |
| 6 |
| 7 ```html |
| 8 <!-- Polyfill Web Components for older browsers --> |
| 9 <script src="webcomponentsjs/webcomponents-lite.js"></script> |
| 10 |
| 11 <!-- Import element --> |
| 12 <link rel="import" href="google-map.html"> |
| 13 |
| 14 <!-- Use element --> |
| 15 <google-map latitude="37.790" longitude="-122.390"></google-map> |
| 16 ``` |
| 17 |
| 18 Check out [polymer-project.org](https://www.polymer-project.org) for all of the
library documentation, including getting started guides, tutorials, developer re
ference, and more. |
| 19 |
| 20 Or if you'd just like to download the library, check out our [releases page](htt
ps://github.com/polymer/polymer/releases). |
| 21 |
| 22 ## Polymer 2.0 now at Release Candidate! |
| 23 Polymer 2.0 is now at Release Candidate stage, and will be the future focus of P
olymer development going forward. We intend to keep the 2.x public API stable b
arring critical feedback or issues during the release candidate period. For bac
kground and migration information on the 2.x see the [2.0 documentation](https:/
/www.polymer-project.org/2.0/docs/about_20) on the website or the [2.0 section b
elow](#release-candidate), and we welcome your feedback via [issues](https://git
hub.com/Polymer/polymer/issues/new) or [Slack](https://polymer-slack.herokuapp.c
om/). |
| 24 |
| 25 **To evaluate Polymer 2.0**, please point your bower at the latest `2.0.0-rc.x`
tag for polymer, and be sure load to the `webcomponentsjs/webcomponents-lite.js`
or `webcomponentsjs/webcomponents-loader.js` polyfills from the latest `v1.0.0-
rc.x` tag of [`webcomponentsjs`](https://github.com/webcomponents/webcomponentsj
s) |
| 26 |
| 27 👀 **Looking for Polymer v1.x?** Please see the [the v1 branch](https://github.co
m/Polymer/polymer/tree/1.x) |
| 28 |
| 29 ## Overview |
| 30 |
| 31 Polymer is a lightweight library built on top of the web standards-based [Web Co
mponents](http://webcomponents.org/) API's, and makes it easier to build your ve
ry own custom HTML elements. Creating reusable custom elements - and using eleme
nts built by others - can make building complex web applications easier and more
efficient. By being based on the Web Components API's built in the browser (or
[polyfilled](https://github.com/webcomponents/webcomponentsjs) where needed), Po
lymer elements are interoperable at the browser level, and can be used with othe
r frameworks or libraries that work with modern browsers. |
| 32 |
| 33 Among many ways to leverage custom elements, they can be particularly useful for
building reusable UI components. Instead of continually re-building a specific
navigation bar or button in different frameworks and for different projects, you
can define this element once using Polymer, and then reuse it throughout your p
roject or in any future project. |
| 34 |
| 35 Polymer provides a declarative syntax to easily create your own custom elements,
using all standard web technologies - define the structure of the element with
HTML, style it with CSS, and add interactions to the element with JavaScript. |
| 36 |
| 37 Polymer also provides optional two-way data-binding, meaning: |
| 38 |
| 39 1. When properties in the model for an element get updated, the element can upda
te itself in response. |
| 40 2. When the element is updated internally, the changes can be propagated back to
the model. |
| 41 |
| 42 Polymer is designed to be flexible, lightweight, and close to the web platform -
the library doesn't invent complex new abstractions and magic, but uses the bes
t features of the web platform in straightforward ways to simply sugar the creat
ion of custom elements. |
| 43 |
| 44 In addition to the Polymer library for building your own custom elements, the Po
lymer project includes a collection of [pre-built elements](https://elements.pol
ymer-project.org) that you can drop on a page and use immediately, or use as st
arting points for your own custom elements. |
| 45 |
| 46 ## Polymer in 1 Minute |
| 47 |
| 48 Polymer adds convenient features to make it easy to build complex elements: |
| 49 |
| 50 **Basic custom element without Polymer:** |
| 51 |
| 52 ```js |
| 53 // Standard custom element that Extends HTMLElement |
| 54 class MyElement extends HTMLElement { |
| 55 constructor() { |
| 56 super(); |
| 57 console.log('my-element was created!'); |
| 58 } |
| 59 } |
| 60 |
| 61 // Register custom element class with browser |
| 62 customElements.define('my-element', MyElement); |
| 63 ``` |
| 64 |
| 65 ```html |
| 66 <!-- use the element --> |
| 67 <my-element></my-element> |
| 68 ``` |
| 69 |
| 70 **Custom element using Polymer** |
| 71 |
| 72 ```html |
| 73 <!-- Define template that your element will use --> |
| 74 <dom-module id="my-simple-namecard"> |
| 75 <template> |
| 76 <div> |
| 77 Hi! My name is <span>Jane</span> |
| 78 </div> |
| 79 </template> |
| 80 </dom-module> |
| 81 ``` |
| 82 |
| 83 ```js |
| 84 // Custom element that extends Polymer base class |
| 85 class MySimpleNamecard extends Polymer.Element { |
| 86 |
| 87 // Stamp template from this dom-module into element's shadow DOM: |
| 88 static get is() { return 'my-simple-namecard'; } |
| 89 |
| 90 } |
| 91 |
| 92 // Register custom element class with browser |
| 93 customElements.define(MySimpleNamecard.is, MySimpleNamecard); |
| 94 ``` |
| 95 |
| 96 **Configure properties on your element...** |
| 97 |
| 98 ```js |
| 99 // Create an element that takes a property |
| 100 class MyPropertyNamecard extends Polymer.Element { |
| 101 |
| 102 static get is() { return 'my-property-namecard'; } |
| 103 |
| 104 // Define property/attribute API: |
| 105 static get properties() { |
| 106 return { |
| 107 myName: { |
| 108 type: String, |
| 109 observer: 'myNameChanged' |
| 110 } |
| 111 }; |
| 112 } |
| 113 |
| 114 myNameChanged(myName) { |
| 115 this.textContent = 'Hi! My name is ' + myName; |
| 116 } |
| 117 |
| 118 } |
| 119 |
| 120 customElements.define(MyPropertyNamecard.is, MyPropertyNamecard); |
| 121 ``` |
| 122 |
| 123 **...and have them set using declarative attributes** |
| 124 |
| 125 ```html |
| 126 <!-- using the element --> |
| 127 <my-property-namecard my-name="Jim"></my-property-namecard> |
| 128 ``` |
| 129 |
| 130 > Hi! My name is Jim |
| 131 |
| 132 **Bind data into your element using the familiar mustache-syntax** |
| 133 |
| 134 ```html |
| 135 <!-- Define template with bindings --> |
| 136 <dom-module id="my-bound-namecard"> |
| 137 <template> |
| 138 <div> |
| 139 Hi! My name is <span>[[myName]]</span> |
| 140 </div> |
| 141 </template> |
| 142 </dom-module> |
| 143 ``` |
| 144 ```js |
| 145 class MyBoundNamecard extends Polymer.Element { |
| 146 |
| 147 static get is() { return 'my-bound-namecard'; } |
| 148 |
| 149 static get properties() { |
| 150 return { |
| 151 myName: String |
| 152 }; |
| 153 } |
| 154 |
| 155 } |
| 156 |
| 157 customElements.define(MyBoundNamecard.is, MyBoundNamecard); |
| 158 ``` |
| 159 |
| 160 ```html |
| 161 <!-- using the element --> |
| 162 <my-bound-namecard my-name="Josh"></my-bound-namecard> |
| 163 ``` |
| 164 |
| 165 > Hi! My name is Josh |
| 166 |
| 167 **Style the internals of your element, without the style leaking out** |
| 168 |
| 169 ```html |
| 170 <!-- Add style to your element --> |
| 171 <dom-module id="my-styled-namecard"> |
| 172 <template> |
| 173 <style> |
| 174 /* This would be crazy without webcomponents, but with shadow DOM */ |
| 175 /* it only applies to this element's private "shadow DOM" */ |
| 176 span { |
| 177 font-weight: bold; |
| 178 } |
| 179 </style> |
| 180 |
| 181 <div> |
| 182 Hi! My name is <span>{{myName}}</span> |
| 183 </div> |
| 184 </template> |
| 185 </dom-module> |
| 186 ``` |
| 187 ```js |
| 188 class MyStyledNamecard extends Polymer.Element { |
| 189 |
| 190 static get is() { return 'my-styled-namecard'; } |
| 191 |
| 192 static get properties() { |
| 193 return { |
| 194 myName: String |
| 195 }; |
| 196 } |
| 197 |
| 198 } |
| 199 |
| 200 customElements.define(MyStyledNamecard.is, MyStyledNamecard); |
| 201 ``` |
| 202 ```html |
| 203 <!-- using the element --> |
| 204 <my-styled-namecard my-name="Jesse"></my-styled-namecard> |
| 205 ``` |
| 206 |
| 207 > Hi! My name is **Jesse** |
| 208 |
| 209 **and so much more!** |
| 210 |
| 211 Web components are an incredibly powerful new set of primitives baked into the w
eb platform, and open up a whole new world of possibility when it comes to compo
nentizing front-end code and easily creating powerful, immersive, app-like exper
iences on the web. |
| 212 |
| 213 By being based on Web Components, elements built with Polymer are: |
| 214 |
| 215 * Built from the platform up |
| 216 * Self-contained |
| 217 * Don't require an overarching framework - are interoperable across frameworks |
| 218 * Re-usable |
| 219 |
| 220 ## Contributing |
| 221 |
| 222 The Polymer team loves contributions from the community! Take a look at our [con
tributing guide](CONTRIBUTING.md) for more information on how to contribute. |
| 223 |
| 224 ## Communicating with the Polymer team |
| 225 |
| 226 Beyond Github, we try to have a variety of different lines of communication avai
lable: |
| 227 |
| 228 * [Blog](https://blog.polymer-project.org/) |
| 229 * [Twitter](https://twitter.com/polymer) |
| 230 * [Google+ community](https://plus.google.com/communities/115626364525706131031) |
| 231 * [Mailing list](https://groups.google.com/forum/#!forum/polymer-dev) |
| 232 * [Slack channel](https://bit.ly/polymerslack) |
| 233 |
| 234 # License |
| 235 |
| 236 The Polymer library uses a BSD-like license that is available [here](./LICENSE.t
xt) |
| 237 |
| 238 ----------- |
| 239 |
| 240 <a name="release-candidate"></a> |
| 241 # Polymer 2.0 (release candidate) |
| 242 |
| 243 Polymer 2.0 is a major new release of Polymer that is compatible with the latest
web components standards and web platform APIs, and makes significant improveme
nts over the 1.x version of the library. The following section provides context
and migration information for existing users of Polymer 1.x: |
| 244 |
| 245 ## Goals of Polymer 2.0 |
| 246 |
| 247 1. **Take advantage of native "v1" Web Components implementations across browser
s.** |
| 248 |
| 249 The primary goal of the Polymer 2.0 release is to take advantage of native, c
ross-browser support for Web Components. |
| 250 |
| 251 Polymer 1.x is built on top of the so-called "v0" Web Components specs, which
are supported natively only in Google Chrome; using Polymer in other browsers h
as always required the use of polyfills. |
| 252 |
| 253 Beginning this fall, multiple browsers will be shipping native implementation
s of the new "v1" specs for Shadow DOM and Custom Elements, yielding better web
components performance and reducing the need for polyfills. |
| 254 |
| 255 Polymer 2.0 features full support for the v1 specs, taking advantage of nativ
e browser implementations where they are available and depending on updated v1 p
olyfills from [webcomponentsjs](https://github.com/webcomponents/webcomponentsjs
) where necessary. |
| 256 |
| 257 Polymer 2.0 also embraces the new ES-class-based mechanism for defining custo
m elements, bringing idiomatic Polymer style closer to "vanilla" custom element
authoring. |
| 258 |
| 259 1. **Provide a smooth migration path from Polymer 1.x.** |
| 260 |
| 261 Our second major goal is to provide as easy a transition as possible for deve
lopers who have built elements and apps with Polymer 1.x, making Polymer 2.0 a s
turdy bridge to the future. |
| 262 |
| 263 To upgrade, you will need to make some changes to your 1.x-based elements and
apps. These changes are necessitated by both the v0-to-v1 spec transition and a
handful of key improvements in Polymer itself (see our remaining goals, below). |
| 264 |
| 265 However, we've taken care to limit the number of changes that are strictly re
quired and to ease the process of upgrading: |
| 266 |
| 267 * Polymer 2.0 introduces a new [ES6 class-based syntax](#20-es6-class-based-s
yntax), but we've provided a [lightweight compatibility layer](#10-compatibility
-layer) allowing you to upgrade your 1.x code with minimal modifications. Depend
ing on your needs, you can either take advantage of the compatibility layer or j
ump straight to idiomatic 2.0 style. |
| 268 |
| 269 * Before releasing Polymer 2.0, we'll also provide an upgrade tool to automat
e as many of the changes (both required and recommended) as possible. |
| 270 |
| 271 * Finally, we're working on guidelines for building and testing "hybrid" elem
ents that will run in both Polymer 1.x and Polymer 2.0. We plan to ship hybrid v
ersions of all of the elements that we provide, easing the transition for develo
pers who use them. Third-party element providers may also choose to ship hybrid
elements. |
| 272 |
| 273 * If you have an especially large app or constraints that don't allow for an
all-at-once upgrade, you can also use hybrid elements to migrate your app from 1
.x to 2.0 in piecewise fashion: update your elements to hybrid form, individuall
y or in batches, while running against Polymer 1.x; then cut over to Polymer 2.0
when all of your elements have been updated. |
| 274 |
| 275 1. **Eliminate leaky abstractions.** |
| 276 |
| 277 Seamless interoperability is one of Web Components' major selling points. Gen
erally speaking, web components "just work" anywhere you use HTML elements. To u
se them, you need only be aware of their public attributes, properties, methods
and events; you don't need to know anything about their inner workings. This mea
ns you can easily mix standard HTML elements, third-party elements and elements
you've defined yourself. |
| 278 |
| 279 Unfortunately, there are a couple of cases in Polymer 1.x (the `Polymer.dom`
API and the `set`/`notifyPath` API) where implementation details of Polymer-base
d elements leak out, requiring users of the elements to interact with them in no
n-standard ways. These "leaks" were by design – compromises we chose to make in
the interest of performance – but in hindsight we aren't happy with the tradeoff
. |
| 280 |
| 281 In Polymer 2.0 we've found ways to eliminate these leaky abstractions without
unduly compromising performance, which means that your Polymer 2.x-based elemen
ts will be indistinguishable from "vanilla" elements from a consumer's point of
view (unless you leak implementation details of your own). |
| 282 |
| 283 1. **Make targeted improvements to the Polymer data system.** |
| 284 |
| 285 Based on developer feedback and observations of Polymer apps in the wild, we'
ve also made some key improvements to Polymer's data system. These changes are d
esigned to make it easier to reason about and debug the propagation of data thro
ugh and between elements: |
| 286 |
| 287 * Changes are now batched, and the effects of those changes are run in well-d
efined order. |
| 288 * We ensure that multi-property observers run exactly once per turn for any s
et of changes to dependencies (removing the [multi-property undefined rule](http
s://www.polymer-project.org/1.0/docs/devguide/observers#multi-property-observers
)). |
| 289 * To add compatibility with more approaches to state management, we now provi
de a mixin (and legacy behavior) to skip dirty-checking properties whose values
are objects or arrays and always consider them dirty, causing their side effects
to run. |
| 290 |
| 291 1. **Improve factoring of Polymer and the polyfills** |
| 292 |
| 293 We've done major refactoring of Polymer and the webcomponentsjs polyfills to
improve efficiency, utility and flexibility: |
| 294 |
| 295 * The "Shady DOM" shim that was part of Polymer 1.x has been factored out of
Polymer and added to the webcomponentsjs polyfills, along with the related shim
for CSS Custom Properties. (As noted above, the Shady DOM shim no longer exposes
an alternative API but instead patches the native DOM API for transparent usage
). |
| 296 |
| 297 * Polymer itself has been internally factored into several loosely coupled li
braries. |
| 298 |
| 299 * The new `Polymer.Element` class extends from the native `HTMLElement` and
mixes in functionality from these libraries. |
| 300 |
| 301 * The idiomatic way of using Polymer 2.0 (assuming you're not using the 1.x
compatibility layer) is to define your own custom elements that subclass `Polym
er.Element`, using standard ES class definition syntax. |
| 302 |
| 303 * If you're interested in using pieces of Polymer's functionality in _a la
carte_ fashion, you can try defining your own base element class, utilizing a su
bset of the libraries. For now, this use case should be considered experimental,
as the factoring of libraries is subject to change and is not part of the offic
ial Polymer 2.0 API. |
| 304 |
| 305 ## 1.0 Compatibility Layer |
| 306 Polymer 2.0 retains the existing `polymer/polymer.html` import that current Poly
mer 1.0 users can continue to import, which strives to provide a very minimally-
breaking change for code written to the Polymer 1.0 API. For the most part, exi
sting users upgrading to Polymer 2.0 will only need to adapt existing code to be
compliant with the V1 Shadow DOM API related to content distribution and stylin
g, as well as minor breaking changes introduced due to changes in the V1 Custom
Elements spec and data-layer improvements listed [below](#breaking-changes). |
| 307 |
| 308 ## 2.0 ES6 Class-based Syntax |
| 309 With the widespread adoption of ES6 in browsers, as well as the requirement that
V1 Custom Elements be defined as ES6 class extensions of `HTMLElement`, Polymer
2.0 shifts its primary API for defining new elements to an ES6 class-centric sy
ntax. Using this syntax, users will extend `Polymer.Element` (a subclass of `HT
MLElement`), which provides meta-programming for most of the same features of Po
lymer 1.0 based on static configuration data supplied on the class definition. |
| 310 |
| 311 Basic syntax looks like this: |
| 312 |
| 313 ```html |
| 314 <!-- Load the Polymer.Element base class --> |
| 315 <link rel="import" href="bower_components/polymer/polymer-element.html"> |
| 316 ``` |
| 317 |
| 318 ```js |
| 319 // Extend Polymer.Element base class |
| 320 class MyElement extends Polymer.Element { |
| 321 static get is() { return 'my-element'; } |
| 322 static get properties() { return { /* properties metadata */ } } |
| 323 static get observers() { return [ /* observer descriptors */ ] } |
| 324 constructor() { |
| 325 super(); |
| 326 ... |
| 327 } |
| 328 connectedCallback() { |
| 329 super.connectedCallback(); |
| 330 ... |
| 331 } |
| 332 ... |
| 333 } |
| 334 |
| 335 // Register custom element definition using standard platform API |
| 336 customElements.define(MyElement.is, MyElement); |
| 337 ``` |
| 338 |
| 339 Users can then leverage native subclassing support provided by ES6 to extend and
customize existing elements defined using ES6 syntax: |
| 340 |
| 341 ```js |
| 342 // Subclass existing element |
| 343 class MyElementSubclass extends MyElement { |
| 344 static get is() { return 'my-element-subclass'; } |
| 345 static get properties() { return { /* properties metadata */ } } |
| 346 static get observers() { return [ /* observer descriptors */ ] } |
| 347 constructor() { |
| 348 super(); |
| 349 ... |
| 350 } |
| 351 ... |
| 352 } |
| 353 |
| 354 // Register custom element definition using standard platform API |
| 355 customElements.define(MyElementSubclass.is, MyElementSubclass); |
| 356 ``` |
| 357 |
| 358 Below are the general steps for defining a custom element using this new syntax: |
| 359 |
| 360 * Extend from `Polymer.Element`. This class provides the minimal surface area to
integrate with 2.0's data binding system. It provides only standard custom elem
ent lifecycle with the addition of `ready`. (You can apply the `Polymer.LegacyEl
ementMixin` to get all of the Polymer 1.0 element API, but since most of this AP
I was rarely used, this should not often be needed.) |
| 361 * Implement "behaviors" as [mixins that return class expressions](http://justinf
agnani.com/2015/12/21/real-mixins-with-javascript-classes/) and apply to the bas
e class you are extending from. |
| 362 * Property metadata (`properties`) and multi-property/wildcard observers (`obser
vers`) should be put on the class as static getters, but otherwise match the 1.x
syntax. |
| 363 * In order to provide a template to stamp into the element's shadow DOM, either
define a static `is` getter that returns the id of a `dom-module` containing the
element's template, or else provide a static `template` getter that returns a t
emplate to stamp. The `template` getter may either return an HTMLTemplateElemen
t or a string containing HTML which will be parsed into a template. |
| 364 * `listeners` and `hostAttributes` have been removed from element metadata; list
eners and attributes should be installed using standard platform API (`this.addE
ventListener`, `this.setAttribute`) how and when needed (e.g. in `connectedCallb
ack`). For convenience `_ensureAttribute` is available that sets an attribute i
f and only if the element does not yet have that attribute, to match `hostAttrib
ute` semantics. |
| 365 |
| 366 Note that `Polymer.Element` provides a cleaner base class void of a lot of sugar
ed utility API that present on elements defined with `Polymer()`, such as `fire`
, `transform`, etc. With web platform surface area becoming far more stable acr
oss browsers, we intend to hew towards sugaring less and embracing the raw platf
orm API more. So when using `Polymer.Element`, instead of using the legacy `thi
s.fire('some-event')` API, simply use the equivalent platform API's such as `thi
s.dispatchEvent(new CustomEvent('some-event', {bubbles: true})`. #usetheplatfor
m |
| 367 |
| 368 See below for a visual guide on migrating Polymer 1.0's declarative syntax to th
e ES6 class syntax in Polymer 2.0: |
| 369 |
| 370  |
| 371 |
| 372 ## Polyfills |
| 373 |
| 374 Polymer 2.0 has been developed alongside and tested with a new suite of V1-spec
compatible polyfills for Custom Elements and Shadow DOM. Polymer 2.0 is compat
ible the latest releases of [`webcomponentsjs/webcomponents-lite.js`](https://gi
thub.com/webcomponents/webcomponentsjs), which is included as a bower dependency
to Polymer 2.x. |
| 375 |
| 376 ## Breaking Changes |
| 377 Below is a list of intentional breaking changes made in Polymer 2.0, along with
their rationale/justification and migration guidance. If you find changes that
broke existing code not documented here, please [file an issue](https://github.c
om/Polymer/polymer/issues/new) and we'll investigate to determine whether they a
re expected/intentional or not. |
| 378 |
| 379 |
| 380 ### Polymer.dom |
| 381 On browsers that lack native V1 Shadow DOM support, Polymer 2.0 is designed to b
e used with the new [V1 Shady DOM shim](https://github.com/webcomponents/shadydo
m), which patches native DOM API as necessary to be mostly equivalent to native
Shadow DOM. This removes the requirement to use the `Polymer.dom` API when inte
racting with the DOM. `Polymer.dom` can be eliminated for elements targeting Po
lymer 2.0, in favor of the native DOM API's. |
| 382 |
| 383 Note that `Polymer.dom` is still provided in the `polymer.html` backward-compati
bility layer which simply facades the native API, but usage of it in 2.0 can be
removed. Note that `Polymer.dom` will no longer return `Array`s for API's where
the platform returns e.g. `NodeList`'s, so code may need to be updated to avoid
direct use of array methods. |
| 384 |
| 385 ### V1 Shadow DOM |
| 386 Polymer 2.0 elements will stamp their templates into shadow roots created using
V1's `attachShadow({mode: 'open'})` by default. As such, user code related to s
coped styling, distribution, and events must be adapted to native V1 API. For a
great writeup on all Shadow DOM V1 spec changes, [see this writeup](http://haya
to.io/2016/shadowdomv1/). Required changes for V1 are summarized below: |
| 387 |
| 388 #### Distribution |
| 389 * <a name="breaking-slot"></a>`<content>` insertion points must be changed to `<
slot>` |
| 390 * <a name="breaking-slot-name"></a>Insertion points that selected content via `<
content select="...">` must be changed to named slots: `<slot name="...">` |
| 391 * <a name="breaking-slot-slot"></a>Selection of distributed content into named s
lots must use `slot="..."` rather than tag/class/attributes selected by `<conten
t>` |
| 392 * <a name="breaking-redistribution"></a>Re-distributing content by placing a `<s
lot>` into an element that itself has named slots requires placing a `name` attr
ibute on the `<slot>` to indicate what content _it_ selects from its host childr
en, and placing a `slot` attribute to indicate where its selected content should
be slotted into its parent |
| 393 * <a name="breaking-async-distribution"></a>In the V1 "Shady DOM" shim, initial
distribution of children into `<slot>` is asynchronous (microtask) to creating t
he `shadowRoot`, meaning distribution occurs after observers/`ready` (in Polymer
1.0's shim, initial distribution occurred before `ready`). In order to force d
istribution synchronously, call `ShadyDOM.flush()`. |
| 394 * <a name="breaking-observe-nodes-flush"></a>Calling `Polymer.dom.flush` no long
er results in callbacks registered with `Polymer.dom.observeNodes` being called.
Instead, the object returned from `Polymer.dom.observeNodes` now contains a `fl
ush` method which can be used to immediately call the registered callback if any
changes are pending. |
| 395 |
| 396 #### Scoped styling |
| 397 |
| 398 * <a name="breaking-styling-content"></a>`::content` CSS pseudo-selectors must b
e changed to `::slotted`, and may only target immediate children and use no desc
endant selectors |
| 399 * <a name="breaking-styling-host-context"></a>`:host-context()` pseudo-selectors
have been removed. These were primarily useful for writing bidi rules (e.g. `:h
ost-context([dir=rtl])`); these should be replaced with the [new `:dir(rtl)` sel
ector](https://developer.mozilla.org/en-US/docs/Web/CSS/:dir), which we plan to
polyfill in the [styling shim](https://github.com/webcomponents/shadycss) soon |
| 400 * <a name="breaking-deep"></a>The previously deprecated `/deep/` and `::shadow`
selectors have been completely removed from V1 native support and must not be us
ed (use [CSS custom properties](https://www.polymer-project.org/1.0/docs/devguid
e/styling#custom-css-properties) to customize styling instead) |
| 401 |
| 402 #### Scoped events |
| 403 |
| 404 * <a name="breaking-event-localTarget"></a>Code using `Polymer.dom(event).localT
arget` should change to the V1 standard API `event.target` |
| 405 * <a name="breaking-event-path"></a>Code using `Polymer.dom(event).path` (aka V0
`event.path`) should change to the V1 standard API `event.composedPath()` |
| 406 * <a name="breaking-event-rootTarget"></a>Code using `Polymer.dom(event).rootTar
get` (aka V0 `event.path[0]`) should change to the V1 standard API `event.compo
sedPath()[0]` |
| 407 |
| 408 ### V1 Custom Elements |
| 409 Polymer 2.0 elements will target the V1 Custom Elements API, which primarily cha
nges the "created" step to actually invoke the `class` constructor, imposes new
restrictions on what can be done in the `constructor` (previously `createdCallba
ck`), and introduces different callback names. |
| 410 |
| 411 * <a name="breaking-callbacks"></a>Changes to callback names: |
| 412 * When using `Polymer({...})` from the compatibility layer, all callbacks shou
ld use legacy Polymer API names (`created`, `attached`, `detached`, `attributeCh
anged`) |
| 413 * When extending from `Polymer.Element`, users should override the V1 standard
callback names and call `super()`: |
| 414 * `created` changes to `constructor` |
| 415 * `attached` changes to `connectedCallback` |
| 416 * `detached` changes to `disconnectedCallback` |
| 417 * `attributeChanged` changes to `attributeChangedCallback` |
| 418 * <a name="breaking-attributes"></a>The V1 Custom Elements spec forbids reading
attributes, children, or parent information from the DOM API in the `constructor
` (or `created` when using the legacy API). Likewise, attributes and children m
ay not be added in the `constructor`. Any such work must be deferred (e.g. unti
l `connectedCallback` or microtask/`setTimeout`/`requestAnimationFrame`). |
| 419 * <a name="breaking-is"></a>Polymer will no longer produce type-extension elemen
ts (aka `is="..."`). Although they are still included in the V1 Custom Elements
[spec](https://html.spec.whatwg.org/#custom-elements-customized-builtin-example)
and scheduled for implementation in Chrome, because Apple [has stated](https://
github.com/w3c/webcomponents/issues/509#issuecomment-233419167) it will not impl
ement `is`, we will not be encouraging its use to avoid indefinite reliance on t
he Custom Elements polyfill. Instead, a wrapper custom element can surround a na
tive element, e.g. `<a is="my-anchor">...</a>` could become `<my-anchor><a>...</
a></my-anchor>`. Users will need to change existing `is` elements where necessar
y. |
| 420 * <a name="breaking-templates"></a>All template type extensions provided by Poly
mer have now been changed to standard custom elements that take a `<template>` i
n their light dom, e.g. |
| 421 |
| 422 ```html |
| 423 <template is="dom-repeat" items="{{items}}">...</template> |
| 424 ``` |
| 425 |
| 426 should change to |
| 427 |
| 428 ```html |
| 429 <dom-repeat items="{{items}}"> |
| 430 <template>...</template> |
| 431 </dom-repeat> |
| 432 ``` |
| 433 |
| 434 For the time being, Polymer (both legacy and class API) will automatically wra
p template extensions used in Polymer element templates during template processi
ng for backward-compatibility, although we may decide to remove this auto-wrappi
ng in the future. Templates used in the main document must be manually wrapped. |
| 435 * <a name="breaking-custom-style"></a>The `custom-style` element has also been c
hanged to a standard custom element that must wrap a style element e.g. |
| 436 |
| 437 ```html |
| 438 <style is="custom-style">...</style> |
| 439 ``` |
| 440 |
| 441 should change to |
| 442 |
| 443 ```html |
| 444 <custom-style> |
| 445 <style>...</style> |
| 446 </custom-style> |
| 447 ``` |
| 448 |
| 449 ### CSS Custom Property Shim |
| 450 Polymer 2.0 will continue to use a [shim](https://github.com/webcomponents/shady
css) to provide limited [CSS Custom Properties](#https://www.polymer-project.org
/1.0/docs/devguide/styling#custom-css-properties) support on browsers that do no
t yet natively support custom properties, to allow an element to expose a custom
styling API. The following changes have been made in the shim that Polymer 2.0
will use: |
| 451 |
| 452 * <a name="breaking-css-native"></a>The shim will now always use native CSS Cust
om Properties by default on browsers that implement them (this was opt-in in 1.0
). The shim will perform a one-time transformation of stylesheets containing [C
SS Custom Property mixins](https://www.polymer-project.org/1.0/docs/devguide/sty
ling#custom-css-mixins) to leverage individual native CSS properties where possi
ble for better performance. This introduces [some limitations](https://github.c
om/webcomponents/shadycss#custom-properties-and-apply) to be aware of. |
| 453 * <a name="breaking-css-syntax"></a>The following invalid styling syntax was pre
viously accepted by the 1.0 custom property shim. In order to support native CS
S Custom Properties, rules should be correct to use only natively valid syntax: |
| 454 * `:root {}` |
| 455 * Should be `:host > * {}` (in a shadow root) |
| 456 * Should be `html {}` (in main document) |
| 457 * Thus, cannot share old `:root` styles for use in both main document and sh
adow root |
| 458 * `var(--a, --b)` |
| 459 * Should be `var(--a, var(--b))` |
| 460 * `@apply(--foo)` |
| 461 * Should be `@apply --foo;` |
| 462 * <a name="breaking-customStyle"></a>`element.customStyle` as an object that can
be assigned to has been removed; use `element.updateStyles({...})` instead. |
| 463 * <a name="breaking-style-location"></a>`<style>` inside of a `<dom-module>`, bu
t outside of `<template>` is no longer supported |
| 464 |
| 465 ### Data system |
| 466 * <a name="breaking-data-init"></a>An element's template is not stamped & data s
ystem not initialized (observers, bindings, etc.) until the element has been con
nected to the main document. This is a direct result of the V1 changes that pre
vent reading attributes in the constructor. |
| 467 * <a name="breaking-data-batching"></a>Propagation of data through the binding s
ystem is now batched, such that multi-property computing functions and observers
run once with a set of coherent changes. Single property accessors still propa
gate data synchronously, although there is a new `setProperties({...})` API on P
olymer elements that can be used to propagate multiple values as a coherent set. |
| 468 * <a name="breaking-notify-order"></a>Property change notification event dispatc
h (`notify: true`) occurs after all other side effects of a property change occu
rs. In 1.x notification happened after binding side effects, but before observe
rs, which was counter-intuitive. This rationalizes the concept of upward notifi
cation to ensure it happens after _all_ local and downward side-effects based on
the change occur. Concretely, the order of effect processing in 2.x is as foll
ows: |
| 469 1. computed properties (`computed`) |
| 470 1. template bindings (both property bindngs `[[...]]` and computed bindings `[
[compute(...)]]`, including any side-effects on child elements for the bound pro
perty/attribute changes) |
| 471 1. attribute reflection (`reflectToAttribute: true`) |
| 472 1. observers (both single-property `observer` and multi-property `observers`) |
| 473 1. property-changed event notification (`notify: true`, including any side-eff
ects on host elements for the bound property changes) |
| 474 * <a name="breaking-method-args"></a>Multi-property observers, computed methods,
and computed bindings are now called once at initialization if any arguments ar
e defined (and will see `undefined` for any undefined arguments). As such, the
[1.x rule requiring all properties of a multi-property observer to be defined](h
ttps://www.polymer-project.org/1.0/docs/devguide/observers#multi-property-observ
ers) no longer applies, as this was a major source of confusion and unintended c
onsequences. Subsequently setting multi-property method arguments will cause th
e method to be called once for each property changed via accessors, or once per
batch of changes via `setProperties({...})`. |
| 475 * <a name="breaking-dynamic-functions"> Declaring a method name used as an obser
ver or computing function in the `properties` block causes the method property i
tself to become a dependency for any effects it is used in, meaning the effect f
or that method will run whenever the method is set, similar to 1.x. However, due
to removing the `undefined` rule noted above, in 2.x if such a method exists wh
en the element is created, it will run with initial values of arguments, even in
the case some or all arguments are `undefined`. |
| 476 * <a name="breaking-binding-notifications>‘notify’ events are no longer fired wh
en value changes _as result of binding from host_, as a major performance optimi
zation over 1.x behavior. Use cases such as `<my-el foo="{{bar}}" on-foo-change
d="fooChanged">` are no longer supported. In this case you should simply user a
`bar` observer in the host. Use cases such as dynamically adding a `property-c
hanged` event listener on for properties bound by an element's host by an actor
other than the host are no longer supported. |
| 477 * <a name="breaking-properties-deserialization"></a>In order for a property to b
e deserialized from its attribute, it must be declared in the `properties` metad
ata object |
| 478 * <a name="breaking-colleciton"></a>The `Polymer.Collection` and associated key-
based path and splice notification for arrays has been eliminated. See [explana
tion here](https://github.com/Polymer/polymer/pull/3970#issue-178203286) for mor
e details. |
| 479 * <a name="feature-mutable-data"></a>While not a breaking change, Polymer now sh
ips with a `Polymer.MutableData` mixin (and legacy `Polymer.MutableDataBehavior`
behavior) to provide more options for managing data. By default, `Polymer.Prop
ertyEffects` performs strict dirty checking on objects, which means that any dee
p modifications to an object or array will not be propagated unless "immutable"
data patterns are used (i.e. all object references from the root to the mutation
were changed). Polymer also provides a proprietary data mutation and path noti
fication API (e.g. `notifyPath`, `set`, and array mutation API's) that allow eff
icient mutation and notification of deep changes in an object graph to all eleme
nts bound to the same object graph. In cases where neither immutable patterns or
the data mutation API can be used, applying this mixin will cause Polymer to sk
ip dirty checking for objects and arrays and always consider them to be "dirty".
This allows a user to make a deep modification to a bound object graph, and th
en either simply re-set the object (e.g. `this.items = this.items`) or call `not
ifyPath` (e.g. `this.notifyPath('items')`) to update the tree. Note that all el
ements that wish to be updated based on deep mutations must apply this mixin or
otherwise skip strict dirty checking for objects/arrays. |
| 480 |
| 481 ### Removed API |
| 482 * <a name="breaking-instanceof"></a>`Polymer.instanceof` and `Polymer.isInstance
`: no longer needed, use |
| 483 `instanceof` and `instanceof Polymer.Element` instead. |
| 484 * <a name="breaking-dom-module"></a>`dom-module`: Removed ability to use `is` an
d `name` attribute to |
| 485 configure the module name. The only supported declarative way set the module |
| 486 id is to use `id`. |
| 487 * <a name="breaking-getPropertyInfo"></a>`element.getPropertyInfo`: This api ret
urned unexpected information some of the time and was rarely used. |
| 488 * <a name="breaking-getNativePrototype"></a>`element.getNativePrototype`: Remove
d because it is no longer needed for internal code and was unused by users. |
| 489 * <a name="breaking-beforeRegister"></a>`element.beforeRegister`: This was origi
nally added for metadata compatibility with ES6 classes. We now prefer users cre
ate ES6 classes by extending `Polymer.Element`, specifying metadata in the stati
c `properties`, `observers`, and `is` properties. For legacy use via `Polymer({.
..})`, dynamic effects may still be added by using `beforeRegister` but it is no
w equivalent to the `registered` lifecycle method. An element's `is` property ca
nnot be set in `beforeRegister` as it could in Polymer 1.x. |
| 490 * <a name="breaking-attributeFollows"></a>`element.attributeFollows`: Removed du
e to disuse. |
| 491 * <a name="breaking-classFollows"></a>`element.classFollows`: Removed due to dis
use. |
| 492 * <a name="breaking-copyOwnProperty"></a>`element.copyOwnProperty`: Removed due
to disuse. |
| 493 * <a name="breaking-listeners"></a>`listeners`: Removed ability to use `id.event
` to add listeners to elements in local dom. Use declarative template event hand
lers instead. |
| 494 * <a name="breaking-protected"></a>Methods starting with `_` are not guaranteed
to exist (most have been removed) |
| 495 |
| 496 ### Other |
| 497 * <a name="breaking-transpiling"></a>Polymer 2.0 uses ES2015 syntax, and can be
run without transpilation in current Chrome, Safari 10, Safari Technology Previe
w, Firefox, and Edge. Transpilation is required to run in IE11 and Safari 9 (as
well as Edge for high reliability, due to certain [bugs](https://github.com/Mic
rosoft/ChakraCore/issues/1496) in their ES6 implementation). The [`polymer-cli`
](https://github.com/Polymer/polymer-cli) tools such as `polymer serve` and `pol
ymer build` have built-in support for transpiling when needed. |
| 498 * <a name="breaking-deferred-attach"></a>The `attached` legacy callback is no lo
nger deferred until first render time; it now runs at the time of the native `co
nnectedCallback`, which may be before styles have resolved and measurement is po
ssible. Instead when measurement is needed use `Polymer.RenderStatus.beforeNext
Render`. |
| 499 * <a name="breaking-created-timing"></a>The legacy `created` callback is no long
er called before default values in `properties` have been set. As such, you sho
uld not rely on properties set in `created` from within `value` functions that d
efine property defaults. However, you can now set _any_ property defaults withi
n the `created` callback (in 1.0 this was forbidden for observed properties) in
lieu of using the `value` function in `properties`. |
| 500 * <a name="breaking-boolean-attribute-binidng"></a>Binding a default value of `f
alse` via an _attribute binding_ to a boolean property will not override a defau
lt `true` property of the target, due to the semantics of boolean attributes. I
n general, property binding should always be used when possible, and will avoid
such situations. |
| 501 * <a name="breaking-lazyRegister"></a>`lazyRegister` option removed and all meta
-programming (parsing template, creating accessors on prototype, etc.) is deferr
ed until the first instance of the element is created |
| 502 * <a name="breaking-hostAttributes-class"></a>In Polymer 1.x, the `class` attrib
ute was explicitly blacklisted from `hostAttributes` and never serialized. This
is no longer the case using the 2.0 legacy API. |
| 503 * <a name="breaking-url-changes"></a>In Polymer 1.x, URLs in attributes and styl
es inside element templates were re-written to be relative to the element HTMLIm
port. Based on user feedback, we are changing this behavior. |
| 504 |
| 505 Two new properties are being added to `Polymer.Element`: `importPath` and `roo
tPath`. The `importPath` property is a static getter on the element class that d
efaults to the element HTMLImport document URL and is overridable. It may be use
ful to override `importPath` when an element `template` is not retrieved from a
`dom-module` or the element is not defined using an HTMLImport. The `rootPath` p
roperty is set to the value of `Polymer.rootPath` which is globally settable and
defaults to the main document URL. It may be useful to set `Polymer.rootPath` t
o provide a stable application mount path when using client side routing. URL's
in styles are re-written to be relative to the `importPath` property. Inside ele
ment templates, URLs in element attributes are *no longer* re-written. Instead,
they should be bound using `importPath` and `rootPath` where appropriate. For ex
ample: |
| 506 |
| 507 A Polymer 1.x template that included: |
| 508 |
| 509 ```html |
| 510 <img src="foo.jpg"> |
| 511 ``` |
| 512 |
| 513 in Polymer 2.x should be: |
| 514 |
| 515 ```html |
| 516 <img src$="[[importPath]]foo.jpg"> |
| 517 ``` |
| OLD | NEW |