Spaces:
Running
Running
File size: 5,895 Bytes
cc2caf9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
import React, { useState, useEffect, useRef } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { Search, Bell, User, Menu, X } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
const Navbar = () => {
const [isScrolled, setIsScrolled] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);
const [searchVisible, setSearchVisible] = useState(false);
const [searchTerm, setSearchTerm] = useState('');
const searchInputRef = useRef<HTMLInputElement>(null);
const navigate = useNavigate();
useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 50) {
setIsScrolled(true);
} else {
setIsScrolled(false);
}
};
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, []);
// Focus the search input when it becomes visible
useEffect(() => {
if (searchVisible && searchInputRef.current) {
setTimeout(() => {
searchInputRef.current?.focus();
}, 200);
}
}, [searchVisible]);
const toggleMenu = () => {
setIsMenuOpen(!isMenuOpen);
};
const toggleSearch = () => {
setSearchVisible(!searchVisible);
};
const handleSearchSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (searchTerm.trim()) {
navigate(`/search?q=${encodeURIComponent(searchTerm)}`);
setSearchVisible(false);
setSearchTerm('');
}
};
return (
<nav
className={`fixed top-0 left-0 w-full z-50 transition-all duration-300 px-4 sm:px-6 ${
isScrolled ? 'bg-theme-background-dark shadow-lg py-2' : 'bg-gradient-to-b from-black/80 to-transparent py-4'
}`}
>
<div className="flex items-center justify-between">
{/* Logo */}
<Link to="/" className="flex items-center">
<h1 className="text-theme-primary text-2xl sm:text-3xl font-bold">NEXORA</h1>
</Link>
{/* Desktop Nav Links */}
<div className="hidden md:flex items-center space-x-6">
<Link to="/" className="text-white hover:text-theme-primary transition-colors">Home</Link>
<Link to="/movies" className="text-white hover:text-theme-primary transition-colors">Movies</Link>
<Link to="/tv-shows" className="text-white hover:text-theme-primary transition-colors">TV Shows</Link>
<Link to="/my-list" className="text-white hover:text-theme-primary transition-colors">My List</Link>
</div>
{/* Right side icons */}
<div className="flex items-center space-x-4">
{/* Search bar with animation */}
<div className="relative flex items-center">
<AnimatePresence>
{searchVisible ? (
<motion.form
onSubmit={handleSearchSubmit}
className="flex items-center"
initial={{ width: 0, opacity: 0 }}
animate={{ width: 200, opacity: 1 }}
exit={{ width: 0, opacity: 0 }}
transition={{ duration: 0.2 }}
>
<input
ref={searchInputRef}
type="text"
placeholder="Titles, people, genres"
className="bg-theme-background-dark border border-theme-border rounded px-3 py-1
focus:outline-none focus:border-theme-primary text-white w-full"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
<button
type="button"
onClick={toggleSearch}
className="ml-2 text-white hover:text-theme-primary"
>
<X className="w-5 h-5" />
</button>
</motion.form>
) : (
<button
onClick={toggleSearch}
className="hover:text-theme-primary transition-colors"
>
<Search className="w-5 h-5" />
</button>
)}
</AnimatePresence>
</div>
<button className="hover:text-theme-primary transition-colors hidden sm:block">
<Bell className="w-5 h-5" />
</button>
<Link to="/profile" className="hover:text-theme-primary transition-colors hidden sm:block">
<User className="w-5 h-5" />
</Link>
{/* Mobile menu button */}
<button onClick={toggleMenu} className="md:hidden hover:text-theme-primary">
<Menu className="w-6 h-6" />
</button>
</div>
</div>
{/* Mobile menu */}
<AnimatePresence>
{isMenuOpen && (
<motion.div
className="md:hidden absolute top-full left-0 right-0 bg-theme-background-dark/95 border-t border-theme-border"
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
transition={{ duration: 0.3 }}
>
<div className="flex flex-col p-4 space-y-3">
<Link to="/" className="text-white py-2 hover:text-theme-primary">Home</Link>
<Link to="/movies" className="text-white py-2 hover:text-theme-primary">Movies</Link>
<Link to="/tv-shows" className="text-white py-2 hover:text-theme-primary">TV Shows</Link>
<Link to="/my-list" className="text-white py-2 hover:text-theme-primary">My List</Link>
<Link to="/profile" className="text-white py-2 hover:text-theme-primary">Profile</Link>
</div>
</motion.div>
)}
</AnimatePresence>
</nav>
);
};
export default Navbar;
|