diff --git a/src/components/markdown/TableOfContents.tsx b/src/components/markdown/TableOfContents.tsx index 614fd54..cdb3c94 100644 --- a/src/components/markdown/TableOfContents.tsx +++ b/src/components/markdown/TableOfContents.tsx @@ -59,7 +59,19 @@ export function TableOfContents({ headings }: { headings: Heading[] }) { e.preventDefault(); const element = document.getElementById(headingId); if (element) { - element.scrollIntoView({ behavior: "smooth", block: "start" }); + // Calculate the scroll offset to account for fixed header + // Mobile: 80px, Desktop: 120px + const isMobile = window.innerWidth < 768; + const scrollOffset = isMobile ? 80 : 120; + + const elementPosition = element.getBoundingClientRect().top + window.scrollY; + const targetPosition = elementPosition - scrollOffset; + + window.scrollTo({ + top: targetPosition, + behavior: "smooth", + }); + // Update URL without causing a page reload window.history.pushState(null, "", `#${headingId}`); // Close mobile menu after click @@ -83,13 +95,14 @@ export function TableOfContents({ headings }: { headings: Heading[] }) {