|
| 1 | +import React, {type ReactNode} from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import { |
| 4 | + useThemeConfig, |
| 5 | + ErrorCauseBoundary, |
| 6 | + ThemeClassNames, |
| 7 | +} from '@docusaurus/theme-common'; |
| 8 | +import { |
| 9 | + splitNavbarItems, |
| 10 | + useNavbarMobileSidebar, |
| 11 | +} from '@docusaurus/theme-common/internal'; |
| 12 | +import NavbarItem, {type Props as NavbarItemConfig} from '@theme/NavbarItem'; |
| 13 | +import NavbarColorModeToggle from '@theme/Navbar/ColorModeToggle'; |
| 14 | +import SearchBar from '@theme/SearchBar'; |
| 15 | +import NavbarMobileSidebarToggle from '@theme/Navbar/MobileSidebar/Toggle'; |
| 16 | +import NavbarLogo from '@theme/Navbar/Logo'; |
| 17 | +import NavbarSearch from '@theme/Navbar/Search'; |
| 18 | + |
| 19 | +function useNavbarItems() { |
| 20 | + return useThemeConfig().navbar.items as NavbarItemConfig[]; |
| 21 | +} |
| 22 | + |
| 23 | +function NavbarItems({items}: {items: NavbarItemConfig[]}): ReactNode { |
| 24 | + return ( |
| 25 | + <> |
| 26 | + {items.map((item, i) => ( |
| 27 | + <ErrorCauseBoundary |
| 28 | + key={i} |
| 29 | + onError={(error) => |
| 30 | + new Error( |
| 31 | + `A theme navbar item failed to render. |
| 32 | +Please double-check the following navbar item (themeConfig.navbar.items) of your Docusaurus config: |
| 33 | +${JSON.stringify(item, null, 2)}`, |
| 34 | + {cause: error}, |
| 35 | + ) |
| 36 | + }> |
| 37 | + <NavbarItem {...item} /> |
| 38 | + </ErrorCauseBoundary> |
| 39 | + ))} |
| 40 | + </> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +export default function NavbarContent(): ReactNode { |
| 45 | + const mobileSidebar = useNavbarMobileSidebar(); |
| 46 | + const items = useNavbarItems(); |
| 47 | + const [leftItems, rightItems] = splitNavbarItems(items); |
| 48 | + const hasSearchItem = items.some((item) => item.type === 'search'); |
| 49 | + const rightItemsWithoutSearch = rightItems.filter((item) => item.type !== 'search'); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="navbar__inner tb-navbar-inner"> |
| 53 | + <div |
| 54 | + className={clsx( |
| 55 | + ThemeClassNames.layout.navbar.containerLeft, |
| 56 | + 'navbar__items tb-navbar-left', |
| 57 | + )}> |
| 58 | + {!mobileSidebar.disabled && <NavbarMobileSidebarToggle />} |
| 59 | + <NavbarLogo /> |
| 60 | + <NavbarItems items={leftItems} /> |
| 61 | + </div> |
| 62 | + |
| 63 | + {hasSearchItem && ( |
| 64 | + <div className="tb-navbar-center"> |
| 65 | + <NavbarSearch className="tb-navbar-search"> |
| 66 | + <SearchBar /> |
| 67 | + </NavbarSearch> |
| 68 | + </div> |
| 69 | + )} |
| 70 | + |
| 71 | + <div |
| 72 | + className={clsx( |
| 73 | + ThemeClassNames.layout.navbar.containerRight, |
| 74 | + 'navbar__items navbar__items--right tb-navbar-right', |
| 75 | + )}> |
| 76 | + <NavbarItems items={rightItemsWithoutSearch} /> |
| 77 | + <NavbarColorModeToggle /> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
0 commit comments