File size: 1,528 Bytes
1e92f2d |
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 |
"use client"
import { scrollIntoView } from "@/app/docs/scroll-into-view"
import { Box, Stack, Text, chakra } from "@chakra-ui/react"
import Link from "next/link"
import { useEffect } from "react"
import { useScrollSpy } from "../lib/use-scroll-spy"
interface TocItem {
title: React.ReactNode
url: string
depth: number
}
interface Props {
items: TocItem[]
}
const TocLink = chakra(Link, {
base: {
fontSize: "sm",
color: "fg.muted",
_currentPage: { color: "fg", fontWeight: "medium" },
_hover: { color: "fg" },
ms: "calc(1rem * var(--toc-depth))",
},
})
export const Toc = (props: Props) => {
const { items } = props
const activeItem = useScrollSpy(items.map((entry) => entry.url))
useEffect(() => {
const activeLink = document.querySelector("[data-toc][aria-current='page']")
const toc = document.getElementById("toc")
if (toc && activeLink) {
scrollIntoView(toc, activeLink as HTMLElement, 120)
}
}, [activeItem])
if (!items.length) {
return <div />
}
return (
<Box as="nav" fontSize="sm">
<Text fontWeight="semibold">On this page</Text>
<Stack mt="3">
{items.map((item, index) => (
<TocLink
data-toc
id={item.url}
key={index}
href={item.url}
aria-current={item.url === activeItem ? "page" : undefined}
css={{ "--toc-depth": item.depth }}
>
{item.title}
</TocLink>
))}
</Stack>
</Box>
)
}
|