AI Resource Pro / website · airesourcepro.com / src / components / tutorial / table-of-contents.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { ChevronDown } from "lucide-react"; |
| 5 | import { cn } from "@/lib/utils"; |
| 6 | |
| 7 | type Heading = { id: string; text: string; depth: 2 | 3 }; |
| 8 | |
| 9 | // Tracks which section is on screen, so the outline can highlight it |
| 10 | function useActiveHeading(headings: Heading[]) { |
| 11 | const [active, setActive] = useState<string | null>(headings[0]?.id ?? null); |
| 12 | useEffect(() => { |
| 13 | const els = headings.map((h) => document.getElementById(h.id)).filter((el): el is HTMLElement => !!el); |
| 14 | if (els.length === 0) return; |
| 15 | const onScroll = () => { |
| 16 | const line = window.innerHeight * 0.3; |
| 17 | let current = els[0].id; |
| 18 | for (const el of els) if (el.getBoundingClientRect().top <= line) current = el.id; |
| 19 | setActive(current); |
| 20 | }; |
| 21 | onScroll(); |
| 22 | window.addEventListener("scroll", onScroll, { passive: true }); |
| 23 | return () => window.removeEventListener("scroll", onScroll); |
| 24 | }, [headings]); |
| 25 | return active; |
| 26 | } |
| 27 | |
| 28 | function OutlineList({ headings, active, onPick }: { headings: Heading[]; active: string | null; onPick?: () => void }) { |
| 29 | let section = 0; |
| 30 | return ( |
| 31 | <ol className="space-y-0.5"> |
| 32 | {headings.map((h) => { |
| 33 | if (h.depth === 2) section += 1; |
| 34 | const on = h.id === active; |
| 35 | return ( |
| 36 | <li key={h.id}> |
| 37 | <a |
| 38 | href={`#${h.id}`} |
| 39 | onClick={onPick} |
| 40 | aria-current={on ? "location" : undefined} |
| 41 | className={cn( |
| 42 | "group flex items-baseline gap-3 rounded-lg border-l-2 py-2 pr-2 leading-snug transition-colors", |
| 43 | h.depth === 3 ? "pl-9 text-[13.5px]" : "pl-3 text-[14.5px]", |
| 44 | on |
| 45 | ? "border-[var(--tc,var(--brand))] bg-[color-mix(in_srgb,var(--tc,var(--brand))_12%,transparent)] font-normal text-foreground" |
| 46 | : "border-transparent text-[var(--link)] hover:border-[color-mix(in_srgb,var(--link)_50%,transparent)] hover:bg-foreground/5" |
| 47 | )} |
| 48 | > |
| 49 | {h.depth === 2 && ( |
| 50 | <span className={cn("font-mono text-[11px] tabular-nums", on ? "text-[var(--tc,var(--brand))]" : "text-[var(--ash)]")}> |
| 51 | {String(section).padStart(2, "0")} |
| 52 | </span> |
| 53 | )} |
| 54 | <span className="underline decoration-transparent underline-offset-4 transition-colors group-hover:decoration-current"> |
| 55 | {h.text} |
| 56 | </span> |
| 57 | </a> |
| 58 | </li> |
| 59 | ); |
| 60 | })} |
| 61 | </ol> |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const heading = "mb-3 flex items-center gap-2 font-mono text-xs font-medium uppercase tracking-[0.08em] text-foreground"; |
| 66 | |
| 67 | // The sticky "On this page" outline beside the article on wide screens |
| 68 | export function TableOfContents({ headings }: { headings: Heading[] }) { |
| 69 | const active = useActiveHeading(headings); |
| 70 | return ( |
| 71 | <nav aria-label="On this page" className="rounded-2xl border border-border bg-card p-4"> |
| 72 | <p className={heading}>On this page</p> |
| 73 | <OutlineList headings={headings} active={active} /> |
| 74 | <a |
| 75 | href="#top" |
| 76 | className="mt-3 block border-t border-border pt-3 font-mono text-[11px] uppercase tracking-[0.08em] text-[var(--ash)] transition-colors hover:text-foreground" |
| 77 | > |
| 78 | ↑ Back to top |
| 79 | </a> |
| 80 | </nav> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | // A collapsible outline at the top of the article on smaller screens |
| 85 | export function InlineTableOfContents({ headings }: { headings: Heading[] }) { |
| 86 | const [open, setOpen] = useState(false); |
| 87 | const active = useActiveHeading(headings); |
| 88 | return ( |
| 89 | <nav aria-label="On this page" className="mt-8 rounded-2xl border border-border bg-card xl:hidden"> |
| 90 | <button |
| 91 | type="button" |
| 92 | onClick={() => setOpen(!open)} |
| 93 | aria-expanded={open} |
| 94 | className={cn(heading, "mb-0 w-full justify-between p-4")} |
| 95 | > |
| 96 | On this page · {headings.filter((h) => h.depth === 2).length} sections |
| 97 | <ChevronDown className={cn("h-4 w-4 transition-transform", open && "rotate-180")} /> |
| 98 | </button> |
| 99 | {open && ( |
| 100 | <div className="px-4 pb-4"> |
| 101 | <OutlineList headings={headings} active={active} onPick={() => setOpen(false)} /> |
| 102 | </div> |
| 103 | )} |
| 104 | </nav> |
| 105 | ); |
| 106 | } |