Component · New
Prompt bar
A chat composer with a searchable model switcher, file chips, and send, stop and streaming states.
Why would I want several models in one chat?
Claude Sonnet
Because no single model is best at everything. Ask once, compare, keep the answer that is right.
Usage
const [busy, setBusy] = useState(false);
<PromptBar
models={[
{ id: "claude", name: "Claude Sonnet", provider: "Anthropic", color: "#d97757" },
{ id: "gpt", name: "GPT", provider: "OpenAI", color: "#10a37f" },
]}
busy={busy}
onSend={({ text, model, files }) => streamReply(text, model.id, files)}
onStop={() => abort()}
/>Source
Copy both files into your project. The component has no dependencies beyond React.
"use client";
import { useEffect, useMemo, useRef, useState, type KeyboardEvent } from "react";
import styles from "./prompt-bar.module.css";
export type PromptModel = { id: string; name: string; provider: string; note?: string; color?: string };
export type PromptPayload = { text: string; model: PromptModel; files: File[] };
type PromptBarProps = {
models: PromptModel[];
defaultModelId?: string;
placeholder?: string;
/** True while a reply is streaming: the send button becomes a stop button. */
busy?: boolean;
maxLength?: number;
onSend: (payload: PromptPayload) => void;
onStop?: () => void;
};
function fileSize(bytes: number) {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
export function PromptBar({
models,
defaultModelId,
placeholder = "Ask anything…",
busy = false,
maxLength,
onSend,
onStop,
}: PromptBarProps) {
const [text, setText] = useState("");
const [files, setFiles] = useState<File[]>([]);
const [modelId, setModelId] = useState(defaultModelId ?? models[0]?.id);
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
const [cursor, setCursor] = useState(0);
const area = useRef<HTMLTextAreaElement>(null);
const picker = useRef<HTMLDivElement>(null);
const fileInput = useRef<HTMLInputElement>(null);
const model = models.find((item) => item.id === modelId) ?? models[0];
const matches = useMemo(() => {
const needle = query.trim().toLowerCase();
return models.filter((item) => `${item.name} ${item.provider}`.toLowerCase().includes(needle));
}, [models, query]);
// Grow with the text, up to a ceiling; after that the box scrolls.
useEffect(() => {
const node = area.current;
if (!node) return;
node.style.height = "auto";
node.style.height = `${Math.min(node.scrollHeight, 200)}px`;
}, [text]);
useEffect(() => {
if (!open) return;
function onDown(event: PointerEvent) {
if (!picker.current?.contains(event.target as Node)) setOpen(false);
}
document.addEventListener("pointerdown", onDown);
return () => document.removeEventListener("pointerdown", onDown);
}, [open]);
const canSend = !busy && text.trim().length > 0 && Boolean(model);
function send() {
if (!canSend || !model) return;
onSend({ text: text.trim(), model, files });
setText("");
setFiles([]);
}
function onAreaKey(event: KeyboardEvent<HTMLTextAreaElement>) {
if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
event.preventDefault();
send();
}
}
function pick(item: PromptModel) {
setModelId(item.id);
setOpen(false);
setQuery("");
area.current?.focus();
}
function onPickerKey(event: KeyboardEvent<HTMLInputElement>) {
if (event.key === "ArrowDown") {
event.preventDefault();
setCursor((value) => Math.min(value + 1, matches.length - 1));
} else if (event.key === "ArrowUp") {
event.preventDefault();
setCursor((value) => Math.max(value - 1, 0));
} else if (event.key === "Enter") {
event.preventDefault();
if (matches[cursor]) pick(matches[cursor]);
} else if (event.key === "Escape") {
setOpen(false);
area.current?.focus();
}
}
return (
<div className={styles.bar} data-busy={busy ? "true" : "false"}>
{files.length ? (
<ul className={styles.files}>
{files.map((file, index) => (
<li key={`${file.name}-${index}`}>
<span className={styles.fileName}>{file.name}</span>
<span className={styles.fileSize}>{fileSize(file.size)}</span>
<button
type="button"
aria-label={`Remove ${file.name}`}
onClick={() => setFiles((list) => list.filter((_, at) => at !== index))}
>
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="M3 3l6 6M9 3l-6 6" />
</svg>
</button>
</li>
))}
</ul>
) : null}
<textarea
ref={area}
className={styles.area}
rows={1}
value={text}
maxLength={maxLength}
placeholder={placeholder}
aria-label="Message"
onChange={(event) => setText(event.target.value)}
onKeyDown={onAreaKey}
/>
<div className={styles.row}>
<button type="button" className={styles.icon} aria-label="Attach files" onClick={() => fileInput.current?.click()}>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M13 7.500 7.800 12.700a3 3 0 0 1-4.300-4.300l5.600-5.600a2 2 0 0 1 2.900 2.900L6.500 11.200a1 1 0 0 1-1.500-1.500L9.800 5" />
</svg>
</button>
<input
ref={fileInput}
type="file"
multiple
hidden
onChange={(event) => {
const picked = Array.from(event.target.files ?? []);
setFiles((list) => [...list, ...picked]);
event.target.value = "";
}}
/>
<div className={styles.picker} ref={picker}>
<button
type="button"
className={styles.model}
aria-haspopup="listbox"
aria-expanded={open}
onClick={() => {
setOpen((value) => !value);
setCursor(0);
}}
>
<span className={styles.dot} style={{ background: model?.color }} />
{model?.name}
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="M3 4.500 6 7.500l3-3" />
</svg>
</button>
{open ? (
<div className={styles.menu}>
<input
autoFocus
className={styles.search}
value={query}
placeholder="Search models"
aria-label="Search models"
onChange={(event) => {
setQuery(event.target.value);
setCursor(0);
}}
onKeyDown={onPickerKey}
/>
<ul role="listbox" className={styles.list}>
{matches.length === 0 ? <li className={styles.empty}>No model matches.</li> : null}
{matches.map((item, index) => (
<li key={item.id} role="option" aria-selected={item.id === modelId}>
{index === 0 || matches[index - 1].provider !== item.provider ? (
<span className={styles.group}>{item.provider}</span>
) : null}
<button
type="button"
data-active={index === cursor ? "true" : "false"}
onPointerMove={() => setCursor(index)}
onClick={() => pick(item)}
>
<span className={styles.dot} style={{ background: item.color }} />
<span className={styles.itemName}>{item.name}</span>
{item.note ? <span className={styles.note}>{item.note}</span> : null}
{item.id === modelId ? (
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="M2.500 6.500 5 9l4.500-6" />
</svg>
) : null}
</button>
</li>
))}
</ul>
</div>
) : null}
</div>
<span className={styles.count}>{maxLength ? `${text.length}/${maxLength}` : "Enter to send"}</span>
{busy ? (
<button type="button" className={styles.send} data-stop="true" aria-label="Stop" onClick={onStop}>
<svg viewBox="0 0 16 16" aria-hidden="true">
<rect x="4.500" y="4.500" width="7" height="7" rx="1.500" fill="currentColor" stroke="none" />
</svg>
</button>
) : (
<button type="button" className={styles.send} aria-label="Send" disabled={!canSend} onClick={send}>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M8 13V3.500M4 7.500l4-4 4 4" />
</svg>
</button>
)}
</div>
</div>
);
}