({ label, href, subitems }, depth = 0)
| 15 | let count = 0; |
| 16 | const makeID = () => `toc-element-${count++}`; |
| 17 | const createItem = ({ label, href, subitems }, depth = 0) => { |
| 18 | const a = document.createElement(href ? "a" : "span"); |
| 19 | a.innerText = label; |
| 20 | a.setAttribute("role", "treeitem"); |
| 21 | a.tabIndex = -1; |
| 22 | a.style.paddingInlineStart = `${(depth + 1) * 24}px`; |
| 23 | list.push(a); |
| 24 | if (href) { |
| 25 | if (!map.has(href)) map.set(href, a); |
| 26 | a.href = href; |
| 27 | a.onclick = (event) => { |
| 28 | event.preventDefault(); |
| 29 | onclick(href); |
| 30 | }; |
| 31 | } else a.onclick = (event) => a.firstElementChild?.onclick(event); |
| 32 | |
| 33 | const li = document.createElement("li"); |
| 34 | li.setAttribute("role", "none"); |
| 35 | li.append(a); |
| 36 | if (subitems?.length) { |
| 37 | a.setAttribute("aria-expanded", "false"); |
| 38 | const expander = createExpanderIcon(); |
| 39 | expander.onclick = (event) => { |
| 40 | event.preventDefault(); |
| 41 | event.stopPropagation(); |
| 42 | const expanded = a.getAttribute("aria-expanded"); |
| 43 | a.setAttribute("aria-expanded", expanded === "true" ? "false" : "true"); |
| 44 | }; |
| 45 | a.prepend(expander); |
| 46 | const ol = document.createElement("ol"); |
| 47 | ol.id = makeID(); |
| 48 | ol.setAttribute("role", "group"); |
| 49 | a.setAttribute("aria-owns", ol.id); |
| 50 | ol.replaceChildren(...subitems.map((item) => createItem(item, depth + 1))); |
| 51 | li.append(ol); |
| 52 | } |
| 53 | return li; |
| 54 | }; |
| 55 | return createItem; |
| 56 | }; |
| 57 |
no test coverage detected