GooseUI

Components

Searchbar

New

A text field that loads suggestions from any source while the user types. Requests are debounced, a stale request is aborted as soon as the query changes, and the list follows the ARIA combobox pattern. JavaScript is needed for the network requests. A native <datalist> is not used as the base: it cannot render custom items, show loading and error states or hand back the picked object, and browsers filter its options again on their own.

Browser Support

interpolate-size

Animates the results list height

Your browser:

Preview

Type two letters of a European city. Use the arrow keys and Enter, or click an option.

Installation

npx shadcn@latest add @gooseui/searchbar
pnpm dlx shadcn@latest add @gooseui/searchbar
yarn dlx shadcn@latest add @gooseui/searchbar
bunx --bun shadcn@latest add @gooseui/searchbar

Usage

Pass a fetcher that takes the query and an AbortSignal. Hand the signal to fetch, and a request that is no longer needed gets cancelled.

import { Searchbar } from "@/components/ui/searchbar"

type City = { id: string; name: string }

async function searchCities(query: string, signal: AbortSignal) {
  const res = await fetch(`/api/cities?q=${encodeURIComponent(query)}`, { signal })
  if (!res.ok) throw new Error("Request failed")
  return (await res.json()) as City[]
}

export default function Example() {
  return (
    <Searchbar<City>
      aria-label="City"
      placeholder="Search a city…"
      fetcher={searchCities}
      getItemLabel={(city) => city.name}
      getItemKey={(city) => city.id}
      onSelect={(city) => console.log(city.id)}
    />
  )
}

Examples

Custom option layout

renderItem gets the item and the current query, so you can highlight the match or show extra details.

<Searchbar<City>
  fetcher={searchCities}
  getItemLabel={(city) => city.name}
  renderItem={(city, { query }) => (
    <div className="flex items-center gap-3">
      <MapPin className="size-4 text-muted-foreground" />
      <div className="flex-1">
        <Highlight text={city.name} query={query} />
        <div className="text-xs text-muted-foreground">{city.country}</div>
      </div>
    </div>
  )}
/>

Loading, empty and error states

Here requests start from three letters and fail while the checkbox is on. Turn it off and type again to see results or the empty state.

<Searchbar
  fetcher={searchCities}
  minLength={3}
  debounce={400}
  labels={{
    loading: "Searching…",
    empty: "No cities match",
    error: "Server did not respond",
  }}
/>

Controlled value

Control the text with value and onValueChange. After a pick the list stays closed until the text is edited.

const [query, setQuery] = useState("")
const [city, setCity] = useState<City | null>(null)

<Searchbar<City>
  value={query}
  onValueChange={(next) => {
    setQuery(next)
    setCity(null)
  }}
  onSelect={setCity}
  fetcher={searchCities}
  getItemLabel={(item) => item.name}
/>

Props

PropTypeDefaultDescription
fetcher(query: string, signal: AbortSignal) => Promise<T[]>-Loads suggestions for the trimmed query. Reject or abort when the signal fires
valuestring-Controlled text value
defaultValuestring""Initial text for uncontrolled use
onValueChange(value: string) => void-Called on every text change, including a pick
onSelect(item: T) => void-Called with the picked item
getItemLabel(item: T) => stringString(item)Text for an item. It goes into the field after a pick
getItemKey(item: T, index: number) => string-Stable key for an item
renderItem(item: T, state: { active, query }) => ReactNode-Custom content of an option
debouncenumber250Delay before a request, ms
minLengthnumber2Minimum query length that triggers a request
labels{ loading?, empty?, error? }-Texts for the loading, empty and error states
classNamestring-Classes for the wrapper
inputClassNamestring-Classes for the input
listClassNamestring-Classes for the popup
...propsComponentProps<"input">-Other input attributes: placeholder, name, disabled, aria-label

Features

  • Animation: transitions respect reduced motion preferences
  • Debounce: a request goes out only after the user pauses typing
  • Cancelling: the previous request is aborted through AbortController, so an old answer never replaces a new one
  • Keyboard: arrow keys move through options, Enter picks, Esc closes, aria-activedescendant points at the active option
  • Closing: the list closes when the field loses focus; after a pick it does not reopen until the text changes
  • States: loading, nothing found and error are announced through a live region