From 0c46a1dab2d74d221490720572e7917cf671bae5 Mon Sep 17 00:00:00 2001 From: Caleb Jasik Date: Tue, 27 Jul 2021 01:31:07 -0500 Subject: Docs/show docsidebar on mobile (#878) --- docs/src/components/DocSidebar/TableOfContents.tsx | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 docs/src/components/DocSidebar/TableOfContents.tsx (limited to 'docs/src/components/DocSidebar/TableOfContents.tsx') diff --git a/docs/src/components/DocSidebar/TableOfContents.tsx b/docs/src/components/DocSidebar/TableOfContents.tsx new file mode 100644 index 000000000..803b05568 --- /dev/null +++ b/docs/src/components/DocSidebar/TableOfContents.tsx @@ -0,0 +1,55 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect, useRef } from 'preact/hooks'; + +const TableOfContents: FunctionalComponent<{ headers: any[] }> = ({ + headers = [], +}) => { + 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 ( + <> +

On this page

+ + + ); +}; + +export default TableOfContents; -- cgit v1.2.3