diff --git a/components/TableContents.tsx b/components/TableContents.tsx index d40cd160..a06c64f3 100644 --- a/components/TableContents.tsx +++ b/components/TableContents.tsx @@ -56,11 +56,18 @@ export default function TOC({ headings, isList, setIsList }) { // Detect screen size — show mobile dropdown below 1440px (matches grid breakpoint) useEffect(() => { - const check = () => setIsSmallScreen(window.innerWidth < 1440); - check(); - window.addEventListener("resize", check); - return () => window.removeEventListener("resize", check); - }, []); + if (!tocRef.current) return; + + const container = tocRef.current; + + function resizeHandler() { + setIsList(container.scrollHeight > container.clientHeight); + } + + resizeHandler() + window.addEventListener("resize", resizeHandler) + + return () => { window.removeEventListener("resize", resizeHandler) } // isList fallback (collapse to select if extremely long) useEffect(() => { @@ -71,7 +78,26 @@ export default function TOC({ headings, isList, setIsList }) { window.addEventListener("resize", handler); return () => window.removeEventListener("resize", handler); // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, []) + + + const handleItemClick = (id) => { + const sanitizedId = sanitizeStringForURL(id, true); + const element = document.getElementById(sanitizedId); + if (element) { + const offset = 80; + const offsetPosition = element.offsetTop - offset; + window.scrollTo({ + top: offsetPosition, + behavior: "smooth", + }); + + window.history.replaceState(null, null, `#${sanitizedId}`); + } + }; + + // State to track screen width + const [isSmallScreen, setIsSmallScreen] = useState(false); // Active section tracking useEffect(() => { @@ -170,83 +196,22 @@ export default function TOC({ headings, isList, setIsList }) { ))} - - ) : ( - - )} - + ) : ( + + )} + + ); }