@@ -6,22 +6,39 @@ import React, { useEffect, useState } from "react";
66import { brainwave } from "@/public/assets/index" ;
77import { cn } from "@/lib/utils" ;
88import { navigation } from "@/constants" ;
9- import { useParams } from "next/navigation" ;
109import Button from "../atoms/button" ;
1110import MenuSvg from "../svg/menu-svg" ;
1211import { HamburgerMenu } from "../design/navbar" ;
1312
1413type Props = { } ;
15- type TSection = "hero" | "features" | "collaboration" | "how-to-use" | "pricing" ;
1614
1715const Navbar = ( props : Props ) => {
18- const params = useParams ( ) ;
19- const [ hash , setHash ] = useState < TSection > ( "hero" ) ;
16+ const [ hash , setHash ] = useState < string > ( "hero" ) ;
2017 const [ openNavigation , setOpenNavigation ] = useState < boolean > ( false ) ;
2118
2219 useEffect ( ( ) => {
23- setHash ( window . location . hash as TSection ) ;
24- } , [ params ] ) ;
20+ const dynamicNavbarHighlight = ( ) => {
21+ const sections = document . querySelectorAll ( "section[id]" ) ;
22+
23+ sections . forEach ( ( current ) => {
24+ if ( current === null ) return ;
25+
26+ const sectionId = current . getAttribute ( "id" ) ;
27+ // @ts -ignore
28+ const sectionHeight = current . offsetHeight ;
29+ const sectionTop = current . getBoundingClientRect ( ) . top - sectionHeight * 0.2 ;
30+
31+ if ( sectionTop < 0 && sectionTop + sectionHeight > 0 && hash !== sectionId ) {
32+ setHash ( `#${ sectionId as string } ` ) ;
33+ }
34+ } ) ;
35+ } ;
36+
37+ window . addEventListener ( "scroll" , dynamicNavbarHighlight ) ;
38+
39+ return ( ) => window . removeEventListener ( "scroll" , dynamicNavbarHighlight ) ;
40+ // eslint-disable-next-line react-hooks/exhaustive-deps
41+ } , [ ] ) ;
2542
2643 const toggleNavigation = ( ) => setOpenNavigation ( ! openNavigation ) ;
2744 const handleClick = ( ) => {
0 commit comments