-
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.
feat: table of content for non-components
- Loading branch information
monteri
committed
Feb 13, 2023
1 parent
d1f96b4
commit c29bc55
Showing
15 changed files
with
253 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
SEGMENT_KEY='' | ||
FEATURE_ENABLE_AXE='true' | ||
FEATURE_ENABLE_AXE='' |
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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Container, Row, Col, | ||
} from '~paragon-react'; | ||
import AutoToc from './AutoToc'; | ||
|
||
export interface IContentWrapper { | ||
children: React.ReactNode, | ||
tab?: string, | ||
addAnchors?: boolean, | ||
} | ||
|
||
function ContentWrapper({ | ||
children, tab, addAnchors, | ||
}: IContentWrapper) { | ||
return ( | ||
<Container fluid size={undefined}> | ||
<Row className="flex-xl-nowrap"> | ||
<Col className="d-none d-xl-block" xl={2} /> | ||
<Col | ||
xl={8} | ||
lg={9} | ||
md={12} | ||
as="main" | ||
className="flex-grow-1" | ||
> | ||
{children} | ||
</Col> | ||
<Col | ||
xl={2} | ||
lg={3} | ||
as={AutoToc} | ||
tab={tab} | ||
addAnchors={addAnchors} | ||
className="d-none d-lg-block" | ||
/> | ||
</Row> | ||
</Container> | ||
); | ||
} | ||
|
||
ContentWrapper.propTypes = { | ||
children: PropTypes.node, | ||
addAnchors: PropTypes.bool, | ||
tab: PropTypes.string, | ||
}; | ||
|
||
ContentWrapper.defaultProps = { | ||
children: undefined, | ||
addAnchors: undefined, | ||
tab: undefined, | ||
}; | ||
|
||
export default ContentWrapper; |
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 |
---|---|---|
|
@@ -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
Oops, something went wrong.