diff --git a/components/layout/mobile-menu.tsx b/components/layout/mobile-menu.tsx index 87793e5..2371a99 100644 --- a/components/layout/mobile-menu.tsx +++ b/components/layout/mobile-menu.tsx @@ -76,6 +76,7 @@ export function MobileMenu({ navItems, isOpen, onClose }: MobileMenuProps) { > window.scrollTo(0, 0)} className={cn( "block py-4 text-2xl font-medium transition-colors", isActive(item.href) diff --git a/components/layout/top-nav.tsx b/components/layout/top-nav.tsx index 4109ec9..67e5e82 100644 --- a/components/layout/top-nav.tsx +++ b/components/layout/top-nav.tsx @@ -23,6 +23,26 @@ export function TopNav() { const pathname = usePathname() const router = useRouter() const [mobileMenuOpen, setMobileMenuOpen] = React.useState(false) + const navContainerRef = React.useRef(null) + const [indicator, setIndicator] = React.useState({ left: 0, width: 0, opacity: 0 }) + + const isActive = (href: string) => { + if (href === "/") return pathname === "/" + return pathname.startsWith(href) + } + + const activeIndex = navItems.findIndex(item => isActive(item.href)) + + // Measure active tab position (scroll-independent via offsetLeft/offsetWidth) + React.useLayoutEffect(() => { + const container = navContainerRef.current + if (!container || activeIndex === -1) return + const links = container.querySelectorAll("a") + const activeLink = links[activeIndex] + if (activeLink) { + setIndicator({ left: activeLink.offsetLeft, width: activeLink.offsetWidth, opacity: 1 }) + } + }, [activeIndex]) React.useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { @@ -31,6 +51,7 @@ export function TopNav() { const num = parseInt(e.key) if (num >= 1 && num <= navItems.length) { + window.scrollTo(0, 0) router.push(navItems[num - 1].href) } } @@ -39,11 +60,6 @@ export function TopNav() { return () => window.removeEventListener("keydown", handleKeyDown) }, [router]) - const isActive = (href: string) => { - if (href === "/") return pathname === "/" - return pathname.startsWith(href) - } - const closeMobileMenu = React.useCallback(() => { setMobileMenuOpen(false) }, []) @@ -55,11 +71,17 @@ export function TopNav() {
{/* Desktop Navigation */} -
+
+ {navItems.map((item, index) => ( window.scrollTo(0, 0)} className={cn( "relative px-3 py-1.5 text-sm whitespace-nowrap transition-colors rounded-md", isActive(item.href) @@ -67,17 +89,6 @@ export function TopNav() { : "text-muted-foreground hover:text-foreground" )} > - {isActive(item.href) && ( - - )} {item.title}