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(h2, h3, h4)'); itemOffsets.current = Array.from(titles).map((title) => ({ id: title.id, topOffset: title.getBoundingClientRect().top + window.scrollY, })); }; const onScroll = () => { const itemIndex = itemOffsets.current.findIndex((item) => item.topOffset > window.scrollY + window.innerHeight / 3); if (itemIndex === 0) { setActiveId(undefined); } else if (itemIndex === -1) { setActiveId(itemOffsets.current[itemOffsets.current.length - 1].id); } else { setActiveId(itemOffsets.current[itemIndex - 1].id); } }; getItemOffsets(); window.addEventListener('resize', getItemOffsets); window.addEventListener('scroll', onScroll); return () => { window.removeEventListener('resize', getItemOffsets); window.removeEventListener('scroll', onScroll); }; }, []); return ( ); }; export default DocSidebar; > Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/packages/astro/src/runtime (unfollow)
AgeCommit message (Collapse)AuthorFilesLines
2023-07-11Filter out astro from `peerDependencies` in `astro add` (#7620)Gravatar Chris Swithinbank 2-1/+7
2023-07-11[ci] formatGravatar bluwy 1-1/+1
2023-07-11Refactor simple CLI commands (#7619)Gravatar Bjorn Lu 12-115/+123
2023-07-10fix: `astro info` command fallback for package manager (#7618)Gravatar Emanuele Stoppa 2-1/+7
2023-07-10Add CLI startup speed benchmark (#7617)Gravatar Bjorn Lu 5-11/+95
2023-07-10[ci] formatGravatar bluwy 1-1/+1