@@ -1,4 +1,4 @@
1−import { useState, useEffect } from "react";
1+import { useState, useEffect, useRef } from "react";
22import { useRouter } from "next/router";
33import Head from "next/head";
44import ErrorPage from "../pages/404";
@@ -12,13 +12,14 @@ import { decode } from "html-entities";
1212
1313const TableOfContents = () => {
1414 const { nestedHeadings } = useHeadingsData();
15+ const [activeId, setActiveId] = useState();
16+ useIntersectionObserver(setActiveId);
1517 return (
1618 <nav
17− className="sticky min-h-0 h-screen overflow-y-scroll w-52 flex-shrink-0 pb-8 hidden lg:block"
18− style={{ top: 0 }}
19+ className="sticky min-h-0 overflow-y-scroll w-52 flex-shrink-0 pb-24 hidden lg:block"
20+ style={{ top: "8rem", height: "calc(100vh - 16rem)" }}
1921 >
20− <p>On this page</p>
21− <Headings headings={nestedHeadings} />
22+ <Headings headings={nestedHeadings} activeId={activeId} />
2223 </nav>
2324 );
2425};
@@ -55,17 +56,20 @@ const useHeadingsData = () => {
5556 return { nestedHeadings };
5657};
5758
58−const Headings = ({ headings }) => (
59+const Headings = ({ headings, activeId }) => (
5960 <ul>
6061 {headings.map((heading) => (
6162 <li key={heading.id}>
62− <a className="font-semibold text-sm" href={`#${heading.id}`}>
63+ <a className="font-semibold" href={`#${heading.id}`}>
6364 {heading.title}
6465 </a>
6566 {heading.items.length > 0 && (
66− <ul className="pl-2 text-sm">
67+ <ul className="pl-2">
6768 {heading.items.map((child) => (
68− <li key={child.id}>
69+ <li
70+ key={child.id}
71+ className={child.id === activeId ? "text-green" : "text-gray"}
72+ >
6973 <a href={`#${child.id}`}>{child.title}</a>
7074 </li>
7175 ))}
@@ -76,6 +80,46 @@ const Headings = ({ headings }) => (
7680 </ul>
7781);
7882
83+const useIntersectionObserver = (setActiveId) => {
84+ const headingElementsRef = useRef({});
85+ useEffect(() => {
86+ const callback = (headings) => {
87+ headingElementsRef.current = headings.reduce((map, headingElement) => {
88+ map[headingElement.target.id] = headingElement;
89+ return map;
90+ }, headingElementsRef.current);
91+
92+ const visibleHeadings = [];
93+ Object.keys(headingElementsRef.current).forEach((key) => {
94+ const headingElement = headingElementsRef.current[key];
95+ if (headingElement.isIntersecting) visibleHeadings.push(headingElement);
96+ });
97+
98+ const getIndexFromId = (id) =>
99+ headingElements.findIndex((heading) => heading.id === id);
100+
101+ if (visibleHeadings.length === 1) {
102+ setActiveId(visibleHeadings[0].target.id);
103+ } else if (visibleHeadings.length > 1) {
104+ const sortedVisibleHeadings = visibleHeadings.sort(
105+ (a, b) => getIndexFromId(a.target.id) > getIndexFromId(b.target.id)
106+ );
107+ setActiveId(sortedVisibleHeadings[0].target.id);
108+ }
109+ };
110+
111+ const observer = new IntersectionObserver(callback, {
112+ rootMargin: "0px 0px -50% 0px",
113+ });
114+
115+ const headingElements = Array.from(document.querySelectorAll("h2, h3"));
116+
117+ headingElements.forEach((element) => observer.observe(element));
118+
119+ return () => observer.disconnect();
120+ }, [setActiveId]);
121+};
122+
79123export default function PageWithIndex({ post, markdown, search }) {
80124 const router = useRouter();
81125 if (!router.isFallback && !post?.slug) {