Skip to content

Commit 99d2d81

Browse files
committed
Sync createTabs from WebProfilerBundle
1 parent baa172c commit 99d2d81

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

Resources/assets/js/exception.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,31 @@
3939
}
4040

4141
(function createTabs() {
42+
/* the accessibility options of this component have been defined according to: */
43+
/* www.w3.org/WAI/ARIA/apg/example-index/tabs/tabs-manual.html */
4244
var tabGroups = document.querySelectorAll('.sf-tabs:not([data-processed=true])');
4345

4446
/* create the tab navigation for each group of tabs */
4547
for (var i = 0; i < tabGroups.length; i++) {
4648
var tabs = tabGroups[i].querySelectorAll(':scope > .tab');
47-
var tabNavigation = document.createElement('ul');
49+
var tabNavigation = document.createElement('div');
4850
tabNavigation.className = 'tab-navigation';
51+
tabNavigation.setAttribute('role', 'tablist');
4952

5053
var selectedTabId = 'tab-' + i + '-0'; /* select the first tab by default */
5154
for (var j = 0; j < tabs.length; j++) {
5255
var tabId = 'tab-' + i + '-' + j;
5356
var tabTitle = tabs[j].querySelector('.tab-title').innerHTML;
5457

55-
var tabNavigationItem = document.createElement('li');
58+
var tabNavigationItem = document.createElement('button');
59+
addClass(tabNavigationItem, 'tab-control');
5660
tabNavigationItem.setAttribute('data-tab-id', tabId);
61+
tabNavigationItem.setAttribute('role', 'tab');
62+
tabNavigationItem.setAttribute('aria-controls', tabId);
5763
if (hasClass(tabs[j], 'active')) { selectedTabId = tabId; }
58-
if (hasClass(tabs[j], 'disabled')) { addClass(tabNavigationItem, 'disabled'); }
64+
if (hasClass(tabs[j], 'disabled')) {
65+
addClass(tabNavigationItem, 'disabled');
66+
}
5967
tabNavigationItem.innerHTML = tabTitle;
6068
tabNavigation.appendChild(tabNavigationItem);
6169

@@ -69,24 +77,31 @@
6977

7078
/* display the active tab and add the 'click' event listeners */
7179
for (i = 0; i < tabGroups.length; i++) {
72-
tabNavigation = tabGroups[i].querySelectorAll(':scope >.tab-navigation li');
80+
tabNavigation = tabGroups[i].querySelectorAll(':scope > .tab-navigation .tab-control');
7381

7482
for (j = 0; j < tabNavigation.length; j++) {
7583
tabId = tabNavigation[j].getAttribute('data-tab-id');
76-
document.getElementById(tabId).querySelector('.tab-title').className = 'hidden';
84+
var tabPanel = document.getElementById(tabId);
85+
tabPanel.setAttribute('role', 'tabpanel');
86+
tabPanel.setAttribute('aria-labelledby', tabId);
87+
tabPanel.querySelector('.tab-title').className = 'hidden';
7788

7889
if (hasClass(tabNavigation[j], 'active')) {
79-
document.getElementById(tabId).className = 'block';
90+
tabPanel.className = 'block';
91+
tabNavigation[j].setAttribute('aria-selected', 'true');
92+
tabNavigation[j].removeAttribute('tabindex');
8093
} else {
81-
document.getElementById(tabId).className = 'hidden';
94+
tabPanel.className = 'hidden';
95+
tabNavigation[j].removeAttribute('aria-selected');
96+
tabNavigation[j].setAttribute('tabindex', '-1');
8297
}
8398

8499
tabNavigation[j].addEventListener('click', function(e) {
85100
var activeTab = e.target || e.srcElement;
86101

87102
/* needed because when the tab contains HTML contents, user can click */
88-
/* on any of those elements instead of their parent '<li>' element */
89-
while (activeTab.tagName.toLowerCase() !== 'li') {
103+
/* on any of those elements instead of their parent '<button>' element */
104+
while (activeTab.tagName.toLowerCase() !== 'button') {
90105
activeTab = activeTab.parentNode;
91106
}
92107

@@ -96,9 +111,13 @@
96111
var tabId = tabNavigation[k].getAttribute('data-tab-id');
97112
document.getElementById(tabId).className = 'hidden';
98113
removeClass(tabNavigation[k], 'active');
114+
tabNavigation[k].removeAttribute('aria-selected');
115+
tabNavigation[k].setAttribute('tabindex', '-1');
99116
}
100117

101118
addClass(activeTab, 'active');
119+
activeTab.setAttribute('aria-selected', 'true');
120+
activeTab.removeAttribute('tabindex');
102121
var activeTabId = activeTab.getAttribute('data-tab-id');
103122
document.getElementById(activeTabId).className = 'block';
104123
});

0 commit comments

Comments
 (0)