Command dock
A small button that opens into a field, with the answer streaming in underneath, row by row.
Demo
Usage
import { CommandDock, type CommandDockSource } from '@tuttn/slant'
// The source answers a question with rows, one at a time. An async generator
// sets the pace; the signal aborts when the answer is stopped, replaced by a
// new question, or the dock closes.
const answer: CommandDockSource = async function* (query, { signal }) {
const response = await fetch(`/api/ask?q=${encodeURIComponent(query)}`, { signal })
for await (const action of readActions(response)) {
yield { id: action.id, label: action.title, description: action.detail, onSelect: () => run(action) }
}
}
// Uncontrolled: it keeps its own open state and question. It opens to the
// width of its own box, so give it one in a flex row.
<CommandDock source={answer} style={{ maxWidth: 460 }} />
// Controlled: own the open state (to open it from a shortcut, say) and the
// question (to prefill it). Opening it moves focus into the field.
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
<CommandDock source={answer} open={open} onOpenChange={setOpen}
value={query} onValueChange={setQuery} onSubmit={query => log(query)} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
source | CommandDockSource | — | Called with each question (trimmed) and the run's abort signal; the rows it yields stream in under the field. |
open | boolean | — | Whether the dock is open as a field. Pass it to control the dock; leave it out and it keeps its own state. |
defaultOpen | boolean | false | Whether the dock starts open when uncontrolled. Starting open doesn't move focus. |
onOpenChange | (open: boolean) => void | — | Called with the requested state: true from the button, false from Escape or a press outside. |
value | string | — | The question in the field. Pass it to control the field; leave it out and the dock keeps its own. |
defaultValue | string | '' | The question the field starts with when uncontrolled. |
onValueChange | (query: string) => void | — | Called with the new question as the user types. The question stays after it is asked. |
onSubmit | (query: string) => void | — | Called with the question (trimmed) when it is asked, by Enter or the ask button, before the source runs. |
label | string | 'Ask' | The field's accessible name. From the root's `messages.ask`. |
placeholder | string | 'Ask or run a command…' | Shown in the empty field. From the root's `messages.askPlaceholder`. |
title | ReactNode | 'Ask' | The closed button's visible text, and its name. From the root's `messages.ask`. |
icon | ReactNode | <CommandIcon /> | Drawn on the closed button and at the start of the field. |
size | 'sm' | 'md' | 'md' | The button's and the field's height, on the control scale. |
…rest | HTMLAttributes<HTMLDivElement> | — | The ref, className, style and other attributes go on the root div, whose width is the width the field opens to. |
A row is { id, label, text?, description?, icon?, tone?, onSelect? }. A row with onSelect is a button filling the row; one without is plain text, for notes and headings in the answer. The tone colours the row's icon, or the slanted pixel drawn in its place. The source may be an async generator or any iterable; when it throws, the answer ends quietly with the rows it gave.
Keyboard
Enter or Space on the closed button opens it and moves focus to the field. Enter asks; a new question while one is answering aborts the old answer. The ask button turns into Stop while the answer streams in. ArrowDown from the field reaches the first row button, Up and Down move between row buttons (passing over plain rows), and ArrowUp from the first row or Escape from any row returns to the field. Escape in the field stops a running answer; otherwise it closes the dock, and focus returns to the button.
Pointer
A press outside the dock closes it, aborting any answer, and leaves focus where the press put it. The question and the rows stay while it is closed, so reopening shows the last answer.
Screen readers
The closed button reports aria-expanded and controls the panel. While an answer streams, the list is aria-busy, and the announcer says “Answering…” when it starts and the count when it ends (politely, so typing isn't interrupted); the status line under the rows says the same, and describes the field. The list and the status line carry no live role of their own, so nothing is read twice. Strings come from the root's messages (ask, askPlaceholder, answering, answerDone, noAnswer, stop).
Motion
Opening grows the surface from the button's width to the field's, and each row rises in as it arrives. Under reduced motion the dock opens and the rows appear at once, and the answering dots stand still.