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 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 Example using icon from default iconset:
30 <core-icon icon="menu"></core-icon>
32 Example using icon `cherry` from custom iconset `fruit`:
34 <core-icon icon="fruit:cherry"></core-icon>
36 See [core-iconset](#core-iconset) and [core-iconset-svg](#core-iconset-svg) for more information about
37 how to use a custom iconset.
39 See [core-icons](http://www.polymer-project.org/components/core-icons/demo.html) for the default set of icons. To use the default set of icons you'll need to include an import for `core-icons.html`. To use a different built-in set of icons, you'll need to include an import for `core-icons/iconsets/<iconset>.html`.
41 @group Polymer Core Elements
43 @homepage polymer.github.io
45 <link rel="import" href="../core-iconset/core-iconset.html">
47 <link rel="stylesheet" href="core-icon.css" shim-shadowdom>
49 <polymer-element name="core-icon" attributes="src icon alt">
56 Polymer('core-icon', {
59 * The URL of an image for the icon. If the src property is specified,
60 * the icon property should not be.
69 * Specifies the icon name or index in the set of icons available in
70 * the icon's icon set. If the icon property is specified,
71 * the src property should not be.
80 * Alternative text content for accessibility support.
81 * 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.
82 * If alt is present and is an empty string, '', it will hide the element from the accessibility layer
83 * 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.
96 defaultIconset: 'icons',
100 meta = document.createElement('core-iconset');
103 // Allow user-provided `aria-label` in preference to any other text alternative.
104 if (this.hasAttribute('aria-label')) {
105 // Set `role` if it has not been overridden.
106 if (!this.hasAttribute('role')) {
107 this.setAttribute('role', 'img');
114 srcChanged: function() {
115 var icon = this._icon || document.createElement('div');
116 icon.textContent = '';
117 icon.setAttribute('fit', '');
118 icon.style.backgroundImage = 'url(' + this.src + ')';
119 icon.style.backgroundPosition = 'center';
120 icon.style.backgroundSize = '100%';
121 if (!icon.parentNode) {
122 this.appendChild(icon);
127 getIconset: function(name) {
128 return meta.byId(name || this.defaultIconset);
131 updateIcon: function(oldVal, newVal) {
136 var parts = String(this.icon).split(':');
137 var icon = parts.pop();
139 var set = this.getIconset(parts.pop());
141 this._icon = set.applyIcon(this, icon);
143 this._icon.setAttribute('fit', '');
147 // Check to see if we're using the old icon's name for our a11y fallback
149 if (oldVal.split(':').pop() == this.getAttribute('aria-label')) {
155 updateAlt: function() {
156 // Respect the user's decision to remove this element from
158 if (this.getAttribute('aria-hidden')) {
162 // Remove element from a11y tree if `alt` is empty, otherwise
163 // use `alt` as `aria-label`.
164 if (this.alt === '') {
165 this.setAttribute('aria-hidden', 'true');
166 if (this.hasAttribute('role')) {
167 this.removeAttribute('role');
169 if (this.hasAttribute('aria-label')) {
170 this.removeAttribute('aria-label');
173 this.setAttribute('aria-label', this.alt ||
174 this.icon.split(':').pop());
175 if (!this.hasAttribute('role')) {
176 this.setAttribute('role', 'img');
178 if (this.hasAttribute('aria-hidden')) {
179 this.removeAttribute('aria-hidden');