%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/docs/ |
| Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/docs/DocsToc.tsx |
import { useEffect, useState } from "react";
export interface TocItem {
id: string;
title: string;
}
/** Daftar isi sticky dengan penanda section aktif saat scroll. */
export default function DocsToc({ items }: { items: readonly TocItem[] }) {
const [active, setActive] = useState(items[0]?.id ?? "");
useEffect(() => {
const elements = items
.map((item) => document.getElementById(item.id))
.filter((el): el is HTMLElement => Boolean(el));
if (elements.length === 0) return;
const observer = new IntersectionObserver(
(entries) => {
const visible = entries
.filter((entry) => entry.isIntersecting)
.sort(
(a, b) =>
a.boundingClientRect.top - b.boundingClientRect.top,
);
if (visible[0]) setActive(visible[0].target.id);
},
{ rootMargin: "-96px 0px -65% 0px", threshold: [0, 1] },
);
elements.forEach((el) => observer.observe(el));
return () => observer.disconnect();
}, [items]);
return (
<nav className="hidden xl:block" aria-label="Daftar isi dokumentasi">
<div className="sticky top-24">
<p className="mb-2 text-[10px] font-semibold uppercase tracking-widest text-slate-600">
On this page
</p>
<ul className="space-y-0.5 border-l border-white/[0.06]">
{items.map((item) => {
const isActive = item.id === active;
return (
<li key={item.id}>
<a
href={`#${item.id}`}
className={`-ml-px block border-l py-1.5 pl-3 text-xs transition-colors ${
isActive
? "border-brand-400 text-white"
: "border-transparent text-slate-500 hover:border-white/20 hover:text-slate-300"
}`}
>
{item.title}
</a>
</li>
);
})}
</ul>
</div>
</nav>
);
}