-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[site/ToC][m]: separate ToC component created
- Loading branch information
Showing
6 changed files
with
126 additions
and
153 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,24 +1,30 @@ | ||
import { Tooltip } from './Tooltip'; | ||
import dynamic from "next/dynamic"; | ||
|
||
const Tooltip = dynamic(() => { | ||
return import("./Tooltip").then((module) => module.Tooltip); | ||
}); | ||
|
||
/** | ||
* Component for adding previews on hovering over anchor tags with relative paths | ||
*/ | ||
export const Anchor = (props) => { | ||
/* Check if the path is relative */ | ||
const pathIsRelative = (path) => { | ||
return path && | ||
return ( | ||
path && | ||
path.indexOf("http:") !== 0 && | ||
path.indexOf("https:") !== 0 && | ||
path.indexOf("#") !== 0 | ||
} | ||
); | ||
}; | ||
|
||
if (pathIsRelative(props.href)) { | ||
return ( | ||
<Tooltip {...props} render={ tooltipTriggerProps => ( | ||
<a {...tooltipTriggerProps} /> | ||
)} | ||
<Tooltip | ||
{...props} | ||
render={(tooltipTriggerProps) => <a {...tooltipTriggerProps} />} | ||
/> | ||
) | ||
); | ||
} | ||
return <a {...props} />; | ||
}; |
This file was deleted.
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 |
---|---|---|
@@ -1,28 +1,18 @@ | ||
import React from 'react' | ||
import Head from 'next/head' | ||
import { useMDXComponent } from 'next-contentlayer/hooks'; | ||
import { Paragraph } from './Paragraph' | ||
import { Anchor } from './Anchor' | ||
import { Heading } from './Heading' | ||
import Head from "next/head"; | ||
import { useMDXComponent } from "next-contentlayer/hooks"; | ||
|
||
import { Anchor } from "./Anchor"; | ||
import { Paragraph } from "./Paragraph"; | ||
|
||
const MdxContent = ({ body, observer }) => { | ||
const MdxContent = ({ body }) => { | ||
const customComponents = { | ||
Head, | ||
p: Paragraph, | ||
a: Anchor, | ||
h1: Heading({ level: 1, observer }), | ||
h2: Heading({ level: 2, observer }), | ||
h3: Heading({ level: 3, observer }), | ||
h4: Heading({ level: 4, observer }), | ||
h5: Heading({ level: 5, observer }), | ||
h6: Heading({ level: 6, observer }), | ||
} | ||
}; | ||
const Component = useMDXComponent(body.code); | ||
|
||
return <Component components={ customComponents }/> | ||
return <Component components={customComponents} />; | ||
}; | ||
|
||
// prevent rerendering of the component if it's props don't change | ||
// i.e. re-render only when the observer is set | ||
export default React.memo(MdxContent); | ||
export default MdxContent; |
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,64 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
const TableOfContents = () => { | ||
const [activeHeading, setActiveHeading] = useState(""); | ||
|
||
/* Runs only after the first render, in order to preserve the observer | ||
* between component rerenderings (e.g. after activeHeading change). */ | ||
useEffect(() => { | ||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
setActiveHeading(entry.target.id); | ||
} | ||
}); | ||
}, | ||
{ | ||
root: null, | ||
threshold: 0.55, | ||
rootMargin: "-65px 0% -85% 0%", // 65px is a navbar height | ||
} | ||
); | ||
|
||
const tocLinks = document.querySelectorAll(".toc-link"); | ||
tocLinks.forEach((link) => { | ||
const headingId = link.href.split("#").pop(); | ||
const headingElement = document.getElementById(headingId); | ||
// add custom classes | ||
headingElement.classList.add( | ||
"c-heading", | ||
"scroll-mt-16", | ||
"cursor-pointer" | ||
); | ||
// observe | ||
observer.observe(headingElement); | ||
}); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, []); | ||
|
||
/* On initial render activeHeading will be `null`, since the observer | ||
* has not been instantiated yet. However, we still want to highlight | ||
* the current heading in the ToC based on the current url. */ | ||
useEffect(() => { | ||
if (!activeHeading) { | ||
return; | ||
} | ||
|
||
const tocLink = document.querySelector( | ||
`.toc-link[href="#${activeHeading}"]` | ||
); | ||
tocLink.classList.add("active"); | ||
|
||
return () => { | ||
tocLink.classList.remove("active"); | ||
}; | ||
}, [activeHeading]); | ||
|
||
return null; | ||
}; | ||
|
||
export default TableOfContents; |
This file was deleted.
Oops, something went wrong.