diff options
author | 2023-06-06 11:21:19 -0500 | |
---|---|---|
committer | 2023-06-06 11:21:19 -0500 | |
commit | 5ed2a2f666707e579e18f2890ab89b7cc6f717c3 (patch) | |
tree | 9895d49bc5bd33a63ff0f2e94053e771a54b9dc4 /examples/docs/src/components/RightSidebar/TableOfContents.tsx | |
parent | d5a089810f8218f694ac56591648d36051708560 (diff) | |
download | astro-5ed2a2f666707e579e18f2890ab89b7cc6f717c3.tar.gz astro-5ed2a2f666707e579e18f2890ab89b7cc6f717c3.tar.zst astro-5ed2a2f666707e579e18f2890ab89b7cc6f717c3.zip |
chore: remove docs example (#7306)
Diffstat (limited to 'examples/docs/src/components/RightSidebar/TableOfContents.tsx')
-rw-r--r-- | examples/docs/src/components/RightSidebar/TableOfContents.tsx | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/examples/docs/src/components/RightSidebar/TableOfContents.tsx b/examples/docs/src/components/RightSidebar/TableOfContents.tsx deleted file mode 100644 index 962d64ec2..000000000 --- a/examples/docs/src/components/RightSidebar/TableOfContents.tsx +++ /dev/null @@ -1,93 +0,0 @@ -import type { MarkdownHeading } from 'astro'; -import type { FunctionalComponent } from 'preact'; -import { unescape } from 'html-escaper'; -import { useState, useEffect, useRef } from 'preact/hooks'; - -type ItemOffsets = { - id: string; - topOffset: number; -}; - -const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({ - headings = [], -}) => { - const toc = useRef<HTMLUListElement>(); - const onThisPageID = 'on-this-page-heading'; - const itemOffsets = useRef<ItemOffsets[]>([]); - const [currentID, setCurrentID] = useState('overview'); - 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); - }; - }, []); - - useEffect(() => { - if (!toc.current) return; - - const setCurrent: IntersectionObserverCallback = (entries) => { - for (const entry of entries) { - if (entry.isIntersecting) { - const { id } = entry.target; - if (id === onThisPageID) continue; - setCurrentID(entry.target.id); - break; - } - } - }; - - const observerOptions: IntersectionObserverInit = { - // Negative top margin accounts for `scroll-margin`. - // Negative bottom margin means heading needs to be towards top of viewport to trigger intersection. - rootMargin: '-100px 0% -66%', - threshold: 1, - }; - - const headingsObserver = new IntersectionObserver(setCurrent, observerOptions); - - // Observe all the headings in the main page content. - document.querySelectorAll('article :is(h1,h2,h3)').forEach((h) => headingsObserver.observe(h)); - - // Stop observing when the component is unmounted. - return () => headingsObserver.disconnect(); - }, [toc.current]); - - const onLinkClick = (e) => { - setCurrentID(e.target.getAttribute('href').replace('#', '')); - }; - - return ( - <> - <h2 id={onThisPageID} className="heading"> - On this page - </h2> - <ul ref={toc}> - {headings - .filter(({ depth }) => depth > 1 && depth < 4) - .map((heading) => ( - <li - className={`header-link depth-${heading.depth} ${ - currentID === heading.slug ? 'current-header-link' : '' - }`.trim()} - > - <a href={`#${heading.slug}`} onClick={onLinkClick}> - {unescape(heading.text)} - </a> - </li> - ))} - </ul> - </> - ); -}; - -export default TableOfContents; |