polymer README

The project's README from github.com/Polymer/polymer. As one markdown file: README.md.

Polymer

Build Status Published on npm Published on webcomponents.org

ℹ️ 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, as a successor to the PolymerElement base class in this library.

If you're starting a new project, we recommend that you consider using LitElement instead.

If you have a project you've built with an earlier version of the Polymer library, we recommend that you migrate to 3.0 for best compatibility with the JavaScript 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 once you have updated your project to Polymer 3.0, you can migrate to LitElement incrementally, one element at a time. See our blog post on the Polymer Project roadmap for more information.

Polymer lets you build encapsulated, reusable Web Components that work just like standard HTML elements, to use in building web applications. Using a Web Component built with Polymer is as simple as importing its definition then using it like any other HTML element:

<!-- Import a component -->
<script src="https://unpkg.com/@polymer/paper-checkbox@next/paper-checkbox.js?module" type="module" ></script>

<!-- Use it like any other HTML element -->
<paper-checkbox>Web Components!</paper-checkbox>

Web Components are now implemented natively on Safari and Chrome (~70% of installed browsers), and run well on Firefox, Edge, and IE11 using polyfills. Read more below.

Getting started

CodePen.

Minimal Example

  1. Create a class that extends PolymerElement.
  2. Implement a static properties getter that describes the element's public property/attribute API (these automatically become observed attributes).
  3. Then implement a template getter that returns an HTMLTemplateElement describing the element's rendering, including encapsulated styling and any property bindings.
  <script src="node_modules/@webcomponents/webcomponents-loader.js"></script>
  <script type="module">
    import {PolymerElement, html} from '@polymer/polymer';

    class MyElement extends PolymerElement {
      static get properties() { return { mood: String }}
      static get template() {
        return html`
          <style> .mood { color: green; } </style>
          Web Components are <span class="mood">[[mood]]</span>!
        `;
      }
    }

    customElements.define('my-element', MyElement);
  </script>

  <my-element mood="happy"></my-element>

Overview

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.

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 APIs built in the browser (or polyfilled where needed), elements built with Polymer are:

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.

About Polymer 3.0

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 almost entirely backward compatible with Polymer 2.0 (the only changes are removing APIs related to HTML Imports like importHref, and converting Polymer's API to be module-based rather than globals-based).

Migrating to Polymer 3.0 by hand is mostly mechanical:

However, the polymer-modulizer tool automates the vast majority of this migration work. Please see details on that repo for automated conversion of Polymer 2.0 apps and elements to Polymer 3.0.

👀 Looking for Polymer v2.x? Please see the v2 branch.

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

Contributing

The Polymer team loves contributions from the community! Take a look at our contributing guide for more information on how to contribute. Please file issues on the Polymer issue tracker following the issue template and contributing guide issues.

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 (Polymer/polymer)

Searching Database…

这是什么?Polymer 是谷歌推出的开源库,用于构建 Web Components,也就是封装好的、可复用的自定义 HTML 元素,它们在网页应用中的使用方式就跟标准的 HTML 标签一样。这个仓库保存的是当前的稳定版本 Polymer 3.0。
它是像 React 那样的完整框架,还是更轻量的东西?它是一个构建在浏览器原生 Web Components API 之上的轻量级库,并不是完整的框架——它让用 HTML、CSS 和 JavaScript 创建自定义元素变得更简单,还提供可选的双向数据绑定,本身并没有引入什么重量级的抽象概念。
我该怎么试用它,怎么把它加到项目里?你可以直接在 StackBlitz 或 Glitch 这类在线练习场里立刻试用,也可以通过 npm 安装。要在浏览器里运行,需要一个支持模块的工具链:可以用 Polymer CLI(开发时用 'polymer serve',发布生产版本用 'polymer build'),也可以用 webpack、Rollup 这类工具,它们会把 npm 的导入路径改写成 URL。
它到底能在哪些浏览器里运行?Web Components 在 Safari 和 Chrome 里是原生支持的——大约覆盖 70% 的已安装浏览器——在 Firefox、Edge 和 IE11 上通过 polyfill 也能运行良好。Polymer 3.0 以 JavaScript 模块的形式发布在 npm 上,所以你只需要一个现代浏览器的原生模块加载器,再加一套基于 npm 的工具链就够了。
我今天开新项目还应该用它吗?不建议用于新项目——在 2018 年的 Google I/O 大会上,团队宣布了继任者 LitElement,并推荐新项目使用它。现有的应用应该先升级到 Polymer 3.0,之后可以逐步迁移到 LitElement,一次迁移一个元素。
我该怎么定义自己的元素?创建一个继承 PolymerElement 的类,添加一个静态的 properties getter 来描述公开的属性(property/attribute)接口(这些属性会自动变成可观察的 attribute),再添加一个 template getter,返回一个包含元素标记、封装样式和绑定的 HTMLTemplateElement。最后用 customElements.define 注册这个类。
它支持双向数据绑定吗?支持,而且是可选的:当元素模型里的属性被更新时,元素可以随之自我更新;反过来,元素内部发生的更改也能传回模型。
Polymer 元素能和 React、Vue 或 LitElement 的代码混用吗?可以。因为 Polymer 元素是基于 Web 平台标准构建的,所以能跨框架互操作,用 Polymer 3.0 和 LitElement 构建的元素也可以在同一个应用里随意混搭——这也是为什么可以一次一个元素地逐步迁移到 LitElement。
Polymer 3.0 相比 2.x 有什么变化?分发方式从 Bower 的 HTML Imports 改成了 npm 上的 JS 模块。API 几乎完全向后兼容——变化主要就是移除了 importHref,以及从全局变量改成模块。迁移工作大多是机械性的,polymer-modulizer 工具能自动化完成其中绝大部分;v1.x 和 v2.x 则保留在各自的分支上。
Creator
Polymer
Size
31 MB
Stars
22K
Age
14y
Commits · 90 Days