# Product Search

> A storefront search field with live suggestions, categories and product results. Handles typos, slow or failing searches, recent searches and keyboard-only use.

Canonical: https://hextaui.com/blocks/product-search
Last updated: 2026-10-29

## Overview

Product Search is the search field in a store header. On a desktop it opens a dropdown under the field, and on a phone a full-screen sheet with the keyboard up. Before anything is typed it shows recent searches, trending searches and a row of popular products.

As the shopper types, it shows up to eight query suggestions with the predicted part in bold, category matches such as “linen in Bedding”, a few products with a thumbnail and price, and a final “Search for” row. Arrowing onto a suggestion copies it into the field so it can be edited, Escape puts back what was typed, and Tab completes the suggestion.

onSearch(query, { signal }) is called after a short debounce. Older requests are aborted, answers are cached so backspacing is instant, and the previous results stay on screen while the next ones load. The spinner only appears when a search takes longer than 150ms. A failed search shows its error message with a Try again row, and a search with no results offers “Did you mean” and popular categories.

Recent searches are kept in localStorage under recentStorageKey, or controlled with recent and onRecentChange. SearchResultsHeader is the matching heading for a results page, “Results for “vase” (4)”, with a Did you mean line. The fuzzy helpers in search.ts, fuzzySearch, completeQuery and correctQuery, power the demo and work for small catalogs on their own.

## Installation

### Step 1: Get HextaUI Pro

One payment unlocks every block, including future ones. [See pricing](https://hextaui.com/pricing).

### Step 2: Add the Pro registry to components.json

```json title="components.json"
{
  "registries": {
    "@hextaui-pro": {
      "url": "https://hextaui.com/r/pro/{name}.json",
      "headers": {
        "Authorization": "Bearer ${HEXTAUI_PRO_TOKEN}"
      }
    }
  }
}
```

### Step 3: Add your token

Create a token on your [account page](https://hextaui.com/account) and put it in `.env.local` as `HEXTAUI_PRO_TOKEN`.

### Step 4: Add the block

```bash
npx shadcn@latest add @hextaui-pro/product-search
```

## Usage

### Shopify predictive search

Call the Storefront API’s predictiveSearch with the signal ProductSearch passes in, and map queries, collections and products to SearchResults. Prices are already Money, so they go straight through.

```tsx
"use client"

import { ProductSearch } from "@/components/blocks/product-search/product-search"
import type { SearchFn } from "@/components/blocks/product-search/search"

const endpoint = "https://your-shop.myshopify.com/api/2025-07/graphql.json"

const query = `query Predictive($q: String!) {
  predictiveSearch(query: $q, limit: 6, types: [QUERY, COLLECTION, PRODUCT]) {
    queries { text }
    collections { id title handle }
    products {
      id title handle
      featuredImage { url altText }
      priceRange { minVariantPrice { amount currencyCode } }
      compareAtPriceRange { minVariantPrice { amount currencyCode } }
    }
  }
}`

type Money = { amount: string; currencyCode: string }

type PredictiveSearch = {
  queries: { text: string }[]
  collections: { id: string; title: string; handle: string }[]
  products: {
    id: string
    title: string
    handle: string
    featuredImage: { url: string; altText: string | null } | null
    priceRange: { minVariantPrice: Money }
    compareAtPriceRange: { minVariantPrice: Money }
  }[]
}

const search: SearchFn = async (q, { signal }) => {
  const response = await fetch(endpoint, {
    method: "POST",
    signal,
    headers: {
      "Content-Type": "application/json",
      "X-Shopify-Storefront-Access-Token": process.env.NEXT_PUBLIC_SHOPIFY_TOKEN ?? "",
    },
    body: JSON.stringify({ query, variables: { q } }),
  })
  if (!response.ok) throw new Error("Search is unavailable right now. Try again in a moment.")
  const { data } = (await response.json()) as {
    data: { predictiveSearch: PredictiveSearch }
  }
  const result = data.predictiveSearch
  return {
    suggestions: result.queries.map((item) => item.text),
    collections: result.collections.map((item) => ({
      id: item.id,
      title: item.title,
      href: `/collections/${item.handle}`,
    })),
    products: result.products.map((item) => ({
      id: item.id,
      title: item.title,
      href: `/products/${item.handle}`,
      image: item.featuredImage
        ? { src: `${item.featuredImage.url}&width=160`, alt: item.featuredImage.altText ?? "" }
        : undefined,
      price: item.priceRange.minVariantPrice,
      compareAt: item.compareAtPriceRange.minVariantPrice,
    })),
  }
}

export function StoreSearch() {
  return (
    <ProductSearch
      onSearch={search}
      onSubmit={(q) => window.location.assign(`/search?q=${encodeURIComponent(q)}`)}
      recentStorageKey="store:recent-searches"
      trending={["linen bedding", "ring vase"]}
    />
  )
}
```

### Algolia

One multi-query request fetches query suggestions and products together. Algolia’s typo tolerance and facet counts fill the suggestions, the “in Bedding” rows and the result count.

```tsx
"use client"

import { money } from "../price/money"
import { ProductSearch } from "@/components/blocks/product-search/product-search"
import type { SearchFn } from "@/components/blocks/product-search/search"

const appId = "YOUR_APP_ID"
const searchKey = "YOUR_SEARCH_ONLY_KEY"

type ProductHit = {
  objectID: string
  title: string
  handle: string
  image: string
  price: number
  collection: string
}

type SuggestionHit = { query: string }

type Response = {
  results: [
    { hits: SuggestionHit[] },
    { hits: ProductHit[]; nbHits: number; facets?: { collection?: Record<string, number> } },
  ]
}

const search: SearchFn = async (query, { signal }) => {
  const response = await fetch(`https://${appId}-dsn.algolia.net/1/indexes/*/queries`, {
    method: "POST",
    signal,
    headers: {
      "X-Algolia-Application-Id": appId,
      "X-Algolia-API-Key": searchKey,
    },
    body: JSON.stringify({
      requests: [
        { indexName: "products_query_suggestions", query, hitsPerPage: 8 },
        { indexName: "products", query, hitsPerPage: 4, facets: ["collection"], typoTolerance: true },
      ],
    }),
  })
  if (!response.ok) throw new Error("Search is unavailable right now. Try again in a moment.")
  const { results } = (await response.json()) as Response
  const [suggestions, products] = results
  return {
    suggestions: suggestions.hits.map((hit) => hit.query),
    collections: Object.entries(products.facets?.collection ?? {})
      .slice(0, 2)
      .map(([title, count]) => ({ id: title, title, count })),
    products: products.hits.map((hit) => ({
      id: hit.objectID,
      title: hit.title,
      href: `/products/${hit.handle}`,
      image: { src: hit.image },
      price: money(hit.price, "USD"),
    })),
    total: products.nbHits,
  }
}

export function AlgoliaSearch() {
  return <ProductSearch onSearch={search} recentStorageKey="recent-searches" />
}
```

### Next.js search page

Send Enter to /search?q= with the router, and seed the field from the URL so the results page echoes the query. Products and categories open their own routes.

```tsx
"use client"

import { useRouter, useSearchParams } from "next/navigation"

import { ProductSearch } from "@/components/blocks/product-search/product-search"
import type { SearchFn } from "@/components/blocks/product-search/search"

const search: SearchFn = async (query, { signal }) => {
  const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`, { signal })
  if (!response.ok) throw new Error("Search is unavailable right now. Try again in a moment.")
  return response.json()
}

export function HeaderSearch() {
  const router = useRouter()
  const params = useSearchParams()

  return (
    <ProductSearch
      key={params.get("q") ?? ""}
      defaultValue={params.get("q") ?? ""}
      onSearch={search}
      onSubmit={(query) => router.push(`/search?q=${encodeURIComponent(query)}`)}
      onSelectProduct={(product) => product.href && router.push(product.href)}
      onSelectCollection={(collection, query) =>
        router.push(`${collection.href}?q=${encodeURIComponent(query)}`)
      }
      recentStorageKey="recent-searches"
    />
  )
}
```

## Anatomy

The parts you compose, from the outside in.

| Part | Description |
| --- | --- |
| `ProductSearch` | The field, the ⌘K hint and the dropdown or full-screen sheet. |
| `SearchResultsHeader` | The results page heading with the count and a spelling suggestion. |
| `fuzzySearch, completeQuery, correctQuery` | Typo-tolerant matching, query completion and spelling fixes for small catalogs. |
| `splitCompletion, addRecent, removeRecent` | Split a suggestion into typed and predicted parts, and edit a recent searches list. |

## API reference

### ProductSearch

Also takes div props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onSearch` | `(query, { signal }) => Promise<SearchResults>` | – | Fetches results. Abort when signal fires. A thrown error’s message is shown in the dropdown. |
| `onSubmit` | `(query: string) => void` | – | Called when the shopper searches for text or picks a suggestion, recent or trending search. |
| `onSelectProduct` | `(product, query) => void` | – | Called when a product is picked. Without it, the product’s href opens. |
| `onSelectCollection` | `(collection, query) => void` | – | Called when a category is picked. Without it, the category’s href opens. |
| `value` | `string` | – | The query, when controlled. |
| `defaultValue` | `string` | `""` | The starting query. |
| `onValueChange` | `(query: string) => void` | – | Called as the shopper types. |
| `open` | `boolean` | – | Whether the dropdown or sheet is open, when controlled. |
| `onOpenChange` | `(open: boolean) => void` | – | Called when it opens or closes. |
| `recentStorageKey` | `string` | – | Keep recent searches in localStorage under this key. |
| `recent` | `string[]` | – | Recent searches, when controlled. |
| `defaultRecent` | `string[]` | `[]` | Recent searches to show when nothing is stored. |
| `onRecentChange` | `(recent: string[]) => void` | – | Called when a search is added, removed or cleared. |
| `maxRecent` | `number` | `5` | How many recent searches to keep. |
| `trending` | `string[]` | `[]` | Trending searches for the empty state. |
| `popularProducts` | `SearchProduct[]` | `[]` | Products for the empty state. Shows four, or three on a phone. |
| `popularCollections` | `SearchCollection[]` | `[]` | Categories to offer when nothing matches. |
| `maxSuggestions` | `number` | `8, or 5 on a phone` | Query suggestions to show. |
| `maxProducts` | `number` | `4, or 3 on a phone` | Product results to show. |
| `debounce` | `number` | `120` | Milliseconds to wait after typing before searching. |
| `shortcut` | `boolean` | `true` | Open with ⌘K, Ctrl+K and /. |
| `align` | `"start" \| "center" \| "end"` | `"start"` | Align the dropdown with the field. |
| `locale` | `Intl.LocalesArgument` | `"en-US"` | Locale for prices and counts. |
| `labels` | `Partial<ProductSearchLabels>` | – | Words for headings, rows, hints and announcements. |

### SearchResults

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `suggestions` | `string[]` | – | Query completions, best first. Only suggest queries that have results. |
| `collections` | `{ id; title; href?; count? }[]` | – | Categories that match, shown as “query in Bedding”. |
| `products` | `{ id; title; href?; image?; price?; compareAt?; subtitle? }[]` | – | Product results. price is Money or { min, max }. |
| `total` | `number` | – | The full result count for the live announcement. Defaults to products.length. |
| `correction` | `string` | – | The corrected query when you matched a typo. Shown as “Showing results for”, or as “Did you mean” when nothing matched. |

### SearchResultsHeader

Also takes div props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `query` | `string` | – | The query shown in the heading. |
| `count` | `number \| null` | – | The result count. Hidden while null. |
| `suggestion` | `string \| null` | – | A spelling suggestion shown as “Did you mean”. |
| `correctedFrom` | `string \| null` | – | The original query when the results are for a correction, shown as “Search instead for”. |
| `onSuggestionSelect` | `(query: string) => void` | – | Called when either link is clicked. |
| `headingLevel` | `1 \| 2 \| 3` | `1` | The heading level. |

## Keyboard

| Key | Action |
| --- | --- |
| `Meta+K` `Control+K` | Focus the field and open search. |
| `/` | Open search, unless you are typing in another field. |
| `ArrowDown` `ArrowUp` | Move through every row. Suggestions are copied into the field as you go. |
| `Enter` | Open the highlighted row, or search for what was typed. |
| `Tab` | Complete the highlighted or top suggestion and keep typing. |
| `Shift+Delete` | Remove the highlighted recent search. |
| `Escape` | Put back what was typed, then close, then clear the field. |

## Accessibility

- The field is a combobox with a listbox of grouped options. aria-activedescendant follows the highlighted row, so focus never leaves the field.
- Result counts such as “6 results” are announced once each search settles, never per keystroke. A failed search is announced as an alert.
- Closing the phone sheet returns focus to the search button. The field uses 16px text on touch screens so iOS doesn’t zoom.
- Highlighted rows get an outline in forced colors mode, and the spinner, fades and arrow nudge are reduced or removed with reduced motion.

## Built with

The free HextaUI components Product Search is made from. Each one installs on its own.

- [Button](https://hextaui.com/docs/button) - Buttons in every variant and size, with a built-in loading, success and error flow that skips the spinner for fast requests.
- [Kbd](https://hextaui.com/docs/kbd) - Key caps for shortcuts that show the right symbols on every platform, read them out properly and press down with the real keys.
- [Popover](https://hextaui.com/docs/popover) - A floating panel anchored to a trigger that resizes smoothly with its content and follows the trigger’s direction.
- [Sheet](https://hextaui.com/docs/sheet) - A panel that slides in from any edge, with swipe-to-dismiss, scroll lock and stacked nesting.
- [Spinner](https://hextaui.com/docs/spinner) - A loading indicator with Apple-style ticks or a breathing ring that can wait before showing and stay long enough not to flicker.

## Code

6 files, added to components/blocks/product-search.
