|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | +import { Link, useNavigate } from 'react-router-dom'; |
| 3 | +import { Button } from './button'; |
| 4 | +import { Menu, X, Atom } from 'lucide-react'; |
| 5 | + |
| 6 | +const Navbar = () => { |
| 7 | + const [isScrolled, setIsScrolled] = useState(false); |
| 8 | + const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); |
| 9 | + const navigate = useNavigate(); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + const handleScroll = () => { |
| 13 | + setIsScrolled(window.scrollY > 20); |
| 14 | + }; |
| 15 | + window.addEventListener('scroll', handleScroll); |
| 16 | + return () => window.removeEventListener('scroll', handleScroll); |
| 17 | + }, []); |
| 18 | + |
| 19 | + const scrollToSection = (sectionId: string) => { |
| 20 | + const element = document.getElementById(sectionId); |
| 21 | + if (element) { |
| 22 | + element.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
| 23 | + setIsMobileMenuOpen(false); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + const handleNavClick = (path: string, sectionId?: string) => { |
| 28 | + if (window.location.pathname !== '/') { |
| 29 | + navigate('/'); |
| 30 | + setTimeout(() => { |
| 31 | + if (sectionId) { |
| 32 | + scrollToSection(sectionId); |
| 33 | + } |
| 34 | + }, 100); |
| 35 | + } else if (sectionId) { |
| 36 | + scrollToSection(sectionId); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const navLinks = [ |
| 41 | + { name: 'Home', action: () => navigate('/') }, |
| 42 | + { name: 'Simulation', action: () => navigate('/simulation') }, |
| 43 | + { name: 'Theory', action: () => handleNavClick('/', 'theory') }, |
| 44 | + { name: 'Meet the Team', action: () => window.location.href = `${import.meta.env.BASE_URL}team.html` }, |
| 45 | + { name: 'Quiz', action: () => navigate('/quiz') }, |
| 46 | + { name: 'Contact', action: () => handleNavClick('/', 'contact') }, |
| 47 | + ]; |
| 48 | + |
| 49 | + return ( |
| 50 | + <nav |
| 51 | + className={`fixed top-0 left-0 right-0 z-50 transition-all duration-300 ${ |
| 52 | + isScrolled |
| 53 | + ? 'bg-background/98 backdrop-blur-xl border-b border-primary/30 shadow-2xl shadow-primary/10' |
| 54 | + : 'bg-gradient-to-b from-background/80 to-transparent backdrop-blur-sm' |
| 55 | + }`} |
| 56 | + > |
| 57 | + <div className="container mx-auto px-4"> |
| 58 | + <div className="flex items-center justify-between h-18 py-3"> |
| 59 | + |
| 60 | + {/* Logo */} |
| 61 | + <Link |
| 62 | + to="/" |
| 63 | + className="flex items-center gap-3 group" |
| 64 | + > |
| 65 | + <div className="relative"> |
| 66 | + <Atom className="w-9 h-9 text-primary group-hover:rotate-180 transition-transform duration-500" /> |
| 67 | + <div className="absolute inset-0 blur-xl bg-primary/40 group-hover:bg-primary/60 transition-all" /> |
| 68 | + </div> |
| 69 | + <span className="text-xl font-bold gradient-text hidden sm:block"> |
| 70 | + BB84 Quantum |
| 71 | + </span> |
| 72 | + </Link> |
| 73 | + |
| 74 | + {/* Desktop Navigation */} |
| 75 | + <div className="hidden md:flex items-center gap-2"> |
| 76 | + {navLinks.map((link) => ( |
| 77 | + <Button |
| 78 | + key={link.name} |
| 79 | + variant="ghost" |
| 80 | + className="text-sm font-semibold hover:text-primary hover:bg-primary/10 transition-all px-4 py-2 rounded-lg border border-transparent hover:border-primary/30" |
| 81 | + onClick={link.action} |
| 82 | + > |
| 83 | + {link.name} |
| 84 | + </Button> |
| 85 | + ))} |
| 86 | + </div> |
| 87 | + |
| 88 | + {/* Mobile Menu Button */} |
| 89 | + <Button |
| 90 | + variant="ghost" |
| 91 | + size="icon" |
| 92 | + className="md:hidden" |
| 93 | + onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)} |
| 94 | + > |
| 95 | + {isMobileMenuOpen ? ( |
| 96 | + <X className="w-6 h-6" /> |
| 97 | + ) : ( |
| 98 | + <Menu className="w-6 h-6" /> |
| 99 | + )} |
| 100 | + </Button> |
| 101 | + </div> |
| 102 | + |
| 103 | + {/* Mobile Menu */} |
| 104 | + {isMobileMenuOpen && ( |
| 105 | + <div className="md:hidden pb-4 animate-in slide-in-from-top duration-300"> |
| 106 | + <div className="flex flex-col gap-2 pt-2"> |
| 107 | + {navLinks.map((link) => ( |
| 108 | + <Button |
| 109 | + key={link.name} |
| 110 | + variant="ghost" |
| 111 | + className="justify-start text-base font-medium hover:text-primary hover:bg-primary/10" |
| 112 | + onClick={() => { |
| 113 | + link.action(); |
| 114 | + setIsMobileMenuOpen(false); |
| 115 | + }} |
| 116 | + > |
| 117 | + {link.name} |
| 118 | + </Button> |
| 119 | + ))} |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + </nav> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default Navbar; |
0 commit comments