Skip to content

Prompt Input

The field where the person writes to the agent, with attachments, model and effort choices and a send button.

Installation

pnpm dlx shadcn@latest add @silkware/ai-prompt-input

To copy the pattern by hand, install the components it composes:

pnpm dlx shadcn@latest add @silkware/attachment @silkware/dropdown-menu @silkware/input-group

Then install the icon package and copy the pattern into components/ai-elements/prompt-input.tsx:

pnpm add lucide-react
components/ai-elements/prompt-input.tsx
"use client"
import * as React from "react"import {  ArrowUpIcon,  ChevronDownIcon,  FileTextIcon,  PlusIcon,  XIcon,} from "lucide-react"
import {  Attachment,  AttachmentAction,  AttachmentActions,  AttachmentContent,  AttachmentMedia,  AttachmentTitle,} from "@/components/ui/attachment"import {  DropdownMenu,  DropdownMenuGroup,  DropdownMenuItem,  DropdownMenuLabel,  DropdownMenuSeparator,  DropdownMenuTrigger,} from "@/components/ui/dropdown-menu"import {  InputGroup,  InputGroupAddon,  InputGroupButton,  InputGroupTextarea,} from "@/components/ui/input-group"
type AttachmentFile = {  id: string  name: string}
const providers = [  {    name: "OpenAI",    models: [      { id: "gpt-5", name: "GPT-5" },      { id: "gpt-5-mini", name: "GPT-5 mini" },    ],  },  {    name: "Anthropic",    models: [      { id: "claude-sonnet-4-5", name: "Claude Sonnet 4.5" },      { id: "claude-haiku-4-5", name: "Claude Haiku 4.5" },    ],  },]
const models = providers.flatMap((provider) => provider.models)
const efforts = [  { id: "low", name: "Low" },  { id: "medium", name: "Medium" },  { id: "high", name: "High" },]
export function PromptInputDemo() {  const [text, setText] = React.useState("")  const [files, setFiles] = React.useState<AttachmentFile[]>([])  const [model, setModel] = React.useState(models[0].id)  const [effort, setEffort] = React.useState(efforts[1].id)  const formRef = React.useRef<HTMLFormElement>(null)  const inputRef = React.useRef<HTMLInputElement>(null)  const currentModel = models.find((item) => item.id === model) ?? models[0]  const currentEffort = efforts.find((item) => item.id === effort) ?? efforts[1]  const canSend = text.trim().length > 0
  function onSubmit(event: React.FormEvent<HTMLFormElement>) {    event.preventDefault()    if (!canSend) {      return    }
    setText("")    setFiles([])  }
  function onAttach(event: React.ChangeEvent<HTMLInputElement>) {    const selected = event.target.files    if (!selected) {      return    }
    setFiles((current) => [      ...current,      ...Array.from(selected, (file) => ({        id: crypto.randomUUID(),        name: file.name,      })),    ])    event.target.value = ""  }
  return (    <div className="mx-auto w-full max-w-lg py-6">      <form ref={formRef} onSubmit={onSubmit}>        <input          ref={inputRef}          type="file"          multiple          hidden          className="sr-only"          tabIndex={-1}          onChange={onAttach}        />        <InputGroup>          {files.length > 0 && (            <InputGroupAddon align="block-start" className="flex-wrap">              {files.map((file) => (                <Attachment key={file.id} size="sm">                  <AttachmentMedia>                    <FileTextIcon />                  </AttachmentMedia>                  <AttachmentContent>                    <AttachmentTitle>{file.name}</AttachmentTitle>                  </AttachmentContent>                  <AttachmentActions>                    <AttachmentAction                      aria-label={`Remove ${file.name}`}                      onPress={() =>                        setFiles((current) =>                          current.filter((item) => item.id !== file.id)                        )                      }                    >                      <XIcon />                    </AttachmentAction>                  </AttachmentActions>                </Attachment>              ))}            </InputGroupAddon>          )}          <InputGroupTextarea            value={text}            onChange={(event) => {              setText(                typeof event === "string" ? event : event.currentTarget.value              )            }}            aria-label="Message"            placeholder="Ask the agent to change your code..."            onKeyDown={(event) => {              if (event.key === "Enter" && !event.shiftKey) {                event.preventDefault()                formRef.current?.requestSubmit()              }            }}            className="p-3"          />          <InputGroupAddon align="block-end">            <InputGroupButton              size="icon-sm"              variant="outline"              className="rounded-full"              aria-label="Attach files"              onPress={() => inputRef.current?.click()}            >              <PlusIcon />            </InputGroupButton>            <DropdownMenuTrigger>              <InputGroupButton                size="sm"                variant="outline"                aria-label={`Model: ${currentModel.name}`}                className="ml-auto"              >                {currentModel.name}                <ChevronDownIcon />              </InputGroupButton>              <DropdownMenu                className="w-48"                selectionMode="single"                disallowEmptySelection                selectedKeys={[model]}                onSelectionChange={(keys) => setModel([...keys][0] as string)}              >                {providers.map((provider, index) => (                  <React.Fragment key={provider.name}>                    {index > 0 && <DropdownMenuSeparator />}                    <DropdownMenuGroup>                      <DropdownMenuLabel>{provider.name}</DropdownMenuLabel>                      {provider.models.map((item) => (                        <DropdownMenuItem key={item.id} id={item.id}>                          {item.name}                        </DropdownMenuItem>                      ))}                    </DropdownMenuGroup>                  </React.Fragment>                ))}              </DropdownMenu>            </DropdownMenuTrigger>            <DropdownMenuTrigger>              <InputGroupButton                size="sm"                variant="outline"              >                <span className="text-muted-foreground">Effort</span>                {currentEffort.name}                <ChevronDownIcon />              </InputGroupButton>              <DropdownMenu                className="w-36"                selectionMode="single"                disallowEmptySelection                selectedKeys={[effort]}                onSelectionChange={(keys) => setEffort([...keys][0] as string)}              >                <DropdownMenuGroup>                  <DropdownMenuLabel>Effort</DropdownMenuLabel>                  {efforts.map((item) => (                    <DropdownMenuItem key={item.id} id={item.id}>                      {item.name}                    </DropdownMenuItem>                  ))}                </DropdownMenuGroup>              </DropdownMenu>            </DropdownMenuTrigger>            <InputGroupButton              type="submit"              size="icon-sm"              variant="default"              className="rounded-full"              isDisabled={!canSend}              aria-label="Send"            >              <ArrowUpIcon />            </InputGroupButton>          </InputGroupAddon>        </InputGroup>      </form>    </div>  )}

Usage

import {  InputGroup,  InputGroupAddon,  InputGroupButton,  InputGroupTextarea,} from "@/components/ui/input-group"
<form onSubmit={onSubmit}>  <InputGroup>    <InputGroupTextarea      value={text}      onChange={(event) =>        setText(typeof event === "string" ? event : event.currentTarget.value)      }      placeholder="Ask the agent..."    />    <InputGroupAddon align="block-end">      <InputGroupButton type="submit" size="icon-sm" isDisabled={!canSend} aria-label="Send">        <ArrowUpIcon />      </InputGroupButton>    </InputGroupAddon>  </InputGroup></form>

The textarea's onChange can receive a string or an event, so read it as the pattern does. The pattern submits on Enter and keeps Shift + Enter for a new line:

onKeyDown={(event) => {  if (event.key === "Enter" && !event.shiftKey) {    event.preventDefault()    formRef.current?.requestSubmit()  }}}

Always go through requestSubmit(), so the form's own onSubmit and its validation run.

Examples

Attachments

The plus button opens a hidden file input. Each chosen file becomes an Attachment in a block-start addon above the text, with a remove button labelled Remove <file name>. The attachments and the text clear when the message is sent. The pattern stores only the file names; you keep the File objects and send them with the message.

Model and effort

Two dropdown menus in the footer choose the model and how much effort the agent spends. They are single-selection menus with disallowEmptySelection, so one value is always chosen. The model trigger's aria-label includes the current value, for example "Model: GPT-5"; the effort trigger shows its label and value as text, which is its name. See Model Selector for the selector on its own.

While the agent works

When a reply is streaming, replace the send button with a stop button and say what is happening. Keep the field editable so the person can prepare the next message.

Giulia's price list is being compared

AI SDK

Keep the text in state, send it with sendMessage and clear the field. Send files in the same call. Branch on status: send while ready, stop while submitted or streaming.

const { sendMessage, status, stop } = useChat()const busy = status === "submitted" || status === "streaming"
function onSubmit(event: React.FormEvent<HTMLFormElement>) {  event.preventDefault()  if (busy || text.trim().length === 0) {    return  }
  void sendMessage({ text: text.trim(), files: fileList })  setText("")  setFiles([])}

files takes a FileList or an array of file parts. stop() aborts the request and keeps the text already received. Send the model and effort with the request, through sendMessage's second argument, then read them on the route:

void sendMessage({ text }, { body: { model, effort } })

During development, createChat in lib/ai.ts stands in for the model. The chat-demo pattern shows the offline version: it fills the field from chat.next(messages), so pressing send streams the next scripted reply.

Accessibility

KeyAction
EnterSends the message from the text, or opens a menu or presses a button when one has focus.
ShiftEnterAdds a new line.
TabMoves focus between the text, the attach button, the menus and the send button.
ArrowDownMoves to the next item in an open menu.
EscCloses an open menu and returns focus to its button.

The composed components provide:

  • A textarea inside a single bordered group, with one focus ring around the whole field.
  • React Aria menus with typeahead, arrow-key movement and focus return, and a selected state read to screen readers.
  • Attachments with their own remove button.

You provide:

  • A label for the textarea. The demo sets aria-label="Message", as the Chat pattern does; keep it, or use a visible label.
  • A label on every icon button: "Attach files", "Send", "Remove" followed by the file name.
  • A status message when a reply starts or stops, in a live region, such as role="status".
  • Focus that stays in the field after sending. Do not unmount it.

Guidelines

When to use

  • Where the person gives the agent an instruction, a question or a file.
  • As the one field of a Chat thread.

When not to use

  • For a short form with named fields. Use Field and Input.
  • For a search box. Use Command or an input group with a search icon.

Content

  • Write the placeholder as an invitation to act: "Ask the agent to change your code...".
  • Keep choices in the footer to the few that change the result, such as model and effort.
  • Show a file's name, not its path, and let the person remove it before sending.

Human in the loop

The person must be able to stop work they started. Show a stop control as soon as the agent is running and confirm that it stopped. Never submit on a keystroke the person did not mean: Enter sends, so keep Shift + Enter and the send button as visible alternatives.

Do

Disable the send button when the field is empty, and say why in the placeholder.

Don’t

Don’t lock the whole field while the agent works. The person cannot prepare the next message or stop the run.

Composition

ComponentWhat it does here
Input GroupThe bordered field: InputGroupTextarea for the text, InputGroupAddon for the rows above and below it, InputGroupButton for the controls.
AttachmentShows a chosen file with a remove button.
Dropdown MenuThe model and effort menus, with single selection.

The pattern exports one component, PromptInputDemo, which owns the text, files, model and effort. providers, models and efforts are plain arrays at the top of the file: replace them with your own list.