Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 32b5b9d

Browse files
fix(icon-button): Remove unused styles, update docs, code cleanup (#2957)
Fixes a bug with the foundation always being initialized "on".
1 parent 01abc11 commit 32b5b9d

File tree

7 files changed

+37
-51
lines changed

7 files changed

+37
-51
lines changed

packages/mdc-icon-button/README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ npm install @material/icon-button
5151
```scss
5252
@import "@material/icon-button/mdc-icon-button";
5353
```
54+
5455
### JavaScript Instantiation
5556

5657
The icon button will work without JavaScript, but you can enhance it to have a ripple effect by instantiating `MDCRipple` on the root element.
@@ -81,7 +82,7 @@ The icon button can be used to toggle between an on and off icon. To style an ic
8182
data-toggle-on-content="favorite"
8283
data-toggle-on-label="Remove from favorites"
8384
data-toggle-off-content="favorite_border"
84-
data-toggle-off-label="Add to favorites">favorites_border</button>
85+
data-toggle-off-label="Add to favorites">favorite_border</button>
8586
```
8687

8788
```js
@@ -101,10 +102,27 @@ Attribute | Description
101102
`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.
102103
`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.
103104

105+
#### Icon Button Toggle with Font Awesome
106+
107+
The icon button toggle can be used with other font libraries such as Font Awesome that use an inner icon element.
108+
109+
```html
110+
<button id="star-this-item"
111+
class="mdc-icon-button"
112+
aria-label="Unstar this item"
113+
aria-hidden="true"
114+
aria-pressed="true"
115+
data-toggle-on-class="fa-star"
116+
data-toggle-on-label="Unstar this item"
117+
data-toggle-off-class="fa-star-o"
118+
data-toggle-off-label="Star this item"><i class="fa fa-2x fa-star"></i></button>
119+
```
120+
104121
### Icons
105122

106-
The icon button can be used with a standard icon library or an `svg`. The icon button toggle should only be used with
107-
an standard icon library. We recommend you use [Material Icons](https://material.io/tools/icons) from Google Fonts.
123+
The icon button can be used with a standard icon library such as Material Icons or Font Awesome, or with an `svg`.
124+
The icon button toggle should only be used with an standard icon library. We recommend you use
125+
[Material Icons](https://material.io/tools/icons) from Google Fonts.
108126

109127
### Disabled
110128

packages/mdc-icon-button/adapter.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ class MDCIconButtonToggleAdapter {
5858
/** @param {string} text */
5959
setText(text) {}
6060

61-
/** @return {number} */
62-
getTabIndex() {}
63-
64-
/** @param {number} tabIndex */
65-
setTabIndex(tabIndex) {}
66-
6761
/**
6862
* @param {string} name
6963
* @return {string}
@@ -76,9 +70,6 @@ class MDCIconButtonToggleAdapter {
7670
*/
7771
setAttr(name, value) {}
7872

79-
/** @param {string} name */
80-
removeAttr(name) {}
81-
8273
/** @param {!IconButtonToggleEvent} evtData */
8374
notifyChange(evtData) {}
8475
}

packages/mdc-icon-button/foundation.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,23 @@ class MDCIconButtonToggleFoundation extends MDCFoundation {
3939
registerInteractionHandler: (/* type: string, handler: EventListener */) => {},
4040
deregisterInteractionHandler: (/* type: string, handler: EventListener */) => {},
4141
setText: (/* text: string */) => {},
42-
getTabIndex: () => /* number */ 0,
43-
setTabIndex: (/* tabIndex: number */) => {},
4442
getAttr: (/* name: string */) => /* string */ '',
4543
setAttr: (/* name: string, value: string */) => {},
46-
removeAttr: (/* name: string */) => {},
4744
notifyChange: (/* evtData: IconButtonToggleEvent */) => {},
4845
};
4946
}
5047

5148
constructor(adapter) {
5249
super(Object.assign(MDCIconButtonToggleFoundation.defaultAdapter, adapter));
5350

51+
const {ARIA_PRESSED} = MDCIconButtonToggleFoundation.strings;
52+
5453
/** @private {boolean} */
55-
this.on_ = false;
54+
this.on_ = this.adapter_.getAttr(ARIA_PRESSED) === 'true';
5655

5756
/** @private {boolean} */
5857
this.disabled_ = false;
5958

60-
/** @private {number} */
61-
this.savedTabIndex_ = -1;
62-
6359
/** @private {?IconButtonToggleState} */
6460
this.toggleOnData_ = null;
6561

@@ -72,7 +68,6 @@ class MDCIconButtonToggleFoundation extends MDCFoundation {
7268

7369
init() {
7470
this.refreshToggleData();
75-
this.savedTabIndex_ = this.adapter_.getTabIndex();
7671
this.adapter_.registerInteractionHandler('click', this.clickHandler_);
7772
}
7873

packages/mdc-icon-button/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ class MDCIconButtonToggle extends MDCComponent {
6464
registerInteractionHandler: (type, handler) => this.root_.addEventListener(type, handler),
6565
deregisterInteractionHandler: (type, handler) => this.root_.removeEventListener(type, handler),
6666
setText: (text) => this.iconEl_.textContent = text,
67-
getTabIndex: () => /* number */ this.root_.tabIndex,
68-
setTabIndex: (tabIndex) => this.root_.tabIndex = tabIndex,
69-
getAttr: (name, value) => this.root_.getAttribute(name, value),
67+
getAttr: (name) => this.root_.getAttribute(name),
7068
setAttr: (name, value) => this.root_.setAttribute(name, value),
71-
removeAttr: (name) => this.root_.removeAttribute(name),
7269
notifyChange: (evtData) => this.emit(MDCIconButtonToggleFoundation.strings.CHANGE_EVENT, evtData),
7370
});
7471
}

packages/mdc-icon-button/mdc-icon-button.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,4 @@
2323
@include mdc-states;
2424
}
2525

26-
.mdc-icon-button--disabled {
27-
@include mdc-theme-prop(color, text-disabled-on-light);
28-
29-
pointer-events: none;
30-
}
31-
3226
// postcss-bem-linter: end

test/unit/mdc-icon-button/foundation.test.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ test('exports cssClasses', () => {
3636
test('defaultAdapter returns a complete adapter implementation', () => {
3737
verifyDefaultAdapter(MDCIconButtonToggleFoundation, [
3838
'addClass', 'removeClass', 'registerInteractionHandler', 'deregisterInteractionHandler',
39-
'setText', 'getTabIndex', 'setTabIndex', 'getAttr', 'setAttr', 'removeAttr', 'notifyChange',
39+
'setText', 'getAttr', 'setAttr', 'notifyChange',
4040
]);
4141
});
4242

4343
const setupTest = () => setupFoundationTest(MDCIconButtonToggleFoundation);
4444

4545
test('#constructor sets on to false', () => {
46-
const {foundation} = setupTest();
47-
assert.isNotOk(foundation.isOn());
46+
const {mockAdapter} = setupTest();
47+
td.when(mockAdapter.getAttr(strings.ARIA_PRESSED)).thenReturn('false');
48+
const foundation = new MDCIconButtonToggleFoundation(mockAdapter);
49+
assert.isFalse(foundation.isOn());
50+
});
51+
52+
test('#constructor sets on to true if the toggle is pressed', () => {
53+
const {mockAdapter} = setupTest();
54+
td.when(mockAdapter.getAttr(strings.ARIA_PRESSED)).thenReturn('true');
55+
const foundation = new MDCIconButtonToggleFoundation(mockAdapter);
56+
assert.isTrue(foundation.isOn());
4857
});
4958

5059
test('#toggle flips on', () => {

test/unit/mdc-icon-button/mdc-icon-button-toggle.test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,6 @@ test('#adapter.setText sets the text content of the inner icon element when used
158158
assert.equal(root.querySelector('#icon').textContent, 'foo');
159159
});
160160

161-
test('#adapter.getTabIndex returns the tabIndex of the element', () => {
162-
const {component} = setupTest({tabIndex: 4});
163-
assert.equal(component.getDefaultFoundation().adapter_.getTabIndex(), 4);
164-
});
165-
166-
test('#adapter.setTabIndex sets the tabIndex of the element', () => {
167-
const {root, component} = setupTest({tabIndex: 4});
168-
component.getDefaultFoundation().adapter_.setTabIndex(2);
169-
assert.equal(root.tabIndex, 2);
170-
});
171-
172161
test('#adapter.getAttr retrieves an attribute from the root element', () => {
173162
const {root, component} = setupTest();
174163
root.setAttribute('aria-label', 'hello');
@@ -181,13 +170,6 @@ test('#adapter.setAttr sets an attribute on the root element', () => {
181170
assert.equal(root.getAttribute('aria-label'), 'hello');
182171
});
183172

184-
test('#adapter.removeAttr removes an attribute from the root element', () => {
185-
const {root, component} = setupTest();
186-
root.setAttribute('aria-label', 'hello');
187-
component.getDefaultFoundation().adapter_.removeAttr('aria-label');
188-
assert.isNotOk(root.hasAttribute('aria-label'));
189-
});
190-
191173
test(`#adapter.notifyChange broadcasts a ${MDCIconButtonToggleFoundation.strings.CHANGE_EVENT} custom event`, () => {
192174
const {root, component} = setupTest();
193175
const handler = td.func('custom event handler');

0 commit comments

Comments
 (0)