{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "search-expand",
  "title": "Expanding search",
  "description": "Search icon that springs open into a field with a recent-searches dropdown and a / shortcut; Esc or blur folds it back when empty.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/inputs/search-expand.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { Clock, CornerDownLeft, Search, X } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring as springs } from \"@/lib/goodui-motion\"\n\nexport type SearchExpandLabels = { search: string; open: string; clear: string; recent: string; matching: string; clearRecent: string }\nconst DEFAULT_LABELS: SearchExpandLabels = {\n  search: \"Search\",\n  open: \"Open search\",\n  clear: \"Clear search\",\n  recent: \"Recent searches\",\n  matching: \"Matching recent\",\n  clearRecent: \"Clear\",\n}\n\nexport type SearchExpandProps = Omit<React.ComponentProps<\"div\">, \"onSubmit\" | \"defaultValue\"> & {\n  /** Controlled query text; pair with onValueChange. */\n  value?: string\n  /** Initial query when uncontrolled. */\n  defaultValue?: string\n  /** Called on every keystroke, on clear and when a recent search fills the field. */\n  onValueChange?: (value: string) => void\n  /** Called with the trimmed query on Enter (never with an empty string). */\n  onSubmit?: (query: string) => void\n  /** Controlled open state; pair with onOpenChange. */\n  open?: boolean\n  /** Start expanded when uncontrolled. */\n  defaultOpen?: boolean\n  /** Called when the field expands or folds back. */\n  onOpenChange?: (open: boolean) => void\n  /** Shown in a dropdown while the field is focused; filtered by what is typed. */\n  recent?: string[]\n  /** Defaults to filling the field and submitting. */\n  onRecentSelect?: (query: string) => void\n  /** Shows a \"Clear\" button in the dropdown header when set. */\n  onClearRecent?: () => void\n  /** Placeholder text; also used as the input's aria-label. */\n  placeholder?: string\n  /** Width in px when open (it never exceeds the parent). */\n  expandedWidth?: number\n  /** Global key that opens the search; `null` to disable. */\n  shortcut?: string | null\n  /** Which edge the dropdown aligns to; use \"end\" when the search sits on the right. */\n  align?: \"start\" | \"end\"\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<SearchExpandLabels>\n}\n\nconst isEditable = (el: EventTarget | null) =>\n  el instanceof HTMLElement && (el.isContentEditable || el.tagName === \"INPUT\" || el.tagName === \"TEXTAREA\" || el.tagName === \"SELECT\")\n\n/** Icon button that springs open into a search field with recent searches; Esc or blur closes it when empty. */\nexport function SearchExpand({\n  value: valueProp,\n  defaultValue = \"\",\n  onValueChange,\n  onSubmit,\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  recent = [],\n  onRecentSelect,\n  onClearRecent,\n  placeholder = \"Search…\",\n  expandedWidth = 280,\n  shortcut = \"/\",\n  align = \"start\",\n  labels,\n  className,\n  ...props\n}: SearchExpandProps) {\n  const l = useLabels(\"search-expand\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const [innerValue, setInnerValue] = React.useState(defaultValue)\n  const value = valueProp ?? innerValue\n  const [innerOpen, setInnerOpen] = React.useState(defaultOpen)\n  const open = openProp ?? innerOpen\n  const [focused, setFocused] = React.useState(false)\n  const [activeIdx, setActiveIdx] = React.useState(-1)\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const inputRef = React.useRef<HTMLInputElement>(null)\n  const triggerRef = React.useRef<HTMLButtonElement>(null)\n  const uid = React.useId()\n  const listId = `${uid}-recent`\n\n  const setValue = (v: string) => {\n    if (valueProp === undefined) setInnerValue(v)\n    onValueChange?.(v)\n  }\n  const setOpen = (o: boolean) => {\n    if (openProp === undefined) setInnerOpen(o)\n    onOpenChange?.(o)\n  }\n  const openAndFocus = () => {\n    setOpen(true)\n    // The input is always mounted, so it can take focus in the same tick (keeps mobile keyboards happy).\n    inputRef.current?.focus()\n  }\n  const close = (refocus: boolean) => {\n    setOpen(false)\n    setActiveIdx(-1)\n    if (refocus) requestAnimationFrame(() => triggerRef.current?.focus())\n  }\n\n  // The global listener reads the latest openAndFocus (and so the latest onOpenChange) through a ref.\n  const openRef = React.useRef(openAndFocus)\n  React.useLayoutEffect(() => {\n    openRef.current = openAndFocus\n  })\n  React.useEffect(() => {\n    if (!shortcut) return\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key !== shortcut || e.metaKey || e.ctrlKey || e.altKey || isEditable(e.target)) return\n      e.preventDefault()\n      openRef.current()\n    }\n    window.addEventListener(\"keydown\", onKey)\n    return () => window.removeEventListener(\"keydown\", onKey)\n  }, [shortcut])\n\n  const q = value.trim().toLowerCase()\n  const items = recent.filter((r) => !q || (r.toLowerCase().includes(q) && r.toLowerCase() !== q)).slice(0, 6)\n  const showList = open && focused && items.length > 0\n\n  const submit = (query: string) => {\n    const v = query.trim()\n    if (!v) return\n    onSubmit?.(v)\n    setActiveIdx(-1)\n  }\n\n  const pick = (r: string) => {\n    if (onRecentSelect) onRecentSelect(r)\n    else {\n      setValue(r)\n      submit(r)\n    }\n    setActiveIdx(-1)\n  }\n\n  const spring = reduce ? { duration: 0 } : springs.ui\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\"relative inline-flex max-w-full\", className)}\n      onFocus={() => setFocused(true)}\n      onBlur={(e) => {\n        if (rootRef.current?.contains(e.relatedTarget as Node)) return\n        setFocused(false)\n        setActiveIdx(-1)\n        if (!value.trim()) close(false)\n      }}\n      {...props}\n    >\n      <GooduiTokens />\n      <motion.div\n        role=\"search\"\n        className={cn(\n          \"relative flex h-9 max-w-full items-center overflow-hidden rounded-full border bg-background shadow-xs transition-[border-color,box-shadow,background-color] dark:bg-input/30\",\n          open ? \"border-input focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50\" : \"border-transparent bg-transparent shadow-none hover:bg-muted dark:bg-transparent\",\n        )}\n        initial={false}\n        animate={{ width: open ? expandedWidth : 36 }}\n        transition={spring}\n      >\n        <button\n          ref={triggerRef}\n          type=\"button\"\n          aria-label={open ? l.search : l.open}\n          aria-expanded={open}\n          tabIndex={open ? -1 : 0}\n          onClick={() => (open ? inputRef.current?.focus() : openAndFocus())}\n          className=\"pc-press grid size-9 shrink-0 place-items-center rounded-full text-muted-foreground outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n        >\n          <motion.span className=\"grid place-items-center\" initial={false} animate={{ scale: open ? 0.9 : 1 }} transition={spring}>\n            <Search className={cn(\"size-4 transition-colors\", !open && \"text-foreground\")} />\n          </motion.span>\n        </button>\n        <input\n          ref={inputRef}\n          type=\"search\"\n          role=\"combobox\"\n          aria-label={placeholder}\n          aria-expanded={showList}\n          aria-controls={listId}\n          aria-activedescendant={showList && activeIdx >= 0 ? `${listId}-${activeIdx}` : undefined}\n          aria-hidden={!open || undefined}\n          tabIndex={open ? 0 : -1}\n          value={value}\n          placeholder={placeholder}\n          onChange={(e) => {\n            setValue(e.target.value)\n            setActiveIdx(-1)\n          }}\n          onKeyDown={(e) => {\n            if (e.key === \"Escape\") {\n              e.preventDefault()\n              if (showList && activeIdx >= 0) setActiveIdx(-1)\n              else if (value) setValue(\"\")\n              else close(true)\n            } else if (e.key === \"ArrowDown\" && items.length) {\n              e.preventDefault()\n              setActiveIdx((i) => (i + 1) % items.length)\n            } else if (e.key === \"ArrowUp\" && items.length) {\n              e.preventDefault()\n              setActiveIdx((i) => (i <= 0 ? items.length - 1 : i - 1))\n            } else if (e.key === \"Enter\") {\n              e.preventDefault()\n              const hit = showList ? items[activeIdx] : undefined\n              if (hit !== undefined) pick(hit)\n              else submit(value)\n            }\n          }}\n          className={cn(\n            \"h-full min-w-0 flex-1 bg-transparent pe-2 text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden\",\n            \"transition-opacity duration-150\",\n            open ? \"opacity-100\" : \"pointer-events-none opacity-0\",\n          )}\n        />\n        <AnimatePresence initial={false} mode=\"popLayout\">\n          {open && (value ? (\n            <motion.button\n              key=\"clear\"\n              type=\"button\"\n              aria-label={l.clear}\n              onClick={() => {\n                setValue(\"\")\n                inputRef.current?.focus()\n              }}\n              initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.6 }}\n              animate={{ opacity: 1, scale: 1 }}\n              exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.6 }}\n              transition={{ duration: reduce ? 0.1 : 0.15 }}\n              className=\"pc-press me-1.5 grid size-6 shrink-0 place-items-center rounded-full text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n            >\n              <X className=\"size-3.5\" />\n            </motion.button>\n          ) : shortcut ? (\n            <motion.kbd\n              key=\"kbd\"\n              aria-hidden=\"true\"\n              initial={{ opacity: 0 }}\n              animate={{ opacity: 1, transition: { delay: reduce ? 0 : 0.12 } }}\n              exit={{ opacity: 0 }}\n              className=\"me-2 hidden h-5 shrink-0 items-center rounded border bg-muted px-1.5 font-mono text-[10px] text-foreground/80 sm:inline-flex\"\n            >\n              {shortcut}\n            </motion.kbd>\n          ) : null)}\n        </AnimatePresence>\n      </motion.div>\n\n      <AnimatePresence>\n        {showList && (\n          <motion.div\n            initial={reduce ? { opacity: 0 } : { opacity: 0, y: -6, scale: 0.97 }}\n            animate={{ opacity: 1, y: 0, scale: 1 }}\n            exit={reduce ? { opacity: 0 } : { opacity: 0, y: -4, scale: 0.97, transition: { duration: 0.12 } }}\n            transition={springs.snap}\n            style={{ width: expandedWidth }}\n            className={cn(\n              \"absolute top-full z-30 mt-2 max-w-[calc(100vw-2rem)] overflow-hidden rounded-xl border bg-popover p-1 text-popover-foreground shadow-[var(--pc-e2)]\",\n              align === \"end\" ? \"end-0 origin-top-right rtl:origin-top-left\" : \"start-0 origin-top-left rtl:origin-top-right\",\n            )}\n          >\n            <div className=\"flex items-center justify-between px-2 pt-1 pb-1.5\">\n              <span className=\"text-xs font-medium text-muted-foreground\">{q ? l.matching : l.recent}</span>\n              {onClearRecent && (\n                <button\n                  type=\"button\"\n                  onMouseDown={(e) => e.preventDefault()}\n                  onClick={onClearRecent}\n                  className=\"pc-press rounded px-1 text-xs text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n                >\n                  {l.clearRecent}\n                </button>\n              )}\n            </div>\n            <ul id={listId} role=\"listbox\" aria-label={l.recent}>\n              {items.map((r, i) => (\n                <li\n                  key={r}\n                  id={`${listId}-${i}`}\n                  role=\"option\"\n                  aria-selected={i === activeIdx}\n                  onMouseDown={(e) => e.preventDefault()}\n                  onMouseEnter={() => setActiveIdx(i)}\n                  onClick={() => pick(r)}\n                  className=\"relative flex cursor-pointer items-center gap-2 rounded-lg px-2 py-1.5 text-sm\"\n                >\n                  {i === activeIdx && (\n                    <motion.span layoutId={`${uid}-hl`} className=\"absolute inset-0 rounded-lg bg-muted\" transition={reduce ? { duration: 0 } : springs.snap} />\n                  )}\n                  <Clock className=\"relative size-3.5 shrink-0 text-muted-foreground\" />\n                  <span className=\"relative flex-1 truncate\">{r}</span>\n                  {i === activeIdx && <CornerDownLeft className=\"relative size-3.5 text-muted-foreground\" />}\n                </li>\n              ))}\n            </ul>\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/search-expand.tsx"
    }
  ],
  "categories": [
    "components",
    "inputs"
  ],
  "type": "registry:component"
}