Skip to content
Browse documentation

The field at the top of every list: ⌘K focuses it from anywhere, search runs after a pause, the magnifier blurs into a spinner while it loads.

Used forList and table headersDocs and help centresApp top barsPickers and sidebars

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/search-input

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/search-input.json. More

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • List and table headers

    Search above customers, invoices or files.

  • Docs and help centres

    A "/" shortcut like GitHub and MDN.

  • App top bars

    A ⌘K field that focuses from anywhere.

  • Pickers and sidebars

    Filter a long list as you type.

What it handles for you

Each search box ends up with its own timer, its own keyboard listener and a spinner bolted on beside it. This one has them built in and keeps them out of each other's way.

Instead of wiring updebounce hookkeyboard shortcut listenerloading spinnerclear button
  1. Debounced search

    Runs after 250ms without typing; Enter runs it immediately.

  2. A real shortcut

    ⌘K / Ctrl K or "/" focuses and selects the field, never while you type elsewhere.

  3. Loading in place

    The magnifier blurs into a spinner and back.

  4. Clear and Escape

    The clear button folds in with the first letter; Escape leaves the field.

Overview

Search fields usually wire their own debounce, shortcut and spinner, and the shortcut hint overlaps the text. This one runs onSearch after a pause (Enter runs it at once), focuses on ⌘K or "/" from anywhere on the page, steps its key hint aside as you type, and turns the magnifier into a spinner while results load.

States

rest
Magnifier, placeholder and the ⌘K hint.
typing
The hint folds away (320ms) and the clear button folds in.
loading
The magnifier blurs into a spinner (scale 0.6, 3px).
focused by shortcut
The field takes focus and selects its text.

Usage

usage.tsx
import { SearchInput } from "@/components/search-input";<SearchInput placeholder="Search customers" loading={isFetching} onSearch={(q) => setQuery(q)} />

Props

onSearch
(query: string) => void
After `debounce` ms without typing, and at once on Enter.
debounce= 250
number
Wait in ms.
loading= false
boolean
Spinner in place of the magnifier.
shortcut= "mod+k"
"mod+k" | "/" | null
Key that focuses the field.
...Input props
InputProps
label, size, value, onValueChange and every native prop.

Accessibility and motion

  • Shares field.tsx with every text control: one surface, one focus ring, one way of showing hints and errors.
  • role="searchbox"; the shortcut hint is aria-hidden (the key is a convenience, not a name).
  • The browser's own cancel button is hidden in favour of the folding one.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

Source

"use client";import * as React from "react";import { Input, type InputProps } from "./input";import { EASE_OUT, interactionIcon, useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * SEARCH INPUT: the field at the top of every list * *   shortcut  a ⌘K (or "/") hint sits at the end; the key *             focuses the field from anywhere on the page, and *             the hint steps aside once you type *   search    onSearch runs after a pause in typing (debounced); *             Enter runs it at once, Escape clears and leaves *   loading   the magnifier blurs into a spinner and back *   clear     the clear button folds in with the first letter * ───────────────────────────────────────────────────────── */export interface SearchInputProps extends Omit<InputProps, "leading" | "type" | "clearable"> {  /** Called with the query after `debounce` ms without typing, and at once on Enter. */  onSearch?: (query: string) => void;  debounce?: number;  /** Shows a spinner in place of the magnifier. */  loading?: boolean;  /** "mod+k" (⌘K / Ctrl K), "/" or null for no shortcut. */  shortcut?: "mod+k" | "/" | null;}function Magnifier() {  return (    <svg viewBox="0 0 16 16" fill="none" className="size-3.5">      <circle cx="7" cy="7" r="4.6" stroke="currentColor" strokeWidth="1.5" />      <path d="M10.4 10.4 13.5 13.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />    </svg>  );}const subscribeNoop = () => () => {};const isMac = () => /Mac|iPhone|iPad/.test(navigator.platform);export function SearchInput({  onSearch,  debounce = 250,  loading = false,  shortcut = "mod+k",  placeholder = "Search",  value,  defaultValue,  onValueChange,  onKeyDown,  onFocus,  onBlur,  size = "md",  ref,  ...props}: SearchInputProps) {  const reduced = useInteractionReducedMotion();  const inner = React.useRef<HTMLInputElement>(null);  const [text, setText] = React.useState(String(defaultValue ?? ""));  const [focused, setFocused] = React.useState(false);  const current = value !== undefined ? String(value) : text;  const mac = React.useSyncExternalStore(subscribeNoop, isMac, () => true);  const searchRef = React.useRef(onSearch);  React.useEffect(() => {    searchRef.current = onSearch;  });  // Debounced search: each keystroke restarts the wait.  const first = React.useRef(true);  React.useEffect(() => {    if (first.current) {      first.current = false;      return;    }    const timer = window.setTimeout(() => searchRef.current?.(current), debounce);    return () => window.clearTimeout(timer);  }, [current, debounce]);  // The shortcut focuses the field from anywhere, unless you're typing somewhere else.  React.useEffect(() => {    if (!shortcut) return;    const onKey = (e: KeyboardEvent) => {      const target = e.target as HTMLElement;      const typing = target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName);      const hit = shortcut === "/" ? e.key === "/" && !typing : e.key.toLowerCase() === "k" && (e.metaKey || e.ctrlKey);      if (!hit) return;      e.preventDefault();      inner.current?.focus();      inner.current?.select();    };    window.addEventListener("keydown", onKey);    return () => window.removeEventListener("keydown", onKey);  }, [shortcut]);  const showKey = !!shortcut && !focused && !current;  const keyLabel = shortcut === "/" ? "/" : mac ? "⌘K" : "Ctrl K";  return (    <Input      {...props}      ref={(el) => {        inner.current = el;        if (typeof ref === "function") ref(el);        else if (ref) (ref as React.RefObject<HTMLInputElement | null>).current = el;      }}      type="search"      role="searchbox"      size={size}      clearable      placeholder={placeholder}      value={value}      defaultValue={defaultValue}      inputClassName="[&::-webkit-search-cancel-button]:appearance-none"      onValueChange={(v) => {        if (value === undefined) setText(v);        onValueChange?.(v);      }}      onFocus={(e) => {        setFocused(true);        onFocus?.(e);      }}      onBlur={(e) => {        setFocused(false);        onBlur?.(e);      }}      onKeyDown={(e) => {        onKeyDown?.(e);        if (e.key === "Enter") onSearch?.(current);        if (e.key === "Escape" && current) {          e.preventDefault();          e.currentTarget.blur();        }      }}      leading={        <span className="relative size-3.5" aria-hidden="true">          <span className="absolute inset-0 flex items-center justify-center" style={interactionIcon(!loading, reduced)}>            <Magnifier />          </span>          <span className="absolute inset-0 flex items-center justify-center" style={interactionIcon(loading, reduced)}>            <span className={`size-3 rounded-full border-[1.5px] border-current border-t-transparent ${loading ? "animate-spin" : ""}`} />          </span>        </span>      }      trailing={        shortcut ? (          <span            className="grid"            style={{ gridTemplateColumns: showKey ? "1fr" : "0fr", transition: reduced ? "none" : `grid-template-columns 320ms ${EASE_OUT}` }}          >            <span className="min-w-0 overflow-hidden">              <kbd                aria-hidden="true"                className={`mr-1.5 block whitespace-nowrap rounded-full px-1.5 font-mono text-[10.5px] leading-[18px] text-muted-foreground shadow-[inset_0_0_0_1px_var(--border)]`}                style={{ opacity: showKey ? 1 : 0, filter: showKey ? "none" : "blur(3px)", transition: reduced ? "none" : `opacity 200ms ${EASE_OUT}, filter 200ms ${EASE_OUT}` }}              >                {keyLabel}              </kbd>            </span>          </span>        ) : undefined      }    />  );}export default SearchInput;

Registry item: /r/search-input.json