|
| 1 | +import { Menu, Transition } from '@headlessui/react' |
| 2 | +import cn from 'clsx' |
| 3 | +import { useFSRoute } from '../nextra/hooks' |
| 4 | +import { ArrowRightIcon, MenuIcon } from '../nextra/icons' |
| 5 | +import type { Item, MenuItem, PageItem } from '../nextra/normalize-pages' |
| 6 | +import type { ReactElement, ReactNode } from 'react' |
| 7 | +import { useConfig, useMenu } from '../contexts' |
| 8 | +import { renderComponent } from '../utils' |
| 9 | +import { Anchor } from './anchor' |
| 10 | + |
| 11 | +export type NavBarProps = { |
| 12 | + flatDirectories: Item[] |
| 13 | + items: (PageItem | MenuItem)[] |
| 14 | +} |
| 15 | + |
| 16 | +const classes = { |
| 17 | + link: cn('nx-text-sm contrast-more:nx-text-gray-700 contrast-more:dark:nx-text-gray-100'), |
| 18 | + active: cn('nx-font-medium nx-subpixel-antialiased'), |
| 19 | + inactive: cn( |
| 20 | + 'nx-text-gray-600 hover:nx-text-gray-800 dark:nx-text-gray-400 dark:hover:nx-text-gray-200', |
| 21 | + ), |
| 22 | +} |
| 23 | + |
| 24 | +function NavbarMenu({ |
| 25 | + className, |
| 26 | + menu, |
| 27 | + children, |
| 28 | +}: { |
| 29 | + className?: string |
| 30 | + menu: MenuItem |
| 31 | + children: ReactNode |
| 32 | +}): ReactElement { |
| 33 | + const { items } = menu |
| 34 | + const routes = Object.fromEntries((menu.children || []).map((route) => [route.name, route])) |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="nx-relative nx-inline-block"> |
| 38 | + <Menu> |
| 39 | + <Menu.Button |
| 40 | + className={cn( |
| 41 | + className, |
| 42 | + '-nx-ml-2 nx-hidden nx-items-center nx-whitespace-nowrap nx-rounded nx-p-2 md:nx-inline-flex', |
| 43 | + classes.inactive, |
| 44 | + )} |
| 45 | + > |
| 46 | + {children} |
| 47 | + </Menu.Button> |
| 48 | + <Transition leave="nx-transition-opacity" leaveFrom="nx-opacity-100" leaveTo="nx-opacity-0"> |
| 49 | + <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-bg-white nx-py-1 nx-text-sm nx-shadow-lg nx-ring-1 nx-ring-black/5 dark:nx-bg-neutral-800 dark:nx-ring-white/20"> |
| 50 | + {Object.entries(items || {}).map(([key, item]) => ( |
| 51 | + <Menu.Item key={key}> |
| 52 | + <Anchor |
| 53 | + href={item.href || routes[key]?.route || menu.route + '/' + key} |
| 54 | + className={cn( |
| 55 | + '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', |
| 56 | + 'nx-py-1.5 nx-transition-colors ltr:nx-pl-3 ltr:nx-pr-9 rtl:nx-pl-9 rtl:nx-pr-3', |
| 57 | + )} |
| 58 | + newWindow={item.newWindow} |
| 59 | + > |
| 60 | + {item.title || key} |
| 61 | + </Anchor> |
| 62 | + </Menu.Item> |
| 63 | + ))} |
| 64 | + </Menu.Items> |
| 65 | + </Transition> |
| 66 | + </Menu> |
| 67 | + </div> |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +export function Header({ flatDirectories, items }: NavBarProps): ReactElement { |
| 72 | + const config = useConfig() |
| 73 | + const activeRoute = useFSRoute() |
| 74 | + const { menu, setMenu } = useMenu() |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="nextra-nav-container nx-w-full"> |
| 78 | + <div |
| 79 | + className={cn( |
| 80 | + 'nextra-nav-container-blur', |
| 81 | + 'nx-pointer-events-none nx-z-[-1] nx-h-full nx-w-full nx-bg-white dark:nx-bg-dark', |
| 82 | + 'nx-shadow-[0_2px_4px_rgba(0,0,0,.02),0_1px_0_rgba(0,0,0,.06)] dark:nx-shadow-[0_-1px_0_rgba(255,255,255,.1)_inset]', |
| 83 | + 'contrast-more:nx-shadow-[0_0_0_1px_#000] contrast-more:dark:nx-shadow-[0_0_0_1px_#fff]', |
| 84 | + )} |
| 85 | + > |
| 86 | + <nav className="nx-mx-auto nx-flex nx-h-[var(--nextra-navbar-height)] nx-max-w-[120rem] 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)]"> |
| 87 | + {config.logoLink ? ( |
| 88 | + <Anchor |
| 89 | + href={typeof config.logoLink === 'string' ? config.logoLink : '/'} |
| 90 | + className="nx-flex nx-items-center hover:nx-opacity-75 ltr:nx-mr-auto rtl:nx-ml-auto" |
| 91 | + > |
| 92 | + {renderComponent(config.logo)} |
| 93 | + </Anchor> |
| 94 | + ) : ( |
| 95 | + <div className="nx-flex nx-items-center ltr:nx-mr-auto rtl:nx-ml-auto"> |
| 96 | + {renderComponent(config.logo)} |
| 97 | + </div> |
| 98 | + )} |
| 99 | + {items.map((pageOrMenu) => { |
| 100 | + if (pageOrMenu.display === 'hidden') return null |
| 101 | + |
| 102 | + if (pageOrMenu.type === 'menu') { |
| 103 | + const menu = pageOrMenu as MenuItem |
| 104 | + return ( |
| 105 | + <NavbarMenu |
| 106 | + key={menu.title} |
| 107 | + className={cn(classes.link, 'nx-flex nx-gap-1', classes.inactive)} |
| 108 | + menu={menu} |
| 109 | + > |
| 110 | + {menu.title} |
| 111 | + <ArrowRightIcon |
| 112 | + className="nx-h-[18px] nx-min-w-[18px] nx-rounded-sm nx-p-0.5" |
| 113 | + pathClassName="nx-origin-center nx-transition-transform nx-rotate-90" |
| 114 | + /> |
| 115 | + </NavbarMenu> |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + const page = pageOrMenu as PageItem |
| 120 | + let href = page.href || page.route || '#' |
| 121 | + |
| 122 | + // If it's a directory |
| 123 | + if (page.children) { |
| 124 | + href = (page.withIndexPage ? page.route : page.firstChildRoute) || href |
| 125 | + } |
| 126 | + |
| 127 | + const isActive = page.route === activeRoute || activeRoute.startsWith(page.route + '/') |
| 128 | + |
| 129 | + return ( |
| 130 | + <Anchor |
| 131 | + href={href} |
| 132 | + key={href} |
| 133 | + className={cn( |
| 134 | + classes.link, |
| 135 | + 'nx-relative -nx-ml-2 nx-hidden nx-whitespace-nowrap nx-p-2 md:nx-inline-block', |
| 136 | + !isActive || page.newWindow ? classes.inactive : classes.active, |
| 137 | + )} |
| 138 | + newWindow={page.newWindow} |
| 139 | + aria-current={!page.newWindow && isActive} |
| 140 | + > |
| 141 | + <span className="nx-absolute nx-inset-x-0 nx-text-center">{page.title}</span> |
| 142 | + <span className="nx-invisible nx-font-medium">{page.title}</span> |
| 143 | + </Anchor> |
| 144 | + ) |
| 145 | + })} |
| 146 | + |
| 147 | + {renderComponent(config.search.component, { |
| 148 | + directories: flatDirectories, |
| 149 | + className: 'nx-hidden md:nx-inline-block mx-min-w-[200px]', |
| 150 | + })} |
| 151 | + |
| 152 | + {config.project.link ? ( |
| 153 | + <Anchor className="nx-p-2 nx-text-current" href={config.project.link} newWindow> |
| 154 | + {renderComponent(config.project.icon)} |
| 155 | + </Anchor> |
| 156 | + ) : null} |
| 157 | + |
| 158 | + {config.chat.link ? ( |
| 159 | + <Anchor className="nx-p-2 nx-text-current" href={config.chat.link} newWindow> |
| 160 | + {renderComponent(config.chat.icon)} |
| 161 | + </Anchor> |
| 162 | + ) : null} |
| 163 | + |
| 164 | + {renderComponent(config.navbar.extraContent)} |
| 165 | + |
| 166 | + <button |
| 167 | + type="button" |
| 168 | + aria-label="Menu" |
| 169 | + className="nextra-hamburger -nx-mr-2 nx-rounded nx-p-2 active:nx-bg-gray-400/20 md:nx-hidden" |
| 170 | + onClick={() => setMenu(!menu)} |
| 171 | + > |
| 172 | + <MenuIcon className={cn({ open: menu })} /> |
| 173 | + </button> |
| 174 | + </nav> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ) |
| 178 | +} |
0 commit comments