|
| 1 | +import React, { useState, useEffect, useContext, createContext } from 'react' |
| 2 | +import { useRouter } from 'next/router' |
| 3 | +import Head from 'next/head' |
| 4 | +import Link from 'next/link' |
| 5 | +import slugify from '@sindresorhus/slugify' |
| 6 | + |
| 7 | +import getDirectories from './directories' |
| 8 | +import Theme from './theme' |
| 9 | + |
| 10 | +import GitHubIcon from '../components/github-icon' |
| 11 | +import config from '../nextra.config' |
| 12 | + |
| 13 | +const directories = getDirectories() |
| 14 | +const TreeState = new Map() |
| 15 | +const titleType = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] |
| 16 | +const MenuContext = createContext(false) |
| 17 | + |
| 18 | +function Folder ({ item, anchors }) { |
| 19 | + const route = useRouter().route + '/' |
| 20 | + const active = route.startsWith(item.route + '/') |
| 21 | + const open = TreeState[item.route] || active |
| 22 | + const [_, render] = useState(false) |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (active) { |
| 26 | + TreeState[item.route] = true |
| 27 | + } |
| 28 | + }, [active]) |
| 29 | + |
| 30 | + return <li className={open ? 'active' : ''}> |
| 31 | + <button onClick={() => { |
| 32 | + if (active) return |
| 33 | + TreeState[item.route] = !open |
| 34 | + render(x => !x) |
| 35 | + }}>{item.title}</button> |
| 36 | + <div style={{ |
| 37 | + display: open ? '' : 'none' |
| 38 | + }}><Menu dir={item.children} base={item.route} anchors={anchors} /></div> |
| 39 | + </li> |
| 40 | +} |
| 41 | + |
| 42 | +function File ({ item, anchors }) { |
| 43 | + const { setMenu } = useContext(MenuContext) |
| 44 | + const route = useRouter().route + '/' |
| 45 | + const active = route.startsWith(item.route + '/') |
| 46 | + |
| 47 | + let title = item.title |
| 48 | + // if (item.title.startsWith('> ')) { |
| 49 | + // title = title.substr(2) |
| 50 | + if (anchors?.length) { |
| 51 | + if (active) { |
| 52 | + return <li className={active ? 'active' : ''}> |
| 53 | + <Link href={item.route}><a>{title}</a></Link> |
| 54 | + <ul>{anchors.map(anchor => { |
| 55 | + const slug = slugify(anchor || '') |
| 56 | + return <a href={'#' + slug} key={`a-${slug}`} onClick={() => setMenu(false)}> |
| 57 | + <span className="flex"><span className="mr-2 opacity-25">#</span><span className="inline-block">{anchor}</span></span> |
| 58 | + </a> |
| 59 | + })}</ul> |
| 60 | + </li> |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return <li className={active ? 'active' : ''}> |
| 65 | + <Link href={item.route}><a onClick={() => setMenu(false)}>{title}</a></Link> |
| 66 | + </li> |
| 67 | +} |
| 68 | + |
| 69 | +function Menu ({ dir, anchors }) { |
| 70 | + return <ul>{dir.map(item => { |
| 71 | + if (item.children) { |
| 72 | + return <Folder key={item.name} item={item} anchors={anchors} /> |
| 73 | + } |
| 74 | + return <File key={item.name} item={item} anchors={anchors} /> |
| 75 | + })}</ul> |
| 76 | +} |
| 77 | + |
| 78 | +function Sidebar ({ show, anchors }) { |
| 79 | + return <aside className={`h-screen bg-white flex-shrink-0 w-full md:w-64 md:border-r md:block fixed md:sticky z-10 ${show ? '' : 'hidden'}`} style={{ |
| 80 | + top: '4rem', |
| 81 | + height: 'calc(100vh - 4rem)' |
| 82 | + }}> |
| 83 | + <div className="sidebar w-full p-4 pb-40 md:pb-16 h-full overflow-y-auto"> |
| 84 | + <Menu dir={directories} anchors={anchors} /> |
| 85 | + </div> |
| 86 | + </aside> |
| 87 | +} |
| 88 | + |
| 89 | +export default ({ children }) => { |
| 90 | + const [menu, setMenu] = useState(false) |
| 91 | + |
| 92 | + const titles = React.Children.toArray(children).filter(child => titleType.includes(child.props.mdxType)) |
| 93 | + const title = titles.find(child => child.props.mdxType === 'h1')?.props.children || 'Untitled' |
| 94 | + const anchors = titles.filter(child => child.props.mdxType === 'h2').map(child => child.props.children) |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + if (menu) { |
| 98 | + document.body.classList.add('overflow-hidden') |
| 99 | + } else { |
| 100 | + document.body.classList.remove('overflow-hidden') |
| 101 | + } |
| 102 | + }, [menu]) |
| 103 | + |
| 104 | + return <> |
| 105 | + <Head> |
| 106 | + <title>{title}{config.titleSuffix || ''}</title> |
| 107 | + {config.head} |
| 108 | + </Head> |
| 109 | + <div className="main-container flex flex-col"> |
| 110 | + <nav className="flex items-center bg-white z-20 fixed top-0 left-0 right-0 h-16 border-b px-6"> |
| 111 | + <div className="w-full flex items-center"> |
| 112 | + <Link href="/"><a className="no-underline text-current inline-flex items-center hover:opacity-75"> |
| 113 | + {config.logo} |
| 114 | + </a></Link> |
| 115 | + </div> |
| 116 | + {config.github ? <a className="text-current p-2 -mr-2" href={config.github} target="_blank"><GitHubIcon height={28}/></a> : null} |
| 117 | + <button className="block md:hidden p-2 -mr-2 ml-2" onClick={() => setMenu(!menu)}> |
| 118 | + <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg> |
| 119 | + </button> |
| 120 | + </nav> |
| 121 | + <main className="flex flex-1 h-full"> |
| 122 | + <MenuContext.Provider value={{ setMenu }}> |
| 123 | + <Sidebar show={menu} anchors={anchors}/> |
| 124 | + </MenuContext.Provider> |
| 125 | + <content className="relative pt-20 pb-16 px-6 md:px-8 w-full max-w-full overflow-x-hidden"> |
| 126 | + <div className="max-w-screen-md"> |
| 127 | + <Theme>{children}</Theme> |
| 128 | + </div> |
| 129 | + </content> |
| 130 | + </main> |
| 131 | + </div> |
| 132 | + </> |
| 133 | +} |
0 commit comments