-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(icon-button): Add SVG support #3310
Changes from 4 commits
c4799bc
fe86346
b18cfba
ff8e432
ceaa86a
49eba69
4cb157c
0d1146c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,19 +70,20 @@ iconButtonRipple.unbounded = true; | |
|
||
### Icon Button Toggle | ||
|
||
The icon button can be used to toggle between an on and off icon. To style an icon button as an icon button toggle, add the | ||
`data-toggle-on` and `data-toggle-off` attributes to the `mdc-icon-button` element. Then instantiate an `MDCIconButtonToggle` on the root element. | ||
The icon button can be used to toggle between an on and off icon. To style an icon button as an icon button toggle, add | ||
both icons as child elements and place the `data-toggle-on` attribute on the icon that should be on. If the button | ||
should be initialized in the "on" state, then add the `mdc-icon-button--on` class to the parent `button`. Then | ||
instantiate an `MDCIconButtonToggle` on the root element. | ||
|
||
```html | ||
<button id="add-to-favorites" | ||
class="mdc-icon-button material-icons" | ||
class="mdc-icon-button" | ||
aria-label="Add to favorites" | ||
aria-hidden="true" | ||
aria-pressed="false" | ||
data-toggle-on-content="favorite" | ||
data-toggle-on-label="Remove from favorites" | ||
data-toggle-off-content="favorite_border" | ||
data-toggle-off-label="Add to favorites">favorite_border</button> | ||
aria-pressed="false"> | ||
<i class="material-icons mdc-icon-button__icon">favorite</i> | ||
<i class="material-icons mdc-icon-button__icon" data-toggle-on>favorite_border</i> | ||
</button> | ||
``` | ||
|
||
```js | ||
|
@@ -91,38 +92,54 @@ var toggleButton = new mdc.iconButton.MDCIconButtonToggle(document.getElementByI | |
|
||
#### Icon Button Toggle States | ||
|
||
Note the use of `data-toggle-*` properties in the above examples. When an MDCIconButtonToggle | ||
instance is toggled, it looks at these data attributes to determine how to update the element. This is what | ||
allows MDCIconButtonToggle to be so flexible. The `data-toggle-on-*` properties will be used when the is | ||
MDCIconButtonToggle is toggled on, and vice versa for `data-toggle-off-*`. | ||
Note the use of `data-toggle-on` attribute in the above examples. This attribute indicates which element is the | ||
on element. When the `mdc-icon-button--on` class is present, the element with the `data-toggle-on` attribute | ||
will be shown and the other element will be hidden. When the `mdc-icon-button--on` class is not present, the element | ||
with the `data-toggle-on` attribute will be hidden and the other element will beshown. This is what allows | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. beshown -> be shown |
||
MDCIconButtonToggle to be so flexible. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This is what allows MDCIconButtonToggle to be so flexible." -> "This structure allows MDCIconButton to be flexible enough to support both icon fonts and images/SVGs." |
||
|
||
Attribute | Description | ||
--- | --- | ||
`data-toggle-<TOGGLE STATE>-label` | The value to apply to the element's "aria-label" attribute. | ||
`data-toggle-<TOGGLE STATE>-content` | The text content to set on the element. Note that if an inner icon is used, the text content will be set on that element instead. | ||
`data-toggle-<TOGGLE STATE>-class` | A CSS class to apply to the icon element. The same rules regarding inner icon elements described for `content` apply here as well. | ||
`data-toggle-on` | Used to indicate which element in an icon button toggle is the `on` value. | ||
|
||
#### Icon Button Toggle with Font Awesome | ||
#### Icon Button Toggle with SVG | ||
|
||
The icon button toggle can be used with other font libraries such as Font Awesome that use an inner icon element. | ||
The icon button toggle can be used with SVGs. | ||
|
||
```html | ||
<button id="star-this-item" | ||
class="mdc-icon-button" | ||
class="mdc-icon-button mdc-icon-button--on" | ||
aria-label="Unstar this item" | ||
aria-hidden="true" | ||
aria-pressed="true"> | ||
<svg class="mdc-icon-button__icon"> | ||
... | ||
</svg> | ||
<svg class="mdc-icon-button__icon" data-toggle-on> | ||
... | ||
</svg> | ||
</button> | ||
``` | ||
|
||
#### Icon Button Toggle with an Image | ||
|
||
The icon button toggle can be used with `img` tags. | ||
|
||
```html | ||
<button id="star-this-item" | ||
class="mdc-icon-button mdc-icon-button--on" | ||
aria-label="Unstar this item" | ||
aria-hidden="true" | ||
aria-pressed="true" | ||
data-toggle-on-class="fa-star" | ||
data-toggle-on-label="Unstar this item" | ||
data-toggle-off-class="fa-star-o" | ||
data-toggle-off-label="Star this item"><i class="fa fa-2x fa-star"></i></button> | ||
aria-pressed="true"> | ||
<img src="" class="mdc-icon-button__icon"/> | ||
<img src="" class="mdc-icon-button__icon" data-toggle-on/> | ||
</button> | ||
``` | ||
|
||
### Icons | ||
|
||
The icon button can be used with a standard icon library such as Material Icons or Font Awesome, or with an `svg`. | ||
The icon button toggle should only be used with an standard icon library. We recommend you use | ||
[Material Icons](https://material.io/tools/icons) from Google Fonts. | ||
The icon button can be used with a standard icon library such as Material Icons, or with an `svg`. | ||
We recommend you use [Material Icons](https://material.io/tools/icons) from Google Fonts. | ||
|
||
### Disabled | ||
|
||
|
@@ -140,6 +157,7 @@ cannot be disabled. Disabled icon buttons cannot be interacted with and have no | |
CSS Class | Description | ||
--- | --- | ||
`mdc-icon-button` | Mandatory. | ||
`mdc-icon-button--on` | Used to indicate the toggle button icon that is the not-selected option. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't sound right... This class is applied to the root element when the toggle should be in the "on" state, right? |
||
|
||
### Sass Mixins | ||
|
||
|
@@ -171,9 +189,8 @@ If you are using a JavaScript framework, such as React or Angular, you can creat | |
|
||
Method Signature | Description | ||
--- | --- | ||
`addClass(className: string) => void` | Adds a class to the root element, or the inner icon element. | ||
`removeClass(className: string) => void` | Removes a class from the root element, or the inner icon element. | ||
`setText(text: string) => void` | Sets the text content of the root element, or the inner icon element. | ||
`addClass(className: string) => void` | Adds a class to a icon element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why were these docs changed? Doesn't this still apply to the root element? |
||
`removeClass(className: string) => void` | Removes a class from a icon element. | ||
`getAttr(name: string) => string` | Returns the value of the attribute `name` on the root element. Can also return `null`, similar to `getAttribute()`. | ||
`setAttr(name: string, value: string) => void` | Sets the attribute `name` to `value` on the root element. | ||
`notifyChange(evtData: {isOn: boolean}) => void` | Broadcasts a change notification, passing along the `evtData` to the environment's event handling system. In our vanilla implementation, Custom Events are used for this. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,31 +44,16 @@ class MDCIconButtonToggleAdapter { | |
removeClass(className) {} | ||
|
||
/** | ||
* @param {string} type | ||
* @param {!EventListener} handler | ||
*/ | ||
registerInteractionHandler(type, handler) {} | ||
|
||
/** | ||
* @param {string} type | ||
* @param {!EventListener} handler | ||
*/ | ||
deregisterInteractionHandler(type, handler) {} | ||
|
||
/** @param {string} text */ | ||
setText(text) {} | ||
|
||
/** | ||
* @param {string} name | ||
* @return {string} | ||
*/ | ||
getAttr(name) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You removed getAttr but it's still in the docs |
||
* @param {string} className | ||
* @return {boolean} | ||
* */ | ||
hasClass(className) {} | ||
|
||
/** | ||
* @param {string} name | ||
* @param {string} value | ||
* @param {string} attrName | ||
* @param {string} attrValue | ||
*/ | ||
setAttr(name, value) {} | ||
setAttr(attrName, attrValue) {} | ||
|
||
/** @param {!IconButtonToggleEvent} evtData */ | ||
notifyChange(evtData) {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,12 @@ | |
/** @enum {string} */ | ||
const cssClasses = { | ||
ROOT: 'mdc-icon-button', | ||
ICON_BUTTON_ON_CLASS: 'mdc-icon-button--on', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}; | ||
|
||
/** @enum {string} */ | ||
const strings = { | ||
DATA_TOGGLE_ON_LABEL: 'data-toggle-on-label', | ||
DATA_TOGGLE_ON_CONTENT: 'data-toggle-on-content', | ||
DATA_TOGGLE_ON_CLASS: 'data-toggle-on-class', | ||
DATA_TOGGLE_OFF_LABEL: 'data-toggle-off-label', | ||
DATA_TOGGLE_OFF_CONTENT: 'data-toggle-off-content', | ||
DATA_TOGGLE_OFF_CLASS: 'data-toggle-off-class', | ||
DATA_TOGGLE_ON_ATTR: 'data-toggle-on', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer used since it's now a class |
||
ARIA_PRESSED: 'aria-pressed', | ||
ARIA_LABEL: 'aria-label', | ||
CHANGE_EVENT: 'MDCIconButtonToggle:change', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we need to select on this data attribute in CSS, it might make sense to make it a class instead (e.g.
mdc-icon-button__icon--on
?) - attribute selectors perform a lot slower in IE and Edge.