Skip to content
WebJs
Menu

Prompt Input

A message field with attachments and a send button.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import {
  inputGroupClass, inputGroupAddonClass, inputGroupButtonClass, inputGroupTextareaClass,
} from '#components/ui/input-group.ts';
import { ArrowUpIcon, ChevronDownIcon, PlusIcon } from '#lib/ui/lucide.ts';

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 efforts = [
  { id: 'low', name: 'Low' },
  { id: 'medium', name: 'Medium' },
  { id: 'high', name: 'High' },
];

const model = providers[0].models[0];
const effort = efforts[1];

// The field before anything is typed, so Send starts disabled. The attach
// button is a <label> for the hidden file input, which opens the file picker
// with no script. Showing the picked files as attachments, enabling Send and
// submitting on Enter are the reader's to wire (see Attachments and Keyboard).
export default function PromptInputDemo() {
  return html`
    <div class="mx-auto w-full max-w-lg py-6">
      <form>
        <input id="prompt-input-files" type="file" name="files" multiple tabindex="-1" class="sr-only">
        <div data-slot="input-group" role="group" class=${inputGroupClass()}>
          <textarea data-slot="input-group-control" name="text" placeholder="Ask the agent to change your code..." aria-label="Message" class=${cn(inputGroupTextareaClass(), 'p-3')}></textarea>
          <div role="group" data-slot="input-group-addon" data-align="block-end" class=${inputGroupAddonClass({ align: 'block-end' })}>
            <label for="prompt-input-files" aria-label="Attach files" data-slot="button" data-variant="outline" data-size="icon-sm" class=${cn(inputGroupButtonClass({ variant: 'outline', size: 'icon-sm' }), 'cursor-pointer rounded-full')}>
              ${PlusIcon()}
            </label>
            <ui-dropdown-menu class="ml-auto">
              <ui-dropdown-menu-trigger>
                <button type="button" aria-label=${`Model: ${model.name}`} data-slot="button" data-variant="outline" data-size="sm" class=${inputGroupButtonClass({ variant: 'outline', size: 'sm' })}>
                  ${model.name}
                  ${ChevronDownIcon()}
                </button>
              </ui-dropdown-menu-trigger>
              <ui-dropdown-menu-content align="start" class="w-48">
                <ui-dropdown-menu-group aria-label="Model">
                  ${providers.map((provider, index) => html`
                    ${index > 0 ? html`<ui-dropdown-menu-separator></ui-dropdown-menu-separator>` : ''}
                    <ui-dropdown-menu-label>${provider.name}</ui-dropdown-menu-label>
                    ${provider.models.map((item) => html`
                      <ui-dropdown-menu-item type="radio" value=${item.id} ?checked=${item.id === model.id}>${item.name}</ui-dropdown-menu-item>
                    `)}
                  `)}
                </ui-dropdown-menu-group>
              </ui-dropdown-menu-content>
            </ui-dropdown-menu>
            <ui-dropdown-menu>
              <ui-dropdown-menu-trigger>
                <button type="button" aria-label=${`Effort: ${effort.name}`} data-slot="button" data-variant="outline" data-size="sm" class=${inputGroupButtonClass({ variant: 'outline', size: 'sm' })}>
                  <span class="text-muted-foreground">Effort</span>
                  ${effort.name}
                  ${ChevronDownIcon()}
                </button>
              </ui-dropdown-menu-trigger>
              <ui-dropdown-menu-content align="start" class="w-36">
                <ui-dropdown-menu-group>
                  <ui-dropdown-menu-label>Effort</ui-dropdown-menu-label>
                  <ui-dropdown-menu-group aria-label="Effort">
                    ${efforts.map((item) => html`
                      <ui-dropdown-menu-item type="radio" value=${item.id} ?checked=${item.id === effort.id}>${item.name}</ui-dropdown-menu-item>
                    `)}
                  </ui-dropdown-menu-group>
                </ui-dropdown-menu-group>
              </ui-dropdown-menu-content>
            </ui-dropdown-menu>
            <button type="submit" disabled aria-label="Send" data-slot="button" data-variant="default" data-size="icon-sm" class=${cn(inputGroupButtonClass({ variant: 'default', size: 'icon-sm' }), 'rounded-full')}>
              ${ArrowUpIcon()}
            </button>
          </div>
        </div>
      </form>
    </div>
  `;
}
View Code

Prompt Input is the field the person types into to message the agent. It is an Input Group with a textarea, an attach button, the Model Selector menus and a send button. Attached files show as Attachment chips above the text.

Installation

Prompt Input is built with the Input Group, Attachment and Dropdown Menu components.

pnpm dlx @webjsdev/ui add input-group attachment dropdown-menu

Usage

import {
  inputGroupAddonClass,
  inputGroupButtonClass,
  inputGroupClass,
  inputGroupTextareaClass,
} from '#components/ui/input-group.ts';
<form action=${sendMessage}>
  <input id="files" type="file" name="files" multiple tabindex="-1" class="sr-only">
  <div data-slot="input-group" role="group" class=${inputGroupClass()}>
    <textarea data-slot="input-group-control" name="text" aria-label="Message"
      placeholder="Ask the agent to change your code..."
      class=${cn(inputGroupTextareaClass(), 'p-3')}></textarea>
    <div data-slot="input-group-addon" data-align="block-end" role="group" class=${inputGroupAddonClass({ align: 'block-end' })}>
      <label for="files" aria-label="Attach files" data-slot="button"
        class=${cn(inputGroupButtonClass({ variant: 'outline', size: 'icon-sm' }), 'rounded-full')}>
        ${PlusIcon()}
      </label>
      <button type="submit" aria-label="Send" data-slot="button"
        class=${cn(inputGroupButtonClass({ variant: 'default', size: 'icon-sm' }), 'ml-auto rounded-full')}>
        ${ArrowUpIcon()}
      </button>
    </div>
  </div>
</form>

The form binds a 'use server' action, so it sends with JavaScript off too. WebJs gives a bound form method="post" and enctype="multipart/form-data", so the picked files reach the action with the text.

Attachments

The attach button is a <label> for a hidden file input, so it opens the file picker with no script. Show each file as an Attachment in an addon with data-align="block-start", with a button to remove it. Reading the input's change event and rendering the chips is the component's job:

<div data-slot="input-group-addon" data-align="block-start" role="group" class=${cn(inputGroupAddonClass({ align: 'block-start' }), 'flex-wrap')}>
  ${files.map((file) => html`
    <div data-slot="attachment" data-size="sm" class=${attachmentClass({ size: 'sm' })}>
      <div data-slot="attachment-media" class=${attachmentMediaClass()}>${FileTextIcon()}</div>
      <div data-slot="attachment-content" class=${attachmentContentClass()}>
        <span data-slot="attachment-title" class=${attachmentTitleClass()}>${file.name}</span>
      </div>
      <div data-slot="attachment-actions" class=${attachmentActionsClass()}>
        <button type="button" aria-label=${`Remove ${file.name}`} data-slot="attachment-action"
          class=${attachmentActionClass()} @click=${() => this.remove(file)}>
          ${XIcon()}
        </button>
      </div>
    </div>`)}
</div>

Model and Effort

Put the two Model Selector menus in the block-end addon, between the attach and send buttons. Move ml-auto from the send button to the first menu, so both menus sit next to the send button.

Keyboard

Enter sends the message. Shift+Enter adds a new line.

<textarea @keydown=${(event: KeyboardEvent) => {
  if (event.key === 'Enter' && !event.shiftKey) {
    event.preventDefault();
    (event.target as HTMLTextAreaElement).form?.requestSubmit();
  }
}}></textarea>

An @keydown handler runs only in a component's template, since a page does not hydrate. Render the field from your chat component, or add the listener there.

AI SDK

Send the text and the files from the form to a 'use server' action. A bound action receives the FormData, files included:

'use server';

export async function sendMessage(formData: FormData) {
  const text = String(formData.get('text') ?? '');
  const files = formData.getAll('files') as File[];
  // ...
}

The action can stream the reply back as an async generator. See AI SDK.

Migrating from AI SDK Elements

If you used Prompt Input from AI SDK Elements, its wrapper lives in components/ai-elements/prompt-input.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
PromptInputA <form> around an input group. Keep the attachment state in your component.
PromptInputTextareaA textarea with inputGroupTextareaClass() and p-3.
PromptInputActionMenuTriggerinputGroupButtonClass({ variant: 'outline', size: 'icon-sm' }) with rounded-full, holding a plus icon.
PromptInputSubmitA submit button with inputGroupButtonClass({ variant: 'default', size: 'icon-sm' }) and rounded-full, holding an arrow up icon while idle.
PromptInputHeaderA block-start addon with one small attachment per file and a button to remove it.
PromptInputSelect, PromptInputHoverCard, tabsThe kit's Select, Hover Card and Tabs.

API Reference

See Input Group and Attachment.