MCPcopy Create free account
hub / github.com/adobe/react-spectrum / attachToToC

Function attachToToC

packages/dev/docs/src/attachToToC.js:15–54  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13import sideNavStyles from '@adobe/spectrum-css-temp/components/sidenav/vars.css';
14
15export function attachToToC() {
16 let tocLinks = document.querySelectorAll('#toc a');
17 let headers = [];
18 for (let link of tocLinks) {
19 let headerId = link.href.split('#').pop();
20 let header = document.getElementById(headerId);
21 headers.push({link, header});
22 }
23
24 function updateToc() {
25 requestAnimationFrame(() => {
26 // this needs to be improved a little but the math hurts my head right now
27 // right now it's impossible to select the last section if the last two heights combined are smaller than the viewport height
28 headers.some((header, i) => {
29 if (
30 header.header.offsetTop + header.header.getBoundingClientRect().height >
31 document.documentElement.scrollTop
32 ) {
33 let currentSelection = document.querySelectorAll(`#toc .${sideNavStyles['is-selected']}`);
34 if (currentSelection) {
35 currentSelection.forEach(node => {
36 node.classList.remove(sideNavStyles['is-selected']);
37 const link = node.querySelector('[aria-current]');
38 if (link) {
39 link.removeAttribute('aria-current');
40 }
41 });
42 }
43 header.link.parentElement.classList.add(sideNavStyles['is-selected']);
44 header.link.setAttribute('aria-current', 'location');
45 return true;
46 }
47 });
48 });
49 }
50
51 updateToc();
52
53 document.addEventListener('scroll', updateToc);
54}

Callers 1

docs.jsFile · 0.90

Calls 3

updateTocFunction · 0.85
pushMethod · 0.80
addEventListenerMethod · 0.80

Tested by

no test coverage detected