Spaces:
Sleeping
Sleeping
File size: 1,726 Bytes
9dfccd9 8900d0e 9dfccd9 8900d0e 9dfccd9 8900d0e 9dfccd9 | 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 | import { useEffect, useRef } from 'react'
import { cn } from '@/lib/utils'
interface Props {
onSubmit: (query: string) => void
disabled?: boolean
className?: string
defaultValue?: string
}
export function SearchBox({ onSubmit, disabled, className, defaultValue }: Props) {
const ref = useRef<HTMLInputElement>(null)
// Cmd+K / Ctrl+K → focus from anywhere
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault()
ref.current?.focus()
}
}
window.addEventListener('keydown', handler)
return () => window.removeEventListener('keydown', handler)
}, [])
const submit = () => {
const value = ref.current?.value.trim()
if (value) {
onSubmit(value)
if (ref.current) ref.current.value = ''
}
}
return (
<div className={cn('flex w-full items-center gap-2 rounded-xl border border-surface-subtle bg-white px-4 py-3 shadow-sm focus-within:border-brand dark:bg-stone-900', className)}>
<span className="text-stone-400" aria-hidden>⌕</span>
<input
ref={ref}
type="text"
defaultValue={defaultValue}
placeholder="Ask anything… (⌘K)"
disabled={disabled}
onKeyDown={(e) => e.key === 'Enter' && submit()}
className="flex-1 bg-transparent text-sm outline-none placeholder:text-stone-400 disabled:opacity-60"
aria-label="Query input"
/>
<button
onClick={submit}
disabled={disabled}
className="rounded-lg bg-brand px-3 py-1.5 text-xs font-medium text-white hover:bg-brand-dark disabled:opacity-60"
>
Ask
</button>
</div>
)
}
|