'use client' import { useState, useEffect, useRef } from 'react' import { cn } from '@/lib/utils' import { colors, effects } from '@/lib/theme' import { Search, X } from 'lucide-react' import type { DocItem } from '@/lib/docs/types' interface DocsSearchProps { items: DocItem[] onSearch: (query: string) => void className?: string } export default function DocsSearch({ items, onSearch, className, }: DocsSearchProps) { const [query, setQuery] = useState('') const [isFocused, setIsFocused] = useState(false) const inputRef = useRef(null) // Keyboard shortcut (Cmd/Ctrl + K) useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key === 'k') { e.preventDefault() inputRef.current?.focus() } if (e.key === 'Escape') { inputRef.current?.blur() setQuery('') onSearch('') } } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }, [onSearch]) const handleChange = (value: string) => { setQuery(value) onSearch(value) } const handleClear = () => { setQuery('') onSearch('') inputRef.current?.focus() } return (
{ if (!isFocused) { e.currentTarget.style.borderColor = colors.borders.hover } }} onMouseLeave={(e) => { if (!isFocused) { e.currentTarget.style.borderColor = colors.borders.default } }} > handleChange(e.target.value)} onFocus={() => setIsFocused(true)} onBlur={() => setIsFocused(false)} placeholder="Search documentation..." className={cn( 'w-full bg-transparent px-10 py-3', 'text-sm outline-none' )} style={{ color: colors.text.primary, caretColor: colors.text.secondary }} /> {query ? ( ) : ( ⌘K )}
{query && (
{items.length} result{items.length !== 1 ? 's' : ''} found
)}
) }