import type { FunctionalComponent } from 'preact'; import { h } from 'preact'; import { useState, useEffect, useRef } from 'preact/hooks'; import EditOnGithub from './EditOnGithub'; const DocSidebar: FunctionalComponent<{ headers: any[]; editHref: string }> = ({ headers = [], editHref, }) => { const itemOffsets = useRef([]); const [activeId, setActiveId] = useState(undefined); useEffect(() => { const getItemOffsets = () => { const titles = document.querySelectorAll('article :is(h1, h2, h3, h4)'); itemOffsets.current = Array.from(titles).map((title) => ({ id: title.id, topOffset: title.getBoundingClientRect().top + window.scrollY, })); }; getItemOffsets(); window.addEventListener('resize', getItemOffsets); return () => { window.removeEventListener('resize', getItemOffsets); }; }, []); return ( ); }; export default DocSidebar;