Skip to content

fix(material/button): avoid setting a tabindex on all link buttons #22901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-button/button-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const MAT_ANCHOR_HOST = {
// Note that we ignore the user-specified tabindex when it's disabled for
// consistency with the `mat-button` applied on native buttons where even
// though they have an index, they're not tabbable.
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
'[attr.tabindex]': 'disabled ? -1 : tabIndex',
'[attr.aria-disabled]': 'disabled.toString()',
// MDC automatically applies the primary theme color to the button, but we want to support
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
Expand Down
18 changes: 14 additions & 4 deletions src/material-experimental/mdc-button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ describe('MDC-based MatButton', () => {
let fixture = TestBed.createComponent(TestApp);
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('a'))!;
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe(null);
fixture.detectChanges();

expect(buttonDebugElement.nativeElement.hasAttribute('tabindex')).toBe(false);

testComponent.isDisabled = true;
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe('-1');
expect(buttonDebugElement.nativeElement.getAttribute('tabindex')).toBe('-1');
});

it('should add aria-disabled attribute if disabled', () => {
Expand Down Expand Up @@ -219,18 +221,26 @@ describe('MDC-based MatButton', () => {
fixture.componentInstance.tabIndex = 3;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
expect(buttonElement.getAttribute('tabindex'))
.withContext('Expected custom tabindex to be set')
.toBe('3');

testComponent.isDisabled = true;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
expect(buttonElement.getAttribute('tabindex'))
.withContext('Expected custom tabindex to be overwritten when disabled.')
.toBe('-1');
});

it('should not set a default tabindex on enabled links', () => {
const fixture = TestBed.createComponent(TestApp);
const buttonElement = fixture.debugElement.query(By.css('a'))!.nativeElement;
fixture.detectChanges();

expect(buttonElement.hasAttribute('tabindex')).toBe(false);
});

describe('change detection behavior', () => {
it('should not run change detection for disabled anchor but should prevent the default behavior and stop event propagation', () => {
const appRef = TestBed.inject(ApplicationRef);
Expand Down
22 changes: 15 additions & 7 deletions src/material/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ describe('MatButton', () => {
});

it('should remove tabindex if disabled', () => {
const fixture = TestBed.createComponent(TestApp);
const testComponent = fixture.debugElement.componentInstance;
const buttonDebugElement = fixture.debugElement.query(By.css('a'))!;
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe(null);
let fixture = TestBed.createComponent(TestApp);
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('a'))!;
expect(buttonDebugElement.nativeElement.hasAttribute('tabindex')).toBe(false);

testComponent.isDisabled = true;
fixture.detectChanges();
expect(buttonDebugElement.nativeElement.getAttribute('tabIndex')).toBe('-1');
expect(buttonDebugElement.nativeElement.getAttribute('tabindex')).toBe('-1');
});

it('should add aria-disabled attribute if disabled', () => {
Expand Down Expand Up @@ -233,18 +233,26 @@ describe('MatButton', () => {
fixture.componentInstance.tabIndex = 3;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
expect(buttonElement.getAttribute('tabindex'))
.withContext('Expected custom tabindex to be set')
.toBe('3');

testComponent.isDisabled = true;
fixture.detectChanges();

expect(buttonElement.getAttribute('tabIndex'))
expect(buttonElement.getAttribute('tabindex'))
.withContext('Expected custom tabindex to be overwritten when disabled.')
.toBe('-1');
});

it('should not set a default tabindex on enabled links', () => {
const fixture = TestBed.createComponent(TestApp);
const buttonElement = fixture.debugElement.query(By.css('a'))!.nativeElement;
fixture.detectChanges();

expect(buttonElement.hasAttribute('tabindex')).toBe(false);
});

describe('change detection behavior', () => {
it('should not run change detection for disabled anchor but should prevent the default behavior and stop event propagation', () => {
const appRef = TestBed.inject(ApplicationRef);
Expand Down
2 changes: 1 addition & 1 deletion src/material/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class MatButton
// Note that we ignore the user-specified tabindex when it's disabled for
// consistency with the `mat-button` applied on native buttons where even
// though they have an index, they're not tabbable.
'[attr.tabindex]': 'disabled ? -1 : (tabIndex || 0)',
'[attr.tabindex]': 'disabled ? -1 : tabIndex',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': 'disabled.toString()',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
Expand Down