{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"expand-search","type":"registry:ui","title":"Expand Search","description":"A round search button that stretches into a field on press, focuses itself, pops in a clear button as you type, and folds back on Escape or when it loses focus empty.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/expand-search.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { Search, X } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { useEffect, useRef, useState, type FormEvent } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface ExpandSearchProps {\n  /** Width in px when open. */\n  width?: number;\n  placeholder?: string;\n  /** Which side stays put while the field grows. */\n  align?: \"left\" | \"right\";\n  /** Called with the query on Enter. */\n  onSearch?: (query: string) => void;\n  /** Called on every keystroke. */\n  onChange?: (query: string) => void;\n  className?: string;\n}\n\n/**\n * A round search button that stretches into a field when pressed, focuses\n * itself, and folds back into the button on Escape or when it loses focus empty.\n */\nexport function ExpandSearch({\n  width = 260,\n  placeholder = \"Search…\",\n  align = \"right\",\n  onSearch,\n  onChange,\n  className,\n}: ExpandSearchProps) {\n  const [open, setOpen] = useState(false);\n  const [query, setQuery] = useState(\"\");\n  const input = useRef<HTMLInputElement>(null);\n  const button = useRef<HTMLButtonElement>(null);\n\n  useEffect(() => {\n    if (open) input.current?.focus();\n  }, [open]);\n\n  const set = (q: string) => {\n    setQuery(q);\n    onChange?.(q);\n  };\n  const collapse = (refocus: boolean) => {\n    setOpen(false);\n    set(\"\");\n    if (refocus) requestAnimationFrame(() => button.current?.focus());\n  };\n  const submit = (e: FormEvent) => {\n    e.preventDefault();\n    if (query.trim()) onSearch?.(query.trim());\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div className={cn(\"flex\", align === \"right\" ? \"justify-end\" : \"justify-start\", className)}>\n        <motion.form\n          role=\"search\"\n          onSubmit={submit}\n          className=\"relative flex h-10 items-center overflow-hidden rounded-full border border-border bg-card text-foreground shadow-sm\"\n          initial={false}\n          animate={{ width: open ? width : 40 }}\n          transition={{ type: \"spring\", bounce: 0.22, duration: 0.45 }}\n          onBlur={(e) => {\n            if (!e.currentTarget.contains(e.relatedTarget as Node) && !query) collapse(false);\n          }}\n        >\n          <button\n            ref={button}\n            type={open ? \"submit\" : \"button\"}\n            aria-label={open ? \"Submit search\" : \"Open search\"}\n            aria-expanded={open}\n            onClick={() => !open && setOpen(true)}\n            className=\"grid size-10 shrink-0 place-items-center rounded-full text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring\"\n          >\n            <Search className=\"size-4\" />\n          </button>\n          <input\n            ref={input}\n            type=\"search\"\n            value={query}\n            onChange={(e) => set(e.target.value)}\n            onKeyDown={(e) => {\n              if (e.key === \"Escape\") {\n                e.preventDefault();\n                collapse(true);\n              }\n            }}\n            placeholder={placeholder}\n            aria-label=\"Search\"\n            tabIndex={open ? 0 : -1}\n            aria-hidden={!open}\n            className=\"h-full min-w-0 flex-1 bg-transparent pr-2 text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden\"\n          />\n          <AnimatePresence>\n            {open && query && (\n              <motion.button\n                type=\"button\"\n                aria-label=\"Clear search\"\n                onClick={() => {\n                  set(\"\");\n                  input.current?.focus();\n                }}\n                className=\"mr-2 grid size-6 shrink-0 place-items-center rounded-full bg-muted text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                initial={{ scale: 0, opacity: 0 }}\n                animate={{ scale: 1, opacity: 1 }}\n                exit={{ scale: 0, opacity: 0 }}\n                transition={{ type: \"spring\", bounce: 0.4, duration: 0.3 }}\n              >\n                <X className=\"size-3\" />\n              </motion.button>\n            )}\n          </AnimatePresence>\n        </motion.form>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}