Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 'use client'; /** * MobileSearch Component * * A full-screen mobile search overlay with voice search support. */ import { useState, useRef, useEffect, useCallback } from 'react'; // Web Speech API type declarations interface SpeechRecognitionResult { readonly isFinal: boolean; readonly length: number; item(index: number): SpeechRecognitionAlternative; [index: number]: SpeechRecognitionAlternative; } interface SpeechRecognitionAlternative { readonly transcript: string; readonly confidence: number; } interface SpeechRecognitionResultList { readonly length: number; item(index: number): SpeechRecognitionResult; [index: number]: SpeechRecognitionResult; } interface SpeechRecognitionEvent extends Event { readonly resultIndex: number; readonly results: SpeechRecognitionResultList; } interface SpeechRecognitionInstance { lang: string; interimResults: boolean; maxAlternatives: number; onstart: (() => void) | null; onend: (() => void) | null; onresult: ((event: SpeechRecognitionEvent) => void) | null; onerror: ((event: Event) => void) | null; start: () => void; stop: () => void; abort: () => void; } interface SpeechRecognitionConstructor { new (): SpeechRecognitionInstance; } declare global { interface Window { SpeechRecognition?: SpeechRecognitionConstructor; webkitSpeechRecognition?: SpeechRecognitionConstructor; } } import { useRouter } from 'next/navigation'; import { Icon } from '@/components/ui/icons'; import { cn } from '@/lib/core'; interface MobileSearchProps { isOpen: boolean; onClose: () => void; } export function MobileSearch({ isOpen, onClose }: MobileSearchProps) { const [searchQuery, setSearchQuery] = useState(''); const [isListening, setIsListening] = useState(false); const inputRef = useRef<HTMLInputElement>(null); const router = useRouter(); // Check for voice search support - computed on client side const voiceSupported = typeof window !== 'undefined' && ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window); // Focus input when opened useEffect(() => { if (isOpen && inputRef.current) { inputRef.current.focus(); } }, [isOpen]); // Handle escape key useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape' && isOpen) { onClose(); } }; document.addEventListener('keydown', handleEscape); return () => document.removeEventListener('keydown', handleEscape); }, [isOpen, onClose]); const handleSearch = useCallback( (e: React.FormEvent) => { e.preventDefault(); const trimmedQuery = searchQuery.trim(); if (trimmedQuery.length >= 2) { router.push(`/shop?query=${encodeURIComponent(trimmedQuery)}`); setSearchQuery(''); onClose(); } }, [searchQuery, router, onClose] ); const startVoiceSearch = useCallback(() => { if (!voiceSupported) return; const SpeechRecognitionAPI = window.SpeechRecognition || window.webkitSpeechRecognition; if (!SpeechRecognitionAPI) return; const recognition = new SpeechRecognitionAPI(); recognition.lang = 'en-US'; recognition.interimResults = false; recognition.maxAlternatives = 1; recognition.onstart = () => setIsListening(true); recognition.onend = () => setIsListening(false); recognition.onresult = (event: SpeechRecognitionEvent) => { const transcript = event.results[0][0].transcript; setSearchQuery(transcript); }; recognition.onerror = () => { setIsListening(false); }; recognition.start(); }, [voiceSupported]); if (!isOpen) return null; return ( <div className={cn( 'fixed inset-0 z-50', 'bg-white dark:bg-gray-900', 'flex flex-col' )} role="dialog" aria-modal="true" aria-label="Search" > {/* Header */} <div className="flex items-center gap-3 px-4 py-3 border-b border-gray-200 dark:border-gray-800"> <button onClick={onClose} className="p-2 -ml-2 min-h-[44px] min-w-[44px] flex items-center justify-center rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors" aria-label="Close search" > <Icon name="arrow-left" size={24} /> </button> <form onSubmit={handleSearch} className="flex-1"> <div className="relative"> <input ref={inputRef} type="search" value={searchQuery} onChange={(e) => setSearchQuery(e.target.value)} placeholder="Search products..." className={cn( 'w-full h-12 pl-12 pr-12 text-lg', 'bg-gray-100 dark:bg-gray-800', 'border-none rounded-full', 'text-gray-900 dark:text-white', 'placeholder:text-gray-500 dark:placeholder:text-gray-400', 'focus:outline-none focus:ring-2 focus:ring-primary-500' )} autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false" /> <Icon name="search" size={20} className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400" /> {searchQuery && ( <button type="button" onClick={() => setSearchQuery('')} className="absolute right-4 top-1/2 -translate-y-1/2 p-1 text-gray-400 hover:text-gray-600" aria-label="Clear search" > <Icon name="close" size={20} /> </button> )} </div> </form> {/* Voice search button */} {voiceSupported && ( <button onClick={startVoiceSearch} className={cn( 'p-3 min-h-[44px] min-w-[44px] rounded-full transition-colors', isListening ? 'bg-red-100 dark:bg-red-900/30 text-red-600 animate-pulse' : 'bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700' )} aria-label={isListening ? 'Listening...' : 'Voice search'} > <Icon name={isListening ? 'mic-off' : 'mic'} size={24} /> </button> )} </div> {/* Search suggestions / recent searches */} <div className="flex-1 overflow-y-auto p-4"> {isListening ? ( <div className="flex flex-col items-center justify-center h-full"> <div className="w-24 h-24 rounded-full bg-red-100 dark:bg-red-900/30 flex items-center justify-center mb-4 animate-pulse"> <Icon name="mic" size={48} className="text-red-600" /> </div> <p className="text-lg font-medium text-gray-900 dark:text-white"> Listening... </p> <p className="text-sm text-gray-500 dark:text-gray-400 mt-1"> Speak now </p> </div> ) : ( <div> <p className="text-sm text-gray-500 dark:text-gray-400 mb-4"> {searchQuery.length > 0 ? `Press enter to search for "${searchQuery}"` : 'Start typing to search...'} </p> {/* Quick search suggestions */} <div className="space-y-2"> <p className="text-xs font-medium text-gray-400 uppercase tracking-wider mb-3"> Popular Searches </p> {['Party supplies', 'Decorations', 'Balloons', 'Tableware'].map( (suggestion) => ( <button key={suggestion} onClick={() => { router.push(`/shop?query=${encodeURIComponent(suggestion)}`); onClose(); }} className={cn( 'flex items-center gap-3 w-full p-3', 'rounded-lg hover:bg-gray-100 dark:hover:bg-gray-800', 'text-left transition-colors' )} > <Icon name="search" size={20} className="text-gray-400" /> <span className="text-gray-900 dark:text-white">{suggestion}</span> </button> ) )} </div> </div> )} </div> </div> ); } export default MobileSearch; |