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