Skip to content

Commit cdffb03

Browse files
committed
fix(tabs): unable to tab to tab content
Currently the tab's content doesn't have a `tabindex` which means that if it doesn't have any focusable elements, users with assistive technology will skip over the content completely. These changes add a `tabindex` to the content of the currently-selected tab, based on [the example of accessible tabs from the ARIA best practices](https://www.w3.org/TR/wai-aria-practices-1.1/examples/tabs/tabs-2/tabs.html).
1 parent ab0f30d commit cdffb03

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/material/tabs/tab-group.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*ngFor="let tab of _tabsArray; let i = index"
4141
[id]="_getTabContentId(i)"
4242
[attr.aria-labelledby]="_getTabLabelId(i)"
43+
[attr.tabindex]="selectedIndex === i ? 0 : null"
4344
[class.mat-tab-body-active]="selectedIndex == i"
4445
[content]="tab.content!"
4546
[position]="tab.position!"

src/material/tabs/tab-group.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
@include mat-fill;
5151
display: block;
5252
overflow: hidden;
53+
outline: 0;
5354

5455
// Fix for auto content wrapping in IE11
5556
flex-basis: 100%;

src/material/tabs/tab-group.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,26 @@ describe('MatTabGroup', () => {
291291
subscription.unsubscribe();
292292
});
293293

294+
it('should set the correct tabindex on the tab content elements', fakeAsync(() => {
295+
fixture.detectChanges();
296+
tick();
297+
298+
const contentElements: NodeListOf<HTMLElement> =
299+
fixture.nativeElement.querySelectorAll('.mat-tab-body');
300+
301+
expect(contentElements[0].hasAttribute('tabindex')).toBe(false);
302+
expect(contentElements[1].getAttribute('tabindex')).toBe('0');
303+
expect(contentElements[2].hasAttribute('tabindex')).toBe(false);
304+
305+
fixture.componentInstance.selectedIndex = 2;
306+
fixture.detectChanges();
307+
tick();
308+
309+
expect(contentElements[0].hasAttribute('tabindex')).toBe(false);
310+
expect(contentElements[1].hasAttribute('tabindex')).toBe(false);
311+
expect(contentElements[2].getAttribute('tabindex')).toBe('0');
312+
}));
313+
294314
});
295315

296316
describe('aria labelling', () => {

0 commit comments

Comments
 (0)