The Coline design system: accessible React components for Coline Apps, themed live by the host workspace, and for Coline Sites in light and dark. Tailwind v4 stylesheet included. MIT.
| 5 | import { IconSearch, IconX } from "@tabler/icons-react"; |
| 6 | import { cn } from "../lib/utils"; |
| 7 | import { Spinner } from "./spinner"; |
| 8 |
| 9 | /** |
| 10 | * Search field with an icon, a clear button that appears with text, an |
| 11 | * optional loading state, and debounced `onSearch`. Escape clears it. |
| 12 | */ |
| 13 | export function SearchInput({ |
| 14 | value, |
| 15 | onValueChange, |
| 16 | onSearch, |
| 17 | debounceMs = 250, |
| 18 | placeholder = "Search", |
| 19 | loading = false, |
| 20 | className, |
| 21 | autoFocus, |
| 22 | }: { |
| 23 | value: string; |
| 24 | onValueChange: (value: string) => void; |
| 25 | /** Called after typing pauses, and immediately on Enter. */ |
| 26 | onSearch?: (value: string) => void; |
| 27 | debounceMs?: number; |
| 28 | placeholder?: string; |
| 29 | loading?: boolean; |
| 30 | className?: string; |
| 31 | autoFocus?: boolean; |
| 32 | }) { |
| 33 | const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null); |
| 34 | const inputRef = React.useRef<HTMLInputElement | null>(null); |
| 35 | React.useEffect(() => () => { |
| 36 | if (timer.current) clearTimeout(timer.current); |
| 37 | }, []); |
| 38 | const schedule = (next: string) => { |
| 39 | if (!onSearch) return; |
| 40 | if (timer.current) clearTimeout(timer.current); |
| 41 | timer.current = setTimeout(() => onSearch(next), debounceMs); |
| 42 | }; |
| 43 | // Immediate searches (Enter, Escape, Clear) cancel the pending one, so a |
| 44 | // query typed just before clearing cannot fire afterwards. |
| 45 | const searchNow = (next: string) => { |
| 46 | if (timer.current) clearTimeout(timer.current); |
| 47 | timer.current = null; |
| 48 | onSearch?.(next); |
| 49 | }; |
| 50 | return ( |
| 51 | <div |
| 52 | className={cn( |
| 53 | "group/search relative flex h-9 w-full min-w-0 items-center rounded-4xl border border-input bg-input/30 transition-[border-color,box-shadow] duration-150 focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/20", |
| 54 | className, |
| 55 | )} |
| 56 | > |
| 57 | <IconSearch aria-hidden="true" className="pointer-events-none ml-3 size-4 shrink-0 text-muted-foreground" /> |
| 58 | <input |
| 59 | ref={inputRef} |
| 60 | type="search" |
| 61 | value={value} |
| 62 | placeholder={placeholder} |
| 63 | aria-label={placeholder} |
| 64 | autoFocus={autoFocus} |
| 65 | onChange={(event) => { |
| 66 | onValueChange(event.target.value); |
| 67 | schedule(event.target.value); |
| 68 | }} |
| 69 | onKeyDown={(event) => { |
| 70 | if (event.key === "Enter") { |
| 71 | searchNow(value); |
| 72 | } else if (event.key === "Escape" && value) { |
| 73 | event.preventDefault(); |
| 74 | onValueChange(""); |
| 75 | searchNow(""); |
| 76 | } |
| 77 | }} |
| 78 | className="h-full min-w-0 flex-1 bg-transparent px-2 text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden" |
| 79 | /> |
| 80 | <span className="mr-1.5 flex size-6 shrink-0 items-center justify-center"> |
| 81 | <AnimatePresence initial={false} mode="popLayout"> |
| 82 | {loading ? ( |
| 83 | <motion.span key="loading" initial={{ opacity: 0, scale: 0.6 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.6 }} className="flex text-muted-foreground"> |
| 84 | <Spinner size="sm" label="Searching" /> |
| 85 | </motion.span> |
| 86 | ) : value ? ( |
| 87 | <motion.button |
| 88 | key="clear" |
| 89 | type="button" |
| 90 | aria-label="Clear search" |
| 91 | initial={{ opacity: 0, scale: 0.6 }} |
| 92 | animate={{ opacity: 1, scale: 1 }} |
| 93 | exit={{ opacity: 0, scale: 0.6 }} |
| 94 | onClick={() => { |
| 95 | onValueChange(""); |
| 96 | searchNow(""); |
| 97 | inputRef.current?.focus(); |
| 98 | }} |
| 99 | className="flex size-6 items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-muted hover:text-foreground" |
| 100 | > |
| 101 | <IconX className="size-3.5" /> |
| 102 | </motion.button> |
| 103 | ) : null} |
| 104 | </AnimatePresence> |
| 105 | </span> |
| 106 | </div> |
| 107 | ); |
| 108 | } |
| 109 |