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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
---
import { ScrollArea } from '@/components/ui/scroll-area'
import { cn, getHeadingMargin } from '@/lib/utils'
import type { MarkdownHeading } from 'astro'
import { Icon } from 'astro-icon/components'
type Props = {
headings: MarkdownHeading[]
}
const { headings } = Astro.props
---
{
headings && headings.length > 0 && (
<div id="mobile-toc-container" class="w-full xl:hidden">
<details class="group">
<summary class="flex w-full cursor-pointer items-center justify-between">
<div class="mx-auto flex w-full max-w-3xl items-center px-4 py-3">
<div class="relative mr-2 size-4">
<svg class="h-4 w-4" viewBox="0 0 24 24">
<circle
class="text-primary/20"
cx="12"
cy="12"
r="10"
fill="none"
stroke="currentColor"
stroke-width="2"
/>
<circle
id="mobile-toc-progress-circle"
class="text-primary"
cx="12"
cy="12"
r="10"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-dasharray="62.83"
stroke-dashoffset="62.83"
transform="rotate(-90 12 12)"
/>
</svg>
</div>
<span
id="mobile-toc-current-section"
class="text-muted-foreground flex-grow truncate text-sm"
>
Overview
</span>
<span class="text-muted-foreground ml-2">
<Icon
name="lucide:chevron-down"
class="h-4 w-4 transition-transform duration-200 group-open:rotate-180"
/>
</span>
</div>
</summary>
<ScrollArea
client:load
className="mx-auto max-w-3xl"
data-toc-header-scroll
>
<div class="max-h-[30vh]">
<ul
class="flex list-none flex-col gap-y-2 px-4 pb-4"
id="mobile-table-of-contents"
>
{headings.map((heading) => (
<li
class={cn(
'px-4 text-sm',
getHeadingMargin(heading.depth),
'text-foreground/60',
)}
>
<a
href={`#${heading.slug}`}
class="mobile-toc-item underline decoration-transparent underline-offset-[3px] transition-colors duration-200 hover:decoration-inherit"
data-heading-id={heading.slug}
>
{heading.text}
</a>
</li>
))}
</ul>
</div>
</ScrollArea>
</details>
</div>
)
}
<script>
const INITIAL_OVERVIEW_TEXT = 'Overview'
const HEADER_OFFSET = 102 + 36
const PROGRESS_CIRCLE_RADIUS = 10
const PROGRESS_CIRCLE_CIRCUMFERENCE = 2 * Math.PI * PROGRESS_CIRCLE_RADIUS
class MobileTOCState {
progressCircle: HTMLElement | null = null
currentSectionText: HTMLElement | null = null
detailsElement: HTMLDetailsElement | null = null
listElement: HTMLElement | null = null
scrollArea: HTMLElement | null = null
headings: HTMLElement[] = []
regions: { id: string; start: number; end: number }[] = []
activeIds: string[] = []
reset() {
this.progressCircle = document.getElementById(
'mobile-toc-progress-circle',
)
this.currentSectionText = document.getElementById(
'mobile-toc-current-section',
)
this.detailsElement = document.querySelector(
'#mobile-toc-container details',
)
this.listElement = document.getElementById('mobile-table-of-contents')
const tocContainer = document.getElementById('mobile-toc-container')
this.scrollArea =
tocContainer?.querySelector('[data-radix-scroll-area-viewport]') || null
this.headings = []
this.regions = []
this.activeIds = []
if (this.progressCircle) {
this.progressCircle.style.strokeDasharray =
PROGRESS_CIRCLE_CIRCUMFERENCE.toString()
this.progressCircle.style.strokeDashoffset =
PROGRESS_CIRCLE_CIRCUMFERENCE.toString()
}
}
}
const state = new MobileTOCState()
class HeadingRegions {
static build() {
state.headings = Array.from(
document.querySelectorAll<HTMLElement>(
'.prose h2, .prose h3, .prose h4, .prose h5, .prose h6',
),
)
if (state.headings.length === 0) {
state.regions = []
return
}
state.regions = state.headings.map((heading, index) => {
const nextHeading = state.headings[index + 1]
return {
id: heading.id,
start: heading.offsetTop,
end: nextHeading ? nextHeading.offsetTop : document.body.scrollHeight,
}
})
}
static getVisibleIds(): string[] {
if (state.headings.length === 0) return []
const viewportTop = window.scrollY + HEADER_OFFSET
const viewportBottom = window.scrollY + window.innerHeight
const visibleIds = new Set<string>()
const isInViewport = (top: number, bottom: number) =>
(top >= viewportTop && top <= viewportBottom) ||
(bottom >= viewportTop && bottom <= viewportBottom) ||
(top <= viewportTop && bottom >= viewportBottom)
state.headings.forEach((heading) => {
const headingBottom = heading.offsetTop + heading.offsetHeight
if (isInViewport(heading.offsetTop, headingBottom)) {
visibleIds.add(heading.id)
}
})
state.regions.forEach((region) => {
if (region.start <= viewportBottom && region.end >= viewportTop) {
const heading = document.getElementById(region.id)
if (heading) {
const headingBottom = heading.offsetTop + heading.offsetHeight
if (
region.end > headingBottom &&
(headingBottom < viewportBottom || viewportTop < region.end)
) {
visibleIds.add(region.id)
}
}
}
})
return Array.from(visibleIds)
}
}
class TOCHeaderScrollMask {
static update() {
if (!state.scrollArea) return
const { scrollTop, scrollHeight, clientHeight } = state.scrollArea
const threshold = 5
const isAtTop = scrollTop <= threshold
const isAtBottom = scrollTop >= scrollHeight - clientHeight - threshold
state.scrollArea.classList.toggle('mask-t-from-80%', !isAtTop)
state.scrollArea.classList.toggle('mask-b-from-80%', !isAtBottom)
}
}
class MobileTOCLinks {
static update(headingIds: string[]) {
if (!state.listElement || !state.currentSectionText) return
state.listElement.querySelectorAll('.mobile-toc-item').forEach((item) => {
const tocItem = item as HTMLElement
const headingId = tocItem.dataset.headingId
if (headingId && headingIds.includes(headingId)) {
tocItem.classList.add('text-foreground')
} else {
tocItem.classList.remove('text-foreground')
}
})
if (headingIds.length > 0) {
this.scrollToActive(headingIds[0])
}
this.updateCurrentSectionText(headingIds)
}
static updateCurrentSectionText(headingIds: string[]) {
if (!state.currentSectionText) return
let textToShow = INITIAL_OVERVIEW_TEXT
if (headingIds.length > 0) {
const activeTexts = state.headings
.filter(
(heading) => headingIds.includes(heading.id) && heading.textContent,
)
.map((heading) => heading.textContent!.trim())
if (activeTexts.length > 0) {
textToShow = activeTexts.join(', ')
}
}
state.currentSectionText.textContent = textToShow
}
static scrollToActive(activeHeadingId: string) {
if (!state.listElement) return
const activeItem = state.listElement.querySelector(
`[data-heading-id="${activeHeadingId}"]`,
)
if (!activeItem) return
const scrollContainer = state.scrollArea
if (!scrollContainer) return
const { top: containerTop, height: containerHeight } =
scrollContainer.getBoundingClientRect()
const { top: itemTop, height: itemHeight } =
activeItem.getBoundingClientRect()
const currentItemTop = itemTop - containerTop + scrollContainer.scrollTop
const targetScroll = Math.max(
0,
Math.min(
currentItemTop - (containerHeight - itemHeight) / 2,
scrollContainer.scrollHeight - scrollContainer.clientHeight,
),
)
if (Math.abs(targetScroll - scrollContainer.scrollTop) > 5) {
scrollContainer.scrollTop = targetScroll
}
}
static setupInteraction() {
if (!state.listElement) return
state.listElement.querySelectorAll('.mobile-toc-item').forEach((item) => {
item.addEventListener('click', () => {
if (state.detailsElement) state.detailsElement.open = false
})
})
if (state.scrollArea) {
state.scrollArea.addEventListener(
'scroll',
() => TOCHeaderScrollMask.update(),
{ passive: true },
)
}
if (state.detailsElement) {
state.detailsElement.addEventListener('toggle', () => {
if (state.detailsElement?.open) {
setTimeout(TOCHeaderScrollMask.update, 100)
}
})
}
}
}
class ProgressCircle {
static update() {
if (!state.progressCircle) return
const scrollableDistance =
document.documentElement.scrollHeight - window.innerHeight
const scrollProgress =
scrollableDistance > 0
? Math.min(Math.max(window.scrollY / scrollableDistance, 0), 1)
: 0
state.progressCircle.style.strokeDashoffset = (
PROGRESS_CIRCLE_CIRCUMFERENCE *
(1 - scrollProgress)
).toString()
}
}
class MobileTOCController {
static handleScroll() {
const newActiveIds = HeadingRegions.getVisibleIds()
if (JSON.stringify(newActiveIds) !== JSON.stringify(state.activeIds)) {
state.activeIds = newActiveIds
MobileTOCLinks.update(state.activeIds)
}
ProgressCircle.update()
}
static handleResize() {
HeadingRegions.build()
const newActiveIds = HeadingRegions.getVisibleIds()
if (JSON.stringify(newActiveIds) !== JSON.stringify(state.activeIds)) {
state.activeIds = newActiveIds
MobileTOCLinks.update(state.activeIds)
}
ProgressCircle.update()
}
static init() {
state.reset()
HeadingRegions.build()
if (!state.currentSectionText) return
if (state.headings.length === 0) {
state.currentSectionText.textContent = INITIAL_OVERVIEW_TEXT
window.addEventListener('scroll', ProgressCircle.update, {
passive: true,
})
ProgressCircle.update()
return
}
state.activeIds = HeadingRegions.getVisibleIds()
MobileTOCLinks.update(state.activeIds)
ProgressCircle.update()
MobileTOCLinks.setupInteraction()
window.addEventListener('scroll', this.handleScroll, { passive: true })
window.addEventListener('resize', this.handleResize, { passive: true })
}
static cleanup() {
window.removeEventListener('scroll', this.handleScroll)
window.removeEventListener('scroll', ProgressCircle.update)
window.removeEventListener('resize', this.handleResize)
state.activeIds = []
state.headings = []
state.regions = []
}
}
document.addEventListener('astro:page-load', () => MobileTOCController.init())
document.addEventListener('astro:after-swap', () => {
MobileTOCController.cleanup()
MobileTOCController.init()
})
document.addEventListener('astro:before-swap', () =>
MobileTOCController.cleanup(),
)
</script>
|