forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableOfContents.tsx
More file actions
181 lines (164 loc) · 5.82 KB
/
TableOfContents.tsx
File metadata and controls
181 lines (164 loc) · 5.82 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
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useEffect, useRef, useState } from "preact/hooks";
import type { MarkdownHeading } from "../utils/markdown.ts";
export interface TableOfContentsProps {
headings: MarkdownHeading[];
}
function setActiveLink(
container: HTMLElement,
marker: HTMLElement,
id: string,
) {
container.querySelectorAll(`a`).forEach((link) =>
link.classList.remove("active")
);
const tocLink = container.querySelector(
`a[href="#${id}"]`,
) as HTMLElement;
if (tocLink === null) return;
tocLink.classList.add("active");
const rect = tocLink
.getBoundingClientRect();
const markerRect = marker.getBoundingClientRect();
const top = tocLink.offsetTop + (rect.height / 2) -
(markerRect.height / 2);
marker.style.cssText = `transform: translate3d(0, ${top}px, 0); opacity: 1`;
}
export function TableOfContents({ headings }: TableOfContentsProps) {
const ref = useRef<HTMLDivElement>(null);
const refMarker = useRef<HTMLDivElement>(null);
const [isOpen, setIsOpen] = useState(false);
useEffect(() => {
if (!ref.current) return;
const container = ref.current;
const activeList = new Array(headings.length).fill(false);
const visibleList = new Array(headings.length).fill(false);
const marker = refMarker.current!;
const observer = new IntersectionObserver((entries) => {
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
const target = entry.target;
for (let j = 0; j < headings.length; j++) {
const heading = headings[j];
if (heading.id === target.id) {
const active = entry.isIntersecting ||
entry.boundingClientRect.top < 0;
activeList[j] = active;
visibleList[j] = entry.isIntersecting;
}
}
}
// Reset links
for (let i = 0; i < headings.length; i++) {
const id = headings[i].id;
const tocLink = container.querySelector(
`a[href="#${id}"]`,
);
if (tocLink !== null) {
tocLink.classList.remove("active");
}
}
let activeIdx = visibleList.indexOf(true);
if (activeIdx < 0) {
activeIdx = activeList.lastIndexOf(true);
}
if (activeIdx > -1) {
const id = headings[activeIdx].id;
setActiveLink(container, marker, id);
} else {
marker.style.cssText = `transform: translate3d(0, 0, 0); opacity: 0`;
}
});
document.querySelectorAll(
".markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6",
).forEach((elem) => {
observer.observe(elem);
});
return () => {
observer.disconnect();
};
}, [headings]);
return (
<div
ref={ref}
class="relative xl:order-2 w-56 xl:max-w-xs xl:top-14 shrink-0"
>
{headings.length > 0 && (
<>
<div class="xl:hidden mx-4 md:mx-0 mt-4 md:mt-0">
<button
id="toc-outline-btn"
onClick={() => setIsOpen((v) => !v)}
class="bg-background-primary py-2 px-4 rounded border border-foreground-secondary/30 flex items-center hover:border-fresh-green/80 transition-colors text-sm"
>
On this page
<svg
class={`w-4 h-4 inline-block ml-2 -rotate-90 [&.active]:rotate-0 ${
isOpen ? "active" : ""
}`}
>
<use href="/icons.svg#arrow-down" />
</svg>
</button>
{isOpen && (
<div class="mt-2 pl-4 border-l border-foreground-primary/20 text-[13px] leading-7">
<nav aria-labelledby="toc-outline-btn">
<ul>
{headings.map((heading) => {
return (
<li key={heading.id}>
<a
href={`#${heading.id}`}
class="block truncatetext-gray-600"
dangerouslySetInnerHTML={{ __html: heading.html }}
/>
</li>
);
})}
</ul>
</nav>
</div>
)}
</div>
<div class="hidden xl:block xl:sticky xl:top-40">
<div class="relative">
<div
ref={refMarker}
class="marker w-[2px] bg-green-400 h-5 absolute top-0 opacity-0 transition-all"
/>
<div class="pl-4 border-l border-foreground-secondary/20 text-[13px] leading-7">
<div role="heading" aria-level={2} class="font-semibold">
On this page
</div>
<nav aria-labelledby="doc-outline-aria-label">
<span id="doc-outline-aria-label" class="sr-only">
Table of Contents for current page
</span>
<ul>
{headings.map((heading) => {
return (
<li key={heading.id}>
<a
href={`#${heading.id}`}
class="block truncate transition-colors text-gray-600 [&.active]:text-green-600"
onClick={() => {
setActiveLink(
ref.current!,
refMarker.current!,
heading.id,
);
}}
dangerouslySetInnerHTML={{ __html: heading.html }}
/>
</li>
);
})}
</ul>
</nav>
</div>
</div>
</div>
</>
)}
</div>
);
}