11/* eslint-disable import/no-extraneous-dependencies */
2- import React , { useState } from "react" ;
3- import { NavLink } from "react-router-dom" ;
2+ import React , { useState , useEffect } from "react" ;
3+ import { NavLink , useHistory } from "react-router-dom" ;
44import SideDrawer from "../SideDrawer/SideDrawer" ;
55import {
66 NavBg ,
@@ -13,32 +13,45 @@ import {
1313} from "./navbar.style" ;
1414import logo from "../../static/logo.svg" ;
1515
16- export default function NavBar ( ) {
17- const links = [
18- {
19- title : "Getting Started" ,
20- link : "/getting-started" ,
21- } ,
22- {
23- title : "Demo" ,
24- link : "/demo" ,
25- } ,
26- {
27- title : "About" ,
28- link : "/about" ,
29- } ,
30- {
31- title : "Github" ,
32- link : "https://github.com/opensource9ja/dnotebook-react" ,
33- } ,
34- ] ;
16+ const links = [
17+ {
18+ title : "Getting Started" ,
19+ link : "/getting-started" ,
20+ } ,
21+ {
22+ title : "Demo" ,
23+ link : "/demo" ,
24+ } ,
25+ {
26+ title : "About" ,
27+ link : "/about" ,
28+ } ,
29+ {
30+ title : "Github" ,
31+ link : "https://github.com/opensource9ja/dnotebook-react" ,
32+ } ,
33+ ] ;
3534
35+ export default function NavBar ( ) {
36+ const history = useHistory ( ) ;
37+ const { pathname } = history . location ;
3638 const [ openDrawer , setOpenDrawer ] = useState ( false ) ;
39+ const [ active , setActive ] = useState ( "/getting-started" ) ;
3740
3841 const handleDrawer = ( ) => {
3942 setOpenDrawer ( ( ) => ! openDrawer ) ;
4043 } ;
41-
44+ useEffect ( ( ) => {
45+ if ( pathname . includes ( "getting-started" || pathname === "/" ) ) {
46+ setActive ( "/getting-started" ) ;
47+ }
48+ if ( pathname . includes ( "demo" ) ) {
49+ setActive ( "/demo" ) ;
50+ }
51+ if ( pathname . includes ( "about" ) ) {
52+ setActive ( "/about" ) ;
53+ }
54+ } , [ pathname ] ) ;
4255 return (
4356 < >
4457 < NavBg >
@@ -68,7 +81,19 @@ export default function NavBar() {
6881 { item . link . includes ( ":" ) ? (
6982 < a href = { item . link } > { item . title } </ a >
7083 ) : (
71- < NavLink to = { item . link } > { item . title } </ NavLink >
84+ < NavLink onClick = { ( ) => setActive ( item . link ) } to = { item . link } >
85+ { active === item . link ? (
86+ < div
87+ style = { {
88+ color : " #ffdf28" ,
89+ } }
90+ >
91+ { item . title }
92+ </ div >
93+ ) : (
94+ < div > { item . title } </ div >
95+ ) }
96+ </ NavLink >
7297 ) }
7398 </ NavLinkItem >
7499 ) ) }
0 commit comments