e7738de6d2
CI / Deep Native Runtime Cases (1/6) (push) Has been skipped
CI / Native Preflight (push) Failing after 1s
CI / Native Runtime Cases (1/2) (push) Failing after 0s
CI / Native Runtime Cases (2/2) (push) Failing after 1s
CI / Native Metadata Reports (push) Failing after 0s
CI / Native Direct Backend Artifacts (push) Failing after 0s
CI / Native Sanitizer Smoke (push) Failing after 1s
CI / Command Contract Snapshots (push) Failing after 1s
CI / Deep Conformance Suite (push) Has been skipped
CI / Graph Build Perf (push) Failing after 1s
CI / Deep Native Preflight (push) Has been skipped
CI / Deep Native Runtime Cases (2/6) (push) Has been skipped
CI / Deep Native Runtime Cases (3/6) (push) Has been skipped
CI / Conformance Suite (push) Failing after 1s
CI / Workspace Checks (push) Failing after 0s
CI / Deep Native Runtime Cases (5/6) (push) Has been skipped
CI / Deep Native Runtime Cases (6/6) (push) Has been skipped
CI / Deep Native Runtime Cases (4/6) (push) Has been skipped
CI / Deep Graph Build Perf (push) Has been skipped
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import type { Heading } from "@/lib/types";
|
|
|
|
export function DocsToc({ headings }: { headings: Heading[] }) {
|
|
const [activeId, setActiveId] = useState<string | null>(headings[0]?.id ?? null);
|
|
const observerRef = useRef<IntersectionObserver | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined" || headings.length === 0) return;
|
|
|
|
const ids = headings.map((h) => h.id);
|
|
const elements = ids
|
|
.map((id) => document.getElementById(id))
|
|
.filter((el): el is HTMLElement => el !== null);
|
|
if (elements.length === 0) return;
|
|
|
|
const visible = new Set();
|
|
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
for (const entry of entries) {
|
|
if (entry.isIntersecting) visible.add(entry.target.id);
|
|
else visible.delete(entry.target.id);
|
|
}
|
|
|
|
let next = null;
|
|
for (const h of headings) {
|
|
if (visible.has(h.id)) {
|
|
next = h.id;
|
|
break;
|
|
}
|
|
}
|
|
if (!next && visible.size === 0) {
|
|
const scrollY = window.scrollY;
|
|
for (let i = elements.length - 1; i >= 0; i--) {
|
|
if (elements[i].offsetTop <= scrollY + 80) {
|
|
next = elements[i].id;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (next) setActiveId(next);
|
|
},
|
|
{ rootMargin: "-64px 0px -60% 0px", threshold: 0 },
|
|
);
|
|
|
|
elements.forEach((el) => observer.observe(el));
|
|
observerRef.current = observer;
|
|
|
|
return () => observer.disconnect();
|
|
}, [headings]);
|
|
|
|
if (headings.length === 0) return null;
|
|
|
|
return (
|
|
<aside
|
|
aria-label="On this page"
|
|
className="sticky top-14 hidden h-[calc(100vh-3.5rem)] overflow-y-auto py-8 pl-0 pr-4 lg:block"
|
|
>
|
|
<p className="mb-3 pl-4 text-xs font-semibold uppercase tracking-[0.05em] text-fg">
|
|
On this page
|
|
</p>
|
|
<nav>
|
|
{headings.map((h) => {
|
|
const indent = h.level === 3 ? "pl-7" : h.level === 4 ? "pl-10" : "pl-4";
|
|
return (
|
|
<a
|
|
key={h.id}
|
|
href={`#${h.id}`}
|
|
className={`block py-1 pr-4 ${indent} text-[0.8125rem] leading-snug no-underline transition hover:text-fg ${
|
|
activeId === h.id ? "text-fg" : "text-muted"
|
|
}`}
|
|
>
|
|
{h.text}
|
|
</a>
|
|
);
|
|
})}
|
|
</nav>
|
|
</aside>
|
|
);
|
|
}
|