Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

LayoutDocs.js

211 lines6.0 KB

1import * as React from 'react'
2import { MDXProvider } from '@mdx-js/react'
3import { Nav } from 'components/Nav'
4import { Sidebar } from 'components/Sidebar'
5import { SidebarCategory } from 'components/SidebarCategory'
6import { SidebarHeading } from 'components/SidebarHeading'
7import { SidebarMobile } from 'components/SidebarMobile'
8import { SidebarPost } from 'components/SidebarPost'
9import { Sticky } from 'components/Sticky'
10import { useIsMobile } from 'components/useIsMobile'
11import { findRouteByPath } from 'lib/docs/findRouteByPath'
12import { removeFromLast } from 'lib/docs/utils'
13import { getRouteContext } from 'lib/get-route-context'
14import { useRouter } from 'next/router'
15import { Toc } from './Toc'
16import s from './markdown.module.css'
17import { Footer } from './Footer'
18import { DocsPageFooter } from './DocsPageFooter'
19import { Seo } from './Seo'
20import MDXComponents from './MDXComponents'
21import Head from 'next/head'
22import { getManifest } from 'manifests/getManifest'
23import BytesForm from './BytesForm'
24​
25const getSlugAndTag = (path) => {
26 const parts = path.split('/')
27​
28 if (parts[2] === '1.5.8' || parts[2] === '2.1.4') {
29 return {
30 tag: parts[2],
31 slug: `/docs/${parts.slice(2).join('/')}`,
32 }
33 }
34​
35 return {
36 slug: path,
37 }
39​
40const addTagToSlug = (slug, tag) => {
41 return tag ? `/docs/${tag}/${slug.replace('/docs/', '')}` : slug
43​
44export const LayoutDocs = (props) => {
45 const router = useRouter()
46 const { slug, tag } = getSlugAndTag(router.asPath)
47 const { routes } = getManifest(tag)
48​
49 const _route = findRouteByPath(removeFromLast(slug, '#'), routes) // @ts-ignore
50​
51 const isMobile = useIsMobile()
52 const { route, prevRoute, nextRoute } = getRouteContext(_route, routes)
53 const title = route && `${route.title}`
54​
55 return (
56 <>
57 {tag && (
58 <Head>
59 <meta name="robots" content="noindex" />
60 </Head>
61 )}
62 <div>
63 {isMobile ? (
64 <>
65 <Nav />
66 <Sticky shadow>
67 <SidebarMobile>
68 <SidebarRoutes isMobile={true} routes={routes} />
69 </SidebarMobile>
70 </Sticky>
71 </>
72 ) : (
73 <Sticky>
74 <Nav />
75 </Sticky>
76 )}
77 <Seo
78 title={title || props.meta.title}
79 description={props.meta.description ?? props.meta.body}
80 />
81 <div className="block">
82 <>
83 <div className="container mx-auto pb-12 pt-6 content">
84 <div className="flex relative">
85 {!isMobile && (
86 <Sidebar fixed>
87 <SidebarRoutes routes={routes} />
88 </Sidebar>
89 )}
90​
91 <div className={s['markdown'] + ' w-full docs'}>
92 <h1>{props.meta.title}</h1>
93 <MDXProvider components={MDXComponents}>
94 {props.children}
95 </MDXProvider>
96 <DocsPageFooter
97 href={route?.path || ''}
98 route={route}
99 prevRoute={prevRoute}
100 nextRoute={nextRoute}
101 />
102 </div>
103 {props.meta.toc === false ? null : (
104 <div
105 className="hidden xl:block ml-10 flex-shrink-0"
106 style={{
107 width: 200,
108 }}
109 >
110 <div className="sticky top-24 ">
111 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
112 On this page
113 </h4>
114 <Toc />
115​
116 <div className="mt-12 relative">
117 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
118 Subscribe to Bytes
119 </h4>
120 <p className="mt-4 text-sm leading-6 mb-4">
121 The best JavaScript newsletter! Delivered every
122 Monday to over 76,000 devs.
123 </p>
124 <BytesForm />
125 </div>
126​
127 </div>
128 </div>
129 )}
130 </div>
131 </div>
132 </>
133 </div>
134 </div>
135 <Footer />
136 <style jsx>{`
137 .docs {
138 min-width: calc(100% - 300px - 1rem - 200px);
139 word-wrap: break-word;
140 }
141 `}</style>
142 </>
143 )
145​
146function getCategoryPath(routes) {
147 const route = routes.find((r) => r.path)
148 return route && removeFromLast(route.path, '/')
150​
151function SidebarRoutes({ isMobile, routes: currentRoutes, level = 1 }) {
152 const { asPath } = useRouter()
153 let { slug, tag } = getSlugAndTag(asPath)
154 return currentRoutes.map(({ path, title, routes, heading, open }) => {
155 if (routes) {
156 const pathname = getCategoryPath(routes)
157 const selected = slug.startsWith(pathname)
158 const opened = selected || isMobile ? false : open
159​
160 if (heading) {
161 return (
162 <SidebarHeading key={'parent' + pathname} title={title}>
163 <SidebarRoutes
164 isMobile={isMobile}
165 routes={routes}
166 level={level + 1}
167 />
168 </SidebarHeading>
169 )
170 }
171​
172 return (
173 <SidebarCategory
174 key={pathname}
175 isMobile={isMobile}
176 level={level}
177 title={title}
178 selected={selected}
179 opened={opened}
180 >
181 <SidebarRoutes
182 isMobile={isMobile}
183 routes={routes}
184 level={level + 1}
185 />
186 </SidebarCategory>
187 )
188 }
189​
190 const pagePath = removeFromLast(path, '.')
191 const pathname = addTagToSlug(pagePath, tag)
192 const selected = slug === pagePath
193 const route = {
194 href: pagePath,
195 path,
196 title,
197 pathname,
198 selected,
199 }
200 return (
201 <SidebarPost
202 key={title}
203 isMobile={isMobile}
204 level={level}
205 route={route}
206 />
207 )
208 })
210​
211LayoutDocs.displayName = 'LayoutDocs'