# Prompt Autocomplete

> Ghost text that finishes the prompt as you type — Tab takes it all, Right Arrow takes a word, and typing on replaces it.

- Category: composer
- Job: Ask — Get intent into the AI: composers, voice, attachments and asking in context.
- Slug: `prompt-autocomplete`
- HTML: https://ai-patterns.app/patterns/composer/prompt-autocomplete
- JSON: https://ai-patterns.app/api/patterns/composer/prompt-autocomplete
- Seen in: [GitHub Copilot](https://docs.github.com/en/copilot/how-tos/copilot-in-your-ide/copilot-for-common-tasks/get-ide-code-suggestions), [Warp](https://docs.warp.dev/agent-platform/local-agents/active-ai/)

## Summary
Ghost text that finishes the prompt while it's being written. After a short pause the model suggests how the sentence could continue, shown in faint text right after the caret. Tab takes all of it, Right Arrow takes one word, and typing on simply replaces it. It saves keystrokes on common requests and shows people what a fuller prompt looks like, without taking the pen away from them.

## When to use
- Composers where many prompts follow known shapes: "Summarize…", "Write a… for…", "Explain…".
- Products with a history of prompts or a template library that suggestions can be drawn from.
- Domain tools where the right vocabulary helps (query builders, support macros, code assistants).

## When not to use
- Sensitive or personal writing (health, legal, HR complaints), where suggested words put ideas in people's mouths.
- Short commands or search boxes where people know exactly what they want; a dropdown of suggestions, or nothing, fits better.
- When suggestions can't come back in well under a second. A late ghost lands after the person has moved on and reads as noise.
- To rewrite or expand a prompt that's already written — use Prompt Enhancer, which shows what it changed.

## Anatomy
- Prompt field: a normal textarea; the typed text never changes style.
- Ghost text: the suggested continuation in faint text, starting at the caret on the same line.
- Accept hint: a small "Tab to accept" chip that is also a tap target on touch screens.
- Send button.

## Behavior
- Ask for a suggestion only after a pause (around 300 ms) and only once the prompt has a few characters. Cancel the request the moment the text changes, and never show a suggestion made for different text.
- Suggest only when the caret is at the end of the text. Moving the caret into the middle, selecting text or leaving the field clears the ghost.
- Tab accepts the whole suggestion; Right Arrow accepts the next word and keeps the rest on screen; Escape dismisses it until the person types again.
- Typing a character that matches the ghost keeps the rest of the suggestion; anything else replaces it with a fresh request after the next pause.
- Tab is taken only while a ghost is showing. With no ghost, Tab moves focus as usual, so keyboard users are never trapped in the field.
- Enter sends what was typed, not the ghost. Accepting is always a separate, deliberate step.
- The ghost fades in quickly and disappears at once, so it never trails the typing. A failed or empty suggestion shows nothing at all.

## Content guidelines
- Suggest the rest of the sentence, not a new paragraph: one clause or a short phrase is enough.
- Continue in the person's own voice and wording; don't correct what they've typed.
- Keep the hint to "Tab to accept". Teach Right Arrow and Escape in onboarding or a tooltip, not on every suggestion.

## Accessibility
- The textarea keeps its label and sets `aria-autocomplete="inline"`; the ghost layer is `aria-hidden`, so it's never read as typed text.
- A polite live region announces a new suggestion with the keys to accept or dismiss it, and the textarea points to it with `aria-describedby` while it shows.
- The ghost must stay clearly different from typed text in both themes, by more than color alone where possible (lighter weight or opacity), and still readable.
- The accept chip is a real button, so touch and switch users can accept without a Tab key.
- The fade is skipped when the user prefers reduced motion.

## Related patterns
- **Prompt Enhancer** — Use it instead to rewrite a finished prompt into a more specific one, with the changes highlighted.
- **Prompt Quality Meter** — Pair it to show what a prompt is still missing, rather than guessing the words.
- **Prompt Template** — Use it instead when the prompt's shape is fixed and only a few blanks change.
- **Prompt Bar** — The basic composer this builds on.

## Implementation

Reference implementation (React, TypeScript, Tailwind v4, Motion, Radix). Keep every rule from the spec above; if the host project has its own design system, re-express the markup and motion in it rather than copying these classes.

npm dependencies: `lucide-react`, `motion`

Local imports to provide: `@/lib/utils` (`cn` is the usual `clsx` + `tailwind-merge` helper).

```tsx title="component.tsx"
"use client";

import * as React from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { ArrowUp } from "lucide-react";

import { cn } from "@/lib/utils";

export interface PromptAutocompleteProps {
  value: string;
  onChange: (value: string) => void;
  /**
   * Returns the text that would continue `prompt` (not the whole prompt),
   * or an empty string for no suggestion. Called after the person pauses.
   */
  getSuggestion: (prompt: string, signal: AbortSignal) => Promise<string>;
  onSubmit?: (prompt: string) => void;
  placeholder?: string;
  /** Pause, in ms, before asking for a suggestion. */
  delay?: number;
  /** Prompts shorter than this many characters get no suggestion. */
  minLength?: number;
  className?: string;
}

export function PromptAutocomplete({
  value,
  onChange,
  getSuggestion,
  onSubmit,
  placeholder = "Ask anything…",
  delay = 300,
  minLength = 3,
  className,
}: PromptAutocompleteProps) {
  const [suggestion, setSuggestion] = React.useState("");
  // The prompt the current suggestion was made for. A suggestion only shows
  // while the text is still exactly that, so a stale one never flashes.
  const [suggestedFor, setSuggestedFor] = React.useState("");
  const [caretAtEnd, setCaretAtEnd] = React.useState(true);
  const textareaRef = React.useRef<HTMLTextAreaElement>(null);
  const hintId = React.useId();
  const reduceMotion = useReducedMotion();

  const ghost = caretAtEnd && suggestedFor === value ? suggestion : "";

  React.useEffect(() => {
    if (value.trim().length < minLength || !caretAtEnd) return;
    const controller = new AbortController();
    const timer = setTimeout(() => {
      getSuggestion(value, controller.signal)
        .then((next) => {
          if (controller.signal.aborted) return;
          setSuggestion(next);
          setSuggestedFor(value);
        })
        .catch(() => {
          // A failed suggestion is silent: the person just keeps typing.
        });
    }, delay);
    return () => {
      clearTimeout(timer);
      controller.abort();
    };
  }, [value, caretAtEnd, delay, minLength, getSuggestion]);

  function accept(text: string) {
    const next = value + text;
    const rest = suggestion.slice(text.length);
    onChange(next);
    // Keep the rest of the suggestion on screen after a partial accept.
    setSuggestion(rest);
    setSuggestedFor(next);
    requestAnimationFrame(() => {
      const el = textareaRef.current;
      if (el) el.setSelectionRange(next.length, next.length);
    });
  }

  function dismiss() {
    setSuggestion("");
    setSuggestedFor("");
  }

  function onKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>) {
    if (ghost) {
      if (e.key === "Tab" && !e.shiftKey) {
        e.preventDefault();
        accept(ghost);
        return;
      }
      // The caret is at the end whenever a ghost shows, so → has nothing else to do.
      if (e.key === "ArrowRight" && !e.shiftKey && !e.metaKey) {
        e.preventDefault();
        accept(nextWord(ghost));
        return;
      }
      if (e.key === "Escape") {
        e.preventDefault();
        dismiss();
        return;
      }
    }
    if (e.key === "Enter" && !e.shiftKey && onSubmit && value.trim()) {
      e.preventDefault();
      onSubmit(value);
    }
  }

  function syncCaret() {
    const el = textareaRef.current;
    if (!el) return;
    setCaretAtEnd(el.selectionStart === el.value.length && el.selectionEnd === el.value.length);
  }

  // Both layers share these so the ghost lines up with the typed text.
  const textLayer = "px-4 pt-3 pb-12 text-sm leading-relaxed whitespace-pre-wrap break-words";

  return (
    <div className={cn("flex w-full flex-col gap-2", className)}>
      <div className="relative rounded-2xl bg-card focus-within:border-foreground/30 border">
        {/* Mirror layer: sizes the box and draws the ghost after the text. */}
        <div aria-hidden className={cn(textLayer, "min-h-24")}>
          <span className="text-transparent">{value}</span>
          <AnimatePresence>
            {ghost && (
              <motion.span
                key={suggestedFor}
                initial={reduceMotion ? false : { opacity: 0 }}
                animate={{ opacity: 1 }}
                exit={{ opacity: 0, transition: { duration: 0.08 } }}
                transition={{ duration: 0.18 }}
                className="text-muted-foreground/60"
              >
                {ghost}
              </motion.span>
            )}
          </AnimatePresence>
          {/* Keeps a trailing newline from collapsing the last line. */}
          <span>{"​"}</span>
        </div>

        <textarea
          ref={textareaRef}
          value={value}
          onChange={(e) => {
            const next = e.target.value;
            const typed = next.slice(value.length);
            // Typing what the ghost already says keeps the rest of it.
            if (ghost && next.startsWith(value) && typed && ghost.startsWith(typed)) {
              setSuggestion(ghost.slice(typed.length));
              setSuggestedFor(next);
            }
            onChange(next);
            setCaretAtEnd(e.target.selectionStart === e.target.value.length);
          }}
          onKeyDown={onKeyDown}
          onSelect={syncCaret}
          onBlur={dismiss}
          placeholder={placeholder}
          aria-label="Prompt"
          aria-describedby={ghost ? hintId : undefined}
          aria-autocomplete="inline"
          spellCheck
          className={cn(
            textLayer,
            "absolute inset-0 h-full w-full resize-none overflow-hidden bg-transparent outline-none placeholder:text-muted-foreground"
          )}
        />

        <div className="absolute inset-x-3 bottom-2.5 flex items-center justify-between gap-2">
          <AnimatePresence>
            {ghost ? (
              <motion.button
                key="accept"
                type="button"
                initial={{ opacity: 0, y: 2 }}
                animate={{ opacity: 1, y: 0 }}
                exit={{ opacity: 0, y: 2 }}
                transition={{ duration: 0.15 }}
                // Keep focus in the textarea, so the tap doesn't blur and dismiss it.
                onMouseDown={(e) => e.preventDefault()}
                onClick={() => accept(ghost)}
                className="inline-flex items-center gap-1.5 rounded-full bg-background px-2 py-0.5 text-xs text-muted-foreground transition-colors hover:bg-accent hover:text-foreground border"
              >
                <kbd className="rounded border bg-muted px-1 font-sans text-[10px] font-medium">Tab</kbd>
                to accept
              </motion.button>
            ) : (
              <span />
            )}
          </AnimatePresence>

          {onSubmit && (
            <button
              type="button"
              aria-label="Send"
              disabled={!value.trim()}
              onMouseDown={(e) => e.preventDefault()}
              onClick={() => onSubmit(value)}
              className="flex size-8 items-center justify-center rounded-full bg-foreground text-background transition-opacity disabled:opacity-30"
            >
              <ArrowUp className="size-4" aria-hidden />
            </button>
          )}
        </div>
      </div>

      {/* Read once, not on every keystroke: the hint names the suggestion and the keys. */}
      <p id={hintId} className="sr-only" aria-live="polite">
        {ghost ? `Suggestion: ${value.split(/\s+/).slice(-2).join(" ")}${ghost}. Press Tab to accept, Right Arrow for one word, Escape to dismiss.` : ""}
      </p>
    </div>
  );
}

/** The leading whitespace plus the first word of `text`. */
function nextWord(text: string) {
  const match = text.match(/^\s*\S+/);
  return match ? match[0] : text;
}
```
