Skip to content

Commit cd0f694

Browse files
authored
Merge pull request #23 from github/fix-tabs-focus-order-for-nvda
Fix tabs focus order for NVDA
2 parents 31301b8 + 0ecfff9 commit cd0f694

File tree

5 files changed

+1928
-375
lines changed

5 files changed

+1928
-375
lines changed

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<tab-container>
1010
<div role="tablist">
11-
<button type="button" role="tab" aria-selected="true">Tab one</button>
11+
<button type="button" role="tab" tabindex="0" aria-selected="true">Tab one</button>
1212
<button type="button" role="tab" tabindex="-1">Tab two</button>
1313
<button type="button" role="tab" tabindex="-1">Tab three</button>
1414
</div>

index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ export default class TabContainerElement extends HTMLElement {
3939
selectTab(this, index)
4040
})
4141
}
42+
43+
connectedCallback() {
44+
for (const tab of this.querySelectorAll('[role="tablist"] [role="tab"]')) {
45+
if (!tab.hasAttribute('aria-selected')) {
46+
tab.setAttribute('aria-selected', 'false')
47+
}
48+
if (!tab.hasAttribute('tabindex')) {
49+
if (tab.getAttribute('aria-selected') === 'true') {
50+
tab.setAttribute('tabindex', '0')
51+
} else {
52+
tab.setAttribute('tabindex', '-1')
53+
}
54+
}
55+
}
56+
}
4257
}
4358

4459
function selectTab(tabContainer: TabContainerElement, index: number) {
@@ -67,7 +82,7 @@ function selectTab(tabContainer: TabContainerElement, index: number) {
6782
}
6883

6984
selectedTab.setAttribute('aria-selected', 'true')
70-
selectedTab.removeAttribute('tabindex')
85+
selectedTab.setAttribute('tabindex', '0')
7186
selectedTab.focus()
7287
selectedPanel.hidden = false
7388

0 commit comments

Comments
 (0)