From aa7251834b45e6efcc4113d31c67ba1561604606 Mon Sep 17 00:00:00 2001 From: silberengel Date: Mon, 8 Dec 2025 07:14:39 +0100 Subject: [PATCH] make vertical scrollbar on ToC reactive to selections --- .../publications/TableOfContents.svelte | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/components/publications/TableOfContents.svelte b/src/lib/components/publications/TableOfContents.svelte index 1884164..fe15e11 100644 --- a/src/lib/components/publications/TableOfContents.svelte +++ b/src/lib/components/publications/TableOfContents.svelte @@ -71,6 +71,36 @@ toc.expandedMap.set(address, expanded); entry.resolveChildren(); + + // AI-NOTE: When expanding a chapter, scroll it to the top of the TOC so its children are visible + if (expanded) { + // Use setTimeout to allow the expansion animation to start and DOM to update + setTimeout(() => { + // Find the scrollable container (the div with overflow-y-auto in the TOC drawer) + const scrollableContainer = document.querySelector('.overflow-y-auto'); + if (!scrollableContainer) { + return; + } + + // Find all buttons in the TOC that match the entry title + const buttons = scrollableContainer.querySelectorAll('button'); + for (const button of buttons) { + const buttonText = button.textContent?.trim(); + if (buttonText === entry.title) { + // Find the parent container of the dropdown (the SidebarDropdownWrapper) + const dropdownContainer = button.closest('[class*="w-full"]'); + if (dropdownContainer) { + // Scroll the chapter to the top of the TOC container + dropdownContainer.scrollIntoView({ + behavior: 'smooth', + block: 'start', + }); + } + break; + } + } + }, 150); + } } function handleSectionClick(address: string) {