-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathContents.svelte
More file actions
188 lines (163 loc) · 5.86 KB
/
Copy pathContents.svelte
File metadata and controls
188 lines (163 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<script lang="ts">
import { Button, Icon } from '@stackoverflow/stacks-svelte';
import { IconList } from '@stackoverflow/stacks-icons'
import { SvelteMap } from 'svelte/reactivity';
interface TocItem {
id: string;
value: string;
depth: number;
children?: TocItem[];
}
let { toc = [] }: { toc: TocItem[] } = $props();
let isMobileOpen = $state(false);
let activeId = $state<string | null>(null);
let navElement: HTMLElement | null = null;
let linkElements: Map<string, HTMLElement> = new SvelteMap();
// Flatten toc to get all items including children
function flattenToc(items: TocItem[]): TocItem[] {
const result: TocItem[] = [];
for (const item of items) {
result.push(item);
if (item.children) {
result.push(...item.children);
}
}
return result;
}
const allItems = $derived(flattenToc(toc));
function scrollActiveIntoView(id: string) {
const linkElement = linkElements.get(id);
if (linkElement && navElement) {
// Auto-scroll the sidebar to keep active item visible (desktop only)
const scrollableContainer = navElement.closest('.overflow-auto') as HTMLElement;
const isMobile = window.innerWidth < 768;
if (scrollableContainer && !isMobile) {
const containerRect = scrollableContainer.getBoundingClientRect();
const linkRect = linkElement.getBoundingClientRect();
const linkRelativeTop = linkRect.top - containerRect.top;
const linkRelativeBottom = linkRect.bottom - containerRect.top;
if (linkRelativeTop < 0) {
linkElement.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
} else if (linkRelativeBottom > containerRect.height) {
linkElement.scrollIntoView({
behavior: 'smooth',
block: 'end',
inline: 'nearest'
});
}
}
}
}
// Single effect to handle everything
$effect(() => {
if (typeof window === 'undefined' || allItems.length === 0) return;
// The active item is the last heading that has scrolled past the
// top threshold — this is more accurate than IntersectionObserver
// which can fire too early when a heading enters the viewport.
const OFFSET = 120; // px from the top of the viewport
function updateActive() {
let found: string | null = null;
for (const item of allItems) {
const el = document.getElementById(item.id);
if (!el) continue;
if (el.getBoundingClientRect().top <= OFFSET) {
found = item.id;
} else {
break; // headings are in DOM order — stop once we pass the threshold
}
}
const next = found ?? (allItems.length > 0 ? allItems[0].id : null);
if (next && next !== activeId) {
activeId = next;
scrollActiveIntoView(next);
}
}
// Wait for DOM to be ready then attach scroll listener
const timeoutId = setTimeout(() => {
updateActive();
window.addEventListener('scroll', updateActive, { passive: true });
}, 50);
return () => {
clearTimeout(timeoutId);
window.removeEventListener('scroll', updateActive);
};
});
function registerLink(element: HTMLElement, id: string) {
linkElements.set(id, element);
function handleClick() {
activeId = id;
}
element.addEventListener('click', handleClick);
return {
destroy() {
element.removeEventListener('click', handleClick);
linkElements.delete(id);
}
};
}
</script>
{#if toc.length > 0}
<aside class="layout-toc fl-shrink0 w30 md:w100 md:ps-fixed b16 r16 wmn2 wmx3 ff-stack-sans-headline">
<div class="ps-sticky md:ps-static t0 mt6 py24 px32 md:pb0 md:p6 overflow-auto hmx-screen md:hmx-initial">
<nav bind:this={navElement} class={`bg-white ${isMobileOpen ? 'contents-inner-mobile d-block' : 'md:d-none'}`}>
<h2 class="fs-body2 fw-bold mb12 px6 fc-black-400 md:d-none">Contents</h2>
<ul class="s-navigation s-navigation__vertical">
{#each toc as item, index (item.id)}
<li>
<a
href="#{item.id}"
use:registerLink={item.id}
class="s-navigation--item fs-caption bar0 fw-bold fc-black ai-start"
class:bg-black-100={activeId === item.id}
class:is-active={activeId === item.id}
>
<span class="fl-shrink0 w24 d-flex ai-center fc-theme-primary">{(index + 1).toString().padStart(2, "0")}</span>
<span class="truncate">{item.value}</span>
</a>
{#if item.children && item.children.length > 0}
<ul class="s-navigation s-navigation__vertical">
{#each item.children as child (child.id)}
<li>
<a
href="#{child.id}"
use:registerLink={child.id}
class="s-navigation--item fs-caption bar0"
class:bg-black-100={activeId === child.id}
class:is-active={activeId === child.id}
>
{child.value}
</a>
</li>
{/each}
</ul>
{/if}
</li>
{/each}
</ul>
</nav>
<Button
class="d-none md:d-block ml-auto mt12 bar-md p8"
size="sm"
onclick={() => isMobileOpen = !isMobileOpen}
title="Table of contents"
>
<Icon src={IconList} />
</Button>
</div>
</aside>
{/if}
<style>
@media (max-width: 71.875rem) {
.contents-inner-mobile {
max-height: calc(100vh - 200px);
overflow: auto;
padding: var(--su12);
box-shadow: var(--bs-md);
border-radius: var(--br-md);
}
}
</style>