1- import React , { useState } from "react" ;
1+ import React , { useState , useEffect } from "react" ;
22import { Link , useNavigate , useLocation } from "react-router-dom" ;
33import { Button } from "@/components/ui/button" ;
4- import { Menu , X } from "lucide-react" ;
4+ import { Menu , X , Moon , Sun } from "lucide-react" ;
55
66const Navbar : React . FC = ( ) => {
77 const [ isOpen , setIsOpen ] = useState ( false ) ;
8+
9+ const getInitialTheme = ( ) =>
10+ typeof document !== "undefined" && document . documentElement . classList . contains ( "dark" )
11+ ? ( "dark" as const )
12+ : ( "light" as const ) ;
13+
14+ const [ theme , setTheme ] = useState < "light" | "dark" > ( getInitialTheme ) ;
815 const navigate = useNavigate ( ) ;
916 const location = useLocation ( ) ;
1017
18+ useEffect ( ( ) => {
19+ const root = window . document . documentElement ;
20+ if ( theme === "dark" ) root . classList . add ( "dark" ) ;
21+ else root . classList . remove ( "dark" ) ;
22+ try {
23+ localStorage . setItem ( "theme" , theme ) ;
24+ } catch ( err ) {
25+ // ignore (e.g. storage not available)
26+ }
27+ } , [ theme ] ) ;
28+
29+ const toggleTheme = ( ) => setTheme ( ( prev ) => ( prev === "dark" ? "light" : "dark" ) ) ;
30+
1131 const handleNavClick = ( path : string ) => {
1232 navigate ( path ) ;
1333 setIsOpen ( false ) ;
@@ -20,11 +40,11 @@ const Navbar: React.FC = () => {
2040 ] ;
2141
2242 return (
23- < nav className = "w-full bg-white/80 backdrop-blur-md shadow-sm fixed top-0 left-0 z-50" >
43+ < nav className = "w-full bg-white/80 dark:bg-gray-800/80 backdrop-blur-md shadow-sm fixed top-0 left-0 z-50 transition-colors " >
2444 < div className = "container mx-auto px-6 py-3 flex items-center justify-between" >
2545 < Link
2646 to = "/"
27- className = "text-2xl font-extrabold text-blue-800 tracking-tight hover:opacity-80 transition"
47+ className = "text-2xl font-extrabold text-blue-800 tracking-tight hover:opacity-80 transition dark:text-blue-300 "
2848 >
2949 UniLoot
3050 </ Link >
@@ -36,8 +56,8 @@ const Navbar: React.FC = () => {
3656 onClick = { ( ) => handleNavClick ( link . path ) }
3757 className = { `text-base font-semibold transition-colors duration-300 ${
3858 location . pathname === link . path
39- ? "text-blue-800"
40- : "text-gray-700 hover:text-blue-800"
59+ ? "text-blue-800 dark:text-blue-300 "
60+ : "text-gray-700 dark:text-gray-300 hover:text-blue-800 dark:hover:text-blue-300 "
4161 } `}
4262 >
4363 { link . name }
@@ -46,9 +66,20 @@ const Navbar: React.FC = () => {
4666 </ div >
4767
4868 < div className = "hidden md:flex items-center gap-3" >
69+ < button
70+ onClick = { toggleTheme }
71+ className = "p-2 rounded-full border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-all duration-300"
72+ >
73+ { theme === "light" ? (
74+ < Sun size = { 20 } className = "transition-transform duration-300 rotate-0 text-gray-700 dark:text-gray-200" />
75+ ) : (
76+ < Moon size = { 20 } className = "transition-transform duration-300 rotate-0 text-gray-700 dark:text-gray-200" />
77+ ) }
78+ </ button >
79+
4980 < Button
5081 variant = "outline"
51- className = "border-2 border-blue-800 text-blue-800 hover:bg-blue-800 hover:text-white transition"
82+ className = "border-2 border-blue-800 text-blue-800 hover:bg-blue-800 hover:text-white transition dark:text-blue-300 "
5283 onClick = { ( ) => handleNavClick ( "/signin" ) }
5384 >
5485 Sign In
@@ -63,28 +94,36 @@ const Navbar: React.FC = () => {
6394
6495 < button
6596 onClick = { ( ) => setIsOpen ( ! isOpen ) }
66- className = "md:hidden text-blue-800 focus:outline-none"
97+ className = "md:hidden text-blue-800 dark:text-blue-300 focus:outline-none"
6798 >
6899 { isOpen ? < X size = { 28 } /> : < Menu size = { 28 } /> }
69100 </ button >
70101 </ div >
71102
72103 { isOpen && (
73- < div className = "md:hidden bg-white border-t border-gray-200 shadow-md" >
104+ < div className = "md:hidden bg-white dark:bg-slate-900 border-t border-gray-200 dark:border-slate-700 shadow-md" >
74105 < div className = "flex flex-col items-center py-4 space-y-4" >
75106 { navLinks . map ( ( link ) => (
76107 < button
77108 key = { link . name }
78109 onClick = { ( ) => handleNavClick ( link . path ) }
79110 className = { `text-lg font-semibold ${
80111 location . pathname === link . path
81- ? "text-blue-800"
82- : "text-gray-700 hover:text-blue-800"
112+ ? "text-blue-800 dark:text-blue-300 "
113+ : "text-gray-700 dark:text-gray-300 hover:text-blue-800 dark:hover:text-blue-300 "
83114 } `}
84115 >
85116 { link . name }
86117 </ button >
87118 ) ) }
119+
120+ < button
121+ onClick = { toggleTheme }
122+ className = "p-2 rounded-full border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800 transition-all duration-300"
123+ >
124+ { theme === "light" ? < Sun size = { 22 } /> : < Moon size = { 22 } /> }
125+ </ button >
126+
88127 < div className = "flex flex-col gap-2 pt-3 w-4/5" >
89128 < Button
90129 variant = "outline"
0 commit comments