import { useEffect, useState } from "react"; import "../styles/NavBar.css"; const NavBar = () => { const [active, setActive] = useState("home-section"); const [isScrolling, setIsScrolling] = useState(false); useEffect(() => { const sections = document.querySelectorAll("section"); const navHeight = document.querySelector(".navbar")?.offsetHeight + 40 || 120; const handleScroll = () => { if (isScrolling) return; let current = "home-section"; sections.forEach((section) => { const sectionTop = section.offsetTop - navHeight; if (window.scrollY >= sectionTop) { current = section.id; } }); setActive(current); }; window.addEventListener("scroll", handleScroll); handleScroll(); return () => { window.removeEventListener("scroll", handleScroll); }; }, [isScrolling]); const handleClick = (id) => { setActive(id); setIsScrolling(true); document.getElementById(id)?.scrollIntoView({ behavior: "smooth" }); setTimeout(() => setIsScrolling(false), 800); }; return ( ); }; export default NavBar;