/
sidebar.tsx
433 lines (407 loc) · 12.5 KB
/
sidebar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import React, {
useState,
useEffect,
useMemo,
ReactElement,
memo,
useRef
} from 'react'
import cn from 'clsx'
import Slugger from 'github-slugger'
import { useRouter } from 'next/router'
import { Heading } from 'nextra'
import scrollIntoView from 'scroll-into-view-if-needed'
import { useConfig, useMenu, useActiveAnchor } from '../contexts'
import {
Item,
MenuItem,
PageItem,
getFSRoute,
getHeadingText,
renderComponent
} from '../utils'
import { LocaleSwitch } from './locale-switch'
import { ThemeSwitch } from './theme-switch'
import { ArrowRightIcon } from 'nextra/icons'
import { Collapse } from './collapse'
import { Anchor } from './anchor'
import { DEFAULT_LOCALE } from '../constants'
const TreeState: Record<string, boolean> = Object.create(null)
const Folder = memo(FolderImpl)
const classes = {
link: cn(
'nx-flex nx-rounded nx-px-2 nx-py-1.5 nx-text-sm nx-transition-colors [word-break:break-word]',
'nx-cursor-pointer [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] contrast-more:nx-border'
),
inactive: cn(
'nx-text-gray-500 hover:nx-bg-gray-100 hover:nx-text-gray-900',
'dark:nx-text-neutral-500 dark:hover:nx-bg-primary-100/5 dark:hover:nx-text-gray-50',
'contrast-more:nx-text-gray-900 contrast-more:dark:nx-text-gray-50',
'contrast-more:nx-border-transparent contrast-more:hover:nx-border-gray-900 contrast-more:dark:hover:nx-border-gray-50'
),
active: cn(
'nx-bg-primary-50 nx-font-bold nx-text-primary-500 dark:nx-bg-primary-500/10',
'contrast-more:nx-border-primary-500 contrast-more:dark:nx-border-primary-500'
),
list: cn('nx-flex nx-flex-col nx-gap-1'),
border: cn(
'nx-relative before:nx-absolute before:nx-inset-y-1.5',
'before:nx-w-px before:nx-bg-gray-200 before:nx-content-[""] dark:before:nx-bg-neutral-800',
'ltr:nx-pl-3 ltr:before:nx-left-0 rtl:nx-pr-3 rtl:before:nx-right-0'
)
}
function FolderImpl({
item,
anchors
}: {
item: PageItem | MenuItem | Item
anchors: string[]
}): ReactElement {
const { asPath, locale = DEFAULT_LOCALE } = useRouter()
const routeOriginal = getFSRoute(asPath, locale)
const [route] = routeOriginal.split('#')
const active = [route, route + '/'].includes(item.route + '/')
const activeRouteInside = active || route.startsWith(item.route + '/')
const folderLevel = (item.route.match(/\//g) || []).length
const { setMenu } = useMenu()
const config = useConfig()
const { theme } = item as Item
const open =
TreeState[item.route] !== undefined
? TreeState[item.route]
: active ||
activeRouteInside ||
(theme && 'collapsed' in theme
? !theme.collapsed
: folderLevel <= config.sidebar.defaultMenuCollapseLevel)
const rerender = useState({})[1]
useEffect(() => {
if (activeRouteInside) {
TreeState[item.route] = true
}
}, [activeRouteInside])
if (item.type === 'menu') {
const menu = item as MenuItem
const routes = Object.fromEntries(
(menu.children || []).map(route => [route.name, route])
)
item.children = Object.entries(menu.items || {}).map(([key, item]) => {
const route = routes[key] || {
name: key,
locale: menu.locale,
route: menu.route + '/' + key
}
return {
...route,
...item
}
})
}
return (
<li className={cn({ open, active })}>
<Anchor
href={(item as Item).withIndexPage ? item.route : ''}
className={cn(
'nx-items-center nx-justify-between nx-gap-2',
classes.link,
active ? classes.active : classes.inactive
)}
onClick={e => {
const clickedToggleIcon = ['svg', 'path'].includes(
(e.target as HTMLElement).tagName.toLowerCase()
)
if (clickedToggleIcon) {
e.preventDefault()
}
if ((item as Item).withIndexPage) {
// If it's focused, we toggle it. Otherwise, always open it.
if (active || clickedToggleIcon) {
TreeState[item.route] = !open
} else {
TreeState[item.route] = true
setMenu(false)
}
rerender({})
return
}
if (active) return
TreeState[item.route] = !open
rerender({})
}}
>
{renderComponent(config.sidebar.titleComponent, {
title: item.title,
type: item.type
})}
<ArrowRightIcon
className="nx-h-[18px] nx-min-w-[18px] nx-rounded-sm nx-p-0.5 hover:nx-bg-gray-800/5 dark:hover:nx-bg-gray-100/5"
pathClassName={cn(
'nx-origin-center nx-transition-transform rtl:-nx-rotate-180',
open && 'ltr:nx-rotate-90 rtl:nx-rotate-[-270deg]'
)}
/>
</Anchor>
<Collapse className="ltr:nx-pr-0 rtl:nx-pl-0" open={open}>
{Array.isArray(item.children) ? (
<Menu
className={cn(classes.border, 'ltr:nx-ml-1 rtl:nx-mr-1')}
directories={item.children}
base={item.route}
anchors={anchors}
/>
) : null}
</Collapse>
</li>
)
}
function Separator({ title }: { title: string }): ReactElement {
const config = useConfig()
return (
<li
className={cn(
'[word-break:break-word]',
title
? 'nx-mt-5 nx-mb-2 nx-px-2 nx-py-1.5 nx-text-sm nx-font-semibold nx-text-gray-900 first:nx-mt-0 dark:nx-text-gray-100'
: 'nx-my-4'
)}
>
{title ? (
renderComponent(config.sidebar.titleComponent, {
title,
type: 'separator'
})
) : (
<hr className="nx-mx-2 nx-border-t nx-border-gray-200 dark:nx-border-primary-100/10" />
)}
</li>
)
}
function File({
item,
anchors
}: {
item: PageItem | Item
anchors: string[]
}): ReactElement {
const { asPath, locale = DEFAULT_LOCALE } = useRouter()
const route = getFSRoute(asPath, locale)
// It is possible that the item doesn't have any route - for example an extermal link.
const active = item.route && [route, route + '/'].includes(item.route + '/')
const slugger = new Slugger()
const activeAnchor = useActiveAnchor()
const { setMenu } = useMenu()
const config = useConfig()
if (item.type === 'separator') {
return <Separator title={item.title} />
}
return (
<li className={cn(classes.list, { active })}>
<Anchor
href={(item as PageItem).href || item.route}
newWindow={(item as PageItem).newWindow}
className={cn(classes.link, active ? classes.active : classes.inactive)}
onClick={() => {
setMenu(false)
}}
>
{renderComponent(config.sidebar.titleComponent, {
title: item.title,
type: item.type
})}
</Anchor>
{active && anchors.length > 0 && (
<ul
className={cn(
classes.list,
classes.border,
'ltr:nx-ml-3 rtl:nx-mr-3'
)}
>
{anchors.map(text => {
const slug = slugger.slug(text)
return (
<li key={slug}>
<a
href={`#${slug}`}
className={cn(
classes.link,
'nx-flex nx-gap-2 before:nx-opacity-25 before:nx-content-["#"]',
activeAnchor[slug]?.isActive
? classes.active
: classes.inactive
)}
onClick={() => {
setMenu(false)
}}
>
{text}
</a>
</li>
)
})}
</ul>
)}
</li>
)
}
interface MenuProps {
directories: PageItem[] | Item[]
anchors: string[]
base?: string
className?: string
onlyCurrentDocs?: boolean
}
function Menu({
directories,
anchors,
className,
onlyCurrentDocs
}: MenuProps): ReactElement {
return (
<ul className={cn(classes.list, className)}>
{directories.map(item =>
!onlyCurrentDocs || item.isUnderCurrentDocsTree ? (
item.type === 'menu' ||
(item.children && (item.children.length || !item.withIndexPage)) ? (
<Folder key={item.name} item={item} anchors={anchors} />
) : (
<File key={item.name} item={item} anchors={anchors} />
)
) : null
)}
</ul>
)
}
interface SideBarProps {
docsDirectories: PageItem[]
flatDirectories: Item[]
fullDirectories: Item[]
asPopover?: boolean
headings?: Heading[]
includePlaceholder: boolean
}
const emptyHeading: any[] = []
export function Sidebar({
docsDirectories,
flatDirectories,
fullDirectories,
asPopover = false,
headings = emptyHeading,
includePlaceholder
}: SideBarProps): ReactElement {
const config = useConfig()
const { menu, setMenu } = useMenu()
const anchors = useMemo(
() =>
headings
.filter(v => v.children && v.depth === 2 && v.type === 'heading')
.map(getHeadingText)
.filter(Boolean),
[headings]
)
const sidebarRef = useRef<HTMLDivElement>(null)
const containerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (menu) {
document.body.classList.add('nx-overflow-hidden', 'md:nx-overflow-auto')
} else {
document.body.classList.remove(
'nx-overflow-hidden',
'md:nx-overflow-auto'
)
}
}, [menu])
useEffect(() => {
const activeElement = sidebarRef.current?.querySelector('li.active')
if (activeElement && (window.innerWidth > 767 || menu)) {
const scroll = () => {
scrollIntoView(activeElement, {
block: 'center',
inline: 'center',
scrollMode: 'always',
boundary: containerRef.current
})
}
if (menu) {
// needs for mobile since menu has transition transform
setTimeout(scroll, 300)
} else {
scroll()
}
}
}, [menu])
const hasMenu = config.i18n.length > 0 || config.darkMode
return (
<>
{includePlaceholder && asPopover ? (
<div className="nx-hidden nx-h-0 nx-w-64 nx-shrink-0 xl:nx-block" />
) : null}
<div
className={cn(
'[transition:background-color_1.5s_ease] motion-reduce:nx-transition-none',
menu
? 'nx-fixed nx-inset-0 nx-z-10 nx-bg-black/80 dark:nx-bg-black/60'
: 'nx-bg-transparent'
)}
onClick={() => setMenu(false)}
/>
<aside
className={cn(
'nextra-sidebar-container nx-flex nx-flex-col',
'md:nx-top-16 md:nx-w-64 md:nx-shrink-0 md:nx-transform-none',
asPopover ? 'md:nx-hidden' : 'md:nx-sticky md:nx-self-start',
menu
? '[transform:translate3d(0,0,0)]'
: '[transform:translate3d(0,-100%,0)]'
)}
ref={containerRef}
>
<div className={'nx-px-4 nx-pt-4 md:nx-hidden'}>
{renderComponent(config.search.component, {
directories: flatDirectories
})}
</div>
<div
className={cn(
'nextra-scrollbar nx-overflow-y-auto nx-p-4',
'nx-grow md:nx-h-[calc(100vh-var(--nextra-navbar-height)-3.75rem)]'
)}
ref={sidebarRef}
>
<Menu
className="nx-hidden md:nx-flex"
// The sidebar menu, shows only the docs directories.
directories={docsDirectories}
// When the viewport size is larger than `md`, hide the anchors in
// the sidebar when `floatTOC` is enabled.
anchors={config.toc.float ? [] : anchors}
onlyCurrentDocs
/>
<Menu
className="md:nx-hidden"
// The mobile dropdown menu, shows all the directories.
directories={fullDirectories}
// Always show the anchor links on mobile (`md`).
anchors={anchors}
/>
</div>
{hasMenu && (
<div
className={cn(
'nx-relative nx-z-[1]', // for top box nx-shadow
'nx-mx-4 nx-border-t nx-py-4 nx-shadow-[0_-12px_16px_#fff]',
'nx-flex nx-items-center nx-gap-2',
'dark:nx-border-neutral-800 dark:nx-shadow-[0_-12px_16px_#111]',
'contrast-more:nx-border-neutral-400 contrast-more:nx-shadow-none contrast-more:dark:nx-shadow-none'
)}
>
{config.i18n.length > 0 && (
<LocaleSwitch options={config.i18n} className="nx-grow" />
)}
{config.darkMode && <ThemeSwitch lite={config.i18n.length > 0} />}
</div>
)}
</aside>
</>
)
}