'use client'; import { useState, useEffect, useCallback } from 'react'; import { StaticLink } from '@/components/ui/static-link'; import Image from 'next/image'; import { usePathname } from 'next/navigation'; import { Menu, X, MessageCircle } from 'lucide-react'; import { AnimatePresence, motion } from 'framer-motion'; import { Button } from '@/components/ui/button'; import { useSiteConfig, type NavigationItem } from '@/lib/site-config'; import { MegaDropdown } from '@/components/layout/mega-dropdown'; import { useFocusTrap } from '@/hooks/use-focus-trap'; import { cn } from '@/lib/utils'; import { ThemeToggle } from '@/components/theme/theme-toggle'; function HeaderContent() { const [isOpen, setIsOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); const [openDropdown, setOpenDropdown] = useState(null); const pathname = usePathname(); const focusTrapRef = useFocusTrap(isOpen); useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 20); }; const handleGlobalKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape' && isOpen) { setIsOpen(false); } }; window.addEventListener('scroll', handleScroll, { passive: true }); window.addEventListener('keydown', handleGlobalKeyDown); return () => { window.removeEventListener('scroll', handleScroll); window.removeEventListener('keydown', handleGlobalKeyDown); }; }, [isOpen]); const handleKeyDown = useCallback((e: React.KeyboardEvent) => { // Escape 关闭菜单;Enter/Space 已由原生 button 的 onClick 处理, // 此处不再拦截,避免键盘操作时重复触发 toggle。 if (e.key === 'Escape' && isOpen) { e.preventDefault(); setIsOpen(false); } }, [isOpen]); const config = useSiteConfig(); const handleNavClick = useCallback(() => { // StaticLink 新契约:onClick 先于导航执行,不调 preventDefault 即由 // next/link 完成客户端跳转,此处只负责关闭抽屉。 setIsOpen(false); }, []); const isActive = useCallback((item: NavigationItem) => { if (item.id === 'contact') { return pathname === '/contact'; } if (item.id === 'home') { return pathname === '/'; } if (item.id === 'products') { return pathname === '/products' || pathname.startsWith('/products/'); } if (item.id === 'solutions') { return pathname === '/solutions' || pathname.startsWith('/solutions/'); } if (item.id === 'cases') { return pathname === '/cases' || pathname.startsWith('/cases/'); } if (item.id === 'services') { return pathname === '/services' || pathname.startsWith('/services/'); } return pathname === `/${item.id}`; }, [pathname]); return ( <>
{/* Logo 明暗双版:浅色背景用 logo.svg(黑字),深色背景用 logo-white.svg(白字)。 按 html[data-theme] 显隐(规则见 globals.css),修复黑字在深色背景被吞的 bug。 */} {config.name} {config.name}
{isOpen && (
setIsOpen(false)} aria-hidden="true" /> )} ); } export function Header() { return ; }