-
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.
Merge pull request #167 from life-itself/152-rhs-toc
[Site/MDX]: Right hand side table of contents.
- Loading branch information
Showing
12 changed files
with
6,607 additions
and
244 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,18 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
|
||
export const Heading = ({ level, observer }) => (props) => { | ||
useEffect(() => { | ||
/* start observing heading's intersection with the bounding box | ||
* set by observer's `rootMargin` */ | ||
if (!observer) { | ||
return | ||
} | ||
observer.observe(document.getElementById(props.id)); | ||
}); | ||
|
||
return React.createElement(`h${level}`, { | ||
...props, | ||
className: "c-heading scroll-mt-16 cursor-pointer" | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from "react"; | ||
import Head from "next/head"; | ||
import dynamic from "next/dynamic"; | ||
import { useMDXComponent } from "next-contentlayer/hooks"; | ||
|
||
import { Heading } from "./Heading"; | ||
import useHeadingsObserver from "../hooks/useHeadingsObserver"; | ||
|
||
const Anchor = dynamic( | ||
() => import("./Anchor").then((module) => module.Anchor) | ||
// { | ||
// ssr: false, | ||
// } | ||
); | ||
|
||
const Paragraph = dynamic(() => | ||
import("./Paragraph").then((module) => module.Paragraph) | ||
); | ||
|
||
const MdxContent = ({ body }) => { | ||
const observer = useHeadingsObserver(); | ||
|
||
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} />; | ||
}; | ||
|
||
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
Oops, something went wrong.