summaryrefslogtreecommitdiffstats
path: root/src/scripts/lib/toc.ts
blob: 958f2094a2aefb83d14bbe3883f6363e322da348 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
export function mountToc() {
	const observer = new IntersectionObserver((entries) => {
		for (const entry of entries) {
			const id = entry.target.getAttribute("id");
			document
				.querySelector(`.o-toc__item[href="#${id}"]`)
				?.classList.toggle("is-selected", entry.intersectionRatio > 0);
		}
	});

	document.querySelectorAll("section[id]").forEach((section) => {
		observer.observe(section);
	});
}