{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"prompt-input-01","title":"Prompt Input","description":"A chat composer: a growing message field, file attachments with previews, a tools menu (web search, deep research, image), and a send button that turns into stop while the answer streams.","dependencies":["cn","lucide-react"],"registryDependencies":["@sortui/attachment","@sortui/dropdown-menu","@sortui/input-group","@sortui/tooltip"],"files":[{"path":"registry/blocks/prompt-input-01/components/prompt-input.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport {\n  ArrowUpIcon,\n  FileTextIcon,\n  GlobeIcon,\n  ImageIcon,\n  MicIcon,\n  PaperclipIcon,\n  PlusIcon,\n  SquareIcon,\n  TelescopeIcon,\n  XIcon,\n} from \"lucide-react\"\nimport { cn } from \"cn\"\n\nimport {\n  Attachment,\n  AttachmentAction,\n  AttachmentActions,\n  AttachmentContent,\n  AttachmentDescription,\n  AttachmentMedia,\n  AttachmentTitle,\n} from \"@/components/ui/attachment\"\nimport {\n  DropdownMenu,\n  DropdownMenuContent,\n  DropdownMenuGroup,\n  DropdownMenuItem,\n  DropdownMenuLabel,\n  DropdownMenuSeparator,\n  DropdownMenuTrigger,\n} from \"@/components/ui/dropdown-menu\"\nimport {\n  InputGroup,\n  InputGroupAddon,\n  InputGroupButton,\n  InputGroupTextarea,\n} from \"@/components/ui/input-group\"\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\"\n\nexport type PromptTool = \"search\" | \"research\" | \"image\"\n\nexport type PromptSubmission = {\n  text: string\n  files: File[]\n  tool: PromptTool | null\n}\n\nexport const promptTools: {\n  id: PromptTool\n  label: string\n  icon: React.ComponentType\n}[] = [\n  { id: \"search\", label: \"Web search\", icon: GlobeIcon },\n  { id: \"research\", label: \"Deep research\", icon: TelescopeIcon },\n  { id: \"image\", label: \"Create image\", icon: ImageIcon },\n]\n\nexport function PromptInput({\n  onSubmit,\n  onStop,\n  onDictate,\n  status = \"ready\",\n  placeholder = \"Ask anything\",\n  autoFocus,\n  className,\n}: {\n  onSubmit?: (submission: PromptSubmission) => void\n  onStop?: () => void\n  onDictate?: () => void\n  status?: \"ready\" | \"busy\"\n  placeholder?: string\n  autoFocus?: boolean\n  className?: string\n}) {\n  const [text, setText] = React.useState(\"\")\n  const [files, setFiles] = React.useState<PickedFile[]>([])\n  const [tool, setTool] = React.useState<PromptTool | null>(null)\n  const fileInput = React.useRef<HTMLInputElement>(null)\n\n  const previews = React.useRef(new Set<string>())\n  const nextId = React.useRef(0)\n  React.useEffect(() => {\n    const urls = previews.current\n    return () => urls.forEach((url) => URL.revokeObjectURL(url))\n  }, [])\n\n  function release(items: PickedFile[]) {\n    for (const { preview } of items) {\n      if (!preview) continue\n      URL.revokeObjectURL(preview)\n      previews.current.delete(preview)\n    }\n  }\n\n  const busy = status === \"busy\"\n  const canSend = !busy && (text.trim().length > 0 || files.length > 0)\n  const activeTool = promptTools.find((item) => item.id === tool)\n\n  function submit() {\n    if (!canSend) return\n    onSubmit?.({\n      text: text.trim(),\n      files: files.map((item) => item.file),\n      tool,\n    })\n    release(files)\n    setText(\"\")\n    setFiles([])\n  }\n\n  return (\n    <form\n      className={cn(\"w-full\", className)}\n      onSubmit={(event) => {\n        event.preventDefault()\n        submit()\n      }}\n    >\n      <InputGroup className=\"rounded-3xl\">\n        {files.length > 0 && (\n          <InputGroupAddon align=\"block-start\" className=\"flex-wrap gap-2\">\n            {files.map((item) => (\n              <FileChip\n                key={item.id}\n                file={item.file}\n                preview={item.preview}\n                onRemove={() => {\n                  release([item])\n                  setFiles((current) =>\n                    current.filter((other) => other.id !== item.id)\n                  )\n                }}\n              />\n            ))}\n          </InputGroupAddon>\n        )}\n        <InputGroupTextarea\n          aria-label=\"Message\"\n          placeholder={placeholder}\n          autoFocus={autoFocus}\n          value={text}\n          onChange={(event) => setText(event.target.value)}\n          onKeyDown={(event) => {\n            if (\n              event.key === \"Enter\" &&\n              !event.shiftKey &&\n              !event.nativeEvent.isComposing &&\n              event.keyCode !== 229\n            ) {\n              event.preventDefault()\n              submit()\n            }\n          }}\n          className=\"max-h-52 min-h-14\"\n        />\n        <InputGroupAddon align=\"block-end\" className=\"gap-1\">\n          <DropdownMenu>\n            <DropdownMenuTrigger\n              render={\n                <InputGroupButton\n                  size=\"icon-sm\"\n                  variant=\"outline\"\n                  aria-label=\"Add files and tools\"\n                />\n              }\n            >\n              <PlusIcon />\n            </DropdownMenuTrigger>\n            <DropdownMenuContent side=\"top\" align=\"start\" className=\"w-52\">\n              <DropdownMenuItem onClick={() => fileInput.current?.click()}>\n                <PaperclipIcon />\n                Add photos & files\n              </DropdownMenuItem>\n              <DropdownMenuSeparator />\n              <DropdownMenuGroup>\n                <DropdownMenuLabel>Tools</DropdownMenuLabel>\n                {promptTools.map(({ id, label, icon: Icon }) => (\n                  <DropdownMenuItem key={id} onClick={() => setTool(id)}>\n                    <Icon />\n                    {label}\n                  </DropdownMenuItem>\n                ))}\n              </DropdownMenuGroup>\n            </DropdownMenuContent>\n          </DropdownMenu>\n          {activeTool && (\n            <InputGroupButton\n              size=\"sm\"\n              variant=\"secondary\"\n              aria-label={`Turn off ${activeTool.label.toLowerCase()}`}\n              onClick={() => setTool(null)}\n            >\n              <activeTool.icon />\n              {activeTool.label}\n              <XIcon className=\"text-muted-foreground\" />\n            </InputGroupButton>\n          )}\n          <div className=\"ml-auto flex items-center gap-1\">\n            <Tooltip>\n              <TooltipTrigger\n                render={\n                  <InputGroupButton\n                    size=\"icon-sm\"\n                    aria-label=\"Dictate\"\n                    onClick={onDictate}\n                  />\n                }\n              >\n                <MicIcon />\n              </TooltipTrigger>\n              <TooltipContent>Dictate</TooltipContent>\n            </Tooltip>\n            {busy ? (\n              <InputGroupButton\n                size=\"icon-sm\"\n                variant=\"default\"\n                aria-label=\"Stop generating\"\n                onClick={onStop}\n              >\n                <SquareIcon className=\"fill-current\" />\n              </InputGroupButton>\n            ) : (\n              <InputGroupButton\n                type=\"submit\"\n                size=\"icon-sm\"\n                variant=\"default\"\n                aria-label=\"Send\"\n                disabled={!canSend}\n              >\n                <ArrowUpIcon />\n              </InputGroupButton>\n            )}\n          </div>\n        </InputGroupAddon>\n        <input\n          ref={fileInput}\n          type=\"file\"\n          multiple\n          hidden\n          onChange={(event) => {\n            const picked = Array.from(event.target.files ?? []).map((file) => {\n              const preview = file.type.startsWith(\"image/\")\n                ? URL.createObjectURL(file)\n                : null\n              if (preview) previews.current.add(preview)\n              return { id: nextId.current++, file, preview }\n            })\n            setFiles((current) => [...current, ...picked])\n            event.target.value = \"\"\n          }}\n        />\n      </InputGroup>\n    </form>\n  )\n}\n\ntype PickedFile = { id: number; file: File; preview: string | null }\n\nfunction FileChip({\n  file,\n  preview,\n  onRemove,\n}: {\n  file: File\n  preview: string | null\n  onRemove: () => void\n}) {\n  return (\n    <Attachment\n      type={preview ? \"image\" : \"file\"}\n      className={preview ? \"size-13.5\" : \"w-56\"}\n    >\n      <AttachmentMedia variant={preview ? \"image\" : \"icon\"}>\n        {preview ? <img src={preview} alt=\"\" /> : <FileTextIcon />}\n      </AttachmentMedia>\n      <AttachmentContent className={preview ? \"sr-only\" : undefined}>\n        <AttachmentTitle>{file.name}</AttachmentTitle>\n        <AttachmentDescription>{formatSize(file.size)}</AttachmentDescription>\n      </AttachmentContent>\n      <AttachmentActions>\n        <AttachmentAction aria-label={`Remove ${file.name}`} onClick={onRemove}>\n          <XIcon />\n        </AttachmentAction>\n      </AttachmentActions>\n    </Attachment>\n  )\n}\n\nfunction formatSize(bytes: number) {\n  if (bytes < 1024) return `${bytes} B`\n  if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`\n  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n}\n","type":"registry:component"}],"meta":{"kind":"block","pro":false,"iframeHeight":420},"categories":["ai"],"type":"registry:block"}