Skip to content

Commit 0064dec

Browse files
committed
fix(tabs): Add role to mat-tab-nav-bar and mat-tab-link
This is already correctly set on the main tab component: https://material.angular.io/components/tabs/overview mat-tab-group has role="tablist" https://github.com/angular/material2/blob/abc3d38c57146443c848d5ba26fd2fab8ca185d6/src/lib/tabs/tab-header.html#L11 mat-tab has role="tab" https://github.com/angular/material2/blob/abc3d38c57146443c848d5ba26fd2fab8ca185d6/src/lib/tabs/tab-group.html#L6 But the standalone version mat-tab-nav-bar and mat-tab-link do not have role set. This is a potential breaking change insofar as someone was already setting role explicitly (test added for this case to verify it doesn't break), but the impact is low and should be rare: I have found no examples on github and only two examples in google3 of role being set, and at worse it sets role="tablist" on mat-tab-nav-bar, which would result in a duplicate role="tablist" nested one layer below, but which is not that harmful and is easy to fix. Existing code that directly sets role on mat-tab-link will have that role overridden, but I don't know that this is currently happening.
1 parent 69e4ca7 commit 0064dec

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<div class="mat-tab-links" (cdkObserveContent)="_alignInkBar()">
1+
<div class="mat-tab-links" (cdkObserveContent)="_alignInkBar()" role="tablist">
22
<ng-content></ng-content>
33
<mat-ink-bar></mat-ink-bar>
44
</div>
5-

src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('MatTabNavBar', () => {
1616
imports: [MatTabsModule],
1717
declarations: [
1818
SimpleTabNavBarTestApp,
19+
TabLink,
1920
TabLinkWithNgIf,
2021
TabLinkWithTabIndexBinding,
2122
TabLinkWithNativeTabindexAttr,
@@ -273,6 +274,16 @@ describe('MatTabNavBar', () => {
273274

274275
expect(tabLink.tabIndex).toBe(3, 'Expected the tabIndex to be have been set to 3.');
275276
});
277+
278+
it('should set role on tablist and tab', () => {
279+
const fixture = TestBed.createComponent(TabLink);
280+
fixture.detectChanges();
281+
282+
const tabList = fixture.debugElement.query(By.css('.mat-tab-links'));
283+
expect(tabList.nativeElement.getAttribute('role')).toEqual('tablist');
284+
const tabLinkElement = tabList.query(By.directive(MatTabLink)).nativeElement;
285+
expect(tabLinkElement.getAttribute('role')).toEqual('tab');
286+
});
276287
});
277288

278289
@Component({
@@ -301,6 +312,15 @@ class SimpleTabNavBarTestApp {
301312
activeIndex = 0;
302313
}
303314

315+
@Component({
316+
template: `
317+
<nav mat-tab-nav-bar>
318+
<a mat-tab-link role="willbeoverridden">Link</a>
319+
</nav>
320+
`
321+
})
322+
class TabLink {}
323+
304324
@Component({
305325
template: `
306326
<nav mat-tab-nav-bar>

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export const _MatTabLinkMixinBase =
172172
inputs: ['disabled', 'disableRipple', 'tabIndex'],
173173
host: {
174174
'class': 'mat-tab-link',
175+
'role': 'tab',
175176
'[attr.aria-disabled]': 'disabled.toString()',
176177
'[attr.tabIndex]': 'tabIndex',
177178
'[class.mat-tab-disabled]': 'disabled',

0 commit comments

Comments
 (0)