2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 The `core-icon` element displays an icon. By default an icon renders as a 24px square.
15 <core-icon src="star.png"></core-icon>
17 Example setting size to 32px x 32px:
19 <core-icon class="big" src="big_star.png"></core-icon>
28 The core elements include several sets of icons.
29 To use the default set of icons, import `core-icons.html` and use the `icon` attribute to specify an icon:
31 <!-- import default iconset and core-icon -->
32 <link rel="import" href="/components/core-icons/core-icons.html">
34 <core-icon icon="menu"></core-icon>
36 To use a different built-in set of icons, import `core-icons/<iconset>-icons.html`, and
37 specify the icon as `<iconset>:<icon>`. For example:
39 <!-- import communication iconset and core-icon -->
40 <link rel="import" href="/components/core-icons/communication-icons.html">
42 <core-icon icon="communication:email"></core-icon>
44 You can also create custom icon sets of bitmap or SVG icons.
46 Example of using an icon named `cherry` from a custom iconset with the ID `fruit`:
48 <core-icon icon="fruit:cherry"></core-icon>
50 See [core-iconset](#core-iconset) and [core-iconset-svg](#core-iconset-svg) for more information about
51 how to create a custom iconset.
53 See [core-icons](http://www.polymer-project.org/components/core-icons/demo.html) for the default set of icons.
55 @group Polymer Core Elements
57 @homepage polymer.github.io
59 <link rel=
"import" href=
"../core-iconset/core-iconset.html">
61 <link rel=
"stylesheet" href=
"core-icon.css" shim-shadowdom
>
63 <polymer-element name=
"core-icon" attributes=
"src icon alt">
70 Polymer('core-icon', {
73 * The URL of an image for the icon. If the src property is specified,
74 * the icon property should not be.
83 * Specifies the icon name or index in the set of icons available in
84 * the icon's icon set. If the icon property is specified,
85 * the src property should not be.
94 * Alternative text content for accessibility support.
95 * If alt is present and not empty, it will set the element's role to img and add an aria-label whose content matches alt.
96 * If alt is present and is an empty string, '', it will hide the element from the accessibility layer
97 * If alt is not present, it will set the element's role to img and the element will fallback to using the icon attribute for its aria-label.
106 'icon': 'updateIcon',
110 defaultIconset
: 'icons',
114 meta
= document
.createElement('core-iconset');
117 // Allow user-provided `aria-label` in preference to any other text alternative.
118 if (this.hasAttribute('aria-label')) {
119 // Set `role` if it has not been overridden.
120 if (!this.hasAttribute('role')) {
121 this.setAttribute('role', 'img');
128 srcChanged: function() {
129 var icon
= this._icon
|| document
.createElement('div');
130 icon
.textContent
= '';
131 icon
.setAttribute('fit', '');
132 icon
.style
.backgroundImage
= 'url(' + this.src
+ ')';
133 icon
.style
.backgroundPosition
= 'center';
134 icon
.style
.backgroundSize
= '100%';
135 if (!icon
.parentNode
) {
136 this.appendChild(icon
);
141 getIconset: function(name
) {
142 return meta
.byId(name
|| this.defaultIconset
);
145 updateIcon: function(oldVal
, newVal
) {
150 var parts
= String(this.icon
).split(':');
151 var icon
= parts
.pop();
153 var set = this.getIconset(parts
.pop());
155 this._icon
= set.applyIcon(this, icon
);
157 this._icon
.setAttribute('fit', '');
161 // Check to see if we're using the old icon's name for our a11y fallback
163 if (oldVal
.split(':').pop() == this.getAttribute('aria-label')) {
169 updateAlt: function() {
170 // Respect the user's decision to remove this element from
172 if (this.getAttribute('aria-hidden')) {
176 // Remove element from a11y tree if `alt` is empty, otherwise
177 // use `alt` as `aria-label`.
178 if (this.alt
=== '') {
179 this.setAttribute('aria-hidden', 'true');
180 if (this.hasAttribute('role')) {
181 this.removeAttribute('role');
183 if (this.hasAttribute('aria-label')) {
184 this.removeAttribute('aria-label');
187 this.setAttribute('aria-label', this.alt
||
188 this.icon
.split(':').pop());
189 if (!this.hasAttribute('role')) {
190 this.setAttribute('role', 'img');
192 if (this.hasAttribute('aria-hidden')) {
193 this.removeAttribute('aria-hidden');