import type { DocSection } from "@/services/DocCache"; import type { Data } from "@/pages/+data"; import { createSignal, createEffect, For } from "solid-js"; import { useData } from "vike-solid/useData"; import { Link } from "@/components/Link"; import clsx from "clsx"; export function TableOfContents() { const { sections } = useData(); if (!sections) return null; createEffect(() => { for (const [key, value] of Object.entries(sections)) { console.log(`${key}: ${JSON.stringify(value)}`); } }); const [currentSection, setCurrentSection] = createSignal(sections[0]?.hash); const getHeadings = () => { return sections .map((section) => { if (!section.hash) return null; const el = document.getElementById(section.hash); if (!el) return null; const style = window.getComputedStyle(el); const scrollMt = Number.parseFloat(style.scrollMarginTop); const top = window.scrollY + el.getBoundingClientRect().top - scrollMt; return { id: section.hash, top }; }) .filter((x): x is { id: string; top: number } => x !== null); }; createEffect(() => { if (sections.length === 0) return; const headings = getHeadings(); function onScroll() { const top = window.scrollY; let current = headings[0]?.id; for (const heading of headings) { if (top < heading.top - 10) break; current = heading.id; } setCurrentSection(current); } window.addEventListener("scroll", onScroll, { passive: true }); onScroll(); return () => { window.removeEventListener("scroll", onScroll); }; }, [getHeadings, sections]); function isActive(section: DocSection) { if (section.hash === currentSection()) return true; return false; // if (!section.children) return false; // return section.children.findIndex(isActive) > -1; } return ( ); }