| 1 |
const createSVGElement = tag => |
| 2 |
document.createElementNS('http://www.w3.org/2000/svg', tag) |
| 3 |
|
| 4 |
const createExpanderIcon = () => { |
| 5 |
const svg = createSVGElement('svg') |
| 6 |
svg.setAttribute('viewBox', '0 0 13 10') |
| 7 |
svg.setAttribute('width', '13') |
| 8 |
svg.setAttribute('height', '13') |
| 9 |
const polygon = createSVGElement('polygon') |
| 10 |
polygon.setAttribute('points', '2 1, 12 1, 7 9') |
| 11 |
svg.append(polygon) |
| 12 |
return svg |
| 13 |
} |
| 14 |
|
| 15 |
const createTOCItemElement = (list, map, onclick) => { |
| 16 |
let count = 0 |
| 17 |
const makeID = () => `toc-element-${count++}` |
| 18 |
const createItem = ({ label, href, subitems }, depth = 0) => { |
| 19 |
const a = document.createElement(href ? 'a' : 'span') |
| 20 |
a.innerText = label |
| 21 |
a.setAttribute('role', 'treeitem') |
| 22 |
a.tabIndex = -1 |
| 23 |
a.style.paddingInlineStart = `${(depth + 1) * 24}px` |
| 24 |
list.push(a) |
| 25 |
if (href) { |
| 26 |
if (!map.has(href)) map.set(href, a) |
| 27 |
a.href = href |
| 28 |
a.onclick = event => { |
| 29 |
event.preventDefault() |
| 30 |
onclick(href) |
| 31 |
} |
| 32 |
} else a.onclick = event => a.firstElementChild?.onclick(event) |
| 33 |
|
| 34 |
const li = document.createElement('li') |
| 35 |
li.setAttribute('role', 'none') |
| 36 |
li.append(a) |
| 37 |
if (subitems?.length) { |
| 38 |
a.setAttribute('aria-expanded', 'false') |
| 39 |
const expander = createExpanderIcon() |
| 40 |
expander.onclick = event => { |
| 41 |
event.preventDefault() |
| 42 |
event.stopPropagation() |
| 43 |
const expanded = a.getAttribute('aria-expanded') |
| 44 |
a.setAttribute('aria-expanded', expanded === 'true' ? 'false' : 'true') |
| 45 |
} |
| 46 |
a.prepend(expander) |
| 47 |
const ol = document.createElement('ol') |
| 48 |
ol.id = makeID() |
| 49 |
ol.setAttribute('role', 'group') |
| 50 |
a.setAttribute('aria-owns', ol.id) |
| 51 |
ol.replaceChildren(...subitems.map(item => createItem(item, depth + 1))) |
| 52 |
li.append(ol) |
| 53 |
} |
| 54 |
return li |
| 55 |
} |
| 56 |
return createItem |
| 57 |
} |
| 58 |
|
| 59 |
// https://www.w3.org/TR/wai-aria-practices-1.2/examples/treeview/treeview-navigation.html |
| 60 |
export const createTOCView = (toc, onclick) => { |
| 61 |
const $toc = document.createElement('ol') |
| 62 |
$toc.setAttribute('role', 'tree') |
| 63 |
const list = [] |
| 64 |
const map = new Map() |
| 65 |
const createItem = createTOCItemElement(list, map, onclick) |
| 66 |
$toc.replaceChildren(...toc.map(item => createItem(item))) |
| 67 |
|
| 68 |
const isTreeItem = item => item?.getAttribute('role') === 'treeitem' |
| 69 |
const getParents = function* (el) { |
| 70 |
for (let parent = el.parentNode; parent !== $toc; parent = parent.parentNode) { |
| 71 |
const item = parent.previousElementSibling |
| 72 |
if (isTreeItem(item)) yield item |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
let currentItem, currentVisibleParent |
| 77 |
$toc.addEventListener('focusout', () => { |
| 78 |
if (!currentItem) return |
| 79 |
// reset parent focus from last time |
| 80 |
if (currentVisibleParent) currentVisibleParent.tabIndex = -1 |
| 81 |
// if current item is visible, let it have the focus |
| 82 |
if (currentItem.offsetParent) { |
| 83 |
currentItem.tabIndex = 0 |
| 84 |
return |
| 85 |
} |
| 86 |
// current item is hidden; give focus to the nearest visible parent |
| 87 |
for (const item of getParents(currentItem)) { |
| 88 |
if (item.offsetParent) { |
| 89 |
item.tabIndex = 0 |
| 90 |
currentVisibleParent = item |
| 91 |
break |
| 92 |
} |
| 93 |
} |
| 94 |
}) |
| 95 |
|
| 96 |
const setCurrentHref = href => { |
| 97 |
if (currentItem) { |
| 98 |
currentItem.removeAttribute('aria-current') |
| 99 |
currentItem.tabIndex = -1 |
| 100 |
} |
| 101 |
const el = map.get(href) |
| 102 |
if (!el) { |
| 103 |
currentItem = list[0] |
| 104 |
currentItem.tabIndex = 0 |
| 105 |
return |
| 106 |
} |
| 107 |
for (const item of getParents(el)) |
| 108 |
item.setAttribute('aria-expanded', 'true') |
| 109 |
el.setAttribute('aria-current', 'page') |
| 110 |
el.tabIndex = 0 |
| 111 |
el.scrollIntoView({ behavior: 'smooth', block: 'center' }) |
| 112 |
currentItem = el |
| 113 |
} |
| 114 |
|
| 115 |
const acceptNode = node => isTreeItem(node) && node.offsetParent |
| 116 |
? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP |
| 117 |
const iter = document.createTreeWalker($toc, 1, { acceptNode }) |
| 118 |
const getIter = current => (iter.currentNode = current, iter) |
| 119 |
|
| 120 |
for (const el of list) el.addEventListener('keydown', event => { |
| 121 |
let stop = false |
| 122 |
const { currentTarget, key } = event |
| 123 |
switch (key) { |
| 124 |
case ' ': |
| 125 |
case 'Enter': |
| 126 |
currentTarget.click() |
| 127 |
stop = true |
| 128 |
break |
| 129 |
case 'ArrowDown': |
| 130 |
getIter(currentTarget).nextNode()?.focus() |
| 131 |
stop = true |
| 132 |
break |
| 133 |
case 'ArrowUp': |
| 134 |
getIter(currentTarget).previousNode()?.focus() |
| 135 |
stop = true |
| 136 |
break |
| 137 |
case 'ArrowLeft': |
| 138 |
if (currentTarget.getAttribute('aria-expanded') === 'true') |
| 139 |
currentTarget.setAttribute('aria-expanded', 'false') |
| 140 |
else getParents(currentTarget).next()?.value?.focus() |
| 141 |
stop = true |
| 142 |
break |
| 143 |
case 'ArrowRight': |
| 144 |
if (currentTarget.getAttribute('aria-expanded') === 'true') |
| 145 |
getIter(currentTarget).nextNode()?.focus() |
| 146 |
else if (currentTarget.getAttribute('aria-owns')) |
| 147 |
currentTarget.setAttribute('aria-expanded', 'true') |
| 148 |
stop = true |
| 149 |
break |
| 150 |
case 'Home': |
| 151 |
list[0].focus() |
| 152 |
stop = true |
| 153 |
break |
| 154 |
case 'End': { |
| 155 |
const last = list[list.length - 1] |
| 156 |
if (last.offsetParent) last.focus() |
| 157 |
else getIter(last).previousNode()?.focus() |
| 158 |
stop = true |
| 159 |
break |
| 160 |
} |
| 161 |
} |
| 162 |
if (stop) { |
| 163 |
event.preventDefault() |
| 164 |
event.stopPropagation() |
| 165 |
} |
| 166 |
}) |
| 167 |
|
| 168 |
return { element: $toc, setCurrentHref } |
| 169 |
} |