import { useState, useRef, useEffect } from "react" import { useNavigate } from "react-router-dom" import { motion, AnimatePresence } from "framer-motion" import { SearchIcon, X, TrendingUp, Clock, Star } from "lucide-react" import "./Search.css" export default function Search() { const [keyword, setKeyword] = useState("") const [isFocused, setIsFocused] = useState(false) const [showSuggestions, setShowSuggestions] = useState(false) const [suggestions, setSuggestions] = useState([]) const [recentSearches, setRecentSearches] = useState(["iPhone 14", "Samsung Galaxy", "MacBook Pro", "AirPods"]) const [trendingSearches] = useState(["Gaming Laptop", "Wireless Headphones", "Smart Watch", "4K Monitor"]) const navigate = useNavigate() const searchRef = useRef(null) const inputRef = useRef(null) useEffect(() => { const handleClickOutside = (event) => { if (searchRef.current && !searchRef.current.contains(event.target)) { setShowSuggestions(false) setIsFocused(false) } } document.addEventListener("mousedown", handleClickOutside) return () => document.removeEventListener("mousedown", handleClickOutside) }, []) const searchHandler = (e) => { e.preventDefault() if (keyword.trim()) { const updatedRecent = [keyword, ...recentSearches.filter((item) => item !== keyword)].slice(0, 4) setRecentSearches(updatedRecent) navigate(`/productlist?keyword=${keyword}`) setShowSuggestions(false) setIsFocused(false) inputRef.current?.blur() } } const handleInputChange = (e) => { const value = e.target.value setKeyword(value) if (value.length > 0) { const mockSuggestions = [`${value} pro`, `${value} max`, `${value} mini`, `best ${value}`, `${value} 2025`].slice( 0, 3, ) setSuggestions(mockSuggestions) } else { setSuggestions([]) } } const handleSuggestionClick = (suggestion) => { setKeyword(suggestion) navigate(`/productlist?keyword=${suggestion}`) setShowSuggestions(false) setIsFocused(false) } const clearSearch = () => { setKeyword("") inputRef.current?.focus() } return (
{ setIsFocused(true) setShowSuggestions(true) }} className="Premium-search-input" placeholder="Search for products..." autoComplete="off" /> {keyword && ( )}
Search
{} {showSuggestions && isFocused && ( {} {suggestions.length > 0 && (
Suggestions
{suggestions.map((suggestion, index) => ( handleSuggestionClick(suggestion)} initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: index * 0.05 }} whileHover={{ x: 5, backgroundColor: "rgba(255, 255, 255, 0.1)" }} > {suggestion} ))}
)} {} {recentSearches.length > 0 && keyword.length === 0 && (
Recent Searches
{recentSearches.map((search, index) => ( handleSuggestionClick(search)} initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: index * 0.05 }} whileHover={{ x: 5, backgroundColor: "rgba(255, 255, 255, 0.1)" }} > {search} ))}
)} {} {keyword.length === 0 && (
Trending Now
{trendingSearches.map((trend, index) => ( handleSuggestionClick(trend)} initial={{ opacity: 0, x: -20 }} animate={{ opacity: 1, x: 0 }} transition={{ delay: index * 0.05 }} whileHover={{ x: 5, backgroundColor: "rgba(255, 107, 107, 0.1)" }} > {trend} ))}
)} {}
navigate("/productlist")} >
📱
Electronics
navigate("/productlist")} >
👕
Fashion
navigate("/productlist")} >
🏠
Home
navigate("/productlist")} >
🎮
Gaming
)}
) }