|
| 1 | +<h1 id="md-icon">md-icon</h1> |
| 2 | +<p><code>md-icon</code> makes it easier to use <em>vector-based</em> icons in your app. This directive supports both |
| 3 | +icon fonts and SVG icons, but not bitmap-based formats (png, jpg, etc.).</p> |
| 4 | +<div material-docs-example="icon-example"></div> |
| 5 | +<h2 id="usage">Usage</h2> |
| 6 | +<h3 id="registering-new-icons">Registering new icons</h3> |
| 7 | +<p><code>MdIconRegistry</code> is an injectable service that allows you to associate icon names with SVG URLs and |
| 8 | +define aliases for CSS font classes. Its methods are discussed below and listed in the API summary.</p> |
| 9 | +<p>In order to prevent XSS vulnerabilities, any URLs passed to the <code>MdIconRegistry</code> must be marked as |
| 10 | +trusted resource URLs by using Angular's <code>DomSanitizer</code> service.</p> |
| 11 | +<h3 id="font-icons">Font icons</h3> |
| 12 | +<h4 id="ligatures">Ligatures</h4> |
| 13 | +<p>Some fonts are designed to show icons by using |
| 14 | +<a href="https://en.wikipedia.org/wiki/Typographic_ligature">ligatures</a>, for example by rendering the text |
| 15 | +"home" as a home image. To use a ligature icon, put its text in the content of the <code>md-icon</code> |
| 16 | +component.</p> |
| 17 | +<p>By default the |
| 18 | +<a href="http://google.github.io/material-design-icons/#icon-font-for-the-web">Material icons font</a> is used. |
| 19 | +(You will still need to include the HTML to load the font and its CSS, as described in the link). |
| 20 | +You can specify a different font by setting the <code>fontSet</code> input to either the CSS class to apply to |
| 21 | +use the desired font, or to an alias previously registered with |
| 22 | +<code>MdIconRegistry.registerFontClassAlias</code>.</p> |
| 23 | +<h4 id="font-icons-via-css">Font icons via CSS</h4> |
| 24 | +<p>Fonts can also display icons by defining a CSS class for each icon glyph, which typically uses a |
| 25 | +<code>:before</code> selector to cause the icon to appear. |
| 26 | +<a href="https://fortawesome.github.io/Font-Awesome/examples/">FontAwesome</a> uses this approach to display |
| 27 | +its icons. To use such a font, set the <code>fontSet</code> input to the font's CSS class (either the class |
| 28 | +itself or an alias registered with <code>MdIconRegistry.registerFontClassAlias</code>), and set the <code>fontIcon</code> |
| 29 | +input to the class for the specific icon to show.</p> |
| 30 | +<p>For both types of font icons, you can specify the default font class to use when <code>fontSet</code> is not |
| 31 | +explicitly set by calling <code>MdIconRegistry.setDefaultFontSetClass</code>.</p> |
| 32 | +<h3 id="svg-icons">SVG icons</h3> |
| 33 | +<p>When an <code>md-icon</code> component displays an SVG icon, it does so by directly inlining the SVG content |
| 34 | +into the page as a child of the component. (Rather than using an <img> tag or a div background |
| 35 | +image). This makes it easier to apply CSS styles to SVG icons. For example, the default color of the |
| 36 | +SVG content is the CSS <a href="http://www.quirksmode.org/css/color/currentcolor.html">currentColor</a> value. |
| 37 | +This makes SVG icons by default have the same color as surrounding text, and allows you to change |
| 38 | +the color by setting the "color" style on the <code>md-icon</code> element.</p> |
| 39 | +<h4 id="icons-from-urls">Icons from URLs</h4> |
| 40 | +<p>SVG icons can be used either by directly specifying the icon's URL, or by associating an icon name |
| 41 | +with a URL and then referring to the name. To use a URL directly, set the <code>svgSrc</code> input.</p> |
| 42 | +<h4 id="named-icons">Named icons</h4> |
| 43 | +<p>To associate a name with an icon URL, use the <code>addSvgIcon</code> or <code>addSvgIconInNamespace</code> methods of |
| 44 | +<code>MdIconRegistry</code>. After registering an icon, it can be displayed by setting the <code>svgIcon</code> input. |
| 45 | +For an icon in the default namespace, use the name directly. For a non-default namespace, use the |
| 46 | +format <code>[namespace]:[name]</code>.</p> |
| 47 | +<h4 id="icon-sets">Icon sets</h4> |
| 48 | +<p>Icon sets allow grouping multiple icons into a single SVG file. This is done by creating a single |
| 49 | +root <code><svg></code> tag that contains multiple nested <code><svg></code> tags in its <code><defs></code> section. Each of these |
| 50 | +nested tags is identified with an <code>id</code> attribute. This <code>id</code> is used as the name of the icon.</p> |
| 51 | +<p>Icon sets are registered using the <code>addSvgIconSet</code> or <code>addSvgIconSetInNamespace</code> methods of |
| 52 | +<code>MdIconRegistry</code>. After an icon set is registered, each of its embedded icons can be accessed by |
| 53 | +their <code>id</code> attributes. To display an icon from an icon set, use the <code>svgIcon</code> input in the same way |
| 54 | +as for individually registered icons.</p> |
| 55 | +<p>Multiple icon sets can be registered in the same namespace. Requesting an icon whose id appears in |
| 56 | +more than one icon set, the icon from the most recently registered set will be used.</p> |
| 57 | +<p>Note that all SVG icons are fetched via XmlHttpRequest, and due to the same-origin policy their URLs |
| 58 | +must be on the same domain as the containing page, or their servers must be configured to allow |
| 59 | +cross-domain access.</p> |
| 60 | +<h3 id="theming">Theming</h3> |
| 61 | +<p>Icons can be themed to match your "primary" palette, your "accent" palette, or your "warn" palette |
| 62 | +using the <code>color</code> attribute. Simply pass in the desired palette name.</p> |
| 63 | +<h3 id="accessibility">Accessibility</h3> |
| 64 | +<p>If an <code>aria-label</code> attribute is set on the <code>md-icon</code> element, its value will be used as-is. If not, |
| 65 | +the md-icon component will attempt to set the aria-label value from one of these sources:</p> |
| 66 | +<ul> |
| 67 | +<li>The <code>alt</code> attribute</li> |
| 68 | +<li>The <code>fontIcon</code> input</li> |
| 69 | +<li>The name of the icon from the <code>svgIcon</code> input (not including any namespace)</li> |
| 70 | +<li>The text content of the component (for ligature icons)</li> |
| 71 | +</ul> |
0 commit comments