diff --git a/src/components/layout/Navbar.module.css b/src/components/layout/Navbar.module.css index 6347e79..3974cc2 100644 --- a/src/components/layout/Navbar.module.css +++ b/src/components/layout/Navbar.module.css @@ -40,6 +40,7 @@ } .navLinks { + position: relative; display: flex; align-items: center; gap: var(--space-xl); @@ -69,12 +70,13 @@ .activeIndicator { position: absolute; bottom: 0; - left: 50%; - transform: translateX(-50%); + left: 0; width: 4px; height: 4px; border-radius: 50%; background-color: var(--md-sys-color-primary); + transform: translateX(-50%); + pointer-events: none; } .actions { diff --git a/src/components/layout/Navbar.tsx b/src/components/layout/Navbar.tsx index cbe9036..3757333 100644 --- a/src/components/layout/Navbar.tsx +++ b/src/components/layout/Navbar.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { motion } from 'motion/react'; import { useTranslation } from '../../i18n'; @@ -9,6 +9,18 @@ export function Navbar() { const location = useLocation(); const [isScrolled, setIsScrolled] = useState(false); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); + const [indicatorX, setIndicatorX] = useState(0); + const navLinksRef = useRef(null); + const linkRefs = useRef<(HTMLAnchorElement | null)[]>([]); + + const navLinks = [ + { path: '/', label: t.nav.home }, + { path: '/about', label: t.nav.about }, + { path: '/contact', label: t.nav.contact }, + ]; + + // Find active index + const activeIndex = navLinks.findIndex((link) => link.path === location.pathname); useEffect(() => { const handleScroll = () => { @@ -22,11 +34,25 @@ export function Navbar() { setIsMobileMenuOpen(false); }, [location.pathname]); - const navLinks = [ - { path: '/', label: t.nav.home }, - { path: '/about', label: t.nav.about }, - { path: '/contact', label: t.nav.contact }, - ]; + // Calculate indicator position + useEffect(() => { + const updateIndicatorPosition = () => { + const activeLink = linkRefs.current[activeIndex]; + const container = navLinksRef.current; + + if (activeLink && container) { + const containerRect = container.getBoundingClientRect(); + const linkRect = activeLink.getBoundingClientRect(); + // Calculate center of the link relative to the container + const centerX = linkRect.left - containerRect.left + linkRect.width / 2; + setIndicatorX(centerX); + } + }; + + updateIndicatorPosition(); + window.addEventListener('resize', updateIndicatorPosition); + return () => window.removeEventListener('resize', updateIndicatorPosition); + }, [activeIndex, language]); // Recalculate when active link or language changes const toggleLanguage = () => { setLanguage(language === 'de' ? 'en' : 'de'); @@ -45,23 +71,27 @@ export function Navbar() { IT -
- {navLinks.map((link) => ( +
+ {navLinks.map((link, index) => ( { linkRefs.current[index] = el; }} to={link.path} className={`${styles.navLink} ${location.pathname === link.path ? styles.active : ''}`} > {link.label} - {location.pathname === link.path && ( - - )} ))} + {activeIndex !== -1 && ( + + )}