From 956bba7332271a237ce03cb348d6f2eb54557d61 Mon Sep 17 00:00:00 2001 From: Kevin Schaaf Date: Fri, 4 May 2018 14:16:02 -0700 Subject: [PATCH] Update README for 3.x --- README.md | 526 +++++++----------------------------------------------- 1 file changed, 69 insertions(+), 457 deletions(-) diff --git a/README.md b/README.md index 2cfffd68c4..48b8d88d6b 100644 --- a/README.md +++ b/README.md @@ -2,228 +2,122 @@ [![Build Status](https://travis-ci.org/Polymer/polymer.svg?branch=master)](https://travis-ci.org/Polymer/polymer) -Polymer lets you build encapsulated, reusable elements that work just like standard HTML elements, to use in building web applications. +> ℹ️ Note: This is the current stable version of the Polymer library. At Google I/O 2018 we announced a new Web Component base class, [`LitElement`](https://github.com/PolymerLabs/lit-element), as a successor to the `PolymerElement` base class in the Polymer library, and we recommend new users consider using `LitElement` instead. The Polymer 3.0 library will be maintained for the foreseeable future, and we recommend existing users of Polymer 2.0 to [migrate](#) elements to 3.0 for best compatibility with the JS ecosystem. Thanks to the interoperability of Web Components, elements built with Polymer 3.0 and LitElement can be mixed and matched in the same app, so developers can choose to migrate elements from Polymer 3.0 to LitElement incrementally. See our blog post on the [Polymer Project roadmap](https://www.polymer-project.org/blog/2018-05-02-roadmap-update.html) for more information. -```html - - +Polymer lets you build encapsulated, reusable [Web Components](https://www.webcomponents.org/introduction) that work just like standard HTML elements, to use in building web applications. Using a Web Commponent built with Polymer is as simple as importing its definition then using it like any other HTML element: - - +```html + + - - + +Web Components! ``` -Check out [polymer-project.org](https://www.polymer-project.org) for all of the library documentation, including getting started guides, tutorials, developer reference, and more. +Web Components are now implemented natively on Safari and Chrome (~70% of installed browsers), and run well on Firefox, Edge, and IE11 using [polyfills](https://github.com/webcomponents/webcomponentsjs). Read more [below](#overview). -Or if you'd just like to download the library, check out our [releases page](https://github.com/polymer/polymer/releases). +## Getting started -## Polymer 2.0 is out! + * The easiest way to try out Polymer is to use one of these online tools: -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](https://www.polymer-project.org/2.0/docs/about_20) on the website or the [2.0 section below](#2-0), and we welcome your feedback via [issues](https://github.com/Polymer/polymer/issues/new) or [Slack](https://polymer-slack.herokuapp.com/). + * Runs in all [supported](#supported-browsers) browsers: [StackBlitz](https://stackblitz.com/edit/polymer-element-example?file=index.js), [Glitch](https://glitch.com/edit/#!/polymer-element-example?path=index.html) -**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`](https://github.com/webcomponents/webcomponentsjs). + * Runs in browsers with [Javascript Modules](https://caniuse.com/#search=modules): [JSBin](https://jsbin.com/wuxejiz/edit?html,output), + [CodePen](https://codepen.io/kevinpschaaf/pen/BxdErp?editors=1000). -πŸ‘€ **Looking for Polymer v1.x?** Please see the [the v1 branch](https://github.com/Polymer/polymer/tree/1.x). + * You can also save [this HTML](https://gist.githubusercontent.com/kevinpschaaf/8a5acbea7b25d2bb5e82eeea2b105669/raw/c3a86872f07603e2d0ddae736687e52a5c8c499f/index.html) to a local file and run it in any browser that supports [Javascript Modules]((https://caniuse.com/#search=modules)). -⁉️ **Looking to use Polymer with npm?** Polymer 1.x and 2.x both use `bower` for version management, but v3 and on will use `npm`. Please see our [v3 announcement](https://www.polymer-project.org/blog/2017-08-22-npm-modules.html) and [v3 hand-on](https://www.polymer-project.org/blog/2017-08-23-hands-on-30-preview.html) to use a preview version of v3. -Versions before 3.0 are also published to npm "as-is" and are generally unsupported. These packages are for advanced users who configure their own tooling to work around the differences between Bower and npm packages, like package folder layout. + * When you're ready to use Polymer in a project, install it via npm. To run the project in the browser, + a module-compatible toolchain is required. We recommend installing the Polymer CLI to and using its development server as follows. -## Overview + 1. Add Polymer to your project: -Polymer is a lightweight library built on top of the web standards-based [Web Components](http://webcomponents.org/) 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](https://github.com/webcomponents/webcomponentsjs) where needed), Polymer elements are interoperable at the browser level, and can be used with other frameworks or libraries that work with modern browsers. + ```npm i @polymer/polymer``` -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. + 1. Create an element by extending PolymerElement and calling `customElements.define` with your class (see the examples below). -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. + 1. Install the Polymer CLI: -Polymer also provides optional two-way data-binding, meaning: + ```npm i -g polymer-cli@next``` -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. + 1. Run the development server and open a browser pointing to its URL: -In addition to the Polymer library for building your own custom elements, the Polymer project includes a collection of [pre-built elements](https://elements.polymer-project.org) that you can drop on a page and use immediately, or use as starting points for your own custom elements. + ```polymer serve``` -## Polymer in 1 Minute + > Polymer 3.0 is published on [npm](https://www.npmjs.com/package/@polymer/polymer) using Javascript Modules. + This means it can take advantage of the standard native Javascript module loader available in all current major browsers. + > + > However, since Polymer uses npm conventions to reference dependencies by name, a light transform to rewrite specifiers to URLs is required to run in the browser. The polymer-cli's development server `polymer serve`, as well as `polymer build` (for building an optimized app for deployment) automatically handles this transform. -Polymer adds convenient features to make it easy to build complex elements: + Tools like [WebPack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/) can also be used to serve and/or bundle LitElement. -**Basic custom element without Polymer:** -```js -// Standard custom element that Extends HTMLElement -class MyElement extends HTMLElement { - constructor() { - super(); - console.log('my-element was created!'); - } -} +## Minimal Example -// Register custom element class with browser -customElements.define('my-element', MyElement); -``` + 1. Create a class that extends `PolymerElement`. + 1. Implement a static `properties` getter that describes the element's public property/attribute API + (these automatically become observed attributes). + 1. Then implement a `template` getter that reutrns an `HTMLTemplateElement` describing the element's rendering, including encapsulated styling and any property bindings. ```html - - -``` - -**Custom element using Polymer** - -```html - - - - -``` - -```js -// Custom element that extends Polymer base class -class MySimpleNamecard extends Polymer.Element { - - // Stamp template from this dom-module into element's shadow DOM: - static get is() { return 'my-simple-namecard'; } - -} - -// Register custom element class with browser -customElements.define(MySimpleNamecard.is, MySimpleNamecard); -``` - -**Configure properties on your element...** - -```js -// Create an element that takes a property -class MyPropertyNamecard extends Polymer.Element { - - static get is() { return 'my-property-namecard'; } - - // Define property/attribute API: - static get properties() { - return { - myName: { - type: String, - observer: 'myNameChanged' + + -```html - - - - + ``` -```js -class MyBoundNamecard extends Polymer.Element { - static get is() { return 'my-bound-namecard'; } +## About Polymer 3.0 - static get properties() { - return { - myName: String - }; - } +Polymer 3.0 is now released to stable, and introduces a major change to how Polymer is distributed: from HTML Imports on bower, to JS modules on npm. Otherwise, the API is virtually 100% backward compatible with Polymer 2.0 (the only changes are related to removing API's related to HTML Imports like `importHref`, and converting Polymer's API to be module-based rather than globals-based). -} +Migrating to Polyme 3.0 by hand is mostly mechanical: All HTML Imports turn into JS module imports, and templates are encoded in JS using a `static get template()` getter on PolymerElement subclasses, using the `html` tagged template literal function (which parses `HTMLTemplateElement`'s out of strings in JS) rather than using `