import { useGlobal } from '@/lib/global' import Link from 'next/link' import { useCallback, useEffect, useRef, useState } from 'react' import CategoryGroup from './CategoryGroup' import Logo from './Logo' import SearchDrawer from './SearchDrawer' import TagGroups from './TagGroups' import { MenuListTop } from './MenuListTop' import throttle from 'lodash.throttle' import SideBar from './SideBar' import SideBarDrawer from './SideBarDrawer' import { siteConfig } from '@/lib/config' import SearchButton from './SearchButton' import CONFIG from '../config' import { useRouter } from 'next/router' let windowTop = 0 /** * 顶部导航 * @param {*} param0 * @returns */ const TopNav = props => { const searchDrawer = useRef() const { tags, currentTag, categories, currentCategory } = props const { locale } = useGlobal() const router = useRouter() const [isOpen, changeShow] = useState(false) const showSearchButton = siteConfig('HEXO_MENU_SEARCH', false, CONFIG) const toggleMenuOpen = () => { changeShow(!isOpen) } const toggleSideBarClose = () => { changeShow(false) } // 监听滚动 useEffect(() => { window.addEventListener('scroll', topNavStyleHandler) router.events.on('routeChangeComplete', topNavStyleHandler) topNavStyleHandler() return () => { router.events.off('routeChangeComplete', topNavStyleHandler) window.removeEventListener('scroll', topNavStyleHandler) } }, []) const throttleMs = 200 const topNavStyleHandler = useCallback(throttle(() => { const scrollS = window.scrollY const nav = document.querySelector('#sticky-nav') // 首页和文章页会有头图 const header = document.querySelector('#header') // 导航栏和头图是否重叠 const scrollInHeader = header && (scrollS < 10 || scrollS < header?.clientHeight - 50) // 透明导航条的条件 // const textWhite = header && scrollInHeader if (scrollInHeader) { nav && nav.classList.replace('bg-white', 'bg-none') nav && nav.classList.replace('border', 'border-transparent') nav && nav.classList.replace('drop-shadow-md', 'shadow-none') nav && nav.classList.replace('dark:bg-hexo-black-gray', 'transparent') } else { nav && nav.classList.replace('bg-none', 'bg-white') nav && nav.classList.replace('border-transparent', 'border') nav && nav.classList.replace('shadow-none', 'drop-shadow-md') nav && nav.classList.replace('transparent', 'dark:bg-hexo-black-gray') } if (scrollInHeader) { nav && nav.classList.replace('text-black', 'text-white') } else { nav && nav.classList.replace('text-white', 'text-black') } // 导航栏不在头图里,且页面向下滚动一定程度 隐藏导航栏 const showNav = scrollS <= windowTop || scrollS < 5 || (header && scrollS <= header.clientHeight + 100) if (!showNav) { nav && nav.classList.replace('top-0', '-top-20') windowTop = scrollS } else { nav && nav.classList.replace('-top-20', 'top-0') windowTop = scrollS } }, throttleMs) ) const searchDrawerSlot = <> {categories && (
{locale.COMMON.CATEGORY}
{locale.COMMON.MORE}
)} {tags && (
{locale.COMMON.TAGS}
{locale.COMMON.MORE}
)} return (
{/* 导航栏 */} {/* 折叠侧边栏 */}
) } export default TopNav