Installation
Usage
"use client"
import { useState } from "react"
import { PromptComposer, type PromptSubmission } from "@/components/prompt-composer"
export function AskBox() {
const [busy, setBusy] = useState(false)
const [controller, setController] = useState<AbortController | null>(null)
async function send(message: PromptSubmission) {
const abort = new AbortController()
setController(abort)
setBusy(true)
try {
// Your route reads message.text, message.command, message.references and message.attachments.
await fetch("/api/chat", { method: "POST", body: JSON.stringify({ ...message, attachments: message.attachments.map((a) => a.url ?? a.name) }), signal: abort.signal })
} finally {
setBusy(false)
}
}
return (
<PromptComposer
busy={busy}
onStop={() => controller?.abort()}
onSubmit={send}
references={[
{ id: "u1", label: "Elena Novak", kind: "person", hint: "Design" },
{ id: "f1", label: "Q4 report.pdf", kind: "file", hint: "PDF" },
]}
commands={[{ name: "summarize", description: "Summarise the attached files" }]}
toggles={[{ id: "search", label: "Search", icon: "search" }]}
onUpload={async (file, { signal }) => {
const body = new FormData()
body.append("file", file)
const response = await fetch("/api/upload", { method: "POST", body, signal })
const { url } = (await response.json()) as { url: string }
return url
}}
draftKey="ask-box-draft"
/>
)
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| onSubmit | (message) => void | Promise<void> | — | Called on send with { text, json, command, references, attachments, toggles, model }. Return a promise: if it rejects, the message comes back into the box. |
| busy / onStop | boolean / () => void | false | While an answer is on its way, the send button becomes Stop; Escape stops too. |
| references | PromptReference[] | (query) => PromptReference[] | Promise | — | What "@" offers: { id, label, kind?, hint?, group?, color?, avatar? }. People and files have their own look. Pass a function to search your own data. |
| commands | PromptCommand[] | — | What "/" offers at the start of a message: { name, description? }. |
| onUpload | (file, { signal, onProgress }) => Promise<string | void> | — | Uploads an attached file and resolves to its URL, with progress. Sending waits for uploads. Without it, files are sent with the message as they are. |
| attachments / accept / maxFileSize / maxFiles | boolean / string / number / number | true / — / 20 MB / 10 | Attaching by button, drop and paste, and its limits. |
| pasteAsAttachment | number | 1200 | A paste longer than this many characters becomes an attachment you can open; 0 turns it off. |
| maxLength | number | — | A character limit: a counter appears near it and sending stops above it. |
| toggles / onToggleChange | PromptToggle[] | — | Buttons such as Search or Think: { id, label, icon?, defaultPressed? }. Their state arrives with each message. |
| models / model / defaultModel / onModelChange | PromptModel[] / string | — | A model picker: { id, label, description? }. |
| submitKey / hint | "enter" | "mod-enter" / boolean | "enter" / true | Which keys send, and the line below the box that says so and lets people switch. |
| draftKey | string | — | Keeps the unsent message in localStorage under this key. |
| ref | PromptComposerHandle | — | focus(), clear() and setContent(text or JSON), e.g. for suggested prompts. |
| defaultValue / placeholder / disabled / label | string | JSONContent / string / boolean / string | — / "Ask anything…" / false / "Message" | Starting text, the empty-box hint, turning it off, and the accessible name. |
Accessibility
- The box is a named multi-line text box; the "@" and "/" lists follow the combobox pattern, so focus stays in the text, the arrow keys move, Enter or Tab chooses and Escape closes.
- Enter never sends while a Japanese, Chinese or Korean word is still being composed; people who want Enter for new lines can switch to Cmd or Ctrl Enter from the hint.
- Attachments are a labelled list; uploads report progress, each file has a named remove button, and sending says why it is waiting.
- Toggles are pressed buttons, the model picker is a menu of radio items with arrow keys, and Stop is a real button as well as Escape.
- Sending, stopping, attaching, removing and failures are announced politely, and a failed send puts the message back.
- Under prefers-reduced-motion nothing eases.
Changelog
- v1.0.0Latest
First release.