Skip to content
synccn

Search Field

New

Search with a / shortcut, recent searches and highlighted suggestions.

/

Press / anywhere to jump here.

Installation

$ pnpm dlx shadcn@latest add @synccn/search-field

First time in this project? Connect synccn with one command.

Usage

import { SearchField } from "@/components/ui/search-field";<SearchField suggestions={pages} onSearch={go} />

API reference

Generated from the component's TypeScript types, so it always matches the code.

SearchField

Search box with a "/" shortcut, a clear button, recent searches and live suggestions. onSearch fires on Enter or when a suggestion is picked.

  • suggestions
    string[]

    Default []

    Terms matched as the user types.

  • recent
    string[]

    Default []

    Recent searches shown before typing.

  • onSearch
    (term: string) => void

    Called on Enter or when a suggestion is picked.

  • placeholder
    string

    Default "Search…"

    Placeholder and accessible name.

  • shortcut
    string | false

    Default "/"

    Key that focuses the field from anywhere.

  • className
    string

    Extra classes for the root element.

Dependencies

Read from the component's imports when the site builds. The CLI installs all of these for you.

Helpers

  • lib/utilscn (merges class names)
  • lib/hooksuseAutoSide (flips to the side with room), useClickOutside (closes on an outside click)

npm packages

Keyboard

  • /Focus the field from anywhere on the page (configurable with shortcut)
  • ↓or↑Move through suggestions
  • EnterSearch for the highlighted suggestion or the typed text
  • EscClear the text, or close if it's already empty

Accessibility

  • The shortcut is ignored while the user is typing in another field.

From the source

Read from this component's code when the site builds, so it can't drift.

  • ARIA roles: combobox, listbox, option
  • ARIA attributes: aria-autocomplete, aria-expanded, aria-label, aria-selected
  • Keys handled: ArrowUp, ArrowDown, Enter, Escape
  • Closes when you click or tap outside
  • Flips to whichever side has room, so it stays on screen