import React, { useState, useEffect, useLayoutEffect, useRef } from 'react'
import Head from 'next/head'
import NextLink from 'next/link'
import { useRouter } from 'next/router'
import { useFela } from 'react-fela'
import { Box, Spacer, Grid } from 'kilvin'
import { MDXRemote } from 'next-mdx-remote'
import CodeBlock from './CodeBlock'
import Cross from '../icons/Cross'
import Edit from '../icons/Edit'
import Hamburger from '../icons/Hamburger'
import Heading from './Heading'
import Icon from './Icon'
import Layout from './Layout'
import Link from './Link'
import VisuallyHidden from './VisuallyHidden'
function Line({ thickness = 1 }) {
return (
)
}
const NAV_WIDTH = 240
function getFlatNav(nav, flat = {}, prefix = '') {
Object.keys(nav).forEach((title) => {
const subNav = nav[title]
if (typeof subNav === 'object') {
getFlatNav(subNav, flat, prefix + title + ' / ')
} else {
flat[subNav] = prefix + title
}
})
return flat
}
function Headings({ headings }) {
const { theme } = useFela()
if (headings.length < 2) {
return null
}
return (
Table of Contents
{headings.map(([heading, id, level]) => (
false}
extend={{
textDecoration: 'none',
color: theme.colors.foreground,
fontSize: level === 2 ? 14 : 12,
}}>
{heading}
))}
)
}
export function Content({ docsPath, children }) {
const router = useRouter()
const { theme } = useFela()
return (
{
const isExtern = href.indexOf('http') !== -1
const isAnchor = href.indexOf('#') === 0
const resolvedHref = isExtern
? href
: isAnchor
? router.pathname + href
: docsPath + href
return (
{children}
)
},
pre: ({ children }) => children,
h1: ({ children }) => {children},
h2: ({ children }) => {children},
h3: ({ children }) => {children},
h4: ({ children }) => {children},
h5: ({ children }) => {children},
strong: ({ children }) => (
{children}
),
ul: ({ children }) => (
{children}
),
ol: ({ children }) => (
{children}
),
blockquote: ({ children }) => (
{children}
),
code: CodeBlock,
inlineCode: ({ children }) => (
{children}
),
p: ({ children }) => (
{children}
),
img: ({ src, title, alt }) => (
),
tr: ({ children }) => (
{children}
),
td: ({ children }) => (
{children}
),
th: ({ children }) => (
{children}
),
table: ({ children }) => (
{children}
),
hr: () => ,
}}
/>
)
}
function NavigationItem({ path, group, page, docsPath }) {
const router = useRouter()
const isExtern = path.indexOf('http') === 0
const isCurrentPage = router.asPath === docsPath + path
return (
{page}
)
}
export default function DocLayout({ children, toc, version, headings }) {
const [navigationVisible, setNavigationVisible] = useState(false)
const { theme } = useFela()
const router = useRouter()
const flatNav = getFlatNav(toc)
const docsPath = `/docs/${version}/`
const currentPage =
flatNav[router.asPath.substr(docsPath.length).split('#')[0]]
useEffect(() => {
if (navigationVisible) {
const handleResize = () => {
if (window.innerWidth >= 800) {
setNavigationVisible(false)
}
}
window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
}
}, [navigationVisible])
useEffect(() => {
const handleRouteChange = (url) => {
setNavigationVisible(false)
}
router.events.on('routeChangeComplete', handleRouteChange)
// If the component is unmounted, unsubscribe
// from the event with the `off` method:
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])
return (
<>
{currentPage} — Fela
Main navigation
setNavigationVisible(false)}
extend={{
cursor: 'pointer',
background: 'transparent',
padding: 0,
border: 0,
right: 10,
top: 10,
position: 'fixed',
font: 'inherit',
}}>
{Object.keys(toc).map((group) => (
{group}
{Object.keys(toc[group]).map((page) => {
const path = toc[group][page]
if (typeof path === 'object') {
return (
{page}
{Object.keys(path).map((page) => (
))}
)
}
return (
)
})}
))}
setNavigationVisible(true)}
extend={{
cursor: 'pointer',
background: 'transparent',
padding: 0,
border: 0,
font: 'inherit',
}}>
{currentPage}
Edit
{' '}
on GitHub
{children}
>
)
}