Core points
::shadow
and /deep/
selectors to style elements in Shadow DOM. At the recently concluded Google I/O 2015 conference, Google released the long-awaited version of Polymer 1.0 and announced it is ready to go into production. For those who have been using the Polymer library while it is still in the developer preview stage, this article will serve as a guide to migrating existing applications to the latest version of Polymer.
Some important notes about version 1.0:
The steps to install the latest version of Polymer are exactly the same as described in this article. To update an existing Polymer installation, navigate to the application directory and run the following command in the terminal:
$ bower update
It is important to note that this will surely break your existing application, as mentioned earlier, the two versions are incompatible. Therefore, it is recommended to install a new copy in a separate folder instead. To illustrate the changes since version 0.5, we will use the credit card custom element from a previous post on SitePoint as a reference to compare the differences between the two versions.
Providing polyfill for unsupported browsers
New versions of Polymer no longer require the Shadow DOM polyfill included in the webcomponents.js
library. Instead, use a smaller webcomponents-lite.js
library to provide polyfills for older browsers.
Version 0.5:
<🎜>
Version 1.0:
<🎜>
The "lite version" is about 80kb less than its predecessor, which can be very important when performance is a key factor.
Define custom elements
<polymer-element>
Tags have been replaced by <dom-module>
tags containing custom element definitions. The custom element is now identified by the <dom-module>
attribute of the id
tag. The naming rules for custom elements are still the same.
Version 0.5:
$ bower update
Version 1.0:
<🎜>
Register custom elements
Previously, we could register custom elements by simply calling the Polymer()
constructor. If the tag is within the <polymer-element>
tag, specifying a custom element name is optional. In this version, custom elements are now registered by using the is
attribute on the prototype.
Version 0.5:
<🎜>
Version 1.0:
<polymer-element name="credit-card"></polymer-element> ...
is
attribute must match the <dom-module>
attribute of the id
tag of the custom element and is different from the previous version, which is not optional.
tag can be inside or outside the <dom-module>
element, but the template of the element must be parsed before the Polymer
constructor is called.
Custom element attributes
Any attributes contained in the<polymer-element>
tag should now be declared on the properties
object along with the data type.
Version 0.5:
<dom-module id="credit-card"></dom-module> ...
Version 1.0:
Polymer('credit-card', {});
Custom element style
The element style is now defined outside the <template>
tag.
Version 0.5:
Polymer({ is: 'credit-card' });
Version 1.0:
<polymer-element name='credit-card' attributes='amount'></polymer-element>
Use HTML import to support external style sheets.
Data binding
Polymer 1.0 provides two types of data binding:
[[ ]]
Create a one-way binding. Data flows are top-down, from host to child elements, binding never modify host properties. {{ }}
Create an automatic binding. The data flow is one-way or bidirectional, depending on whether the target attribute is configured as a bidirectional binding. In this version, the binding must replace the entire text content of the node or the entire value of the attribute. Therefore, string concatenation is not supported. For property values, it is recommended to use computed binding instead of string concatenation.
Version 0.5:
Polymer({ is: 'credit-card', properties: { amount: { type: Number } } });
Version 1.0:
<polymer-element name="credit-card" attributes="amount"></polymer-element> <template> ... </template>
<dom-module id="credit-card"> <style> ... </style> <template> ... </template> </dom-module>
Note that this means that the node cannot contain any spaces around the bound comment.
New Shady DOM
Polymer 0.5 Use Shadow DOM to provide developers with simpler element interfaces and abstract all complexity by hiding subtrees behind shadow roots. This forms the basis of encapsulation and works well in browsers that support Shadow DOM.
For browsers that do not yet support Shadow DOM, it is very difficult to implement the exact same polyfill as native Shadow DOM, usually slower than native implementations, and requires a lot of code. For these reasons, the Polymer team decided to cancel the Shadow DOM polyfill and instead built a lighter version of the native DOM that provides a packaging similar to Shadow DOM.
It should be noted that Shady DOM and Shadow DOM are compatible with each other, which means that the Shady DOM API uses native Shadow DOM when the browser is available and falls back to Shady DOM in unsupported browsers.
Conclusion
Under the complexity of your application, upgrading your application to Polymer 1.0 can be a very slow process, but it has huge advantages in speeding up load times and significantly reducing payloads. The official migration guide available on the Polymer project website provides a more in-depth look at some other changes to the internal API, so be sure to check it out.
In addition, Chuck Horton created a Github repository called Road to Polymer 1.0, which provides a code conversion tool that can update your code from version 0.5 to version 1.0. This can be a starting point for migration if you don't want to make some appearance changes manually.
FAQs about upgrading to Polymer 1.0
Polymer 1.0 is a complete rewrite of previous versions of Polymer 0.5. The new version is more efficient, takes up less space and faster performance. It also introduces a new, simplified syntax that is easier to understand and use. The data binding system has been improved to improve performance and make its behavior more intuitive. In addition, Polymer 1.0 supports better support for creating custom elements, which are a key part of the Web Components standard.
Upgrading from Polymer 0.5 to Polymer 1.0 includes several steps. First, you need to update the Bower dependencies to point to the new Polymer 1.0 element. You then need to update the HTML import to load the new element. You also need to update the element definition and data binding to use the new Polymer 1.0 syntax. Finally, you need to thoroughly test your project to make sure everything works as expected.
Shadow DOM is a key part of the Web Components standard. It provides a way to encapsulate the details of custom elements implementations, hiding its internal structure and styles outside the rest of the page. In Polymer 1.0, you can use Shadow DOM to create fully encapsulated and reusable custom elements.
Styling elements in Shadow DOM can be a bit tricky because they are encapsulated and isolated from the rest of the page. However, Polymer 1.0 provides several ways to style Shadow DOM elements. You can use CSS custom properties to define styles that can be applied inside Shadow DOM. You can also use the ::shadow
and /deep/
selectors to penetrate Shadow DOM and style its internal elements.
...(The rest of the FAQ answers can be rewritten in the same way to keep the content consistent, but the words and sentences change)
The above is the detailed content of A Guide to Upgrading to Polymer 1.0. For more information, please follow other related articles on the PHP Chinese website!