Prompt input
An AI composer: auto-growing textarea, attachments, model and effort pickers, dictation, mentions and slash commands, a queue, and keyboard shortcuts. Adapted from HextaUI (MIT).
Compose it from its parts: PromptInput is the form and holds the state, PromptInputBody is the card, and the textarea, tools and actions sit inside it. Controls, the footer and the inset are in prompt-controls; mentions and commands are in prompt-menu. onSubmit receives the text, the uploaded files and the mentions.
Plain
"use client"
import { useState } from "react"
import { Message } from "@/components/ui/message"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
} from "@/components/prompt-input/prompt-input"
export default function PromptInputPlain() {
const [sent, setSent] = useState<string[]>([])
return (
<div className="flex w-full max-w-xl flex-col gap-4">
{sent.map((text, index) => (
<Message key={index} from="user" animateIn>
<p className="max-w-[85%] rounded-xl bg-muted px-3 py-2 whitespace-pre-wrap">{text}</p>
</Message>
))}
<PromptInput onSubmit={(message) => setSent((list) => [...list, message.text])}>
<PromptInputBody>
<PromptInputTextarea placeholder="Ask about the March release" />
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
</PromptInput>
</div>
)
}
With suggestions
Nothing sent yet
"use client"
import { useState } from "react"
import { PromptInputSuggestions } from "@/components/prompt-input/prompt-controls"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
} from "@/components/prompt-input/prompt-input"
const suggestions = [
{ label: "Summarise the release", prompt: "Summarise what shipped in the March release." },
{ label: "List open tasks", prompt: "List the tasks that are still open, with their owners." },
{ label: "Draft the notes", prompt: "Draft release notes from the closed tasks." },
]
export default function PromptInputWithSuggestions() {
const [sent, setSent] = useState("")
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput onSubmit={(message) => setSent(message.text)} className="gap-3">
<PromptInputBody>
<PromptInputTextarea placeholder="Ask, or start from a suggestion" />
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
<PromptInputSuggestions suggestions={suggestions} />
</PromptInput>
<p className="text-center text-sm text-muted-foreground">{sent ? `Sent: ${sent}` : "Nothing sent yet"}</p>
</div>
)
}
With attachments
Nothing sent yet
"use client"
import { useState } from "react"
import { FileTextIcon } from "@keyline-icons/react"
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
import { PromptInputAddMenu } from "@/components/prompt-input/prompt-controls"
import {
PromptInput,
PromptInputActions,
PromptInputAttachments,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
PromptInputTools,
usePromptInput,
} from "@/components/prompt-input/prompt-input"
// Stands in for a real upload: no request is made, the progress is a timer.
function upload(_file: File, { onProgress }: { onProgress: (progress: number) => void }) {
return new Promise<void>((resolve) => {
let progress = 0
const timer = setInterval(() => {
progress += 20
onProgress(progress)
if (progress < 100) return
clearInterval(timer)
resolve()
}, 300)
})
}
function AttachSample() {
const { addFiles } = usePromptInput()
const sample = new File(["# March release\n\nTwelve tasks shipped."], "release-notes.md", { type: "text/markdown" })
return (
<DropdownMenuItem onClick={() => addFiles([sample])}>
<FileTextIcon aria-hidden />
Attach a sample file
</DropdownMenuItem>
)
}
export default function PromptInputWithAttachments() {
const [sent, setSent] = useState("")
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput
upload={upload}
maxFiles={4}
maxFileSize={10 * 1024 * 1024}
onSubmit={({ text, files }) => setSent(`${text || "No text"}, with ${files.map((file) => file.name).join(", ")}`)}
>
<PromptInputBody>
<PromptInputAttachments />
<PromptInputTextarea placeholder="Add a file, then ask about it" />
<PromptInputTools>
<PromptInputAddMenu>
<AttachSample />
</PromptInputAddMenu>
</PromptInputTools>
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
</PromptInput>
<p className="text-center text-sm text-muted-foreground">{sent ? `Sent: ${sent}` : "Nothing sent yet"}</p>
</div>
)
}
With mentions and commands
Nothing sent yet
"use client"
import { useState } from "react"
import { FolderIcon } from "@keyline-icons/react"
import {
PromptInput,
PromptInputActions,
PromptInputAttachments,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
} from "@/components/prompt-input/prompt-input"
import { PromptInputMenu } from "@/components/prompt-input/prompt-menu"
const projects = [
{ value: "march-release", label: "March release", icon: <FolderIcon aria-hidden />, hint: "14 tasks" },
{ value: "billing", label: "Billing", icon: <FolderIcon aria-hidden />, hint: "6 invoices" },
{ value: "onboarding", label: "Onboarding", icon: <FolderIcon aria-hidden />, hint: "9 tasks" },
]
const commands = [
{ value: "summarise", label: "summarise", description: "Sum up a project" },
{ value: "tasks", label: "tasks", description: "List open tasks" },
{ value: "notes", label: "notes", description: "Draft release notes" },
]
export default function PromptInputWithMentionsAndCommands() {
const [sent, setSent] = useState("")
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput
onSubmit={({ text, mentions }) =>
setSent(mentions.length ? `${text} (mentions ${mentions.map((mention) => mention.label).join(", ")})` : text)
}
>
<PromptInputBody>
<PromptInputAttachments />
<PromptInputTextarea placeholder="Type @ for a project or / for a command" />
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
<PromptInputMenu trigger="@" items={projects} label="Projects" />
<PromptInputMenu trigger="/" items={commands} />
</PromptInput>
<p className="text-center text-sm text-muted-foreground">{sent ? `Sent: ${sent}` : "Nothing sent yet"}</p>
</div>
)
}
With controls
Nothing sent yet
"use client"
import { useState } from "react"
import { GlobeIcon } from "@keyline-icons/react"
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
import {
PromptInputAddMenu,
PromptInputContextUsage,
PromptInputFooter,
PromptInputInset,
PromptInputModel,
PromptInputSelect,
PromptInputTool,
} from "@/components/prompt-input/prompt-controls"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
PromptInputTools,
} from "@/components/prompt-input/prompt-input"
const models = [
{ value: "quick", label: "Quick" },
{ value: "balanced", label: "Balanced" },
{ value: "thorough", label: "Thorough" },
]
const efforts = [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High" },
]
const scopes = [
{ value: "project", label: "This project", description: "Tasks, files and invoices of March release" },
{ value: "workspace", label: "Whole workspace", description: "Every project you can open" },
]
export default function PromptInputWithControls() {
const [sent, setSent] = useState("")
const [model, setModel] = useState("balanced")
const [effort, setEffort] = useState("medium")
const [thinking, setThinking] = useState(false)
const [scope, setScope] = useState("project")
const [search, setSearch] = useState(false)
const modelLabel = models.find((item) => item.value === model)?.label
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput onSubmit={(message) => setSent(message.text)}>
<PromptInputInset>
<PromptInputBody>
<PromptInputTextarea placeholder="Ask about the March release" />
<PromptInputTools>
<PromptInputAddMenu>
<DropdownMenuItem onClick={() => setSearch(true)}>
<GlobeIcon aria-hidden />
Search the web
</DropdownMenuItem>
</PromptInputAddMenu>
{search ? (
<PromptInputTool icon={<GlobeIcon aria-hidden />} onRemove={() => setSearch(false)}>
Web search
</PromptInputTool>
) : null}
</PromptInputTools>
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
<PromptInputFooter>
<PromptInputSelect label="Scope" options={scopes} value={scope} onValueChange={setScope} heading="Answer from" />
<div className="ms-auto flex items-center gap-0.5">
<PromptInputModel
models={models}
model={model}
onModelChange={setModel}
levels={efforts}
effort={effort}
onEffortChange={setEffort}
recommended="medium"
thinking={thinking}
onThinkingChange={setThinking}
/>
<PromptInputContextUsage used={128_400} limit={200_000} model={modelLabel} />
</div>
</PromptInputFooter>
</PromptInputInset>
</PromptInput>
<p className="text-center text-sm text-muted-foreground">
{sent ? `Sent to ${modelLabel} at ${effort} effort: ${sent}` : "Nothing sent yet"}
</p>
</div>
)
}
While a reply is pending
Nothing sent yet
"use client"
import { useEffect, useState } from "react"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputQueue,
PromptInputSubmit,
PromptInputTextarea,
} from "@/components/prompt-input/prompt-input"
export default function PromptInputWhileAReplyIsPending() {
const [sent, setSent] = useState<string[]>([])
const [replying, setReplying] = useState(false)
// Stands in for a reply: four seconds, then the composer is ready again.
useEffect(() => {
if (!replying) return
const timer = setTimeout(() => setReplying(false), 4000)
return () => clearTimeout(timer)
}, [replying, sent])
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput
status={replying ? "streaming" : "ready"}
onStop={() => setReplying(false)}
onSubmit={(message) => {
setSent((list) => [...list, message.text])
setReplying(true)
}}
>
<PromptInputQueue />
<PromptInputBody>
<PromptInputTextarea placeholder="Send, then send again while the reply runs" />
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
</PromptInput>
<p role="status" className="text-center text-sm text-muted-foreground">
{replying ? `Replying to: ${sent.at(-1)}` : sent.length ? `Replied to ${sent.length}` : "Nothing sent yet"}
</p>
</div>
)
}
With a character limit
Nothing sent yet
"use client"
import { useState } from "react"
import { PromptInputFooter, PromptInputInset, PromptInputMeter } from "@/components/prompt-input/prompt-controls"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
} from "@/components/prompt-input/prompt-input"
export default function PromptInputWithACharacterLimit() {
const [sent, setSent] = useState("")
return (
<div className="flex w-full max-w-xl flex-col gap-4">
<PromptInput
maxLength={80}
submitOn="mod+enter"
defaultValue="Summarise the March release in one line for the project page."
onSubmit={(message) => setSent(message.text)}
>
<PromptInputInset>
<PromptInputBody>
<PromptInputTextarea />
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
<PromptInputFooter className="justify-between">
<span className="px-1 text-xs">Up to 80 characters</span>
<PromptInputMeter showAt={0.5} />
</PromptInputFooter>
</PromptInputInset>
</PromptInput>
<p className="text-center text-sm text-muted-foreground">{sent ? `Sent: ${sent}` : "Nothing sent yet"}</p>
</div>
)
}
Disabled
"use client"
import { PromptInputAddMenu } from "@/components/prompt-input/prompt-controls"
import {
PromptInput,
PromptInputActions,
PromptInputBody,
PromptInputSubmit,
PromptInputTextarea,
PromptInputTools,
} from "@/components/prompt-input/prompt-input"
export default function PromptInputDisabled() {
return (
<PromptInput disabled onSubmit={() => {}} className="w-full max-w-xl">
<PromptInputBody>
<PromptInputTextarea placeholder="This project is archived" />
<PromptInputTools>
<PromptInputAddMenu />
</PromptInputTools>
<PromptInputActions>
<PromptInputSubmit />
</PromptInputActions>
</PromptInputBody>
</PromptInput>
)
}
Installation
npx shadcn@latest add @khaeli/prompt-inputbunx --bun shadcn@latest add @khaeli/prompt-inputInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
Keyboard
Enter- Sends the message. With
submitOn="mod+enter"it starts a new line instead. Shift+Enter- Starts a new line.
⌘+EnterCtrl+Enter- Sends the message, whatever
submitOnis. Escape- Stops the reply while one is pending and
onStopis set. With a mention or command menu open, closes the menu first. @/- Open the menu of a
PromptInputMenuwith that trigger:@anywhere in the text,/only at its start. Keep typing to filter it. Arrow UpArrow Down- Move through the open menu, from the last option round to the first. Focus stays in the textarea.
EnterTab- With a menu open, choose the highlighted option.
Tab- Moves through the composer: the attachments' remove buttons, the textarea, the tools, then Send and the footer's controls. Choosing from a control's menu returns focus to the textarea.
123- In the model picker, choose the model at that position. Arrow Up and Arrow Down move through the models.
Arrow LeftArrow RightHomeEnd- On the effort slider, lower or raise the effort, or go to its ends.
⌘+VCtrl+V- A paste longer than
pasteLimit(5,000 characters) is attached as a file, Pasted text.txt. Its preview can put it back as text.
Accessible name
- The textarea is named Message. Pass
aria-labeltoPromptInputTextareato say what is being written, such as Answer. It is described by a hidden hint that says which key sends. - Send, Stop, Queue message, Add files and more, the remove buttons of files and mentions, and the context ring name themselves. Pass
labeltoPromptInputAddMenuandPromptInputSelectto change theirs. - A picker's button says its value: Model and effort: Balanced Medium, or Scope: This project.
- A
PromptInputToolis named from its text, as in Web search, on. Turn off. - A menu is a
listboxnamed Mentions or Commands unless you passlabel. - The composer announces what happens in a polite status: Message sent, a queued message, a file attached or refused, and why sending is blocked.
- Send is never natively disabled. When there is nothing to send or sending is blocked it is
aria-disabled, and its tooltip gives the reason. In adisabledcomposer Send also leaves the tab order, so nothing in the composer takes focus.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/prompt-input.json