Skip to content

Commit 69f0124

Browse files
committed
fix(material/button): avoid setting a tabindex on all link buttons
Fixes that the button component was always setting a `tabindex="0"` when it's placed on a link element. This is unnecessary, because links are in the tab order by default. We actually had a test that has an assertion against this, but it was passing by accident because we weren't running change detection before making the asserting.
1 parent 23dfbbb commit 69f0124

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/material-experimental/mdc-button/button-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export const MAT_ANCHOR_HOST = {
148148
// Note that we ignore the user-specified tabindex when it's disabled for
149149
// consistency with the `mat-button` applied on native buttons where even
150150
// though they have an index, they're not tabbable.
151-
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
151+
'[attr.tabindex]': 'disabled ? -1 : tabIndex',
152152
'[attr.aria-disabled]': 'disabled.toString()',
153153
// MDC automatically applies the primary theme color to the button, but we want to support
154154
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to

src/material-experimental/mdc-button/button.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ describe('MDC-based MatButton', () => {
163163
let fixture = TestBed.createComponent(TestApp);
164164
let testComponent = fixture.debugElement.componentInstance;
165165
let buttonDebugElement = fixture.debugElement.query(By.css('a'))!;
166-
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe(null);
166+
fixture.detectChanges();
167+
168+
expect(buttonDebugElement.nativeElement.hasAttribute('tabindex')).toBe(false);
167169

168170
testComponent.isDisabled = true;
169171
fixture.detectChanges();
170-
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe('-1');
172+
expect(buttonDebugElement.nativeElement.getAttribute('tabindex')).toBe('-1');
171173
});
172174

173175
it('should add aria-disabled attribute if disabled', () => {
@@ -208,15 +210,23 @@ describe('MDC-based MatButton', () => {
208210
fixture.componentInstance.tabIndex = 3;
209211
fixture.detectChanges();
210212

211-
expect(buttonElement.getAttribute('tabIndex'))
213+
expect(buttonElement.getAttribute('tabindex'))
212214
.toBe('3', 'Expected custom tabindex to be set');
213215

214216
testComponent.isDisabled = true;
215217
fixture.detectChanges();
216218

217-
expect(buttonElement.getAttribute('tabIndex'))
219+
expect(buttonElement.getAttribute('tabindex'))
218220
.toBe('-1', 'Expected custom tabindex to be overwritten when disabled.');
219221
});
222+
223+
it('should not set a default tabindex on enabled links', () => {
224+
const fixture = TestBed.createComponent(TestApp);
225+
const buttonElement = fixture.debugElement.query(By.css('a'))!.nativeElement;
226+
fixture.detectChanges();
227+
228+
expect(buttonElement.hasAttribute('tabindex')).toBe(false);
229+
});
220230
});
221231

222232
// Ripple tests.

src/material/button/button.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ describe('MatButton', () => {
178178
let fixture = TestBed.createComponent(TestApp);
179179
let testComponent = fixture.debugElement.componentInstance;
180180
let buttonDebugElement = fixture.debugElement.query(By.css('a'))!;
181-
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe(null);
181+
expect(buttonDebugElement.nativeElement.hasAttribute('tabindex')).toBe(false);
182182

183183
testComponent.isDisabled = true;
184184
fixture.detectChanges();
185-
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe('-1');
185+
expect(buttonDebugElement.nativeElement.getAttribute('tabindex')).toBe('-1');
186186
});
187187

188188
it('should add aria-disabled attribute if disabled', () => {
@@ -223,15 +223,23 @@ describe('MatButton', () => {
223223
fixture.componentInstance.tabIndex = 3;
224224
fixture.detectChanges();
225225

226-
expect(buttonElement.getAttribute('tabIndex'))
226+
expect(buttonElement.getAttribute('tabindex'))
227227
.toBe('3', 'Expected custom tabindex to be set');
228228

229229
testComponent.isDisabled = true;
230230
fixture.detectChanges();
231231

232-
expect(buttonElement.getAttribute('tabIndex'))
232+
expect(buttonElement.getAttribute('tabindex'))
233233
.toBe('-1', 'Expected custom tabindex to be overwritten when disabled.');
234234
});
235+
236+
it('should not set a default tabindex on enabled links', () => {
237+
const fixture = TestBed.createComponent(TestApp);
238+
const buttonElement = fixture.debugElement.query(By.css('a'))!.nativeElement;
239+
fixture.detectChanges();
240+
241+
expect(buttonElement.hasAttribute('tabindex')).toBe(false);
242+
});
235243
});
236244

237245
// Ripple tests.

src/material/button/button.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class MatButton extends _MatButtonBase
156156
// Note that we ignore the user-specified tabindex when it's disabled for
157157
// consistency with the `mat-button` applied on native buttons where even
158158
// though they have an index, they're not tabbable.
159-
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
159+
'[attr.tabindex]': 'disabled ? -1 : tabIndex',
160160
'[attr.disabled]': 'disabled || null',
161161
'[attr.aria-disabled]': 'disabled.toString()',
162162
'(click)': '_haltDisabledEvents($event)',

0 commit comments

Comments
 (0)