Prompt Input

The Prompt Input 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

pnpm dlx shadcn-svelte@latest add https://svelte-ai-elements.vercel.app/r/prompt-input.json

If you prefer using jsrepo, please install via the command below:

pnpm dlx jsrepo add @ai/elements/ai-elements/prompt-input

Usage

Usage with AI SDK

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

Add the following component to your frontend:

+page.svelte

Add the following route to your backend:

api/chat/+server.ts

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
  • 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)

Props

PromptInput

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the form container
acceptstring-File types to accept (e.g., 'image/*' or leave undefined for any)
multipleboolean-Whether to allow multiple file uploads
globalDropboolean-When true, accepts drops anywhere on document. Default false (opt-in)
syncHiddenInputboolean-When true, renders a hidden input with given name and keeps it in sync for native form posts. Default false
clearOnSubmitbooleantrueWhen true, automatically clears attachments after successful submission
maxFilesnumber-Maximum number of files allowed
maxFileSizenumber-Maximum file size in bytes
onError(err: { code: 'max_files' | 'max_file_size' | 'accept'; message: string }) => void-Error handler for file validation errors
onSubmit(message: PromptInputMessage, event: SubmitEvent) => void | Promise<void>-Submit handler called when the form is submitted
childrenSnippet-Child components (typically PromptInputBody, PromptInputToolbar)

PromptInputProvider

PropTypeDefaultDescription
initialInputstring""Initial input value for the textarea
acceptstring-File types to accept for attachments
multiplebooleantrueWhether to allow multiple file uploads
childrenSnippet-Provider content

PromptInputBody

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the body container
childrenSnippet-Body content (typically PromptInputTextarea and PromptInputAttachments)

PromptInputTextarea

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the textarea
placeholderstring"What would you like to know?"Placeholder text for the textarea
valuestring-Current textarea value (bindable)
onchange(event: Event) => void-Change event handler

PromptInputAttachments

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the attachments container
childrenSnippet<[FileWithId]>-Render function for each attachment (receives file data)

PromptInputAttachment

PropTypeDefaultDescription
dataFileWithId-File data object containing id, filename, mediaType, and url
classstring-Additional CSS classes to apply to the attachment container

PromptInputToolbar

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the toolbar container
childrenSnippet-Toolbar content (typically PromptInputTools and PromptInputSubmit)

PromptInputTools

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the tools container
childrenSnippet-Tool buttons (typically PromptInputButton or PromptInputActionMenu)

PromptInputButton

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the button
childrenSnippet-Button content
variant'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link''ghost'Visual style variant of the button
size'default' | 'sm' | 'lg' | 'icon'-Size of the button (auto-calculated based on children if not provided)

PromptInputSubmit

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the submit button
statusChatStatus-Current chat status ('submitted', 'streaming', 'error', or undefined) - changes the icon displayed
childrenSnippet-Custom button content (defaults to status-based icon if not provided)
variant'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link''default'Visual style variant of the button
size'default' | 'sm' | 'lg' | 'icon''icon'Size of the button

PromptInputActionMenu

PropTypeDefaultDescription
openboolean-Whether the dropdown menu is open (bindable)
childrenSnippet-Menu content (typically PromptInputActionMenuTrigger and PromptInputActionMenuContent)

PromptInputActionMenuTrigger

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the trigger button
childrenSnippet-Trigger content (defaults to PlusIcon if not provided)

PromptInputActionMenuContent

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the dropdown content
childrenSnippet-Menu items (typically PromptInputActionMenuItem components)

PromptInputActionMenuItem

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the menu item
onSelect(event: Event) => void-Handler called when the menu item is selected
childrenSnippet-Menu item content

PromptInputActionAddAttachments

PropTypeDefaultDescription
labelstring"Add photos or files"Label text for the menu item
classstring-Additional CSS classes to apply to the menu item

PromptInputModelSelect

PropTypeDefaultDescription
valuestring-Currently selected model value (bindable)
onValueChange(value: string | undefined) => void-Callback when the selected value changes
childrenSnippet-Select content (typically PromptInputModelSelectTrigger and PromptInputModelSelectContent)

PromptInputModelSelectTrigger

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the select trigger
childrenSnippet-Trigger content (typically PromptInputModelSelectValue)

PromptInputModelSelectValue

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the value display
placeholderstring-Placeholder text when no value is selected
valuestring-Current selected value to display

PromptInputModelSelectContent

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the select dropdown content
childrenSnippet-Select items (typically PromptInputModelSelectItem components)

PromptInputModelSelectItem

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the select item
valuestring-Value of the select item
childrenSnippet-Item content