aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/TOCSidebar.astro
blob: 77be7c6eeb1b2dec084ef60e881a5d3eb746dd90 (plain) (blame)
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
---
import { ScrollArea } from '@/components/ui/scroll-area'
import type { TOCSection } from '@/lib/data-utils'
import { getParentId, isSubpost } from '@/lib/data-utils'
import { cn, getHeadingMargin } from '@/lib/utils'

type Props = {
  sections: TOCSection[]
  currentPostId: string
}

const { sections, currentPostId } = Astro.props
const isCurrentSubpost = isSubpost(currentPostId)
const parentId = isCurrentSubpost ? getParentId(currentPostId) : currentPostId
---

{
  sections.length > 0 && (
    <div
      id="toc-sidebar-container"
      class="sticky top-20 col-start-1 row-span-1 mr-8 ml-auto hidden h-[calc(100vh-5rem)] max-w-md xl:block"
    >
      <ScrollArea
        client:load
        className="flex max-h-[calc(100vh-8rem)] flex-col overflow-y-auto"
        type="hover"
        data-toc-scroll-area
      >
        <div class="flex flex-col gap-2 px-4">
          <span class="text-lg font-medium">Table of Contents</span>
          {sections.map((section, index) => {
            const isFirstSubpost =
              section.type === 'subpost' &&
              (index === 0 || sections[index - 1].type === 'parent')

            return (
              <>
                {isFirstSubpost && (
                  <div class="mt-2 flex items-center gap-2">
                    <div class="bg-border h-px flex-1" />
                    <span class="text-muted-foreground text-xs font-medium">
                      Subposts
                    </span>
                    <div class="bg-border h-px flex-1" />
                  </div>
                )}

                {section.type === 'parent' ? (
                  <ul class="flex list-none flex-col gap-y-2">
                    {section.headings.map((heading) => (
                      <li
                        class={cn(
                          'text-sm',
                          getHeadingMargin(heading.depth),
                          isCurrentSubpost
                            ? 'text-foreground/40'
                            : 'text-foreground/60',
                        )}
                      >
                        <a
                          href={
                            isCurrentSubpost
                              ? `/blog/${parentId}#${heading.slug}`
                              : `#${heading.slug}`
                          }
                          class="marker:text-foreground/30 list-none underline decoration-transparent underline-offset-[3px] transition-colors duration-200 hover:decoration-inherit"
                          data-heading-link={heading.slug}
                        >
                          {heading.text}
                        </a>
                      </li>
                    ))}
                  </ul>
                ) : (
                  <div
                    class={cn(
                      'rounded-md border p-2',
                      section.subpostId === currentPostId ? 'bg-muted/50' : '',
                    )}
                  >
                    <ul class="flex list-none flex-col gap-y-2">
                      <li
                        class={cn(
                          'text-xs font-medium',
                          section.subpostId === currentPostId
                            ? 'text-foreground'
                            : 'text-foreground/60',
                        )}
                      >
                        <a
                          href={
                            section.subpostId === currentPostId
                              ? '#'
                              : `/blog/${section.subpostId}`
                          }
                          class="marker:text-foreground/30 list-none underline decoration-transparent underline-offset-[3px] transition-colors duration-200 hover:decoration-inherit"
                          data-heading-link={
                            section.subpostId === currentPostId
                              ? 'top'
                              : `${section.subpostId}-top`
                          }
                        >
                          {section.title}
                        </a>
                      </li>
                      {section.headings.map((heading) => (
                        <li
                          class={cn(
                            'text-xs',
                            getHeadingMargin(heading.depth),
                            section.subpostId === currentPostId
                              ? 'text-foreground/60'
                              : 'text-foreground/30',
                          )}
                        >
                          <a
                            href={
                              section.subpostId === currentPostId
                                ? `#${heading.slug}`
                                : `/blog/${section.subpostId}#${heading.slug}`
                            }
                            class="marker:text-foreground/30 hover:text-foreground/60 list-none underline decoration-transparent underline-offset-[3px] transition-colors duration-200 hover:decoration-inherit"
                            data-heading-link={
                              section.subpostId === currentPostId
                                ? heading.slug
                                : `${section.subpostId}-${heading.slug}`
                            }
                          >
                            {heading.text}
                          </a>
                        </li>
                      ))}
                    </ul>
                  </div>
                )}
              </>
            )
          })}
        </div>
      </ScrollArea>
    </div>
  )
}

<script>
  const HEADER_OFFSET = 150

  class TOCState {
    links: NodeListOf<Element> = document.querySelectorAll(
      '[data-heading-link]',
    )
    activeIds: string[] = []
    headings: HTMLElement[] = []
    regions: { id: string; start: number; end: number }[] = []
    scrollArea: HTMLElement | null = null
    tocScrollArea: HTMLElement | null = null

    reset() {
      this.links = document.querySelectorAll(
        '#toc-sidebar-container [data-heading-link]',
      )
      this.activeIds = []
      this.headings = []
      this.regions = []
      const tocContainer = document.getElementById('toc-sidebar-container')
      this.scrollArea =
        tocContainer?.querySelector('[data-radix-scroll-area-viewport]') || null
      this.tocScrollArea =
        tocContainer?.querySelector('[data-toc-scroll-area]') || null
    }
  }

  const state = new TOCState()

  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 TOCScrollMask {
    static update() {
      if (!state.scrollArea || !state.tocScrollArea) return

      const { scrollTop, scrollHeight, clientHeight } = state.scrollArea
      const threshold = 5
      const isAtTop = scrollTop <= threshold
      const isAtBottom = scrollTop >= scrollHeight - clientHeight - threshold

      state.tocScrollArea.classList.toggle('mask-t-from-90%', !isAtTop)
      state.tocScrollArea.classList.toggle('mask-b-from-90%', !isAtBottom)
    }
  }

  class TOCLinks {
    static update(headingIds: string[]) {
      state.links.forEach((link) => {
        link.classList.remove('text-foreground')
      })

      headingIds.forEach((id) => {
        if (id) {
          const activeLink = document.querySelector(
            `#toc-sidebar-container [data-heading-link="${id}"]`,
          )
          if (activeLink) {
            activeLink.classList.add('text-foreground')
          }
        }
      })

      this.scrollToActive(headingIds)
    }

    static scrollToActive(headingIds: string[]) {
      if (!state.scrollArea || !headingIds.length) return

      const activeLink = document.querySelector(
        `#toc-sidebar-container [data-heading-link="${headingIds[0]}"]`,
      )
      if (!activeLink) return

      const { top: areaTop, height: areaHeight } =
        state.scrollArea.getBoundingClientRect()
      const { top: linkTop, height: linkHeight } =
        activeLink.getBoundingClientRect()

      const currentLinkTop = linkTop - areaTop + state.scrollArea.scrollTop
      const targetScroll = Math.max(
        0,
        Math.min(
          currentLinkTop - (areaHeight - linkHeight) / 2,
          state.scrollArea.scrollHeight - state.scrollArea.clientHeight,
        ),
      )

      if (Math.abs(targetScroll - state.scrollArea.scrollTop) > 5) {
        state.scrollArea.scrollTop = targetScroll
      }
    }
  }

  class TOCController {
    static handleScroll() {
      const newActiveIds = HeadingRegions.getVisibleIds()

      if (JSON.stringify(newActiveIds) !== JSON.stringify(state.activeIds)) {
        state.activeIds = newActiveIds
        TOCLinks.update(state.activeIds)
      }
    }

    static handleTOCScroll = () => TOCScrollMask.update()

    static handleResize() {
      HeadingRegions.build()
      const newActiveIds = HeadingRegions.getVisibleIds()

      if (JSON.stringify(newActiveIds) !== JSON.stringify(state.activeIds)) {
        state.activeIds = newActiveIds
        TOCLinks.update(state.activeIds)
      }

      TOCScrollMask.update()
    }

    static init() {
      state.reset()
      HeadingRegions.build()

      if (state.headings.length === 0) {
        TOCLinks.update([])
        return
      }

      this.handleScroll()
      setTimeout(TOCScrollMask.update, 100)

      const options = { passive: true }
      window.addEventListener('scroll', this.handleScroll, options)
      window.addEventListener('resize', this.handleResize, options)
      state.scrollArea?.addEventListener(
        'scroll',
        this.handleTOCScroll,
        options,
      )
    }

    static cleanup() {
      window.removeEventListener('scroll', this.handleScroll)
      window.removeEventListener('resize', this.handleResize)
      state.scrollArea?.removeEventListener('scroll', this.handleTOCScroll)

      Object.assign(state, {
        activeIds: [],
        headings: [],
        regions: [],
        scrollArea: null,
        tocScrollArea: null,
      })
    }
  }

  document.addEventListener('astro:page-load', () => TOCController.init())
  document.addEventListener('astro:after-swap', () => {
    TOCController.cleanup()
    TOCController.init()
  })
  document.addEventListener('astro:before-swap', () => TOCController.cleanup())
</script>