|
| 1 | +// See https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/tab_role |
| 2 | +// for the accessibility requirements. |
| 3 | + |
1 | 4 | const initTabs = () => {
|
2 | 5 | const tabNodes = document.querySelectorAll('.tabs');
|
3 | 6 | for (const node of tabNodes) {
|
4 |
| - // bind events to toggle tabs for each component occurrence |
5 |
| - for (const tabBtn of node.querySelectorAll('.tabs__item')) { |
6 |
| - tabBtn.addEventListener('click', event => { |
| 7 | + const tabList = node.querySelector('[role="tablist"]'); |
| 8 | + const tabs = tabList.querySelectorAll(':scope > [role="tab"]'); |
| 9 | + |
| 10 | + // Handle clicks on each tab to activate the associated panel |
| 11 | + for (const tabBtn of tabs) { |
| 12 | + tabBtn.addEventListener('click', () => { |
7 | 13 | // ignore if it's already active
|
8 |
| - if (tabBtn.classList.contains('.tabs__item--active')) { |
| 14 | + if (tabBtn.getAttribute('aria-selected') === 'true') { |
9 | 15 | return;
|
10 | 16 | }
|
11 | 17 |
|
12 |
| - // otherwise, remove the active class from the active button and pane |
13 |
| - node.querySelector('.tabs__item--active').classList.remove('tabs__item--active'); |
14 |
| - node.querySelector('.tabs__pane--active').classList.remove('tabs__pane--active'); |
| 18 | + // Remove all current selected tabs |
| 19 | + // and remove the active class from the active button and pane |
| 20 | + tabList |
| 21 | + .querySelectorAll(':scope > [aria-selected="true"]') |
| 22 | + .forEach(tab => { |
| 23 | + tab.setAttribute("aria-selected", false); |
| 24 | + tab.classList.remove('tabs__item--active'); |
| 25 | + const panelId = tab.getAttribute('aria-controls'); |
| 26 | + const panel = node.querySelector(`#${panelId}`); |
| 27 | + panel.setAttribute('hidden', true) |
| 28 | + }); |
15 | 29 |
|
16 |
| - // and activate the target |
| 30 | + // mark this tab as selected/active |
| 31 | + tabBtn.setAttribute('aria-selected', true); |
17 | 32 | tabBtn.classList.add('tabs__item--active');
|
18 |
| - const id = tabBtn.dataset.id; |
19 |
| - const tabPane = document.getElementById(id); |
20 |
| - tabPane.classList.add("tabs__pane--active"); |
| 33 | + |
| 34 | + // lookup the panel to activate |
| 35 | + const panelId = tabBtn.getAttribute('aria-controls'); |
| 36 | + const panel = node.querySelector(`#${panelId}`); |
| 37 | + panel.removeAttribute('hidden'); |
21 | 38 | });
|
22 | 39 | }
|
| 40 | + |
| 41 | + let tabFocusIndex = 0; |
| 42 | + |
| 43 | + // Handle keyboard navigation to focus other tabs |
| 44 | + tabList.addEventListener('keydown', event => { |
| 45 | + // move right or left -> remove tab focus from current tab |
| 46 | + if (event.key === "ArrowRight" || event.key === "ArrowLeft") { |
| 47 | + tabs[tabFocusIndex].setAttribute("tabindex", -1); |
| 48 | + |
| 49 | + if (event.key === "ArrowRight") { |
| 50 | + tabFocusIndex++; |
| 51 | + // If we're at the end, go to the start |
| 52 | + if (tabFocusIndex >= tabs.length) { |
| 53 | + tabFocusIndex = 0; |
| 54 | + } |
| 55 | + // Move left |
| 56 | + } else if (event.key === "ArrowLeft") { |
| 57 | + tabFocusIndex--; |
| 58 | + // If we're at the start, move to the end |
| 59 | + if (tabFocusIndex < 0) { |
| 60 | + tabFocusIndex = tabs.length - 1; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + tabs[tabFocusIndex].setAttribute("tabindex", 0); |
| 65 | + tabs[tabFocusIndex].focus(); |
| 66 | + } |
| 67 | + }); |
23 | 68 | }
|
24 | 69 | };
|
25 | 70 |
|
|
0 commit comments