import LogoBar from './LogoBar' import { useEffect, useRef, useState } from 'react' import Collapse from '@/components/Collapse' import { MenuBarMobile } from './MenuBarMobile' import { useGlobal } from '@/lib/global' import CONFIG from '../config' import { MenuItemDrop } from './MenuItemDrop' import { siteConfig } from '@/lib/config' import throttle from 'lodash.throttle' /** * 顶部导航栏 + 菜单 * @param {} param0 * @returns */ export default function Header(props) { const { customNav, customMenu } = props const [isOpen, changeShow] = useState(false) const collapseRef = useRef(null) const { locale } = useGlobal() const defaultLinks = [ { icon: 'fas fa-th', name: locale.COMMON.CATEGORY, to: '/category', show: CONFIG.MENU_CATEGORY }, { icon: 'fas fa-tag', name: locale.COMMON.TAGS, to: '/tag', show: CONFIG.MENU_TAG }, { icon: 'fas fa-archive', name: locale.NAV.ARCHIVE, to: '/archive', show: CONFIG.MENU_ARCHIVE }, { icon: 'fas fa-search', name: locale.NAV.SEARCH, to: '/search', show: CONFIG.MENU_SEARCH } ] let links = defaultLinks.concat(customNav) const toggleMenuOpen = () => { changeShow(!isOpen) } // 向下滚动时,调整导航条高度 useEffect(() => { scrollTrigger() window.addEventListener('scroll', scrollTrigger) return () => { window.removeEventListener('scroll', scrollTrigger) } }, []) const throttleMs = 150 const scrollTrigger = throttle(() => { const scrollS = window.scrollY const nav = document.querySelector('#top-navbar') const narrowNav = scrollS > 50 if (narrowNav) { nav && nav.classList.replace('h-24', 'h-14') } else { nav && nav.classList.replace('h-14', 'h-24') } }, throttleMs) // 如果 开启自定义菜单,则覆盖Page生成的菜单 if (siteConfig('CUSTOM_MENU')) { links = customMenu } if (!links || links.length === 0) { return null } return
{/* 导航栏菜单内容 */}
{/* 左侧图标Logo */} {/* 移动端折叠按钮 */}
{isOpen ? : }
{/* 桌面端顶部菜单 */}
{links && links?.map(link => )}
{/* 移动端折叠菜单 */}
collapseRef.current?.updateCollapseHeight(param)} />
}