• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!--
2@license
3Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9-->
10
11<link rel="import" href="../polymer/polymer.html">
12<link rel="import" href="../iron-meta/iron-meta.html">
13<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
14
15<!--
16
17The `iron-icon` element displays an icon. By default an icon renders as a 24px square.
18
19Example using src:
20
21    <iron-icon src="star.png"></iron-icon>
22
23Example setting size to 32px x 32px:
24
25    <iron-icon class="big" src="big_star.png"></iron-icon>
26
27    <style is="custom-style">
28      .big {
29        --iron-icon-height: 32px;
30        --iron-icon-width: 32px;
31      }
32    </style>
33
34The iron elements include several sets of icons.
35To use the default set of icons, import `iron-icons.html` and use the `icon` attribute to specify an icon:
36
37    <link rel="import" href="/components/iron-icons/iron-icons.html">
38
39    <iron-icon icon="menu"></iron-icon>
40
41To use a different built-in set of icons, import the specific `iron-icons/<iconset>-icons.html`, and
42specify the icon as `<iconset>:<icon>`. For example, to use a communication icon, you would
43use:
44
45    <link rel="import" href="/components/iron-icons/communication-icons.html">
46
47    <iron-icon icon="communication:email"></iron-icon>
48
49You can also create custom icon sets of bitmap or SVG icons.
50
51Example of using an icon named `cherry` from a custom iconset with the ID `fruit`:
52
53    <iron-icon icon="fruit:cherry"></iron-icon>
54
55See [iron-iconset](iron-iconset) and [iron-iconset-svg](iron-iconset-svg) for more information about
56how to create a custom iconset.
57
58See the [iron-icons demo](iron-icons?view=demo:demo/index.html) to see the icons available
59in the various iconsets.
60
61To load a subset of icons from one of the default `iron-icons` sets, you can
62use the [poly-icon](https://poly-icon.appspot.com/) tool. It allows you
63to select individual icons, and creates an iconset from them that you can
64use directly in your elements.
65
66### Styling
67
68The following custom properties are available for styling:
69
70Custom property | Description | Default
71----------------|-------------|----------
72`--iron-icon` | Mixin applied to the icon | {}
73`--iron-icon-width` | Width of the icon | `24px`
74`--iron-icon-height` | Height of the icon | `24px`
75`--iron-icon-fill-color` | Fill color of the svg icon | `currentcolor`
76`--iron-icon-stroke-color` | Stroke color of the svg icon | none
77
78@group Iron Elements
79@element iron-icon
80@demo demo/index.html
81@hero hero.svg
82@homepage polymer.github.io
83-->
84
85<dom-module id="iron-icon">
86  <template>
87    <style>
88      :host {
89        @apply(--layout-inline);
90        @apply(--layout-center-center);
91        position: relative;
92
93        vertical-align: middle;
94
95        fill: var(--iron-icon-fill-color, currentcolor);
96        stroke: var(--iron-icon-stroke-color, none);
97
98        width: var(--iron-icon-width, 24px);
99        height: var(--iron-icon-height, 24px);
100        @apply(--iron-icon);
101      }
102    </style>
103  </template>
104</dom-module>
105<script>
106Polymer({
107  is: 'iron-icon',
108
109  properties: {
110
111    /**
112      * The name of the icon to use. The name should be of the form:
113      * `iconset_name:icon_name`.
114      */
115    icon: {
116      type: String
117    },
118
119    /**
120      * The name of the theme to used, if one is specified by the
121      * iconset.
122      */
123    theme: {
124      type: String
125    },
126
127    /**
128      * If using iron-icon without an iconset, you can set the src to be
129      * the URL of an individual icon image file. Note that this will take
130      * precedence over a given icon attribute.
131      */
132    src: {
133      type: String
134    },
135
136    /**
137      * @type {!Polymer.IronMeta}
138      */
139    _meta: {
140      value: Polymer.Base.create('iron-meta', {type: 'iconset'})
141    }
142
143  },
144
145  observers: [
146    '_updateIcon(_meta, isAttached)',
147    '_updateIcon(theme, isAttached)',
148    '_srcChanged(src, isAttached)',
149    '_iconChanged(icon, isAttached)'
150  ],
151
152  _DEFAULT_ICONSET: 'icons',
153
154  _iconChanged: function(icon) {
155    var parts = (icon || '').split(':');
156    this._iconName = parts.pop();
157    this._iconsetName = parts.pop() || this._DEFAULT_ICONSET;
158    this._updateIcon();
159  },
160
161  _srcChanged: function(src) {
162    this._updateIcon();
163  },
164
165  _usesIconset: function() {
166    return this.icon || !this.src;
167  },
168
169  /** @suppress {visibility} */
170  _updateIcon: function() {
171    if (this._usesIconset()) {
172      if (this._img && this._img.parentNode) {
173        Polymer.dom(this.root).removeChild(this._img);
174      }
175      if (this._iconName === "") {
176        if (this._iconset) {
177          this._iconset.removeIcon(this);
178        }
179      } else if (this._iconsetName && this._meta) {
180        this._iconset = /** @type {?Polymer.Iconset} */ (
181          this._meta.byKey(this._iconsetName));
182        if (this._iconset) {
183          this._iconset.applyIcon(this, this._iconName, this.theme);
184          this.unlisten(window, 'iron-iconset-added', '_updateIcon');
185        } else {
186          this.listen(window, 'iron-iconset-added', '_updateIcon');
187        }
188      }
189    } else {
190      if (this._iconset) {
191        this._iconset.removeIcon(this);
192      }
193      if (!this._img) {
194        this._img = document.createElement('img');
195        this._img.style.width = '100%';
196        this._img.style.height = '100%';
197        this._img.draggable = false;
198      }
199      this._img.src = this.src;
200      Polymer.dom(this.root).appendChild(this._img);
201    }
202  }
203});
204</script>
205