(itemIds: string[])
| 4 | import { cn } from "@/lib/utils"; |
| 5 | |
| 6 | function useActiveItem(itemIds: string[]) { |
| 7 | const [activeId, setActiveId] = React.useState<string | null>(null); |
| 8 | |
| 9 | React.useEffect(() => { |
| 10 | // Default to first item on mount if nothing is active yet |
| 11 | if (!activeId && itemIds?.length) { |
| 12 | setActiveId(itemIds[0] ?? null); |
| 13 | } |
| 14 | |
| 15 | const observer = new IntersectionObserver( |
| 16 | (entries) => { |
| 17 | for (const entry of entries) { |
| 18 | if (entry.isIntersecting) { |
| 19 | setActiveId(entry.target.id); |
| 20 | } |
| 21 | } |
| 22 | }, |
| 23 | { rootMargin: "0% 0% -80% 0%" }, |
| 24 | ); |
| 25 | |
| 26 | for (const id of itemIds ?? []) { |
| 27 | const element = document.getElementById(id); |
| 28 | if (element) { |
| 29 | observer.observe(element); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return () => { |
| 34 | for (const id of itemIds ?? []) { |
| 35 | const element = document.getElementById(id); |
| 36 | if (element) { |
| 37 | observer.unobserve(element); |
| 38 | } |
| 39 | } |
| 40 | }; |
| 41 | }, [itemIds, activeId]); |
| 42 | |
| 43 | return activeId; |
| 44 | } |
| 45 | |
| 46 | export function DocsTableOfContents({ |
| 47 | toc, |
no outgoing calls
no test coverage detected