1
0
Fork 0
freesewing/sites/shared/components/wrappers/toc.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
* This is used to wrap a Table of Contents (toc) as returned
* from the mdxLoader method (see shared/mdx/loader.js)
* It is NOT for wrapping plain markdown/mdx
*/
import { useState, useEffect, Fragment } from 'react'
// See: https://mdxjs.com/guides/mdx-on-demand/
import { run } from '@mdx-js/mdx'
import * as runtime from 'react/jsx-runtime'
2022-12-26 17:44:00 +01:00
const TocWrapper = ({ toc }) => {
const [mdxModule, setMdxModule] = useState()
useEffect(() => {
;(async () => {
setMdxModule(await run(toc, runtime))
})()
}, [toc])
// React component for MDX content
const MdxContent = mdxModule ? mdxModule.default : Fragment
2022-10-04 01:20:49 +02:00
// Don't render an empty toc
const children = typeof MdxContent === 'function' ? MdxContent().props.children : false
2022-10-04 01:20:49 +02:00
return children ? (
<div
className={`
2022-12-26 17:10:14 +01:00
mdx mdx-toc text-base-content text-base
2022-10-04 18:49:13 +02:00
sticky top-16 max-h-screen overflow-y-auto
2022-10-04 01:20:49 +02:00
border-2 bg-base-200 bg-opacity-30 p-4 rounded-lg border-base-200
`}
>
2022-10-04 01:20:49 +02:00
{mdxModule && <MdxContent components={{}} />}
</div>
2022-10-04 01:20:49 +02:00
) : null
}
export default TocWrapper