Prompt input

Slant / People & chat

A composer that grows to five lines, with a toolbar, attachments, and send and stop.

Demo

Assistant

Usage

@tuttn/slant
import { PromptInput } from '@tuttn/slant'

// Enter sends and Shift+Enter breaks the line. While busy, send turns into Stop.
<PromptInput onSubmit={send} busy={replying} onStop={cancel} />

// Attachments above the field, and a toolbar in the bar under it.
<PromptInput
  onSubmit={send}
  attachments={files} // { id, name, type, size?, progress, status?, statusText? }
  onRemoveAttachment={id => setFiles(list => list.filter(file => file.id !== id))}
  toolbar={<>
    <PromptInput.Attach onFiles={add} accept="image/*,.pdf" />
    <PromptInput.Models models={[{ id: 'fast', label: 'Fast' }, { id: 'deep', label: 'Deep' }]}
      value={model} onValueChange={setModel} />
  </>}
/>

// Trigger menus: @ inserts an atomic chip ("@Ada Lovelace "), / at the start
// plain text. With triggers, onSubmit also gets the draft's chips.
<PromptInput
  onSubmit={(text, detail) => send(text, detail?.chips ?? [])} // chips: { char, value, label }[]
  triggers={[
    { char: '@', label: 'People', options: people }, // ListOptions, plus an optional chipLabel
    { char: '/', label: 'Commands', options: commands, insert: 'text' },
  ]}
/>

// A server search, with controlled chips and draft.
<PromptInput value={draft} onValueChange={setDraft} chips={chips} onChipsChange={setChips}
  triggers={[{ char: '@', label: 'Files', options: results, filter: false, loading, onQueryChange: search }]} />

Props

PropTypeDefaultDescription
labelstringmessages.promptLabel ("Message")The field's accessible name.
valuestring—The draft. Pass it with onValueChange to control the field; leave it out and it keeps its own, cleared after each send.
defaultValuestring''The draft it starts with, when uncontrolled.
onValueChange(value: string) => void—Called with the new draft as the reader types, and with '' after a send.
onSubmit(value: string, detail?: PromptSubmitDetail) => void—Called with the trimmed draft when the reader sends it, with Enter or the send button. Never called with an empty draft, while busy or while disabled. With `triggers` or chips, a second argument holds the draft's chips (`detail?.chips`).
busybooleanfalseA reply is being written: the send button becomes Stop (the same button, so focus stays on it), and Enter sends nothing. The field stays editable.
onStop() => void—Called when Stop is pressed, or Escape in the field while busy (unless Escape is closing a trigger's menu).
placeholderstringmessages.promptPlaceholder ("Ask anything…")The field's placeholder.
rowsnumber1The lines the field starts at.
maxRowsnumber5The lines the field grows to before it scrolls.
toolbarReactNode—Controls in the bar under the field, at the inline start: PromptInput.Attach, PromptInput.Models, a Menu of tools.
attachmentsreadonly PromptAttachment[]—Files attached to the draft, listed above the field as FileItems: `{ id, name, type, size?, progress, status?, statusText? }`.
onRemoveAttachment(id: string) => void—Called with an attachment's id when its remove button is pressed; focus then moves to the field. Without it, attachments have no remove button.
disabledbooleanfalseDisables the field and its send button. The toolbar's controls and the attachments are yours to disable.
namestring—The textarea's name, so a plain form submit carries the draft.
sendLabelstringmessages.sendThe send button's accessible name.
stopLabelstringmessages.stopGeneratingThe stop button's accessible name.
textareaRefRef<HTMLTextAreaElement>—A ref to the textarea, to focus it.
triggersreadonly PromptTrigger[]—Menus that open as the reader types their character, at the start of the draft or after a space: `@` for people or files, `/` for commands. Focus stays in the field; a pick inserts a chip, or plain text.
chipsreadonly PromptChip[]—The chips in the draft, each `{ char, value, label }`, its text `char + label`. Pass it with onChipsChange to control them; a chip whose text isn't in the draft is dropped.
defaultChipsreadonly PromptChip[]—The chips an uncontrolled composer starts with; their text must be in the draft.
onChipsChange(chips: PromptChip[]) => void—Called with the chips, in draft order, whenever one is added, removed or dropped, and with [] after a send.
PromptInput.Attach onFiles(files: File[]) => void—Called with the picked files; the same file can be picked again.
PromptInput.Attach accept, multiple, labelstring, boolean, stringany, true, messages.attachFilesThe picker's file types, more than one file, and the paperclip button's name. Ref and attributes go on the button.
PromptInput.Models modelsreadonly { id: string; label: string }[]—The models, as radio items in a Menu; the trigger shows the chosen one.
PromptInput.Models value, defaultValue, onValueChange, labelstring, string, (id) => void, string—, the first model, —, messages.modelThe chosen id, controlled or not, and the menu's name, read before the chosen model. Ref and attributes go on the Menu's wrapper.
PromptTrigger{ char; label; options; filter?; onQueryChange?; loading?; startOnly?; insert? }—One menu: its character, name and ListOptions (each with an optional chipLabel). filter: false plus onQueryChange and loading for a search of your own.
…restOmit<FormHTMLAttributes, 'onSubmit' | 'children' | 'defaultValue'>—className, style, other attributes and the ref go on the <form>. Reach the textarea with textareaRef.

Keyboard

Enterin the field, sends the draft; nothing when it is empty or while busy
Shift + Enterin the field, breaks the line
Escapein the field while busy, stops the reply
Tabmoves from the field to the toolbar and the send button
Enter / Spaceon the send button, sends; on Stop, stops
↓ / ↑on the model trigger, opens the menu on the chosen model, or on the last
@ or /starting a word (/ only at the start), opens that trigger's menu; what follows filters it
↓ / ↑with a menu open, moves the active option, wrapping
Enter / Tabwith a menu open, picks the active option instead of sending
Escapewith a menu open, closes only the menu; a reply goes on
Spaceends the trigger word and closes its menu
Backspace / Deletenext to a chip, removes it whole; ← and → step over it

Accessibility

  • The field is a textarea named by label ("Message"); the placeholder is only a hint.
  • Send and Stop are one button: its name changes from "Send" to "Stop" and back, and focus stays on it. An empty draft makes it aria-disabled rather than disabled, so a reader who just sent with it keeps their place.
  • Enter that commits an IME composition never sends.
  • The attach button is named "Attach files". The file input it opens is hidden, out of the tab order and the accessibility tree.
  • The model trigger reads the menu's name before the chosen model ("Model, Fast"); its menu holds radio items and opens on the chosen one.
  • Each attachment is a FileItem. Its remove button is named "Remove <name>", and focus moves to the field when it is pressed.
  • The whole surface takes the focus ring while anything inside it has focus.
  • Announcing replies is the job of the Thread above, a role="log", not the composer's.
  • A trigger's menu is a listbox named by its label, and focus stays in the field, which has aria-autocomplete="list", aria-haspopup="listbox", aria-controls and aria-activedescendant while it is open (no combobox role or aria-expanded, which a textarea can't take).
  • The match count is announced once typing pauses ("3 results"); with none the menu says "No results for …", and while loading it is aria-busy.
  • A chip is plain text in the field, read like any other; the fill behind it is aria-hidden. Picking and deleting one are announced ("Ada Lovelace added", "Ada Lovelace removed").

The field grows from rows (1) to maxRows (5) lines, then scrolls; the first render, on the server too, is rows lines tall. Under right-to-left the toolbar sits on the right and send on the left, and the draft runs right to left.

Trigger menus

A trigger’s character opens its menu when it starts a word, never inside one such as an email address; with startOnly (the default for /) only at the start of the draft. The menu opens above the surface, flips below when there is no room, and works inside a Dialog. Its first option is active, so Enter picks it, and a click picks one without taking focus from the field. A pick replaces the trigger word with the character, the option’s text and a space; by default that text becomes a chip.

Chips

A chip is its text in the draft (char + label), so a form submit, copy and value carry it as text. A mirror of the field behind the transparent textarea marks it with a soft fill. Chips are atomic: Backspace or Delete next to one removes it whole, the arrows step over it, and a click inside one lands at its nearer edge; an edit that cuts into one drops the chip and keeps the text. Controlled, pass chips with a controlled value; a chip whose text leaves the draft is dropped. On send, onSubmit(text, { chips }) gets them in draft order. Without triggers, chips or defaultChips, onSubmit gets the text alone, as before.

Full source