|
| 1 | +(function () { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + // Layer ARIA semantics and keyboard navigation onto the mdbook-tabs |
| 5 | + // plugin's markup. The plugin emits plain <button> / <div> elements |
| 6 | + // without tab/tablist/tabpanel roles; this script fills that gap. |
| 7 | + |
| 8 | + let uidCounter = 0; |
| 9 | + function nextUid() { |
| 10 | + uidCounter += 1; |
| 11 | + return uidCounter; |
| 12 | + } |
| 13 | + |
| 14 | + function initContainer(container) { |
| 15 | + const nav = container.querySelector('.mdbook-tabs'); |
| 16 | + if (nav && !nav.hasAttribute('role')) { |
| 17 | + nav.setAttribute('role', 'tablist'); |
| 18 | + } |
| 19 | + |
| 20 | + const tabs = Array.from(container.querySelectorAll('.mdbook-tab')); |
| 21 | + const panes = Array.from(container.querySelectorAll('.mdbook-tab-content')); |
| 22 | + |
| 23 | + tabs.forEach(function (tab) { |
| 24 | + if (tab.hasAttribute('role')) return; |
| 25 | + const uid = nextUid(); |
| 26 | + const tabId = 'mdbook-tab-' + uid; |
| 27 | + const paneId = 'mdbook-tabpanel-' + uid; |
| 28 | + |
| 29 | + tab.setAttribute('role', 'tab'); |
| 30 | + tab.id = tabId; |
| 31 | + |
| 32 | + const pane = panes.find(function (p) { |
| 33 | + return p.dataset.tabname === tab.dataset.tabname; |
| 34 | + }); |
| 35 | + if (pane) { |
| 36 | + pane.setAttribute('role', 'tabpanel'); |
| 37 | + pane.id = paneId; |
| 38 | + if (!pane.hasAttribute('tabindex')) { |
| 39 | + pane.setAttribute('tabindex', '0'); |
| 40 | + } |
| 41 | + tab.setAttribute('aria-controls', paneId); |
| 42 | + pane.setAttribute('aria-labelledby', tabId); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + function syncContainer(container) { |
| 48 | + container.querySelectorAll('.mdbook-tab').forEach(function (tab) { |
| 49 | + const isActive = tab.classList.contains('active'); |
| 50 | + tab.setAttribute('aria-selected', isActive ? 'true' : 'false'); |
| 51 | + // Roving tabindex: only the active tab is reachable via Tab key; |
| 52 | + // arrow keys move focus within the tablist. |
| 53 | + tab.setAttribute('tabindex', isActive ? '0' : '-1'); |
| 54 | + }); |
| 55 | + container.querySelectorAll('.mdbook-tab-content').forEach(function (pane) { |
| 56 | + pane.setAttribute('aria-hidden', pane.classList.contains('hidden') ? 'true' : 'false'); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + function syncAll() { |
| 61 | + document.querySelectorAll('.mdbook-tabs-container').forEach(syncContainer); |
| 62 | + } |
| 63 | + |
| 64 | + function handleKeyDown(event) { |
| 65 | + const tab = event.target.closest && event.target.closest('.mdbook-tab'); |
| 66 | + if (!tab) return; |
| 67 | + const nav = tab.parentElement; |
| 68 | + if (!nav || !nav.classList.contains('mdbook-tabs')) return; |
| 69 | + |
| 70 | + const tabs = Array.from(nav.querySelectorAll('.mdbook-tab')); |
| 71 | + const currentIndex = tabs.indexOf(tab); |
| 72 | + if (currentIndex < 0) return; |
| 73 | + |
| 74 | + let newIndex = null; |
| 75 | + switch (event.key) { |
| 76 | + case 'ArrowRight': |
| 77 | + case 'Right': |
| 78 | + newIndex = (currentIndex + 1) % tabs.length; |
| 79 | + break; |
| 80 | + case 'ArrowLeft': |
| 81 | + case 'Left': |
| 82 | + newIndex = (currentIndex - 1 + tabs.length) % tabs.length; |
| 83 | + break; |
| 84 | + case 'Home': |
| 85 | + newIndex = 0; |
| 86 | + break; |
| 87 | + case 'End': |
| 88 | + newIndex = tabs.length - 1; |
| 89 | + break; |
| 90 | + } |
| 91 | + if (newIndex === null) return; |
| 92 | + |
| 93 | + event.preventDefault(); |
| 94 | + tabs[newIndex].focus(); |
| 95 | + // Automatic activation: mirrors common tab widgets (Bootstrap, Radix, |
| 96 | + // etc.) where focusing a tab also selects it. |
| 97 | + tabs[newIndex].click(); |
| 98 | + } |
| 99 | + |
| 100 | + document.addEventListener('DOMContentLoaded', function () { |
| 101 | + document.querySelectorAll('.mdbook-tabs-container').forEach(initContainer); |
| 102 | + syncAll(); |
| 103 | + |
| 104 | + document.addEventListener('keydown', handleKeyDown); |
| 105 | + |
| 106 | + // Re-sync ARIA state after every click. Clicks may propagate across |
| 107 | + // multiple containers when a global="..." state is shared, so we |
| 108 | + // resync every container, not just the one that was clicked. |
| 109 | + document.addEventListener('click', function (event) { |
| 110 | + if (!event.target.closest || !event.target.closest('.mdbook-tab')) return; |
| 111 | + syncAll(); |
| 112 | + }); |
| 113 | + }); |
| 114 | +})(); |
0 commit comments