aiellie

Search

Find a component, template or page

GitHub
Components

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.

Open full screen
fig. 01 · open it full screen from the corner

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

  1. Pass a suggestion

    suggestion is only the text that comes after the value. Fetch it as the value changes; a short debounce keeps requests down.

    components/prompt.tsx
    import { 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

    PropTypeDefaultDescription
    value, defaultValue, onValueChangestring""
    suggestionstringOnly the text that comes after the value.
    onAccept(accepted: string, mode: "all" | "word") => void
    onDismiss() => voidFires on Escape.
    onSubmit(value: string) => voidFires on Enter and from the send button, which it adds.
    placeholderstring"Message"
    labelstring"Message"The field's accessible name.
    disabledbooleanfalse
    showHintsbooleantrueShows the keys while a suggestion shows.

    All other div props are forwarded.