Polymer/polymer

Name: polymer

Owner: polymer

Description: Build modern apps using web components

Created: 2012-08-23 20:56:30.0

Updated: 2018-01-19 05:00:01.0

Pushed: 2018-01-18 20:17:39.0

Homepage: https://www.polymer-project.org/

Size: 24525

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Polymer

Build Status

Polymer lets you build encapsulated, reusable elements that work just like standard HTML elements, to use in building web applications.

 Polyfill Web Components for older browsers -->
ipt src="webcomponentsjs/webcomponents-lite.js"></script>

 Import element -->
k rel="import" href="google-map.html">

 Use element -->
gle-map latitude="37.790" longitude="-122.390"></google-map>

Check out polymer-project.org for all of the library documentation, including getting started guides, tutorials, developer reference, and more.

Or if you'd just like to download the library, check out our releases page.

Polymer 2.0 is out!

Polymer 2.0 is released, and will be the future focus of Polymer development going forward. We intend to keep the 2.x public API stable barring critical issues. For background and migration information on the 2.x see the 2.0 documentation on the website or the 2.0 section below, and we welcome your feedback via issues or Slack.

To evaluate Polymer 2.0, please point your bower at the latest 2.0.0 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 tag of webcomponentsjs.

? Looking for Polymer v1.x? Please see the the v1 branch.

Overview

Polymer is a lightweight library built on top of the web standards-based Web Components APIs, and makes it easier to build your very own custom HTML elements. Creating reusable custom elements - and using elements 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 where needed), Polymer elements are interoperable at the browser level, and can be used with other frameworks or libraries that work with modern browsers.

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 project or in any future project.

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.

Polymer also provides optional two-way data-binding, meaning:

  1. When properties in the model for an element get updated, the element can update itself in response.
  2. When the element is updated internally, the changes can be propagated back to the model.

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 best features of the web platform in straightforward ways to simply sugar the creation of custom elements.

In addition to the Polymer library for building your own custom elements, the Polymer project includes a collection of pre-built elements that you can drop on a page and use immediately, or use as starting points for your own custom elements.

Polymer in 1 Minute

Polymer adds convenient features to make it easy to build complex elements:

Basic custom element without Polymer:

tandard custom element that Extends HTMLElement
s MyElement extends HTMLElement {
nstructor() {
super();
console.log('my-element was created!');



egister custom element class with browser
omElements.define('my-element', MyElement);
tml
 use the element -->
element></my-element>

Custom element using Polymer

 Define template that your element will use -->
-module id="my-simple-namecard">
emplate>
<div>
  Hi! My name is <span>Jane</span>
</div>
template>
m-module>
s
ustom element that extends Polymer base class
s MySimpleNamecard extends Polymer.Element {

 Stamp template from this dom-module into element's shadow DOM:
atic get is() { return 'my-simple-namecard'; }



egister custom element class with browser
omElements.define(MySimpleNamecard.is, MySimpleNamecard);

Configure properties on your element…

reate an element that takes a property
s MyPropertyNamecard extends Polymer.Element {

atic get is() { return 'my-property-namecard'; }

 Define property/attribute API:
atic get properties() {
return {
  myName: {
    type: String,
    observer: 'myNameChanged'
  }
};


NameChanged(myName) {
this.textContent = 'Hi! My name is ' + myName;




omElements.define(MyPropertyNamecard.is, MyPropertyNamecard);

…and have them set using declarative attributes

 using the element -->
property-namecard my-name="Jim"></my-property-namecard>

Hi! My name is Jim

Bind data into your element using the familiar mustache-syntax

 Define template with bindings -->
-module id="my-bound-namecard">
emplate>
<div>
  Hi! My name is <span>[[myName]]</span>
</div>
template>
m-module>
s MyBoundNamecard extends Polymer.Element {

atic get is() { return 'my-bound-namecard'; }

atic get properties() {
return {
  myName: String
};




omElements.define(MyBoundNamecard.is, MyBoundNamecard);
tml
 using the element -->
bound-namecard my-name="Josh"></my-bound-namecard>

Hi! My name is Josh

Style the internals of your element, without the style leaking out

 Add style to your element -->
-module id="my-styled-namecard">
emplate>
<style>
  /* This would be crazy without webcomponents, but with shadow DOM */
  /* it only applies to this element's private "shadow DOM" */
  span {
    font-weight: bold;
  }
</style>

<div>
  Hi! My name is <span>{{myName}}</span>
</div>
template>
m-module>
s MyStyledNamecard extends Polymer.Element {

atic get is() { return 'my-styled-namecard'; }

atic get properties() {
return {
  myName: String
};




omElements.define(MyStyledNamecard.is, MyStyledNamecard);
 using the element -->
styled-namecard my-name="Jesse"></my-styled-namecard>

Hi! My name is Jesse

and so much more!

Web components are an incredibly powerful new set of primitives baked into the web platform, and open up a whole new world of possibility when it comes to componentizing front-end code and easily creating powerful, immersive, app-like experiences on the web.

By being based on Web Components, elements built with Polymer are:

Contributing

The Polymer team loves contributions from the community! Take a look at our contributing guide for more information on how to contribute.

Communicating with the Polymer team

Beyond GitHub, we try to have a variety of different lines of communication available:

License

The Polymer library uses a BSD-like license that is available here


Polymer 2.0

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 improvements over the 1.x version of the library. The following section provides context and migration information for existing users of Polymer 1.x:

Goals of Polymer 2.0
  1. Take advantage of native “v1” Web Components implementations across browsers.

    The primary goal of the Polymer 2.0 release is to take advantage of native, cross-browser support for Web Components.

    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 has always required the use of polyfills.

    Beginning this fall, multiple browsers will be shipping native implementations of the new “v1” specs for Shadow DOM and Custom Elements, yielding better web components performance and reducing the need for polyfills.

    Polymer 2.0 features full support for the v1 specs, taking advantage of native browser implementations where they are available and depending on updated v1 polyfills from webcomponentsjs where necessary.

    Polymer 2.0 also embraces the new ES-class-based mechanism for defining custom elements, bringing idiomatic Polymer style closer to “vanilla” custom element authoring.

  2. Provide a smooth migration path from Polymer 1.x.

    Our second major goal is to provide as easy a transition as possible for developers who have built elements and apps with Polymer 1.x, making Polymer 2.0 a sturdy bridge to the future.

    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).

    However, we've taken care to limit the number of changes that are strictly required and to ease the process of upgrading:

  3. Polymer 2.0 introduces a new ES6 class-based syntax, but we've provided a lightweight compatibility layer allowing you to upgrade your 1.x code with minimal modifications. Depending on your needs, you can either take advantage of the compatibility layer or jump straight to idiomatic 2.0 style.

  4. Before releasing Polymer 2.0, we'll also provide an upgrade tool to automate as many of the changes (both required and recommended) as possible.

  5. Finally, we're working on guidelines for building and testing “hybrid” elements that will run in both Polymer 1.x and Polymer 2.0. We plan to ship hybrid versions of all of the elements that we provide, easing the transition for developers who use them. Third-party element providers may also choose to ship hybrid elements.

  6. 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, individually or in batches, while running against Polymer 1.x; then cut over to Polymer 2.0 when all of your elements have been updated.

  7. Eliminate leaky abstractions.

    Seamless interoperability is one of Web Components' major selling points. Generally speaking, web components “just work” anywhere you use HTML elements. To use 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 means you can easily mix standard HTML elements, third-party elements and elements you've defined yourself.

    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-based elements leak out, requiring users of the elements to interact with them in non-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.

    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 elements will be indistinguishable from “vanilla” elements from a consumer's point of view (unless you leak implementation details of your own).

  8. Make targeted improvements to the Polymer data system.

    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 designed to make it easier to reason about and debug the propagation of data through and between elements:

  9. Changes are now batched, and the effects of those changes are run in well-defined order.

  10. We ensure that multi-property observers run exactly once per turn for any set of changes to dependencies (removing the multi-property undefined rule).

  11. To add compatibility with more approaches to state management, we now provide 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.

  12. Improve factoring of Polymer and the polyfills

    We've done major refactoring of Polymer and the webcomponentsjs polyfills to improve efficiency, utility and flexibility:

  13. 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).

  14. Polymer itself has been internally factored into several loosely coupled libraries.

    • The new Polymer.Element class extends from the native HTMLElement and mixes in functionality from these libraries.

    • 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 Polymer.Element, using standard ES class definition syntax.

    • 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 subset 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 official Polymer 2.0 API.

1.0 Compatibility Layer

Polymer 2.0 retains the existing polymer/polymer.html import that current Polymer 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, existing 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 styling, as well as minor breaking changes introduced due to changes in the V1 Custom Elements spec and data-layer improvements listed below.

2.0 ES6 Class-based Syntax

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 syntax. Using this syntax, users will extend Polymer.Element (a subclass of HTMLElement), which provides meta-programming for most of the same features of Polymer 1.0 based on static configuration data supplied on the class definition.

Basic syntax looks like this:

 Load the Polymer.Element base class -->
k rel="import" href="bower_components/polymer/polymer-element.html">
s
xtend Polymer.Element base class
s MyElement extends Polymer.Element {
atic get is() { return 'my-element'; }
atic get properties() { return { /* properties metadata */ } }
atic get observers() { return [ /* observer descriptors */ ] }
nstructor() {
super();
...

nnectedCallback() {
super.connectedCallback();
...

.


egister custom element definition using standard platform API
omElements.define(MyElement.is, MyElement);

Users can then leverage native subclassing support provided by ES6 to extend and customize existing elements defined using ES6 syntax:

ubclass existing element
s MyElementSubclass extends MyElement {
atic get is() { return 'my-element-subclass'; }
atic get properties() { return { /* properties metadata */ } }
atic get observers() { return [ /* observer descriptors */ ] }
nstructor() {
super();
...

.


egister custom element definition using standard platform API
omElements.define(MyElementSubclass.is, MyElementSubclass);

Below are the general steps for defining a custom element using this new syntax:

Note that Polymer.Element provides a cleaner base class void of a lot of sugared utility API that present on elements defined with Polymer(), such as fire, transform, etc. With web platform surface area becoming far more stable across browsers, we intend to hew towards sugaring less and embracing the raw platform API more. So when using Polymer.Element, instead of using the legacy this.fire('some-event') API, simply use the equivalent platform API's such as this.dispatchEvent(new CustomEvent('some-event', {bubbles: true}). #usetheplatform

See below for a visual guide on migrating Polymer 1.0's declarative syntax to the ES6 class syntax in Polymer 2.0:

Difference in Polymer 1.0 and Polymer 2.0 element definition

Polyfills

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 compatible the latest releases of webcomponentsjs/webcomponents-lite.js, which is included as a bower dependency to Polymer 2.x.

Breaking Changes

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 and we'll investigate to determine whether they are expected/intentional or not.

Polymer.dom

On browsers that lack native V1 Shadow DOM support, Polymer 2.0 is designed to be used with the new V1 Shady DOM shim, 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 interacting with the DOM. Polymer.dom can be eliminated for elements targeting Polymer 2.0, in favor of the native DOM API's.

Note that Polymer.dom is still provided in the polymer.html backward-compatibility 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 Arrays 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.

V1 Shadow DOM

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 scoped 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. Required changes for V1 are summarized below:

Distribution Scoped styling Scoped events
V1 Custom Elements

Polymer 2.0 elements will target the V1 Custom Elements API, which primarily changes the “created” step to actually invoke the class constructor, imposes new restrictions on what can be done in the constructor (previously createdCallback), and introduces different callback names.

CSS Custom Property Shim

Polymer 2.0 will continue to use a shim to provide limited CSS Custom Properties support on browsers that do not 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:

Data system
Removed API
Other

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.