-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: [BD-46] table of content for non-components (#2013)
- Loading branch information
1 parent
5aa0bf1
commit 6692b5d
Showing
16 changed files
with
231 additions
and
67 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
SEGMENT_KEY='' | ||
FEATURE_ENABLE_AXE='true' | ||
FEATURE_ENABLE_AXE='' |
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,132 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import { Sticky } from '~paragon-react'; | ||
import slugify from 'slugify'; | ||
|
||
interface IItems { | ||
url?: string, | ||
title?: string, | ||
items?: Array<IItems>, | ||
} | ||
|
||
export interface IAutoToc { | ||
className?: string, | ||
tab?: string, | ||
addAnchors?: boolean, | ||
} | ||
|
||
function createAnchor(slug: string): HTMLAnchorElement { | ||
const anchor = document.createElement('a'); | ||
anchor.ariaHidden = 'true'; | ||
anchor.tabIndex = -1; | ||
anchor.href = `#${slug}`; | ||
const span = document.createElement('span'); | ||
span.className = 'pgn-doc__anchor'; | ||
span.innerText = '#'; | ||
anchor.appendChild(span); | ||
return anchor; | ||
} | ||
|
||
function getNestedHeadingsData(headingsArray: NodeListOf<HTMLHeadElement>): IItems { | ||
const result: IItems = { items: [] }; | ||
let parentHeadingLevel = 2; | ||
headingsArray.forEach(heading => { | ||
const headingLevel = parseInt(heading.tagName.slice(-1), 10); | ||
const headingData = { | ||
url: `#${heading.id}`, | ||
title: heading.firstChild!.textContent!, | ||
items: [], | ||
}; | ||
if (!result.items!.length || headingLevel <= parentHeadingLevel) { | ||
parentHeadingLevel = headingLevel; | ||
result.items!.push(headingData); | ||
} else { | ||
const headingDepth = headingLevel - parentHeadingLevel; | ||
let target = result.items![result.items!.length - 1]; | ||
for (let i = 1; i < headingDepth; i++) { | ||
if (target?.items!.length) { | ||
target = target.items[target.items.length - 1]; | ||
} | ||
} | ||
target.items!.push(headingData); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
function AutoToc({ className, tab = '', addAnchors = true }: IAutoToc) { | ||
const [active, setActive] = useState(''); | ||
const [headingsData, setHeadingsData] = useState<IItems>({ items: [] }); | ||
const observer = useRef<IntersectionObserver>(); | ||
|
||
useEffect(() => { | ||
const handleObserver = (entries: IntersectionObserverEntry[]) => { | ||
if (entries[0].intersectionRatio >= 0.5) { | ||
setActive(entries[0].target.id); | ||
} | ||
}; | ||
|
||
observer.current = new IntersectionObserver(handleObserver, { rootMargin: '-50px 0px -80% 0px', threshold: 0.5 }); | ||
const elements = document.querySelectorAll<HTMLHeadElement>('main h2, main h3, main h4, main h5, main h6'); | ||
if (addAnchors) { | ||
elements.forEach(el => { | ||
if (el.textContent) { | ||
el.classList.add('pgn-doc__heading'); | ||
const slug = slugify(el.textContent, { lower: true }); | ||
el.id = slug; | ||
const anchor = createAnchor(slug); | ||
el.appendChild(anchor); | ||
} | ||
}); | ||
} | ||
const headings = getNestedHeadingsData(elements); | ||
setHeadingsData(headings); | ||
elements.forEach((elem) => observer.current?.observe(elem)); | ||
|
||
return () => observer.current?.disconnect(); | ||
}, [tab, addAnchors]); | ||
|
||
const generateTree = (headings: { items?: Array<IItems> }) => (headings?.items?.length | ||
? ( | ||
<ul className="pgn-doc__toc-list"> | ||
{headings.items.map(heading => ( | ||
<li key={heading.url}> | ||
<a | ||
href={heading.url} | ||
className={classNames({ active: `#${active}` === heading.url })} | ||
> | ||
{heading.title} | ||
</a> | ||
{!!heading.items && generateTree(heading)} | ||
</li> | ||
))} | ||
</ul> | ||
) : null); | ||
|
||
const tocTree = generateTree(headingsData); | ||
|
||
return ( | ||
<Sticky | ||
offset={6} | ||
className={classNames('pgn-doc__toc', className)} | ||
> | ||
<p className="pgn-doc__toc-header">Contents</p> | ||
{tocTree} | ||
</Sticky> | ||
); | ||
} | ||
|
||
AutoToc.propTypes = { | ||
className: PropTypes.string, | ||
tab: PropTypes.string, | ||
addAnchors: PropTypes.bool, | ||
}; | ||
|
||
AutoToc.defaultProps = { | ||
className: undefined, | ||
tab: undefined, | ||
addAnchors: undefined, | ||
}; | ||
|
||
export default AutoToc; |
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 |
---|---|---|
|
@@ -54,7 +54,7 @@ | |
flex-direction: column; | ||
align-items: center; | ||
|
||
h3 { | ||
p { | ||
margin-bottom: 0; | ||
padding: 0 .25rem; | ||
} | ||
|
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
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
Oops, something went wrong.