SunUI
Founding$79 lifetime updates till Jan 11Claim a seat
FreeType

Expanding Search

Store search field that expands into a panel of product suggestions with thumbnails, prices and keyboard navigation (combobox pattern, / shortcut).

When to use it

  • Compact navbars where a full search field does not fit
  • Sites where search is secondary but should stay one click away

Watch out

  • Leave room in the layout for the expanded width so neighbouring items do not jump
  • Built-in filtering is a simple substring match; set filterSuggestions to false for server results

Let your agent add it

One copy holds the install command, when to use it, the props and the design rules. Paste it into Claude Code, Cursor or Codex.

Install with the CLI

Uses the shadcn CLI. The source is copied into your project, so you own every line.

$ npx shadcn@latest add https://sunui.synvize.com/r/expanding-search.json

Or install manually

  1. 1. Install the dependencies.
    $ npm install clsx tailwind-merge framer-motion
  2. 2. Add a cn helper at lib/utils.ts if you do not have one. Not using shadcn/ui? Also add sunui-theme.css for the colour tokens (how). Then copy the component.
    expanding-search.tsx
    "use client";
    
    import { AnimatePresence, motion, useReducedMotion } from "framer-motion";
    import { useCallback, useEffect, useId, useMemo, useRef, useState, type FormEvent, type KeyboardEvent, type ReactNode } from "react";
    
    import { cn } from "@/lib/utils";
    
    export type ExpandingSearchSuggestion = {
      id: string;
      label: string;
      /** Second-line description, for example category or style */
      description?: string;
      /** Auxiliary text on the right, for example price */
      hint?: string;
      /** Thumbnail on the left, for example a product image */
      thumbnail?: ReactNode;
    };
    
    export interface ExpandingSearchProps {
      value?: string;
      defaultValue?: string;
      onValueChange?: (value: string) => void;
      /** Called on Enter (when no suggestion is selected) */
      onSubmit?: (query: string) => void;
      open?: boolean;
      defaultOpen?: boolean;
      onOpenChange?: (open: boolean) => void;
      /** Suggestion list; when not given, no dropdown is shown */
      suggestions?: ExpandingSearchSuggestion[];
      /** Whether to filter suggestions by the input (case-insensitive substring match); set to false to filter yourself */
      filterSuggestions?: boolean;
      onSuggestionSelect?: (suggestion: ExpandingSearchSuggestion) => void;
      /** Width when expanded (px); on narrow screens it never exceeds the parent */
      expandedWidth?: number;
      /** Enable the "/" shortcut to open search; it does not fire while typing in other inputs */
      slashShortcut?: boolean;
      /** When not given, labels.placeholder is used */
      placeholder?: string;
      /** UI language: en, zh-TW, zh-CN, ja; tags such as zh-Hant-HK and ja-JP are also accepted */
      locale?: string;
      /** Override the built-in strings, takes priority over locale */
      labels?: Partial<ExpandingSearchLabels>;
      className?: string;
    }
    
    export interface ExpandingSearchLabels {
      open: string;
      search: string;
      clear: string;
      suggestions: string;
      results: (count: number) => string;
      placeholder: string;
    }
    
    type SunLocale = "en" | "zh-TW" | "zh-CN" | "ja";
    
    /** Map any language tag to one of the four built-in languages; everything else falls back to English */
    function resolveLocale(tag?: string): SunLocale {
      const t = (tag ?? "en").toLowerCase();
      if (t.startsWith("zh")) return /hant|tw|hk|mo/.test(t) ? "zh-TW" : "zh-CN";
      if (t.startsWith("ja")) return "ja";
      return "en";
    }
    
    const MESSAGES: Record<SunLocale, ExpandingSearchLabels> = {
      en: {
        open: "Open search",
        search: "Search",
        clear: "Clear search",
        suggestions: "Suggestions",
        results: (n) => (n === 1 ? "1 suggestion" : `${n} suggestions`),
        placeholder: "Search…",
      },
      "zh-TW": {
        open: "開啟搜尋",
        search: "搜尋",
        clear: "清除搜尋",
        suggestions: "建議",
        results: (n) => `${n} 項建議`,
        placeholder: "搜尋…",
      },
      "zh-CN": {
        open: "打开搜索",
        search: "搜索",
        clear: "清除搜索",
        suggestions: "建议",
        results: (n) => `${n} 条建议`,
        placeholder: "搜索…",
      },
      ja: {
        open: "検索を開く",
        search: "検索",
        clear: "検索をクリア",
        suggestions: "候補",
        results: (n) => `候補 ${n} 件`,
        placeholder: "検索…",
      },
    };
    
    // Design language (DESIGN.md, craft quality): surfaces, hairlines, text colors
    const surface = "bg-card shadow-[0_1px_2px_rgb(0_0_0/0.05),0_4px_12px_-6px_rgb(0_0_0/0.06)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]";
    const surfaceUnder = "bg-muted dark:bg-foreground/10";
    const edge = "border-foreground/10";
    const ink = "text-foreground";
    const inkMuted = "text-muted-foreground";
    
    const springs = {
      gentle: { type: "spring", stiffness: 240, damping: 30 },
    } as const;
    
    const COLLAPSED = 40;
    
    function isTypingTarget(el: EventTarget | null) {
      if (!(el instanceof HTMLElement)) return false;
      return ["INPUT", "TEXTAREA", "SELECT"].includes(el.tagName) || el.isContentEditable;
    }
    
    function Match({ text, query }: { text: string; query: string }) {
      const q = query.trim();
      const at = q ? text.toLowerCase().indexOf(q.toLowerCase()) : -1;
      if (at === -1) return <>{text}</>;
      return (
        <>
          {text.slice(0, at)}
          <mark className="bg-transparent text-current underline decoration-current/40 underline-offset-[3px]">{text.slice(at, at + q.length)}</mark>
          {text.slice(at + q.length)}
        </>
      );
    }
    
    const SearchIcon = ({ className }: { className?: string }) => (
      <svg viewBox="0 0 24 24" className={className} fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true">
        <circle cx="11" cy="11" r="6.5" />
        <path d="m20 20-4.2-4.2" />
      </svg>
    );
    
    export function ExpandingSearch({
      value: valueProp,
      defaultValue = "",
      onValueChange,
      onSubmit,
      open: openProp,
      defaultOpen = false,
      onOpenChange,
      suggestions,
      filterSuggestions = true,
      onSuggestionSelect,
      expandedWidth = 280,
      slashShortcut = false,
      placeholder,
      locale = "en",
      labels,
      className,
    }: ExpandingSearchProps) {
      const reduce = useReducedMotion() ?? false;
      const text = { ...MESSAGES[resolveLocale(locale)], ...labels };
      const baseId = useId();
      const listId = `${baseId}-list`;
      const [innerValue, setInnerValue] = useState(defaultValue);
      const value = valueProp ?? innerValue;
      const [innerOpen, setInnerOpen] = useState(defaultOpen);
      const open = openProp ?? innerOpen;
      const [listOpen, setListOpen] = useState(false);
      const [active, setActive] = useState(-1);
      const rootRef = useRef<HTMLFormElement>(null);
      const inputRef = useRef<HTMLInputElement>(null);
      const triggerRef = useRef<HTMLButtonElement>(null);
      const focusTrigger = useRef(false);
    
      const setOpen = useCallback(
        (next: boolean) => {
          if (openProp === undefined) setInnerOpen(next);
          onOpenChange?.(next);
        },
        [openProp, onOpenChange],
      );
    
      const setValue = (next: string) => {
        if (valueProp === undefined) setInnerValue(next);
        onValueChange?.(next);
        setActive(-1);
      };
    
      const filtered = useMemo(() => {
        if (!suggestions) return [];
        const q = value.trim().toLowerCase();
        if (!filterSuggestions || !q) return suggestions;
        return suggestions.filter((s) => s.label.toLowerCase().includes(q));
      }, [suggestions, value, filterSuggestions]);
    
      const showList = open && listOpen && filtered.length > 0;
    
      // Focus automatically when expanded; when collapsing, return focus to the icon button as appropriate
      useEffect(() => {
        if (open) {
          const raf = requestAnimationFrame(() => inputRef.current?.focus());
          return () => cancelAnimationFrame(raf);
        }
        setListOpen(false);
        setActive(-1);
        if (focusTrigger.current) {
          focusTrigger.current = false;
          requestAnimationFrame(() => triggerRef.current?.focus());
        }
      }, [open]);
    
      useEffect(() => {
        if (active >= 0) document.getElementById(`${baseId}-opt-${active}`)?.scrollIntoView({ block: "nearest" });
      }, [active, baseId]);
    
      useEffect(() => {
        if (!slashShortcut) return;
        const onKey = (e: globalThis.KeyboardEvent) => {
          if (e.key !== "/" || e.metaKey || e.ctrlKey || e.altKey || isTypingTarget(e.target)) return;
          e.preventDefault();
          if (open) inputRef.current?.focus();
          else setOpen(true);
        };
        window.addEventListener("keydown", onKey);
        return () => window.removeEventListener("keydown", onKey);
      }, [slashShortcut, open, setOpen]);
    
      const collapse = (restoreFocus: boolean) => {
        focusTrigger.current = restoreFocus;
        setOpen(false);
      };
    
      const choose = (s: ExpandingSearchSuggestion) => {
        setValue(s.label);
        setListOpen(false);
        onSuggestionSelect?.(s);
      };
    
      const onKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
        const n = filtered.length;
        if ((e.key === "ArrowDown" || e.key === "ArrowUp") && n) {
          e.preventDefault();
          setListOpen(true);
          const down = e.key === "ArrowDown";
          setActive((i) => (!showList ? (down ? 0 : n - 1) : down ? (i + 1) % n : i <= 0 ? n - 1 : i - 1));
        } else if ((e.key === "Home" || e.key === "End") && showList) {
          e.preventDefault();
          setActive(e.key === "Home" ? 0 : n - 1);
        } else if (e.key === "Enter" && !e.nativeEvent.isComposing && showList && active >= 0) {
          e.preventDefault();
          choose(filtered[active]);
        } else if (e.key === "Escape") {
          e.preventDefault();
          // The first press closes the suggestion list, the second collapses it
          if (showList) {
            setListOpen(false);
            setActive(-1);
          } else collapse(true);
        }
      };
    
      const onFormSubmit = (e: FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        if (!value.trim()) return;
        setListOpen(false);
        onSubmit?.(value.trim());
      };
    
      return (
        <form
          ref={rootRef}
          role="search"
          onSubmit={onFormSubmit}
          onBlur={(e) => {
            // Focus leaves the whole component: close the list, and collapse if there is no content
            if (rootRef.current?.contains(e.relatedTarget as Node)) return;
            setListOpen(false);
            if (!value.trim()) collapse(false);
          }}
          className={cn("relative inline-flex max-w-full min-w-0", ink, className)}
        >
          <motion.div
            initial={false}
            animate={{ width: open ? expandedWidth : COLLAPSED }}
            transition={reduce ? { duration: 0 } : springs.gentle}
            className={cn(
              "relative flex h-10 max-w-full items-center overflow-hidden rounded-md border transition-colors",
              open ? cn(surface, edge, "focus-within:border-foreground/40") : "border-transparent",
            )}
          >
            {open ? (
              <>
                <SearchIcon className={cn("pointer-events-none absolute left-3 size-4", inkMuted)} />
                <input
                  ref={inputRef}
                  type="search"
                  role="combobox"
                  aria-label={text.search}
                  aria-expanded={showList}
                  aria-controls={suggestions ? listId : undefined}
                  aria-autocomplete={suggestions ? "list" : "none"}
                  aria-activedescendant={showList && active >= 0 ? `${baseId}-opt-${active}` : undefined}
                  autoComplete="off"
                  enterKeyHint="search"
                  placeholder={placeholder ?? text.placeholder}
                  value={value}
                  onChange={(e) => {
                    setValue(e.target.value);
                    setListOpen(true);
                  }}
                  onFocus={() => setListOpen(true)}
                  onKeyDown={onKeyDown}
                  className="h-full w-full min-w-0 bg-transparent pr-9 pl-9 text-sm outline-none placeholder:text-current/45 [&::-webkit-search-cancel-button]:hidden"
                />
                {value && (
                  <button
                    type="button"
                    aria-label={text.clear}
                    onClick={() => {
                      setValue("");
                      inputRef.current?.focus();
                    }}
                    className={cn("absolute right-2 flex size-6 items-center justify-center rounded-[3px] outline-none hover:bg-current/5 focus-visible:ring-2 focus-visible:ring-ring motion-safe:active:scale-[0.97] transition-[color,background-color,border-color,opacity,scale]", inkMuted)}
                  >
                    <svg viewBox="0 0 24 24" className="size-3.5" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true" strokeLinejoin="round">
                      <path d="M6.5 6.5l11 11M17.5 6.5l-11 11" />
                    </svg>
                  </button>
                )}
              </>
            ) : (
              <button
                ref={triggerRef}
                type="button"
                aria-label={text.open}
                aria-expanded={false}
                aria-keyshortcuts={slashShortcut ? "/" : undefined}
                onClick={() => setOpen(true)}
                className="flex size-10 shrink-0 items-center justify-center rounded-md outline-none transition-[color,background-color,border-color,opacity,scale] hover:bg-current/5 focus-visible:ring-2 focus-visible:ring-ring motion-safe:active:scale-[0.97]"
              >
                <SearchIcon className="size-[18px]" />
              </button>
            )}
          </motion.div>
    
          <AnimatePresence>
            {showList && (
              <motion.ul
                key="list"
                id={listId}
                role="listbox"
                aria-label={text.suggestions}
                initial={reduce ? false : { opacity: 0 }}
                animate={{ opacity: 1 }}
                exit={{ opacity: 0, transition: { duration: reduce ? 0 : 0.1 } }}
                transition={{ duration: 0.12 }}
                className={cn("absolute top-full right-0 left-0 z-20 mt-1.5 max-h-80 overflow-y-auto rounded-md border p-1 shadow-[0_1px_2px_rgb(0_0_0/0.05),0_4px_12px_-6px_rgb(0_0_0/0.06)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]", surface, edge)}
              >
                {filtered.map((s, i) => (
                  <li
                    key={s.id}
                    id={`${baseId}-opt-${i}`}
                    role="option"
                    aria-selected={i === active}
                    onMouseDown={(e) => e.preventDefault()}
                    onPointerMove={() => i !== active && setActive(i)}
                    onClick={() => choose(s)}
                    className={cn("flex cursor-pointer items-center gap-3 rounded-[3px] px-2 py-1.5 text-sm", i === active && surfaceUnder)}
                  >
                    {s.thumbnail && (
                      <span aria-hidden="true" className={cn("size-10 shrink-0 overflow-hidden rounded-[3px] border", edge)}>
                        {s.thumbnail}
                      </span>
                    )}
                    <span className="min-w-0 flex-1">
                      <span className="block truncate">
                        <Match text={s.label} query={value} />
                      </span>
                      {s.description && <span className={cn("block truncate text-xs", inkMuted)}>{s.description}</span>}
                    </span>
                    {s.hint && <span className="shrink-0 text-sm tracking-tight tabular-nums">{s.hint}</span>}
                  </li>
                ))}
              </motion.ul>
            )}
          </AnimatePresence>
    
          <span className="sr-only" role="status" aria-live="polite">
            {showList && value.trim() ? text.results(filtered.length) : ""}
          </span>
        </form>
      );
    }