-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-of-contents.tsx
More file actions
44 lines (40 loc) · 1.07 KB
/
table-of-contents.tsx
File metadata and controls
44 lines (40 loc) · 1.07 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
import { TableOfContentsProvider } from "./table-of-contents-provider";
interface Heading {
id: string;
text: string;
level: number;
}
interface Props {
headings: Heading[];
}
function TableOfContentsList({ headings }: Props) {
return (
<nav className="sticky top-8 z-0 max-h-[calc(100vh-4rem)] overflow-y-auto">
<h4 className="text-sm font-semibold mb-4 text-gray-400">On this page</h4>
<ul className="space-y-2.5">
{headings.map(({ id, text, level }) => (
<li
key={id}
style={{ paddingLeft: `${(level - 1) * 16}px` }}
className="text-sm"
>
<a
href={`#${id}`}
className="hover:text-white transition-colors text-gray-400 block"
data-heading-id={id}
>
{text}
</a>
</li>
))}
</ul>
</nav>
);
}
export function TableOfContents({ headings }: Props) {
return (
<TableOfContentsProvider>
<TableOfContentsList headings={headings} />
</TableOfContentsProvider>
);
}