|
| 1 | +import Collapse from '@/components/Collapse' |
| 2 | +import DarkModeButton from '@/components/DarkModeButton' |
| 3 | +import { siteConfig } from '@/lib/config' |
| 4 | +import { useGlobal } from '@/lib/global' |
| 5 | +import { useRef, useState } from 'react' |
| 6 | +import CONFIG from '../config' |
| 7 | +import LogoBar from './LogoBar' |
| 8 | +import { MenuBarMobile } from './MenuBarMobile' |
| 9 | +import { MenuItemDrop } from './MenuItemDrop' |
| 10 | + |
| 11 | +/** |
| 12 | + * 页头:顶部导航栏 + 菜单 |
| 13 | + * @param {} param0 |
| 14 | + * @returns |
| 15 | + */ |
| 16 | +export default function Header(props) { |
| 17 | + const { className, customNav, customMenu } = props |
| 18 | + const [isOpen, changeShow] = useState(false) |
| 19 | + const collapseRef = useRef(null) |
| 20 | + |
| 21 | + const { locale } = useGlobal() |
| 22 | + |
| 23 | + const defaultLinks = [ |
| 24 | + { |
| 25 | + icon: 'fas fa-th', |
| 26 | + name: locale.COMMON.CATEGORY, |
| 27 | + to: '/category', |
| 28 | + show: siteConfig('GITBOOK_MENU_CATEGORY', null, CONFIG) |
| 29 | + }, |
| 30 | + { |
| 31 | + icon: 'fas fa-tag', |
| 32 | + name: locale.COMMON.TAGS, |
| 33 | + to: '/tag', |
| 34 | + show: siteConfig('GITBOOK_BOOK_MENU_TAG', null, CONFIG) |
| 35 | + }, |
| 36 | + { |
| 37 | + icon: 'fas fa-archive', |
| 38 | + name: locale.NAV.ARCHIVE, |
| 39 | + to: '/archive', |
| 40 | + show: siteConfig('GITBOOK_MENU_ARCHIVE', null, CONFIG) |
| 41 | + }, |
| 42 | + { |
| 43 | + icon: 'fas fa-search', |
| 44 | + name: locale.NAV.SEARCH, |
| 45 | + to: '/search', |
| 46 | + show: siteConfig('GITBOOK_MENU_SEARCH', null, CONFIG) |
| 47 | + } |
| 48 | + ] |
| 49 | + |
| 50 | + let links = defaultLinks.concat(customNav) |
| 51 | + |
| 52 | + const toggleMenuOpen = () => { |
| 53 | + changeShow(!isOpen) |
| 54 | + } |
| 55 | + |
| 56 | + // 如果 开启自定义菜单,则覆盖Page生成的菜单 |
| 57 | + if (siteConfig('CUSTOM_MENU')) { |
| 58 | + links = customMenu |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <div id='top-nav' className={'fixed top-0 w-full z-20 ' + className}> |
| 63 | + {/* 移动端折叠菜单 */} |
| 64 | + <Collapse |
| 65 | + type='vertical' |
| 66 | + collapseRef={collapseRef} |
| 67 | + isOpen={isOpen} |
| 68 | + className='md:hidden'> |
| 69 | + <div className='bg-white dark:bg-hexo-black-gray pt-1 py-2 lg:hidden '> |
| 70 | + <MenuBarMobile |
| 71 | + {...props} |
| 72 | + onHeightChange={param => |
| 73 | + collapseRef.current?.updateCollapseHeight(param) |
| 74 | + } |
| 75 | + /> |
| 76 | + </div> |
| 77 | + </Collapse> |
| 78 | + |
| 79 | + {/* 导航栏菜单 */} |
| 80 | + <div className='flex w-full h-14 shadow glassmorphism bg-white dark:bg-hexo-black-gray px-7 items-between'> |
| 81 | + {/* 左侧图标Logo */} |
| 82 | + <LogoBar {...props} /> |
| 83 | + |
| 84 | + {/* 折叠按钮、仅移动端显示 */} |
| 85 | + <div className='mr-1 flex md:hidden justify-end items-center space-x-4 dark:text-gray-200'> |
| 86 | + <DarkModeButton className='flex text-md items-center h-full' /> |
| 87 | + <div |
| 88 | + onClick={toggleMenuOpen} |
| 89 | + className='cursor-pointer text-lg hover:scale-110 duration-150'> |
| 90 | + {isOpen ? ( |
| 91 | + <i className='fas fa-times' /> |
| 92 | + ) : ( |
| 93 | + <i className='fa-solid fa-ellipsis-vertical' /> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + |
| 98 | + {/* 桌面端顶部菜单 */} |
| 99 | + <div className='hidden md:flex'> |
| 100 | + {links && |
| 101 | + links?.map((link, index) => ( |
| 102 | + <MenuItemDrop key={index} link={link} /> |
| 103 | + ))} |
| 104 | + <DarkModeButton className='text-sm flex items-center h-full' /> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + ) |
| 109 | +} |
0 commit comments