/
navbar.tsx
208 lines (190 loc) · 6.95 KB
/
navbar.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
import React, { ReactElement, ReactNode } from 'react'
import cn from 'clsx'
import { useRouter } from 'next/router'
import { Menu, Transition } from '@headlessui/react'
import { ArrowRightIcon } from 'nextra/icons'
import { useConfig, useMenu } from '../contexts'
import { MenuIcon } from 'nextra/icons'
import { Item, PageItem, MenuItem, renderComponent, getFSRoute } from '../utils'
import { Anchor } from './anchor'
import { DEFAULT_LOCALE } from '../constants'
export type NavBarProps = {
flatDirectories: Item[]
items: (PageItem | MenuItem)[]
}
const classes = {
link: cn(
'nx-text-sm contrast-more:nx-text-gray-700 contrast-more:dark:nx-text-gray-100'
),
active: cn('nx-subpixel-antialiased contrast-more:nx-font-bold'),
inactive: cn(
'nx-text-gray-600 hover:nx-text-gray-800 dark:nx-text-gray-400 dark:hover:nx-text-gray-200'
)
}
function NavbarMenu({
className,
menu,
children
}: {
className?: string
menu: MenuItem
children: ReactNode
}): ReactElement {
const { items } = menu
const routes = Object.fromEntries(
(menu.children || []).map(route => [route.name, route])
)
return (
<div className="nx-relative nx-inline-block">
<Menu>
<Menu.Button
className={cn(
className,
'-nx-ml-2 nx-hidden nx-items-center nx-whitespace-nowrap nx-rounded nx-p-2 md:nx-inline-flex',
classes.inactive
)}
>
{children}
</Menu.Button>
<Transition
leave="nx-transition-opacity"
leaveFrom="nx-opacity-100"
leaveTo="nx-opacity-0"
>
<Menu.Items className="nx-absolute nx-right-0 nx-z-20 nx-mt-1 nx-max-h-64 nx-min-w-full nx-overflow-auto nx-rounded-md nx-border nx-border-black/5 nx-bg-white nx-py-1 nx-text-sm nx-shadow-lg dark:nx-border-white/10 dark:nx-bg-neutral-800">
{Object.entries(items || {}).map(([key, item]) => (
<Menu.Item key={key}>
<Anchor
href={
item.href || routes[key]?.route || menu.route + '/' + key
}
className={cn(
'nx-relative nx-hidden nx-w-full nx-select-none nx-whitespace-nowrap nx-text-gray-600 hover:nx-text-gray-900 dark:nx-text-gray-400 dark:hover:nx-text-gray-100 md:nx-inline-block',
'nx-py-1.5 nx-transition-colors ltr:nx-pl-3 ltr:nx-pr-9 rtl:nx-pr-3 rtl:nx-pl-9'
)}
newWindow={item.newWindow}
>
{item.title || key}
</Anchor>
</Menu.Item>
))}
</Menu.Items>
</Transition>
</Menu>
</div>
)
}
export function Navbar({ flatDirectories, items }: NavBarProps): ReactElement {
const config = useConfig()
const { locale = DEFAULT_LOCALE, asPath } = useRouter()
const activeRoute = getFSRoute(asPath, locale)
const { menu, setMenu } = useMenu()
return (
<div className="nextra-nav-container nx-sticky nx-top-0 nx-z-20 nx-w-full nx-bg-transparent">
<div
className={cn(
'nextra-nav-container-blur',
'nx-pointer-events-none nx-absolute nx-z-[-1] nx-h-full nx-w-full nx-bg-white dark:nx-bg-dark',
'nx-shadow-[0_2px_4px_rgba(0,0,0,.02),0_-1px_0_rgba(0,0,0,.06)_inset] dark:nx-shadow-[0_-1px_0_rgba(255,255,255,.1)_inset]',
'contrast-more:nx-shadow-[0_0_0_1px_#000] contrast-more:dark:nx-shadow-[0_0_0_1px_#fff]'
)}
/>
<nav className="nx-mx-auto nx-flex nx-h-[var(--nextra-navbar-height)] nx-max-w-[90rem] nx-items-center nx-justify-end nx-gap-2 nx-pl-[max(env(safe-area-inset-left),1.5rem)] nx-pr-[max(env(safe-area-inset-right),1.5rem)]">
{config.logoLink ? (
<Anchor
href={typeof config.logoLink === 'string' ? config.logoLink : '/'}
className="nx-flex nx-items-center hover:nx-opacity-75 ltr:nx-mr-auto rtl:nx-ml-auto"
>
{renderComponent(config.logo)}
</Anchor>
) : (
<div className="nx-flex nx-items-center ltr:nx-mr-auto rtl:nx-ml-auto">
{renderComponent(config.logo)}
</div>
)}
{items.map(pageOrMenu => {
if (pageOrMenu.display === 'hidden') return null
if (pageOrMenu.type === 'menu') {
const menu = pageOrMenu as MenuItem
const isActive =
menu.route === activeRoute ||
activeRoute.startsWith(menu.route + '/')
return (
<NavbarMenu
key={menu.title}
className={cn(
classes.link,
'nx-flex nx-gap-1',
isActive ? classes.active : classes.inactive
)}
menu={menu}
>
{menu.title}
<ArrowRightIcon
className="nx-h-[18px] nx-min-w-[18px] nx-rounded-sm nx-p-0.5"
pathClassName="nx-origin-center nx-transition-transform nx-rotate-90"
/>
</NavbarMenu>
)
}
const page = pageOrMenu as PageItem
let href = page.href || page.route || '#'
// If it's a directory
if (page.children) {
href =
(page.withIndexPage ? page.route : page.firstChildRoute) || href
}
const isActive =
page.route === activeRoute ||
activeRoute.startsWith(page.route + '/')
return (
<Anchor
href={href}
key={page.route}
className={cn(
classes.link,
'-nx-ml-2 nx-hidden nx-whitespace-nowrap nx-p-2 md:nx-inline-block',
!isActive || page.newWindow ? classes.inactive : classes.active
)}
newWindow={page.newWindow}
aria-current={!page.newWindow && isActive}
>
{page.title}
</Anchor>
)
})}
{renderComponent(config.search.component, {
directories: flatDirectories,
className: 'nx-hidden md:nx-inline-block mx-min-w-[200px]'
})}
{config.project.link ? (
<Anchor
className="nx-p-2 nx-text-current"
href={config.project.link}
newWindow
>
{renderComponent(config.project.icon)}
</Anchor>
) : null}
{config.chat.link ? (
<Anchor
className="nx-p-2 nx-text-current"
href={config.chat.link}
newWindow
>
{renderComponent(config.chat.icon)}
</Anchor>
) : null}
{renderComponent(config.navbar.extraContent)}
<button
type="button"
aria-label="Menu"
className="nextra-hamburger -nx-mr-2 nx-rounded nx-p-2 active:nx-bg-gray-400/20 md:nx-hidden"
onClick={() => setMenu(!menu)}
>
<MenuIcon className={cn({ open: menu })} />
</button>
</nav>
</div>
)
}