Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 35a38ff

Browse files
add highlighting to headings on scroll
1 parent a80738c commit 35a38ff

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/components/layout/DocToC.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DocTracingBeam } from './DocTracingBeam'
77
import { Button } from '../ui/button'
88
import { GitHubIcon } from '../icons/GitHubIcon'
99
import { useMotionValue } from 'framer-motion'
10+
import { cn } from '@/lib/utils'
1011

1112
interface Toc {
1213
url: string
@@ -22,9 +23,11 @@ const DocToC = ({ doc }: { doc: Doc }) => {
2223
const y1 = useMotionValue(0)
2324
const y2 = useMotionValue(0)
2425

26+
const [activeIndex, setActiveIndex] = useState(-1)
27+
2528
useEffect(() => {
2629
const headings = document
27-
.querySelectorAll<HTMLHeadingElement>('.md-content-header')
30+
.querySelectorAll<HTMLHeadingElement>('h2.md-content-header')
2831
.values()
2932
.toArray()
3033

@@ -42,6 +45,8 @@ const DocToC = ({ doc }: { doc: Doc }) => {
4245
)
4346

4447
y2.set(initial + (index * sectionSize + offset))
48+
49+
setActiveIndex(index)
4550
}
4651
})
4752
}, options)
@@ -68,7 +73,12 @@ const DocToC = ({ doc }: { doc: Doc }) => {
6873
<li key={item.url + i}>
6974
<Link
7075
href={item.url}
71-
className="text-2xs text-muted-foreground transition-colors hover:text-zinc-900 dark:hover:text-zinc-200"
76+
className={cn(
77+
'text-2xs',
78+
activeIndex === i
79+
? 'text-zinc-900 dark:text-zinc-200'
80+
: 'text-muted-foreground transition-colors hover:text-zinc-900 dark:hover:text-zinc-200',
81+
)}
7282
>
7383
{item.value}
7484
</Link>

0 commit comments

Comments
 (0)