{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ask-box","type":"registry:ui","title":"Ask Box","description":"AI prompt input that grows with your text, holds springy attachment chips, and morphs its send arrow into a stop button ringed by a spinner while generating.","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/ui/ask-box.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useId, useLayoutEffect, useRef, useState, type FormEvent, type KeyboardEvent } from \"react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { ArrowUp, FileText, Image as ImageIcon, Paperclip, Square, X } from \"lucide-react\";\n\nexport interface AskBoxProps {\n  /** Called on send. Return a promise to show the generating state until it settles. */\n  onSubmit?: (text: string, files: File[]) => void | Promise<unknown>;\n  /** Called when the stop button is pressed while generating. */\n  onStop?: () => void;\n  /** Force the generating state from outside. */\n  generating?: boolean;\n  /** Files attached on mount. */\n  defaultFiles?: File[];\n  placeholder?: string;\n  /** The field grows up to this many lines, then scrolls. */\n  maxRows?: number;\n  /** File input `accept` attribute. */\n  accept?: string;\n  className?: string;\n}\n\nconst spring = { type: \"spring\", stiffness: 500, damping: 30 } as const;\nconst LINE = 24;\n\nexport function AskBox({\n  onSubmit,\n  onStop,\n  generating = false,\n  defaultFiles = [],\n  placeholder = \"Ask anything…\",\n  maxRows = 6,\n  accept,\n  className,\n}: AskBoxProps) {\n  const id = useId();\n  const [text, setText] = useState(\"\");\n  const seq = useRef(0);\n  const tag = (list: File[]) => list.map((file) => ({ file, id: seq.current++ }));\n  const [files, setFiles] = useState(() => defaultFiles.map((file, id) => ({ file, id: -1 - id })));\n  const [busy, setBusy] = useState(false);\n  const [height, setHeight] = useState(LINE);\n  const mirror = useRef<HTMLTextAreaElement>(null);\n  const field = useRef<HTMLTextAreaElement>(null);\n  const picker = useRef<HTMLInputElement>(null);\n  const run = useRef(0);\n  const isGen = generating || busy;\n  const canSend = !isGen && (text.trim() !== \"\" || files.length > 0);\n\n  // Measure in a hidden twin so the visible field can animate its height.\n  useLayoutEffect(() => {\n    const m = mirror.current;\n    if (m) setHeight(Math.min(Math.max(m.scrollHeight, LINE), LINE * maxRows));\n  }, [text, maxRows]);\n\n  const send = (e?: FormEvent) => {\n    e?.preventDefault();\n    if (isGen) {\n      run.current++;\n      setBusy(false);\n      onStop?.();\n      return;\n    }\n    if (!canSend) return;\n    const result = onSubmit?.(text.trim(), files.map((f) => f.file));\n    setText(\"\");\n    setFiles([]);\n    field.current?.focus();\n    if (result instanceof Promise) {\n      const mine = ++run.current;\n      setBusy(true);\n      result.finally(() => mine === run.current && setBusy(false));\n    }\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLTextAreaElement>) => {\n    if (e.key === \"Enter\" && !e.shiftKey && !e.nativeEvent.isComposing) send(e);\n    if (e.key === \"Backspace\" && text === \"\" && files.length) setFiles((f) => f.slice(0, -1));\n  };\n\n  const textClass = \"block w-full resize-none bg-transparent px-1 text-[15px] leading-6 outline-none\";\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <form\n        onSubmit={send}\n        className={`w-full max-w-xl rounded-3xl border border-border bg-card p-2.5 shadow-xl transition-shadow focus-within:ring-[3px] focus-within:ring-ring/40 ${className ?? \"\"}`}\n      >\n        <AnimatePresence initial={false}>\n          {files.length > 0 && (\n            <motion.ul\n              key=\"chips\"\n              initial={{ height: 0, opacity: 0 }}\n              animate={{ height: \"auto\", opacity: 1 }}\n              exit={{ height: 0, opacity: 0 }}\n              className=\"flex flex-wrap gap-1.5 overflow-hidden px-1\"\n              aria-label=\"Attachments\"\n            >\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {files.map(({ file: f, id: key }) => {\n                  const Icon = f.type.startsWith(\"image/\") ? ImageIcon : FileText;\n                  return (\n                    <motion.li\n                      layout\n                      key={key}\n                      initial={{ scale: 0.4, opacity: 0, y: 8 }}\n                      animate={{ scale: 1, opacity: 1, y: 0 }}\n                      exit={{ scale: 0.4, opacity: 0 }}\n                      transition={{ type: \"spring\", stiffness: 520, damping: 22 }}\n                      className=\"mb-2 flex max-w-48 items-center gap-1.5 rounded-xl border border-border bg-muted py-1 pl-2 pr-1 text-xs text-foreground\"\n                    >\n                      <Icon className=\"size-3.5 shrink-0 text-muted-foreground\" aria-hidden />\n                      <span className=\"truncate\">{f.name}</span>\n                      <button\n                        type=\"button\"\n                        aria-label={`Remove ${f.name}`}\n                        onClick={() => setFiles((all) => all.filter((x) => x.id !== key))}\n                        className=\"grid size-5 shrink-0 place-items-center rounded-md text-muted-foreground outline-none hover:bg-background hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                      >\n                        <X className=\"size-3\" aria-hidden />\n                      </button>\n                    </motion.li>\n                  );\n                })}\n              </AnimatePresence>\n            </motion.ul>\n          )}\n        </AnimatePresence>\n\n        <div className=\"relative px-1.5 pt-1\">\n          <label htmlFor={id} className=\"sr-only\">\n            Prompt\n          </label>\n          <textarea\n            ref={mirror}\n            aria-hidden\n            tabIndex={-1}\n            readOnly\n            rows={1}\n            value={text + \"​\"}\n            className={`${textClass} pointer-events-none invisible absolute inset-x-1.5 top-1 h-0 overflow-hidden`}\n          />\n          <textarea\n            id={id}\n            ref={field}\n            rows={1}\n            value={text}\n            placeholder={placeholder}\n            onChange={(e) => setText(e.target.value)}\n            onKeyDown={onKey}\n            style={{ height, overflowY: height >= LINE * maxRows ? \"auto\" : \"hidden\", outline: \"none\" }} // the form shows the focus ring\n            className={`${textClass} text-foreground transition-[height] duration-200 ease-out placeholder:text-muted-foreground motion-reduce:transition-none`}\n          />\n        </div>\n\n        <div className=\"mt-2 flex items-center justify-between\">\n          <input\n            ref={picker}\n            type=\"file\"\n            multiple\n            accept={accept}\n            className=\"hidden\"\n            onChange={(e) => {\n              const picked = Array.from(e.target.files ?? []);\n              setFiles((f) => [...f, ...tag(picked)]);\n              e.target.value = \"\";\n            }}\n          />\n          <motion.button\n            type=\"button\"\n            aria-label=\"Attach files\"\n            onClick={() => picker.current?.click()}\n            whileHover={{ rotate: -18 }}\n            whileTap={{ scale: 0.85 }}\n            transition={spring}\n            className=\"grid size-9 place-items-center rounded-full text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n          >\n            <Paperclip className=\"size-[18px]\" aria-hidden />\n          </motion.button>\n\n          <div className=\"relative grid size-9 place-items-center\">\n            <AnimatePresence>\n              {isGen && (\n                <motion.svg\n                  key=\"ring\"\n                  aria-hidden\n                  viewBox=\"0 0 44 44\"\n                  className=\"pointer-events-none absolute -inset-1 size-11 animate-spin text-primary [animation-duration:900ms] motion-reduce:animate-none\"\n                  initial={{ opacity: 0, scale: 0.6 }}\n                  animate={{ opacity: 1, scale: 1 }}\n                  exit={{ opacity: 0, scale: 0.6 }}\n                >\n                  <circle cx=\"22\" cy=\"22\" r=\"20\" fill=\"none\" stroke=\"currentColor\" strokeOpacity=\".15\" strokeWidth=\"2.5\" />\n                  <path d=\"M22 2a20 20 0 0 1 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.5\" strokeLinecap=\"round\" />\n                </motion.svg>\n              )}\n            </AnimatePresence>\n            <motion.button\n              type=\"submit\"\n              aria-label={isGen ? \"Stop generating\" : \"Send\"}\n              disabled={!canSend && !isGen}\n              initial={false}\n              animate={{ borderRadius: isGen ? 10 : 18, scale: isGen ? 0.82 : 1 }}\n              whileTap={{ scale: 0.72 }}\n              transition={spring}\n              className=\"grid size-9 place-items-center bg-primary text-primary-foreground outline-none transition-opacity focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card disabled:opacity-30\"\n            >\n              <AnimatePresence mode=\"popLayout\" initial={false}>\n                {isGen ? (\n                  <motion.span key=\"stop\" initial={{ scale: 0, rotate: -90 }} animate={{ scale: 1, rotate: 0 }} exit={{ scale: 0 }} transition={spring}>\n                    <Square className=\"size-3.5 fill-current\" aria-hidden />\n                  </motion.span>\n                ) : (\n                  <motion.span key=\"send\" initial={{ y: 14, opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: -16, opacity: 0 }} transition={spring}>\n                    <ArrowUp className=\"size-[18px]\" strokeWidth={2.5} aria-hidden />\n                  </motion.span>\n                )}\n              </AnimatePresence>\n            </motion.button>\n          </div>\n        </div>\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {isGen ? \"Generating\" : \"\"}\n        </span>\n      </form>\n    </MotionConfig>\n  );\n}\n"}]}