Thinking UI

Prompt Input

An auto-growing chat input whose send button turns into a stop button.

Code

components/thinking-ui/prompt-input.tsx
"use client";

import { AnimatePresence, motion } from "motion/react";
import { useState } from "react";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";

export type PromptInputProps = {
  value?: string;
  onValueChange?: (value: string) => void;
  onSubmit?: (value: string) => void;
  onStop?: () => void;
  /** Turns the send button into a stop button. */
  isGenerating?: boolean;
  placeholder?: string;
  className?: string;
};

export function PromptInput({
  value,
  onValueChange,
  onSubmit,
  onStop,
  isGenerating = false,
  placeholder = "Ask anything…",
  className,
}: PromptInputProps) {
  const [inner, setInner] = useState("");
  const m = useMotion();
  const text = value ?? inner;
  const canSend = text.trim().length > 0;

  const setText = (next: string) => {
    if (value === undefined) setInner(next);
    onValueChange?.(next);
  };

  const submit = () => {
    if (isGenerating) {
      onStop?.();
      return;
    }
    if (!canSend) return;
    onSubmit?.(text.trim());
    setText("");
  };

  const pop = m.pop();

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        submit();
      }}
      className={cn("surface-input transition-ui flex w-full items-end gap-2 rounded-input p-2 pl-4", className)}
    >
      <textarea
        rows={1}
        value={text}
        aria-label="Message"
        placeholder={placeholder}
        onChange={(e) => setText(e.target.value)}
        onKeyDown={(e) => {
          if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
            e.preventDefault();
            submit();
          }
        }}
        className="max-h-40 min-h-9 flex-1 resize-none bg-transparent py-2 text-base text-input-fg outline-none [field-sizing:content] placeholder:text-fg-muted"
      />
      <motion.button
        type="submit"
        aria-label={isGenerating ? "Stop generating" : "Send message"}
        disabled={!canSend && !isGenerating}
        whileTap={m.press}
        transition={m.spring("micro")}
        className="transition-ui relative grid size-control shrink-0 place-items-center rounded-full [&>svg]:[grid-area:1/1] bg-btn-solid text-btn-solid-fg disabled:bg-fill-strong disabled:text-fg-muted"
      >
        <AnimatePresence>
          {isGenerating && (
            <motion.span
              aria-hidden
              className="absolute -inset-[3px] rounded-full border-2 border-transparent border-t-line-heavy"
              initial={{ opacity: 0 }}
              animate={{ opacity: 1, rotate: 360 }}
              exit={{ opacity: 0 }}
              transition={{
                opacity: m.tween("fast"),
                rotate: { duration: 1 * m.scale, ease: "linear", repeat: Infinity },
              }}
            />
          )}
        </AnimatePresence>
        <AnimatePresence mode="popLayout" initial={false}>
          {isGenerating ? (
            <motion.svg key="stop" viewBox="0 0 16 16" className="size-icon-sm" {...pop}>
              <rect x="3.5" y="3.5" width="9" height="9" rx="2" fill="currentColor" />
            </motion.svg>
          ) : (
            <motion.svg key="send" viewBox="0 0 16 16" className="size-icon" {...pop}>
              <path d="M8 13V3M3.5 7.5L8 3l4.5 4.5" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
            </motion.svg>
          )}
        </AnimatePresence>
      </motion.button>
    </form>
  );
}

Motion notes

  • The send and stop icons cross-fade with scale and blur rather than a hard swap.
  • A rotating ring appears around the button while generating, so the stop action has context.
  • The textarea grows with CSS field-sizing, so no JavaScript resize loop.
  • Enter sends, Shift+Enter adds a new line, and IME composition is respected.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts from Get started.

  3. 3

    Paste the source into components/thinking-ui/prompt-input.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<PromptInput
  isGenerating={status === "streaming"}
  onSubmit={send}
  onStop={stop}
/>

Props

PropTypeDefaultDescription
valuestring—Controlled value. Leave it out for uncontrolled use.
onValueChange(value: string) => void—Called on every change.
onSubmit(value: string) => void—Called with the trimmed value.
onStop() => void—Called when stop is pressed while generating.
isGeneratingbooleanfalseShows the stop state.
placeholderstring"Ask anything…"Placeholder text.