Prompt Autocomplete
A prompt field that shows a suggested ending as ghost text. Tab takes all of it, Option-Right the next word, and Escape dismisses it.
Installation
npx shadcn@latest add "@aiellie/prompt-autocomplete"shadcn needs the @aiellie registry in components.json first. npx aiellie add prompt-autocomplete adds it on its first run, then installs the same way.
Prompt Autocomplete is a prompt field that shows a suggested ending in faint text after the caret. Tab takes all of it, Option-Right (Ctrl-Right off a Mac) takes the next word, and Escape dismisses it. Without a suggestion, Tab moves focus as usual.
A suggestion holds while the person types its own characters, and shrinks as they do, so it doesn't flicker between keystrokes. Typing anything else drops it. It only shows with the caret at the end.
Getting started
Pass a suggestion
suggestionis only the text that comes after the value. Fetch it as the value changes; a short debounce keeps requests down.components/prompt.tsximport { PromptAutocomplete } from "@/components/aiellie/prompt-autocomplete" export function Prompt() { const [value, setValue] = useState("") const suggestion = useSuggestion(value) return ( <PromptAutocomplete value={value} onValueChange={setValue} suggestion={suggestion} onSubmit={(message) => send(message)} /> ) }
Anatomy
<div data-slot="prompt-autocomplete" data-suggesting>
<div aria-hidden>
{/* the value, invisible, then the ghost text and a tab key */}
<span data-slot="prompt-autocomplete-ghost" />
</div>
<textarea data-slot="prompt-autocomplete-input" aria-autocomplete="inline" />
<span data-slot="prompt-autocomplete-hints" />
<button data-slot="prompt-autocomplete-send" />
</div>The ghost text is drawn by a copy of the field behind it, wrapped the same way, so it lines up on every line. The tab key at its end can be clicked too.
A new suggestion is read out once, with how to take it. nextWord and remainingCompletion are exported for building your own.
Examples
Accepting and dismissing
onAccept gets the text put in and whether it was all of it or a word.
API reference
PromptAutocomplete
| Prop | Type | Default | Description |
|---|---|---|---|
value, defaultValue, onValueChange | string | "" | |
suggestion | string | Only the text that comes after the value. | |
onAccept | (accepted: string, mode: "all" | "word") => void | ||
onDismiss | () => void | Fires on Escape. | |
onSubmit | (value: string) => void | Fires on Enter and from the send button, which it adds. | |
placeholder | string | "Message" | |
label | string | "Message" | The field's accessible name. |
disabled | boolean | false | |
showHints | boolean | true | Shows the keys while a suggestion shows. |
All other div props are forwarded.