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