Prompt Input

The PromptInput component allows a user to send a message with file attachments to a large language model. It includes a textarea, file upload capabilities, a submit button, and a dropdown for selecting the model.

Installation

npx ai-elements@latest add prompt-input

Usage

import {
PromptInput,
PromptInputActionAddAttachments,
PromptInputActionMenu,
PromptInputActionMenuContent,
PromptInputActionMenuItem,
PromptInputActionMenuTrigger,
PromptInputAttachment,
PromptInputAttachments,
PromptInputBody,
PromptInputButton,
PromptInputProvider,
PromptInputSpeechButton,
PromptInputSubmit,
PromptInputTextarea,
PromptInputFooter,
PromptInputTools,
usePromptInputAttachments,
} from '@/components/ai-elements/prompt-input';
import { GlobeIcon } from 'lucide-react';
<PromptInput onSubmit={() => {}} className="mt-4 relative">
<PromptInputBody>
<PromptInputAttachments>
{(attachment) => (
<PromptInputAttachment data={attachment} />
)}
</PromptInputAttachments>
<PromptInputTextarea onChange={(e) => {}} value={''} />
</PromptInputBody>
<PromptInputFooter>
<PromptInputTools>
<PromptInputActionMenu>
<PromptInputActionMenuTrigger />
<PromptInputActionMenuContent>
<PromptInputActionAddAttachments />
</PromptInputActionMenuContent>
</PromptInputActionMenu>
<PromptInputSpeechButton />
<PromptInputButton>
<GlobeIcon size={16} />
<span>Search</span>
</PromptInputButton>
<PromptInputModelSelect onValueChange={(value) => {}} value="gpt-4o">
<PromptInputModelSelectTrigger>
<PromptInputModelSelectValue />
</PromptInputModelSelectTrigger>
<PromptInputModelSelectContent>
<PromptInputModelSelectItem value="gpt-4o">
GPT-4o
</PromptInputModelSelectItem>
<PromptInputModelSelectItem value="claude-opus-4-20250514">
Claude 4 Opus
</PromptInputModelSelectItem>
</PromptInputModelSelectContent>
</PromptInputModelSelect>
</PromptInputTools>
<PromptInputSubmit
disabled={false}
status={'ready'}
/>
</PromptInputFooter>
</PromptInput>

Usage with AI SDK

Built a fully functional chat app using PromptInput, Conversation with a model picker:

Add the following component to your frontend:

app/page.tsx
'use client';
import {
PromptInput,
PromptInputActionAddAttachments,
PromptInputActionMenu,
PromptInputActionMenuContent,
PromptInputActionMenuTrigger,
PromptInputAttachment,
PromptInputAttachments,
PromptInputBody,
PromptInputButton,
type PromptInputMessage,
PromptInputModelSelect,
PromptInputModelSelectContent,
PromptInputModelSelectItem,
PromptInputModelSelectTrigger,
PromptInputModelSelectValue,
PromptInputSpeechButton,
PromptInputSubmit,
PromptInputTextarea,
PromptInputFooter,
PromptInputTools,
} from '@/components/ai-elements/prompt-input';
import { GlobeIcon } from 'lucide-react';
import { useRef, useState } from 'react';
import { useChat } from '@ai-sdk/react';
import {
Conversation,
ConversationContent,
ConversationScrollButton,
} from '@/components/ai-elements/conversation';
import { Message, MessageContent } from '@/components/ai-elements/message';
import { Response } from '@/components/ai-elements/response';
const models = [
{ id: 'gpt-4o', name: 'GPT-4o' },
{ id: 'claude-opus-4-20250514', name: 'Claude 4 Opus' },
];
const InputDemo = () => {
const [text, setText] = useState<string>('');
const [model, setModel] = useState<string>(models[0].id);
const [useWebSearch, setUseWebSearch] = useState<boolean>(false);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const { messages, status, sendMessage } = useChat();
const handleSubmit = (message: PromptInputMessage) => {
const hasText = Boolean(message.text);
const hasAttachments = Boolean(message.files?.length);
if (!(hasText || hasAttachments)) {
return;
}
sendMessage(
{
text: message.text || 'Sent with attachments',
files: message.files
},
{
body: {
model: model,
webSearch: useWebSearch,
},
},
);
setText('');
};
return (
<div className="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]">
<div className="flex flex-col h-full">
<Conversation>
<ConversationContent>
{messages.map((message) => (
<Message from={message.role} key={message.id}>
<MessageContent>
{message.parts.map((part, i) => {
switch (part.type) {
case 'text':
return (
<Response key={`${message.id}-${i}`}>
{part.text}
</Response>
);
default:
return null;
}
})}
</MessageContent>
</Message>
))}
</ConversationContent>
<ConversationScrollButton />
</Conversation>
<PromptInput onSubmit={handleSubmit} className="mt-4" globalDrop multiple>
<PromptInputBody>
<PromptInputAttachments>
{(attachment) => <PromptInputAttachment data={attachment} />}
</PromptInputAttachments>
<PromptInputTextarea
onChange={(e) => setText(e.target.value)}
ref={textareaRef}
value={text}
/>
</PromptInputBody>
<PromptInputFooter>
<PromptInputTools>
<PromptInputActionMenu>
<PromptInputActionMenuTrigger />
<PromptInputActionMenuContent>
<PromptInputActionAddAttachments />
</PromptInputActionMenuContent>
</PromptInputActionMenu>
<PromptInputSpeechButton
onTranscriptionChange={setText}
textareaRef={textareaRef}
/>
<PromptInputButton
onClick={() => setUseWebSearch(!useWebSearch)}
variant={useWebSearch ? 'default' : 'ghost'}
>
<GlobeIcon size={16} />
<span>Search</span>
</PromptInputButton>
<PromptInputModelSelect
onValueChange={(value) => {
setModel(value);
}}
value={model}
>
<PromptInputModelSelectTrigger>
<PromptInputModelSelectValue />
</PromptInputModelSelectTrigger>
<PromptInputModelSelectContent>
{models.map((model) => (
<PromptInputModelSelectItem key={model.id} value={model.id}>
{model.name}
</PromptInputModelSelectItem>
))}
</PromptInputModelSelectContent>
</PromptInputModelSelect>
</PromptInputTools>
<PromptInputSubmit disabled={!text && !status} status={status} />
</PromptInputFooter>
</PromptInput>
</div>
</div>
);
};
export default InputDemo;

Add the following route to your backend:

app/api/chat/route.ts
import { streamText, UIMessage, convertToModelMessages } from 'ai';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const {
model,
messages,
webSearch
}: {
messages: UIMessage[];
model: string;
webSearch?: boolean;
} = await req.json();
const result = streamText({
model: webSearch ? 'perplexity/sonar' : model,
messages: convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse();
}

Features

  • Auto-resizing textarea that adjusts height based on content
  • File attachment support with drag-and-drop
  • Image preview for image attachments
  • Configurable file constraints (max files, max size, accepted types)
  • Automatic submit button icons based on status
  • Support for keyboard shortcuts (Enter to submit, Shift+Enter for new line)
  • Customizable min/max height for the textarea
  • Flexible toolbar with support for custom actions and tools
  • Built-in model selection dropdown
  • Built-in native speech recognition button (Web Speech API)
  • Optional provider for lifted state management
  • Form automatically resets on submit
  • Responsive design with mobile-friendly controls
  • Clean, modern styling with customizable themes
  • Form-based submission handling
  • Hidden file input sync for native form posts
  • Global document drop support (opt-in)

Examples

Cursor style

Props

<PromptInput />

onSubmit:

(message: PromptInputMessage, event: FormEvent) => void
Handler called when the form is submitted with message text and files.

accept?:

string
File types to accept (e.g., "image/*"). Leave undefined for any.

multiple?:

boolean
Whether to allow multiple file selection.

globalDrop?:

boolean
When true, accepts file drops anywhere on the document.

syncHiddenInput?:

boolean
Render a hidden input with given name for native form posts.

maxFiles?:

number
Maximum number of files allowed.

maxFileSize?:

number
Maximum file size in bytes.

onError?:

(err: { code: "max_files" | "max_file_size" | "accept", message: string }) => void
Handler for file validation errors.

[...props]?:

React.HTMLAttributes<HTMLFormElement>
Any other props are spread to the root form element.

<PromptInputTextarea />

[...props]?:

React.ComponentProps<typeof Textarea>
Any other props are spread to the underlying Textarea component.

<PromptInputFooter />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the toolbar div.

<PromptInputTools />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the tools div.

<PromptInputButton />

[...props]?:

React.ComponentProps<typeof Button>
Any other props are spread to the underlying shadcn/ui Button component.

<PromptInputSubmit />

status?:

ChatStatus
Current chat status to determine button icon (submitted, streaming, error).

[...props]?:

React.ComponentProps<typeof Button>
Any other props are spread to the underlying shadcn/ui Button component.

<PromptInputModelSelect />

[...props]?:

React.ComponentProps<typeof Select>
Any other props are spread to the underlying Select component.

<PromptInputModelSelectTrigger />

[...props]?:

React.ComponentProps<typeof SelectTrigger>
Any other props are spread to the underlying SelectTrigger component.

<PromptInputModelSelectContent />

[...props]?:

React.ComponentProps<typeof SelectContent>
Any other props are spread to the underlying SelectContent component.

<PromptInputModelSelectItem />

[...props]?:

React.ComponentProps<typeof SelectItem>
Any other props are spread to the underlying SelectItem component.

<PromptInputModelSelectValue />

[...props]?:

React.ComponentProps<typeof SelectValue>
Any other props are spread to the underlying SelectValue component.

<PromptInputBody />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the body div.

<PromptInputAttachments />

children:

(attachment: FileUIPart & { id: string }) => React.ReactNode
Render function for each attachment.

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the attachments container.

<PromptInputAttachment />

data:

FileUIPart & { id: string }
The attachment data to display.

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the attachment div.

<PromptInputActionMenu />

[...props]?:

React.ComponentProps<typeof DropdownMenu>
Any other props are spread to the underlying DropdownMenu component.

<PromptInputActionMenuTrigger />

[...props]?:

React.ComponentProps<typeof Button>
Any other props are spread to the underlying Button component.

<PromptInputActionMenuContent />

[...props]?:

React.ComponentProps<typeof DropdownMenuContent>
Any other props are spread to the underlying DropdownMenuContent component.

<PromptInputActionMenuItem />

[...props]?:

React.ComponentProps<typeof DropdownMenuItem>
Any other props are spread to the underlying DropdownMenuItem component.

<PromptInputActionAddAttachments />

label?:

string
Label for the menu item. Defaults to "Add photos or files".

[...props]?:

React.ComponentProps<typeof DropdownMenuItem>
Any other props are spread to the underlying DropdownMenuItem component.

<PromptInputProvider />

initialInput?:

string
Initial text input value.

children:

React.ReactNode
Child components that will have access to the provider context.

Optional global provider that lifts PromptInput state outside of PromptInput. When used, it allows you to access and control the input state from anywhere within the provider tree. If not used, PromptInput stays fully self-managed.

<PromptInputSpeechButton />

textareaRef?:

RefObject<HTMLTextAreaElement | null>
Reference to the textarea element to insert transcribed text.

onTranscriptionChange?:

(text: string) => void
Callback fired when transcription text changes.

[...props]?:

React.ComponentProps<typeof PromptInputButton>
Any other props are spread to the underlying PromptInputButton component.

Built-in button component that provides native speech recognition using the Web Speech API. The button will be disabled if speech recognition is not supported in the browser. Displays a microphone icon and pulses while actively listening.

Hooks

usePromptInputAttachments

Access and manage file attachments within a PromptInput context.

const attachments = usePromptInputAttachments();
// Available methods:
attachments.files // Array of current attachments
attachments.add(files) // Add new files
attachments.remove(id) // Remove an attachment by ID
attachments.clear() // Clear all attachments
attachments.openFileDialog() // Open file selection dialog

usePromptInputController

Access the full PromptInput controller from a PromptInputProvider. Only available when using the provider.

const controller = usePromptInputController();
// Available methods:
controller.textInput.value // Current text input value
controller.textInput.setInput(value) // Set text input value
controller.textInput.clear() // Clear text input
controller.attachments // Same as usePromptInputAttachments

useProviderAttachments

Access attachments context from a PromptInputProvider. Only available when using the provider.

const attachments = useProviderAttachments();
// Same interface as usePromptInputAttachments

<PromptInputHeader />

[...props]?:

Omit<React.ComponentProps<typeof InputGroupAddon>, "align">
Any other props (except align) are spread to the InputGroupAddon component.

<PromptInputHoverCard />

openDelay?:

number
Delay in milliseconds before opening. Defaults to 0.

closeDelay?:

number
Delay in milliseconds before closing. Defaults to 0.

[...props]?:

React.ComponentProps<typeof HoverCard>
Any other props are spread to the HoverCard component.

<PromptInputHoverCardTrigger />

[...props]?:

React.ComponentProps<typeof HoverCardTrigger>
Any other props are spread to the HoverCardTrigger component.

<PromptInputHoverCardContent />

align?:

"start" | "center" | "end"
Alignment of the hover card content. Defaults to "start".

[...props]?:

React.ComponentProps<typeof HoverCardContent>
Any other props are spread to the HoverCardContent component.

<PromptInputTabsList />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the div element.

<PromptInputTab />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the div element.

<PromptInputTabLabel />

[...props]?:

React.HTMLAttributes<HTMLHeadingElement>
Any other props are spread to the h3 element.

<PromptInputTabBody />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the div element.

<PromptInputTabItem />

[...props]?:

React.HTMLAttributes<HTMLDivElement>
Any other props are spread to the div element.

<PromptInputCommand />

[...props]?:

React.ComponentProps<typeof Command>
Any other props are spread to the Command component.

<PromptInputCommandInput />

[...props]?:

React.ComponentProps<typeof CommandInput>
Any other props are spread to the CommandInput component.

<PromptInputCommandList />

[...props]?:

React.ComponentProps<typeof CommandList>
Any other props are spread to the CommandList component.

<PromptInputCommandEmpty />

[...props]?:

React.ComponentProps<typeof CommandEmpty>
Any other props are spread to the CommandEmpty component.

<PromptInputCommandGroup />

[...props]?:

React.ComponentProps<typeof CommandGroup>
Any other props are spread to the CommandGroup component.

<PromptInputCommandItem />

[...props]?:

React.ComponentProps<typeof CommandItem>
Any other props are spread to the CommandItem component.

<PromptInputCommandSeparator />

[...props]?:

React.ComponentProps<typeof CommandSeparator>
Any other props are spread to the CommandSeparator component.