|
| 1 | +--- |
| 2 | +/** |
| 3 | + * TableOfContentsSidebar |
| 4 | + * |
| 5 | + * Sticky right-sidebar table of contents with scroll-spy highlighting. |
| 6 | + * Visible only on xl: screens (≥1280px). Uses IntersectionObserver to |
| 7 | + * track which heading is currently in view and highlights it with an |
| 8 | + * accent left-border. Respects prefers-reduced-motion. |
| 9 | + * |
| 10 | + * Only renders when there are 3+ headings. |
| 11 | + */ |
| 12 | +
|
| 13 | +import { extractHeadings, MIN_HEADINGS_FOR_TOC } from '../utils/toc'; |
| 14 | +
|
| 15 | +interface Props { |
| 16 | + /** Rendered HTML content of the post */ |
| 17 | + content: string; |
| 18 | +} |
| 19 | +
|
| 20 | +const { content } = Astro.props; |
| 21 | +const headings = extractHeadings(content); |
| 22 | +const showToc = headings.length >= MIN_HEADINGS_FOR_TOC; |
| 23 | +--- |
| 24 | + |
| 25 | +{showToc && ( |
| 26 | + <aside |
| 27 | + class="toc-sidebar hidden xl:block" |
| 28 | + aria-label="Table of contents" |
| 29 | + data-toc-sidebar |
| 30 | + > |
| 31 | + <nav> |
| 32 | + <p class="toc-sidebar-title">On this page</p> |
| 33 | + <ol class="toc-sidebar-list"> |
| 34 | + {headings.map((h) => ( |
| 35 | + <li> |
| 36 | + <a |
| 37 | + href={`#${h.id}`} |
| 38 | + class:list={['toc-sidebar-link', h.level === 3 && 'toc-sidebar-link--nested']} |
| 39 | + data-toc-href={h.id} |
| 40 | + > |
| 41 | + {h.text} |
| 42 | + </a> |
| 43 | + </li> |
| 44 | + ))} |
| 45 | + </ol> |
| 46 | + </nav> |
| 47 | + </aside> |
| 48 | +)} |
| 49 | + |
| 50 | +<script> |
| 51 | + function initTocSidebar() { |
| 52 | + const sidebar = document.querySelector('[data-toc-sidebar]'); |
| 53 | + if (!sidebar) return; |
| 54 | + |
| 55 | + const links = sidebar.querySelectorAll<HTMLAnchorElement>('[data-toc-href]'); |
| 56 | + if (links.length === 0) return; |
| 57 | + |
| 58 | + // Map heading IDs to their TOC link elements |
| 59 | + const linkMap = new Map<string, HTMLAnchorElement>(); |
| 60 | + links.forEach((link) => { |
| 61 | + const id = link.dataset.tocHref; |
| 62 | + if (id) linkMap.set(id, link); |
| 63 | + }); |
| 64 | + |
| 65 | + // Track which headings are currently visible |
| 66 | + const visibleHeadings = new Set<string>(); |
| 67 | + let orderedIds: string[] = []; |
| 68 | + |
| 69 | + // Collect heading elements in DOM order |
| 70 | + linkMap.forEach((_, id) => { |
| 71 | + const el = document.getElementById(id); |
| 72 | + if (el) orderedIds.push(id); |
| 73 | + }); |
| 74 | + |
| 75 | + function setActive(id: string | null) { |
| 76 | + links.forEach((link) => link.removeAttribute('data-active')); |
| 77 | + if (id) { |
| 78 | + const activeLink = linkMap.get(id); |
| 79 | + if (activeLink) { |
| 80 | + activeLink.setAttribute('data-active', 'true'); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Pick the topmost visible heading, or the last heading above viewport |
| 86 | + function updateActive() { |
| 87 | + if (visibleHeadings.size > 0) { |
| 88 | + // Find the first visible heading in DOM order |
| 89 | + for (const id of orderedIds) { |
| 90 | + if (visibleHeadings.has(id)) { |
| 91 | + setActive(id); |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // No heading is visible — find the last heading above the viewport |
| 98 | + let lastAbove: string | null = null; |
| 99 | + for (const id of orderedIds) { |
| 100 | + const el = document.getElementById(id); |
| 101 | + if (el && el.getBoundingClientRect().top < 100) { |
| 102 | + lastAbove = id; |
| 103 | + } |
| 104 | + } |
| 105 | + setActive(lastAbove); |
| 106 | + } |
| 107 | + |
| 108 | + const observer = new IntersectionObserver( |
| 109 | + (entries) => { |
| 110 | + entries.forEach((entry) => { |
| 111 | + if (entry.isIntersecting) { |
| 112 | + visibleHeadings.add(entry.target.id); |
| 113 | + } else { |
| 114 | + visibleHeadings.delete(entry.target.id); |
| 115 | + } |
| 116 | + }); |
| 117 | + updateActive(); |
| 118 | + }, |
| 119 | + { rootMargin: '-80px 0px -60% 0px', threshold: 0 } |
| 120 | + ); |
| 121 | + |
| 122 | + orderedIds.forEach((id) => { |
| 123 | + const el = document.getElementById(id); |
| 124 | + if (el) observer.observe(el); |
| 125 | + }); |
| 126 | + |
| 127 | + // Initial highlight |
| 128 | + updateActive(); |
| 129 | + } |
| 130 | + |
| 131 | + initTocSidebar(); |
| 132 | +</script> |
| 133 | + |
| 134 | +<style> |
| 135 | + @reference "../styles/global.css"; |
| 136 | + |
| 137 | + .toc-sidebar { |
| 138 | + position: sticky; |
| 139 | + top: 6rem; |
| 140 | + align-self: start; |
| 141 | + max-height: calc(100vh - 8rem); |
| 142 | + overflow-y: auto; |
| 143 | + scrollbar-width: thin; |
| 144 | + scrollbar-color: var(--color-gray-300) transparent; |
| 145 | + width: 14rem; |
| 146 | + flex-shrink: 0; |
| 147 | + } |
| 148 | + |
| 149 | + .toc-sidebar-title { |
| 150 | + @apply text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400; |
| 151 | + margin: 0 0 0.75rem 0; |
| 152 | + } |
| 153 | + |
| 154 | + .toc-sidebar-list { |
| 155 | + @apply list-none m-0 p-0; |
| 156 | + } |
| 157 | + |
| 158 | + .toc-sidebar-link { |
| 159 | + @apply block text-sm py-1.5 no-underline; |
| 160 | + @apply text-gray-500 dark:text-gray-400; |
| 161 | + border-left: 2px solid transparent; |
| 162 | + padding-left: 0.75rem; |
| 163 | + transition: color 0.15s ease, border-color 0.15s ease; |
| 164 | + } |
| 165 | + |
| 166 | + .toc-sidebar-link:hover { |
| 167 | + @apply text-gray-800 dark:text-gray-200; |
| 168 | + } |
| 169 | + |
| 170 | + .toc-sidebar-link[data-active="true"] { |
| 171 | + color: var(--color-primary); |
| 172 | + border-left-color: var(--color-primary); |
| 173 | + @apply font-medium; |
| 174 | + } |
| 175 | + |
| 176 | + .toc-sidebar-link--nested { |
| 177 | + padding-left: 1.5rem; |
| 178 | + @apply text-xs; |
| 179 | + } |
| 180 | + |
| 181 | + @media (prefers-color-scheme: dark) { |
| 182 | + .toc-sidebar { |
| 183 | + scrollbar-color: var(--color-gray-700) transparent; |
| 184 | + } |
| 185 | + |
| 186 | + .toc-sidebar-link[data-active="true"] { |
| 187 | + color: var(--color-primary-300); |
| 188 | + border-left-color: var(--color-primary-300); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @media (prefers-reduced-motion: reduce) { |
| 193 | + .toc-sidebar-link { |
| 194 | + transition: none; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @media print { |
| 199 | + .toc-sidebar { |
| 200 | + display: none; |
| 201 | + } |
| 202 | + } |
| 203 | +</style> |
0 commit comments