-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathToc.tsx
More file actions
168 lines (149 loc) · 4.29 KB
/
Toc.tsx
File metadata and controls
168 lines (149 loc) · 4.29 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
'use client';
import clsx from 'clsx';
import {allPosts} from 'contentlayer/generated';
import Link from 'next/link';
import {useParams} from 'next/navigation';
import {useCallback, useEffect, useState} from 'react';
import {BugIcon} from './icons/BugIcon';
import {Globe} from './icons/Globe';
import {PencilIcon} from './icons/PencilIcon';
interface Heading {
level: number;
title: string;
id?: string;
}
interface Props {
headings: Heading[];
}
type ParamsType = {
version?: string;
slug: string[];
};
export function TableOfContents({headings}: Props) {
let [currentSection, setCurrentSection] = useState(headings[0]?.id);
const [path, setPath] = useState('');
const params: ParamsType = useParams();
let getHeadings = useCallback((headings: Heading[]) => {
return headings
.map(heading => {
if (heading.id) {
let el = document.getElementById(heading.id);
if (!el) return null;
let style = window.getComputedStyle(el);
let scrollMt = parseFloat(style.scrollMarginTop);
let top =
window.scrollY +
el.getBoundingClientRect().top -
scrollMt;
return {id: heading.id, top};
}
})
.filter((x): x is {id: string; top: number} => x !== null);
}, []);
useEffect(() => {
const path = params.slug.join('/');
if (allPosts) {
const post = allPosts.find(
post => post._raw.flattenedPath === path
);
if (post) {
let currentPath = post._id;
if (params?.version)
currentPath = `versioned/${params.version}/${currentPath}`;
setPath(currentPath);
}
}
}, [params]);
useEffect(() => {
if (headings.length === 0) return;
let headingsWithPositions = getHeadings(headings);
function onScroll() {
let top = window.scrollY;
let current = headingsWithPositions[0]?.id;
for (let heading of headingsWithPositions) {
if (top >= heading.top - 10) {
current = heading.id;
} else {
break;
}
}
setCurrentSection(current);
}
window.addEventListener('scroll', onScroll, {passive: true});
onScroll();
return () => {
window.removeEventListener('scroll', onScroll);
};
}, [getHeadings, headings]);
function isActive(heading: Heading) {
return heading.id === currentSection;
}
return (
<div className="hidden xl:sticky xl:top-[4.75rem] xl:-mr-6 xl:block xl:h-[calc(100vh-4.75rem)] xl:flex-none xl:overflow-y-auto xl:py-16 xl:pr-6">
<nav aria-labelledby="on-this-page-title" className="w-56">
{headings.length > 0 && (
<>
<h2
id="on-this-page-title"
className="font-display text-sm font-medium text-slate-900 dark:text-white"
>
On this page
</h2>
<ol role="list" className="mt-4 space-y-3 text-sm">
{headings.map(heading => (
<li key={heading.id}>
{heading.level === 1 && (
<h3>
<Link
href={`#${heading.id}`}
className={clsx(
isActive(heading)
? 'font-semibold text-link-light dark:text-link'
: 'font-normal text-slate-500 hover:text-slate-700 dark:text-dark-text-secondary dark:hover:text-slate-300'
)}
>
{heading.title}
</Link>
</h3>
)}
{heading.level === 2 && (
<ol
role="list"
className="mt-2 space-y-3 pl-5 text-slate-500 dark:text-dark-text-secondary"
>
<li key={heading.id}>
<Link
href={`#${heading.id}`}
className={clsx(
isActive(heading)
? 'font-semibold text-link-light dark:text-link'
: 'hover:text-slate-600 dark:hover:text-slate-300'
)}
>
{heading.title}
</Link>
</li>
</ol>
)}
</li>
))}
</ol>
{/* Edit doc with GitHub button */}
<hr className="mt-4" />
<div className="mt-4 flex items-center text-sm">
<a
href={`https://github.com/sourcegraph/docs/edit/main/docs/${path}`}
target="_blank"
rel="noopener noreferrer"
className="flex items-center text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
>
<PencilIcon className="mr-1 h-4 w-4" />
Edit this page on GitHub
</a>
</div>
</>
)}
</nav>
</div>
);
}