From 78f067965f590b7001fae294c34abbf949326023 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 30 Oct 2025 08:49:48 +0200 Subject: [PATCH] fix(share): TOC indicator no longer working due to layout changes --- packages/share-theme/src/scripts/modules/toc.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/share-theme/src/scripts/modules/toc.ts b/packages/share-theme/src/scripts/modules/toc.ts index f38b1de1d..6d7ca2cfd 100644 --- a/packages/share-theme/src/scripts/modules/toc.ts +++ b/packages/share-theme/src/scripts/modules/toc.ts @@ -3,14 +3,15 @@ * it even exists for users without client-side js * and that means it loads with the page so it avoids * all potential reshuffling or layout recalculations. - * + * * So, all this function needs to do is make the links * perform smooth animation, and adjust the "active" * entry as the user scrolls. */ export default function setupToC() { + const container = document.getElementById("right-pane"); const toc = document.getElementById("toc"); - if (!toc) return; + if (!toc || !container) return; // Get all relevant elements const sections = document.getElementById("content")!.querySelectorAll("h2, h3, h4, h5, h6"); @@ -23,7 +24,7 @@ export default function setupToC() { if (!target) return; e.preventDefault(); e.stopPropagation(); - + target.scrollIntoView({behavior: "smooth"}); }); } @@ -32,15 +33,15 @@ export default function setupToC() { function changeLinkState() { let index = sections.length; - // Work backkwards to find the first matching section - while (--index && window.scrollY + 50 < (sections[index] as HTMLElement).offsetTop) {} // eslint-disable-line no-empty + // Work backwards to find the first matching section + while (--index && container!.scrollTop + 50 < (sections[index] as HTMLElement).offsetTop) {} // eslint-disable-line no-empty // Update the "active" item in ToC links.forEach((link) => link.classList.remove("active")); links[index].classList.add("active"); } - + // Initial render changeLinkState(); - window.addEventListener("scroll", changeLinkState); -} \ No newline at end of file + container.addEventListener("scroll", changeLinkState); +}