Build a Search Page with UI SDK

UI SDK is Vercel Labs' library of unstyled components: dialogs, menus, comboboxes, drawers and more, with the keyboard, focus and screen reader behaviour worked out and the look left to you. intrface is built on it, and npm installs it with intrface, so you can use all of it in your search page:

bash
npm install @indxsearch/intrface

Start with intrface's own components. The search field, the results, the filter panels and Search or ask are ready to use, take your page's look, and are updated with the package. Use a recipe when you need something they don't cover.

In a recipe, UI SDK gives the component and intrface's hooks give it the search. Each is one React file to copy from the intrface repository, with one stylesheet for them all that follows your page's font and colours, and a shadcn theme where there is one. Put them inside your SearchProvider.

UI SDK is in beta and its API may still change. intrface takes 0.1.0-beta.17 or any later 0.1 from 4.0.1 on; 4.0.0 asks for exactly beta.17.

Search with Cmd+K

UI SDK's Command in a Dialog, opened from anywhere on the site. This is what connects it:

tsx
import { useResultsBinding, useSearchContext, useSearchTextBinding } from '@indxsearch/intrface';
import { Command, CommandInput, CommandItem, CommandList } from '@ui-sdk/react/command';

interface Film { title: string }

function Palette({ onOpen }: { onOpen: (film: Film) => void }) {
  const { state } = useSearchContext();
  const text = useSearchTextBinding();
  const { pick } = useResultsBinding(film => (film as Film).title);
  const results = state.results ?? [];
  return (
    <Command
      filter={false}
      autoHighlight
      query={text.value}
      onQueryChange={text.onValueChange}
      onAction={position => {
        const result = results.find(r => String(r.position) === position);
        if (!result) return;
        pick(position);
        onOpen(result.document as Film);
      }}
    >
      <CommandInput aria-label="Search films" />
      <CommandList aria-label="Results">
        {results.map(r => (
          <CommandItem key={r.documentKey} value={String(r.position)}>{(r.document as Film).title}</CommandItem>
        ))}
      </CommandList>
    </Command>
  );
}
  • filter={false} matters most. Command filters its list by what is typed, and would hide the records Indx found in spite of a typo: "godfahter" finds The Godfather.
  • pick reports the choice, so the dataset's statistics count it as a click.
  • Give the palette a SearchProvider of its own, in the site's header. Sharing one with a search page would change that page's list as the visitor types.

The whole recipe is CommandSearch.tsx, with the dialog, the shortcut and highlighted matches. It also says so when nothing held all the words and the list is the closest records.

A filter with thousands of values

People, brands and authors are too many to list as checkboxes. UI SDK's Combobox takes what is typed, and useFacetList finds it among the field's values on the server, typos included, with each value's count in the visitor's search:

tsx
import { useFacetList } from '@indxsearch/intrface';
import { Combobox, ComboboxContent, ComboboxInput, ComboboxItem, ComboboxList } from '@ui-sdk/react/combobox';

function BrandFilter() {
  const facet = useFacetList({ field: 'brand', label: 'Brand', match: 'any', limit: 20, searchable: true, valuesOnDemand: true });
  if (facet.status !== 'list') return null;
  const { entries, selected, toggle, search } = facet;
  return (
    <Combobox
      multiple
      filter={false}
      autoHighlight
      value={selected}
      onValueChange={next => {
        for (const v of selected) if (!next.includes(v)) toggle(v);
        for (const v of next) if (!selected.includes(v)) toggle(v);
      }}
      query={search.text}
      onQueryChange={search.setText}
      itemToLabel={v => v}
    >
      <ComboboxInput aria-label="Brand" onFocus={search.warm} />
      <ComboboxContent>
        <ComboboxList>
          {entries.filter(e => !e.selected).map(e => (
            <ComboboxItem key={e.value} value={e.value} label={e.value} disabled={e.disabled}>{e.value} {e.count}</ComboboxItem>
          ))}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  );
}
  • The field must be Filterable and Facetable.
  • valuesOnDemand asks for the values as they are needed, so they are not counted in every search. Another panel on the same field would then have none to show: use one control per field.
  • match is 'all' for a field that holds several values per record, such as a cast, and 'any' for one that holds a single value, such as a brand.
  • Leave the picked values out of the list. They come first, and Enter would unpick one.

The whole recipe is FacetCombobox.tsx, with the picked values as chips in the field.

Filters in a drawer

On a phone the filters need a place of their own. UI SDK's Drawer is a sheet that is dragged down to close, around the panels you already have:

tsx
import { RangeFilterPanel, useSearchContext, ValueFilterPanel } from '@indxsearch/intrface';
import { Drawer, DrawerBackdrop, DrawerClose, DrawerContent, DrawerTitle, DrawerTrigger } from '@ui-sdk/react/drawer';

function Filters() {
  const { state } = useSearchContext();
  return (
    <Drawer>
      <DrawerTrigger>Filters</DrawerTrigger>
      <DrawerBackdrop />
      <DrawerContent keepMounted>
        <DrawerTitle>Filters</DrawerTitle>
        <ValueFilterPanel field="genres" label="Genre" />
        <RangeFilterPanel field="year" label="Year" control="slider" />
        <DrawerClose>Show {state.countedTotal ?? 0} films</DrawerClose>
      </DrawerContent>
    </Drawer>
  );
}
  • keepMounted keeps the panels on the page while the drawer is closed. They give the active filters their names, "Genre: Crime" rather than the field's.
  • Draw the panels once: in the drawer on a narrow page, in the column on a wide one.

The whole recipe is FilterDrawer.tsx, with the number of filters in use on its button and a way to clear them.

Other components

intrface's hooks and bindings fit any UI SDK component that takes a value and reports a change: an Input for the search text, a Slider for a range, a Select for the order. The bindings are listed in Make Your Search Page Agent-Ready, and with SearchAgent around them the same page is ready for AI agents too.