forked from TanStack/tanstack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToc.tsx
More file actions
67 lines (64 loc) · 2.02 KB
/
Toc.tsx
File metadata and controls
67 lines (64 loc) · 2.02 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
import { Link } from '@tanstack/react-router'
import * as React from 'react'
import { twMerge } from 'tailwind-merge'
import { MarkdownHeading } from '~/utils/markdown/processor'
const headingLevels: Record<number, string> = {
1: '',
2: '',
3: 'pl-2',
4: 'pl-4',
5: 'pl-6',
6: 'pl-8',
}
type TocProps = {
headings: MarkdownHeading[]
colorFrom?: string
colorTo?: string
textColor?: string
activeHeadings: Array<string>
}
export function Toc({ headings, textColor, activeHeadings }: TocProps) {
return (
<nav className="flex flex-col sticky top-[var(--navbar-height)] max-h-[calc(100dvh-var(--navbar-height))] overflow-hidden">
<div className="py-1">
<h3 className="text-[.8em] lg:text-[.825em] xl:text-[.875em] 2xl:text-[.9em] font-bold">
On this page
</h3>
</div>
<ul
className={twMerge(
'py-1 flex flex-col overflow-y-auto text-[.6em] lg:text-[.65em] xl:text-[.7em] 2xl:text-[.75em]',
)}
>
{headings?.map((heading) => (
<li
key={heading.id}
className={twMerge('w-full', headingLevels[heading.level])}
>
<Link
to="."
title={heading.id}
hash={heading.id}
aria-current={activeHeadings.includes(heading.id) && 'location'}
className={twMerge(
'block py-1 pl-2 border-l-2 rounded-r transition-colors duration-200 opacity-60 hover:opacity-100 hover:bg-gray-500/10',
activeHeadings.includes(heading.id)
? `opacity-100 border-current ${textColor}`
: 'border-transparent',
)}
resetScroll={false}
hashScrollIntoView={{
behavior: 'smooth',
}}
>
<span
className="block break-words overflow-hidden"
dangerouslySetInnerHTML={{ __html: heading.text }}
/>
</Link>
</li>
))}
</ul>
</nav>
)
}