-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-dev): add table of content for documents
- Loading branch information
Showing
4 changed files
with
175 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Link from 'next/link'; | ||
import { cx } from 'nx-dev/ui-primitives'; | ||
import { useHeadingsObserver } from './use-headings-observer'; | ||
|
||
interface Heading { | ||
id: string; | ||
level: number; | ||
title: string; | ||
} | ||
|
||
export function collectHeadings( | ||
node: any, | ||
sections: Heading[] = [] | ||
): Heading[] { | ||
if (node) { | ||
if (node.name && node.name === 'Heading') { | ||
const title = node.children[0]; | ||
|
||
if (typeof title === 'string') { | ||
sections.push({ | ||
id: node.attributes['id'], | ||
level: node.attributes['level'], | ||
title, | ||
}); | ||
} | ||
} | ||
|
||
if (node.children) { | ||
for (const child of node.children) { | ||
collectHeadings(child, sections); | ||
} | ||
} | ||
} | ||
|
||
return sections; | ||
} | ||
|
||
export function TableOfContents({ | ||
elementRef, | ||
headings, | ||
path, | ||
}: { | ||
elementRef: any; | ||
headings: Heading[]; | ||
path: string; | ||
}): JSX.Element { | ||
const headingLevelTargets: number[] = [1, 2, 3]; // matching to: H1, H2, H3... | ||
const items = headings.filter( | ||
(item) => item.id && headingLevelTargets.includes(item.level) | ||
); | ||
|
||
const activeId = useHeadingsObserver( | ||
elementRef, | ||
{ | ||
threshold: [0, 0.25, 0.5, 0.75, 1], | ||
root: null, | ||
rootMargin: '-10% 0% -45% 0%', | ||
}, | ||
headings.find((i) => i.level === 1)?.title || null | ||
); | ||
|
||
return ( | ||
<nav className="toc"> | ||
<span className="pl-4 font-medium">Quick reference</span> | ||
{!!items.length ? ( | ||
<ul className="mt-4 flex-col"> | ||
{items.map((item) => { | ||
const href = `${path}#${item.id}`; | ||
return ( | ||
<li key={item.title}> | ||
<Link | ||
href={href} | ||
className={cx( | ||
'block w-full border-l-4 border-slate-100 py-1 pl-3 transition hover:border-slate-500 dark:border-slate-700/40 dark:hover:border-slate-700', | ||
{ | ||
'border-slate-500 bg-slate-50 dark:border-slate-700 dark:bg-slate-800/60': | ||
activeId === item.id, | ||
'pl-6': item.level === 3, | ||
} | ||
)} | ||
> | ||
{item.title} | ||
</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
) : null} | ||
</nav> | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
nx-dev/feature-doc-viewer/src/lib/use-headings-observer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { RefObject, useEffect, useState } from 'react'; | ||
|
||
export function useHeadingsObserver( | ||
elementRef: RefObject<Element>, | ||
{ threshold = 0, root = null, rootMargin = '0%' }: IntersectionObserverInit, | ||
cachekey: string | null = null | ||
): string { | ||
const [activeId, setActiveId] = useState<string>(''); | ||
|
||
useEffect(() => { | ||
const handleObserver = (entries: IntersectionObserverEntry[]): void => { | ||
for (const entry of entries) { | ||
if (entry?.isIntersecting) setActiveId(entry.target.id); | ||
} | ||
}; | ||
|
||
const node = elementRef?.current; // DOM Ref | ||
const hasIOSupport = !!window.IntersectionObserver; | ||
|
||
if (!hasIOSupport || !node) return; | ||
const observer = new IntersectionObserver(handleObserver, { | ||
threshold, | ||
root, | ||
rootMargin, | ||
}); | ||
|
||
const elements: NodeListOf<Element> = node.querySelectorAll('h1, h2, h3'); | ||
elements.forEach((e: Element) => { | ||
observer.observe(e); | ||
}); | ||
|
||
return () => observer.disconnect(); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ | ||
elementRef?.current, | ||
JSON.stringify(threshold), | ||
root, | ||
rootMargin, | ||
cachekey, | ||
]); | ||
|
||
return activeId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters