How to get <iron-icons> to work in Polymer 2.x - polymer

Narrow Question
By troubleshooting, I think I have narrowed my "larger problem" down to the following question. (But I could be wrong.):
How do I properly download the latest version of <iron-icons> to my local machine?
By "latest version," I mean the version that is imported via the following:
<base href="//polygit.org/polymer+:master/components/">
<link rel="import" href="iron-icons/iron-icons.html">
The above version of importing <iron-icons> works on my demo here.
However, when I do either of the following:
bower install --save PolymerElements/iron-icons
bower update --save
And try to import on my local server like this:
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
it fails silently and I see no icons at all. But I do see extra space where the icons are supposed to render.
Bigger Question (the real problem I'm trying to solve)
I am trying to get my <iron-icons> to render properly in the browser.
What I expect to see
I expect to see a two <iron-icon> elements like this:
(anchored in between other test elements: Hello World and <img>)
What I actually see
No icons at all. But white space where they should render.
Steps to reproduce
Run
bower install --save PolymerElements/iron-icons
or, if <iron-icons> is already installed:
bower update --save
then
polyserve
or
polymer serve
Version
I believe I am using v2.0.1 of <iron-icons>. Here is the entire contents of the bower.json file.
bower.json
"iron-icons": "PolymerElements/iron-icons#^2.0.1",
Configuration
OS: macOS Sierra 10.12.6
Hardware: MacBook Air
Browser: Chrome Version 60.0.3112.113 (Official Build) (64-bit)
Demo
Here is my Plunker demo.
Code
The following code does render <iron-icons> as expected:
http://plnkr.co/edit/pKnrlIbGhfQPFq10aAJS?p=preview
<base href="//polygit.org/polymer+:master/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer-element.html">
<link rel="import" href="iron-icons/iron-icons.html">
<link rel="import" href="iron-icon/iron-icon.html">
<dom-module id="demo-el">
<template>
Hello world
<iron-icon icon="add"></iron-icon>
<iron-icon icon="favorite"></iron-icon>
<img src="//lorempixel.com/400/200/" />
</template>
<script>
class DemoEl extends Polymer.Element {
static get is() {
return 'demo-el'
}
constructor() {
super();
}
}
customElements.define(DemoEl.is, DemoEl);
</script>
</dom-module>
The following code does NOT render <iron-icons> as expected (served locally):
<link rel="import" href="/bower_components/polymer/polymer-element.html">
<link rel="import" href="/bower_components/webcomponentsjs/webcomponents-lite.js">
<link rel="import" href="/bower_components/iron-icon/iron-icon.html">
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
<link rel="import" href="/bower_components/paper-input/paper-input.html">
<dom-module id="app-main">
<template>
Hello world
<iron-icon icon="add"></iron-icon>
<iron-icon icon="favorite"></iron-icon>
<img src="//lorempixel.com/400/200/" />
</template>
<script>
class AppMain extends Polymer.Element {
static get is() {
return 'app-main'
}
constructor() {
super();
}
}
customElements.define(AppMain.is, AppMain);
</script>
</dom-module>
However, the above code does render <iron-icons> as expected when I do a direct substitution of:
<base href="//polygit.org/polymer+:master/components/">
<link rel="import" href="iron-icons/iron-icons.html">
for
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
Edit
By comparing the text of the imported files using the CDN
<base href="//polygit.org/polymer+:master/components/">
<link rel="import" href="iron-icons/iron-icons.html">
and local imports:
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
I discovered there is a discrepancy in the imported version of the iron-iconset-svg.html file. My bower.json file says the dependent version is "iron-iconset-svg": "1 - 2" or "iron-iconset-svg": "polymerelements/iron-iconset-svg#^2.0.0" but the bower.json file here says the dependent version is "iron-iconset-svg": "polymerelements/iron-iconset-svg#^1.0.0"
So, now the question appears to reduce to how do I get the local version of iron-icons.html to import the same version of iron-iconset-svg.html as the online CDN import version.
Edit 2
The problem persists when I just copy the iron-iconset-svg.html file from the CDN and paste it into my local file system. So, apparently something else is going on too.
Edit 3
I followed the instructions here for upgrading to Polymer 2.0:
https://www.polymer-project.org/2.0/docs/upgrade#update-bower-dependencies
Remove the existing bower_components folder.
rm -rf bower_components
Update the Polymer version in bower.json to the latest versions.
Component | Version
---------------------|--------
Polymer | ^2.0.0
webcomponentsjs | ^1.0.0
web-component-tester | ^6.0.0
Polymer elements | ^2.0.0
Install the new dependencies.
bower install
This did not solve the problem. However I could not update Polymer elements to ^2.0.0
bower.json
"dependencies" : {
...
"polymer-elements" : "^2.0.0",
...
}
caused an error and so did:
bower.json
"dependencies" : {
...
"polymerelements" : "^2.0.0",
...
}
and so did:
bower.json
"dependencies" : {
...
"PolymerElements" : "^2.0.0",
...
}

The solution proposed by #Ofisora in the comments works for me—update iron-selector and iron-meta:
bower install --save PolymerElements/iron-meta
bower install --save PolymerElements/iron-selector

iron-icons is a utility import that includes the definition for the
iron-icon element, iron-iconset-svg element, as well as an import
for the default icon set.
This means that iron-icons is directly dependent on both iron-icon and iron-iconset-svg. And both iron-icon and iron-iconset-svg element depends on iron-meta.
When you update iron-icons to the latest version, these elements iron-icon, iron-iconset-svg and iron-meta might not get updated since you will get all these components by default. So, updating iron-meta is one of the solution.
Also, when you install or update element in polymer you will see messages like:
Unable to find a suitable version for iron-icons, please choose one by
typing one of the numbers below:
Always choose the suitable or latest version depending on other element you have.
When finished updating or installing you will see a note like:
Please note that,
iron-iconset-svg#a47e824859 depends on iron-meta#2.0-preview which resolved to iron-meta#7404b31da3
iron-icon#1.0.13, iron-icon#1.0.13, iron-icon#1.0.13, iron-iconset-svg#1.1.2, iron-iconset-svg#1.1.2 depends on iron-meta#^1.0.0 which resolved to iron-meta#1.1.3
iron-icon#2.0.0, iron-iconset-svg#2.0.0 depends on iron-meta#1 - 2 which resolved to iron-meta#2.0.2
Make sure you read this and install/update dependencies required.
Note: In my case I had to update iron-selector as well since I was using iron-icon inside the iron-selector.

Related

Trying to install polymer-quill via bower

New to bower and web components. I'm trying to install the polymer-quill library with bower.
npm install -g bower
mkdir polymerquill
cd polymerquill
bower install --save polymer-quill
Now I have a full bower_components directory. I make an index.html file and add the following:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/polymer-quill/polymer-quill.html">
</head>
<body>
<h1>PolyQuill</h1>
<style is="custom-style">
polymer-quill.full {
--polymer-quill-editor-max-height: 300px;
--polymer-quill-editor-min-height: 100px;
}
</style>
<h2>Full Toolbar, Show Results, Max Height (300px), Min Height (100px), Save as Deltas, Save every 1 second</h2>
<polymer-quill content='{"ops":[{"insert":"Hello World! - Store as Delta"},{"attributes":{"header":2},"insert":"\n"}]}'
class='full'
store-as="delta"
save-interval="1000"
toolbar-type="full"
show-results>
</polymer-quill>
<h2>Standard Toolbar, Hide Results, Default height (100px), Save as HTML, Save every 2 seconds</h2>
<polymer-quill content="<h2>Hello World! - Store as HTML</h2>" store-as="html"></polymer-quill>
</body>
</html>
When I open it in Chrome, however, I only see the h2's. The polymer-quill elements don't render. What am I forgetting to do?
I believe this element currently only works in Polymer's shady DOM (see issue here).
If you're using Polymer 1.0, declare shady DOM by adding the following script in your index.html: (reference)
<script>
/* this script must run before Polymer is imported */
window.Polymer = {
dom: 'shadow',
lazyRegister: true
};
</script>
If you're using Polymer 2.0, when importing webcomponents just add the shadydom attribute as follows:
<script src="webcomponentsjs/webcomponents-loader.js" shadydom></script>

The right way to do html imports with Polymer elements?

Say, I have element A and B. The imports in B are:
<!-- Imports in B -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../another-element/another-element.html">
Now I want to use B in A. What is the "right" way to import B? Should A just import B like this?
<!-- Imports in A: Method 1 -->
<link rel="import" href="../element-b/element-b.html">
Or should A also import all the imports used by B like the following?
<!-- Imports in A: Method 2 -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../another-element/another-element.html">
<link rel="import" href="../element-b/element-b.html">
If Google's own Polymer Elements can be of any guidance, the answer appears to be Method 2, i.e. importing all dependent htmls. But if that is the case, the "import" syntax in Polymer/WebComponents seems to break encapsulation for no obvious reason. For example, here is the "paper-dropdown-menu" element from Polymer:
https://github.com/PolymerElements/paper-dropdown-menu/blob/master/paper-dropdown-menu.html
It imports "paper-input.html", which has its own imports:
https://github.com/PolymerElements/paper-input/blob/master/paper-input.html
The following 2 imports are used in both elements:
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
If paper-dropdown-menu.html already imports paper-input.html, why does paper-dropdown-menu have to import polymer.html and iron-form-element again? The problem with this is that the imports can get out of control very quickly when composing an app with many elements. Yes, I know Vulcanize. But I don't see how that helps the development process when one is creating or using an element. In addition, encapsulation means A shouldn't need to know the internal parts of B in the example above. Or is Polymer/Webcomponent really saying that html imports in a component are part of the "public interface" of that component?
Just import the elements you directly depend on. You can ignore transitive dependencies because they are imported in the elements you import directly anyway.
In addition each element should import <link rel="import" href="../polymer/polymer.html">.
Method 1 is the "better" way. Each polymer element is supposed to bundle its own dependencies, so it can function by itself when used.
Method 2 isn't that good, you will be re-importing the imports of element B twice, thats a waste of bandwidth especially when you aren't using a function (like polymers inbuilt importHref) that checks if an element has already been imported before fetching it again.

"Element name could not be inferred" error in Firefox with Polymer 1.x

I'm updating a little Polymer element to 1.x. It works fine in Chrome and Safari, but in the latest Firefox I get several "uncaught exception: Element name could not be inferred." errors.
Seems to be related to the html imports, because if I remove everything from the component.html except for the imports, then the errors still occur.
In the index.html I have this:
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="src/element.html">
In the element.html this:
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-input/iron-input.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="../bower_components/iron-icons/iron-icons.html">
<link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html">
[EDIT] - Somehow the errors disappeared. I suspect due to either a Firefox or Polymer update.
Clear your browser cache to solve the problem.
install polymer#^1.1.0 which resolved to 1.7.0
it works for me

Polymer paper-input element not rendering

Following is the code, with paper-input in body but it ceases to render.
I was able to render paper-button though.
<script src="../components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="../components/paper-input/paper-input.html">
<body unresolved>
<paper-input label="India"></paper-input>
</body>
I know you have probably resolved your issue by now, hopefully but for others who may run into the same issue, try adding this inside your head tag:
<link rel="import" href="bower_components/paper-input/paper-input.html">
It looks like you did not import paper-input correctly.
Had the same issue and then found out I had paper-input v1.2
I reinstalled it and it changed to 2.x, then it started working.

Is there CDN for polymer elements?

I would like to know if there are any CDNs for polymer elements, since you have to always download the elements and It would be more convinient to import it via cdn. Can't find any on google? Also are there any reasons that it does not exists or just because it is so new?
There is now!
I created this GitHub repository specifically for this purpose:
download/polymer-cdn
All GitHub repositories are automatically in CDN through RawGit. So, using that, we can now import Polymer elements using markup like this (for iron-icons in this case):
<link rel="import"
href="https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/iron-icons/iron-icons.html">
The project structure was set up in such a way that imports from elements that you import (transitive dependencies) resolve correctly.
The readme for the repository has a list of all elements it contains.
Missing something? Let me know and I'll be happy to include it.
Try it
You can try it out right now by hacking on this Codepen:
Polymer-CDN Example.
Or you can run this code snippet:
<base href="https://cdn.rawgit.com/download/polymer-cdn/1.5.0/lib/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="iron-icon/iron-icon.html">
<link rel="import" href="iron-icons/iron-icons.html">
<link rel="import" href="paper-button/paper-button.html">
<link rel="import" href="paper-checkbox/paper-checkbox.html">
<link rel="import" href="paper-tabs/paper-tabs.html">
<link rel="import" href="paper-toggle-button/paper-toggle-button.html">
<style is="custom-style">
:root {
--paper-tabs-selection-bar-color: var(--paper-light-blue-900);
--paper-tab-ink: var(--paper-light-blue-100);
--paper-tabs: {
color: white;
background-color: var(--paper-light-blue-500);
};
}
</style>
<div>
<paper-button raised><iron-icon icon="check"></iron-icon>OK</paper-button>
<paper-button raised><iron-icon icon="clear"></iron-icon>Cancel</paper-button>
</div>
<p><paper-checkbox>Checkbox</paper-checkbox></p>
<p><paper-toggle-button></paper-toggle-button></p>
<paper-tabs selected="0">
<paper-tab>TAB 1</paper-tab>
<paper-tab>TAB 2</paper-tab>
<paper-tab>TAB 3</paper-tab>
</paper-tabs>
You can also access polymer elements directly from polymer-project.org.
Example:
<link rel="import" href="https://www.polymer-project.org/0.5/components/core-ajax/core-ajax.html">
This is an old question, but there is a non-hacky solution now: http://polygit.org/
It uses rawgit behind the curtains but provides a much nicer api.
I do not know any CDN hosting polymer elements right now and I assume it would be better to vulcanize them for a production environment but due to the fact that most of the elements are hosted on github you could link your imports to rawgit.com
Example:
<link rel="import" href="https://rawgit.com/Polymer/core-ajax/master/core-ajax.html">
You might take a look at cloudflares polymer CDN:
http://cdnjs.com/libraries/polymer
rawgit option
You would have to manage some dependencies manually since core-ajax.html returns 404 on polymer.html. Also rawgit.com cache is set to only 5 min (cache-control:max-age=300). 5 min cache is fine for version control, but it should be more for CDN (https://rawgit.com/Polymer/core-ajax/0.4.1/core-xhr.html). Also files are not minified.
vulcanize option
Probably best option before http/2 release. You would have to spend some time with configuration and integration into your build process. Also you don't have any CDN benefits (no data cost, already cached resources from third party domains.)
conclusion
There will be some CDN with minified polymer versions and long expires header on http/2 release. But I don't know about any right now.