Files
lixiyu-net/components/readerpage/TOC.tsx

32 lines
1.1 KiB
TypeScript
Raw Normal View History

import { useActiveHeading } from "@/hooks/useActiveHeading";
2023-12-25 17:21:39 +08:00
import { TTOCItem } from "@/types/toc.type";
import Link from "next/link";
import { twMerge } from "tailwind-merge";
2023-12-25 17:21:39 +08:00
export const TOC = (props: { data: TTOCItem[] }) => {
const activeId = useActiveHeading(props.data.map((item) => `#${item.anchorId}`));
2023-12-25 17:21:39 +08:00
return (
2024-04-03 22:08:27 +08:00
<div className="mx-5">
<div className="text-lg text-center p-2 font-bold border-t-2 border-b-2 border-gray-500">
{"TABLE OF CONTENTS"}
</div>
<div className="px-2 py-2 h-[60vh] overflow-y-auto flat-scrollbar-normal">
<div>
{props.data?.map((item) => (
2024-04-03 22:08:27 +08:00
<Link href={`#${item.anchorId}`} key={`toc-${item.anchorId}`}>
<div
className={twMerge(
2024-04-03 22:08:27 +08:00
`py-2 text-sm rounded-lg hover:text-sky-700 dark:hover:text-sky-400`,
activeId === `#${item.anchorId}` ? "text-sky-700 dark:text-sky-400" : "",
)}
style={{ paddingLeft: `${item.level - 1}em` }}
2024-04-03 22:08:27 +08:00
>{`${item.title}`}</div>
</Link>
))}
2024-04-03 22:08:27 +08:00
</div>
</div>
</div>
2023-12-25 17:21:39 +08:00
);
};