|
| 1 | +import { MenuItem, Menu, MenuButton, MenuItems } from "@headlessui/react" |
| 2 | +import cn from "clsx" |
| 3 | +// eslint-disable-next-line no-restricted-imports -- since we don't need newWindow prop |
| 4 | +import NextLink from "next/link" |
| 5 | +import { Button } from "nextra/components" |
| 6 | +import { useFSRoute } from "nextra/hooks" |
| 7 | +import type * as normalizePages from "nextra/normalize-pages" |
| 8 | +import type { ReactElement, ReactNode } from "react" |
| 9 | +import { useMenu, useThemeConfig } from "nextra-theme-docs" |
| 10 | +import { Anchor } from "@/app/conf/_design-system/anchor" |
| 11 | +import { renderComponent } from "@/components/utils" |
| 12 | + |
| 13 | +import MenuIcon from "@/app/conf/_design-system/pixelarticons/menu.svg?svgr" |
| 14 | +import CloseIcon from "@/app/conf/_design-system/pixelarticons/close.svg?svgr" |
| 15 | + |
| 16 | +type Item = normalizePages.PageItem | normalizePages.MenuItem |
| 17 | +export interface NavBarProps { |
| 18 | + items: Item[] |
| 19 | +} |
| 20 | + |
| 21 | +const classes = { |
| 22 | + link: cn( |
| 23 | + "_text-sm contrast-more:_text-gray-700 contrast-more:dark:_text-gray-100", |
| 24 | + ), |
| 25 | + active: cn("_font-medium _subpixel-antialiased"), |
| 26 | + inactive: cn( |
| 27 | + "_text-gray-600 hover:_text-gray-800 dark:_text-gray-400 dark:hover:_text-gray-200", |
| 28 | + ), |
| 29 | +} |
| 30 | + |
| 31 | +function NavbarMenu({ |
| 32 | + menu, |
| 33 | + children, |
| 34 | +}: { |
| 35 | + menu: normalizePages.MenuItem |
| 36 | + children: ReactNode |
| 37 | +}): ReactElement { |
| 38 | + const routes = Object.fromEntries( |
| 39 | + (menu.children || []).map(route => [route.name, route]), |
| 40 | + ) |
| 41 | + return ( |
| 42 | + <Menu> |
| 43 | + <MenuButton |
| 44 | + className={({ focus }) => |
| 45 | + cn( |
| 46 | + classes.link, |
| 47 | + classes.inactive, |
| 48 | + "max-md:_hidden _items-center _whitespace-nowrap _flex _gap-1.5 _ring-inset", |
| 49 | + focus && "nextra-focusable", |
| 50 | + ) |
| 51 | + } |
| 52 | + > |
| 53 | + {children} |
| 54 | + {"->"} |
| 55 | + </MenuButton> |
| 56 | + <MenuItems |
| 57 | + transition |
| 58 | + className={({ open }) => |
| 59 | + cn( |
| 60 | + "motion-reduce:_transition-none", |
| 61 | + "nextra-focus", |
| 62 | + open ? "_opacity-100" : "_opacity-0", |
| 63 | + "nextra-scrollbar _transition-opacity", |
| 64 | + "_border _border-black/5 dark:_border-white/20", |
| 65 | + "_backdrop-blur-lg bg-[rgb(var(--nextra-bg),.8)]", // todo: full screen overlay |
| 66 | + "_z-20 _rounded-md _py-1 _text-sm", |
| 67 | + // headlessui adds max-height as style, use !important to override |
| 68 | + "!_max-h-[min(calc(100vh-5rem),256px)]", |
| 69 | + ) |
| 70 | + } |
| 71 | + anchor={{ to: "top end", gap: 10, padding: 16 }} |
| 72 | + > |
| 73 | + {Object.entries(menu.items || {}).map(([key, item]) => ( |
| 74 | + <MenuItem |
| 75 | + key={key} |
| 76 | + as={Anchor} |
| 77 | + href={item.href || routes[key]?.route} |
| 78 | + > |
| 79 | + <Anchor |
| 80 | + href={item.href || routes[key]?.route} |
| 81 | + className="data-focus:text-gray-900 block py-1.5 pl-3 pr-9 transition-colors rtl:pl-9 rtl:pr-3" |
| 82 | + target={item.newWindow ? "_blank" : undefined} |
| 83 | + > |
| 84 | + {item.title} |
| 85 | + </Anchor> |
| 86 | + </MenuItem> |
| 87 | + ))} |
| 88 | + </MenuItems> |
| 89 | + </Menu> |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +export function Navbar({ items }: NavBarProps): ReactElement { |
| 94 | + const themeConfig = useThemeConfig() |
| 95 | + |
| 96 | + const activeRoute = useFSRoute() |
| 97 | + const { menu, setMenu } = useMenu() |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className="nextra-nav-container _top-0 _z-20 _w-full _bg-transparent print:_hidden fixed"> |
| 101 | + <div className="nextra-nav-container-blur" /> |
| 102 | + <nav className="mx-auto flex h-[var(--nextra-navbar-height)] max-w-[90rem] items-center justify-end gap-4 pl-[max(env(safe-area-inset-left),1.5rem)] pr-[max(env(safe-area-inset-right),1.5rem)]"> |
| 103 | + {themeConfig.logoLink ? ( |
| 104 | + <NextLink |
| 105 | + href={ |
| 106 | + typeof themeConfig.logoLink === "string" |
| 107 | + ? themeConfig.logoLink |
| 108 | + : "/" |
| 109 | + } |
| 110 | + className="nextra-focus flex items-center hover:opacity-75 ltr:mr-auto rtl:ml-auto" |
| 111 | + > |
| 112 | + {renderComponent(themeConfig.logo)} |
| 113 | + </NextLink> |
| 114 | + ) : ( |
| 115 | + <div className="flex items-center ltr:mr-auto rtl:ml-auto"> |
| 116 | + {renderComponent(themeConfig.logo)} |
| 117 | + </div> |
| 118 | + )} |
| 119 | + <div className="nextra-scrollbar flex gap-4 overflow-x-auto py-1.5"> |
| 120 | + {items.map(pageOrMenu => { |
| 121 | + if (pageOrMenu.display === "hidden") return null |
| 122 | + |
| 123 | + if (pageOrMenu.type === "menu") { |
| 124 | + const menu = pageOrMenu as normalizePages.MenuItem |
| 125 | + return ( |
| 126 | + <NavbarMenu key={menu.title} menu={menu}> |
| 127 | + {menu.title} |
| 128 | + </NavbarMenu> |
| 129 | + ) |
| 130 | + } |
| 131 | + const page = pageOrMenu as normalizePages.PageItem |
| 132 | + let href = page.href || page.route || "#" |
| 133 | + |
| 134 | + // If it's a directory |
| 135 | + if (page.children) { |
| 136 | + href = |
| 137 | + (page.withIndexPage ? page.route : page.firstChildRoute) || href |
| 138 | + } |
| 139 | + |
| 140 | + const isActive = |
| 141 | + page.route === activeRoute || |
| 142 | + activeRoute.startsWith(page.route + "/") |
| 143 | + |
| 144 | + return ( |
| 145 | + <Anchor |
| 146 | + href={href} |
| 147 | + key={href} |
| 148 | + className={cn( |
| 149 | + classes.link, |
| 150 | + "max-md:_hidden _whitespace-nowrap _ring-inset", |
| 151 | + !isActive || page.newWindow |
| 152 | + ? classes.inactive |
| 153 | + : classes.active, |
| 154 | + )} |
| 155 | + target={page.newWindow ? "_blank" : undefined} |
| 156 | + aria-current={!page.newWindow && isActive} |
| 157 | + > |
| 158 | + {page.title} |
| 159 | + </Anchor> |
| 160 | + ) |
| 161 | + })} |
| 162 | + </div> |
| 163 | + |
| 164 | + {process.env.NEXTRA_SEARCH && |
| 165 | + renderComponent(themeConfig.search.component, { |
| 166 | + className: "max-md:_hidden", |
| 167 | + })} |
| 168 | + |
| 169 | + {themeConfig.project.link ? ( |
| 170 | + <Anchor href={themeConfig.project.link}> |
| 171 | + {renderComponent(themeConfig.project.icon)} |
| 172 | + </Anchor> |
| 173 | + ) : null} |
| 174 | + |
| 175 | + {themeConfig.chat.link ? ( |
| 176 | + <Anchor href={themeConfig.chat.link}> |
| 177 | + {renderComponent(themeConfig.chat.icon)} |
| 178 | + </Anchor> |
| 179 | + ) : null} |
| 180 | + |
| 181 | + {renderComponent(themeConfig.navbar.extraContent)} |
| 182 | + |
| 183 | + <Button |
| 184 | + aria-label="Menu" |
| 185 | + className={({ active }) => |
| 186 | + cn( |
| 187 | + "nextra-hamburger _rounded md:_hidden", |
| 188 | + active && "_bg-gray-400/20", |
| 189 | + ) |
| 190 | + } |
| 191 | + onClick={() => setMenu(!menu)} |
| 192 | + > |
| 193 | + {menu ? <CloseIcon /> : <MenuIcon />} |
| 194 | + </Button> |
| 195 | + </nav> |
| 196 | + </div> |
| 197 | + ) |
| 198 | +} |
0 commit comments