AI Prompt Composer
Auto-expanding command composer with model switcher, context chips, and tactile submit morph.
Navbar.tsx
1. Install Dependencies
$npm install framer-motion lucide-react
2. Copy Component Code
components/ui/AiPromptComposer.tsx
1"use client";2
3import React, { useEffect, useId, useRef, useState } from "react";4import { AnimatePresence, motion, useReducedMotion } from "framer-motion";5import {6 ArrowUp,7 Check,8 ChevronDown,9 FileText,10 Loader2,11 Plus,12 Square,13 X,14} from "lucide-react";15
16const APPLE_EASE = [0.22, 1, 0.36, 1] as const;17const LIQUID_GLASS_SPRING = {18 type: "spring",19 stiffness: 350,20 damping: 30,21 mass: 0.9,22} as const;23
24function getLiquidToggleMotion(25 prefersReducedMotion: boolean | null | undefined,26 hasToggled: boolean27) {28 if (prefersReducedMotion || !hasToggled) {29 return {30 initial: false as const,31 animate: { scaleX: 1, scaleY: 1 },32 transition: {33 layout: { duration: 0 },34 duration: 0,35 },36 };37 }38
39 return {40 initial: { scaleX: 1, scaleY: 1 },41 animate: { scaleX: [1, 1.16, 1], scaleY: [1, 0.86, 1] },42 transition: {43 layout: LIQUID_GLASS_SPRING,44 scaleX: { duration: 0.36, times: [0, 0.35, 1], ease: APPLE_EASE },45 scaleY: { duration: 0.36, times: [0, 0.35, 1], ease: APPLE_EASE },46 },47 };48}49
50const DEFAULT_MODELS = ["Claude 4.5", "GPT-5.1", "Gemini 2.5"] as const;51const DEFAULT_FILES = [52 "design-system.md",53 "Navbar.tsx",54 "globals.css",55] as const;56const MODES = ["Chat", "Agent"] as const;57
58export type AiPromptMode = (typeof MODES)[number];59
60export interface AiPromptComposerPayload {61 prompt: string;62 model: string;63 mode: AiPromptMode;64 contextFiles: string[];65}66
67export interface AiPromptComposerProps {68 /** Available AI models shown in the switcher popover. */69 models?: readonly string[];70 /** Initial selected model name. */71 defaultModel?: string;72 /** Initial interaction mode. */73 defaultMode?: AiPromptMode;74 /** Files listed in the "+ Context" attachment popover. */75 availableFiles?: readonly string[];76 /** Initial attached context file names. */77 defaultContextFiles?: readonly string[];78 /** Controlled prompt string. */79 value?: string;80 /** Callback fired when prompt text changes. */81 onValueChange?: (value: string) => void;82 /** Textarea placeholder copy. */83 placeholder?: string;84 /** Optional custom submit handler; falls back to simulated stream status. */85 onSubmit?: (payload: AiPromptComposerPayload) => Promise<void> | void;86 /** Optional callback fired when Stop is pressed during streaming. */87 onStop?: () => void;88 /** Completion status string shown after built-in simulated streaming finishes. */89 simulatedStatusText?: string;90 /** Optional ref callback to focus the textarea externally. */91 textareaRef?: React.RefObject<HTMLTextAreaElement | null>;92 /** Extra classes for the outer wrapper. */93 className?: string;94}95
96/** Auto-expanding AI prompt composer with model switcher, context chips, and submit morph. */97export function AiPromptComposer({98 models = DEFAULT_MODELS,99 defaultModel = DEFAULT_MODELS[0],100 defaultMode = "Agent",101 availableFiles = DEFAULT_FILES,102 defaultContextFiles = ["Navbar.tsx"],103 value,104 onValueChange,105 placeholder = "Ask anything, attach context, or describe an edit...",106 onSubmit,107 onStop,108 simulatedStatusText = "Generated 3 edits in Navbar.tsx",109 textareaRef: externalTextareaRef,110 className = "",111}: AiPromptComposerProps) {112 const prefersReducedMotion = useReducedMotion();113 const modeGroupId = useId();114 const contextListboxId = useId();115 const modelListboxId = useId();116 const internalTextareaRef = useRef<HTMLTextAreaElement>(null);117 const textareaNodeRef = externalTextareaRef ?? internalTextareaRef;118 const containerRef = useRef<HTMLDivElement>(null);119 const streamTimerRef = useRef<number | null>(null);120 const dismissTimerRef = useRef<number | null>(null);121 const lastInputWasKeyboardRef = useRef(false);122
123 const [internalPrompt, setInternalPrompt] = useState("");124 const [isKeyboardFocus, setIsKeyboardFocus] = useState(false);125 const prompt = value !== undefined ? value : internalPrompt;126
127 const [selectedModel, setSelectedModel] = useState<string>(defaultModel);128 const [mode, setMode] = useState<AiPromptMode>(defaultMode);129 const [hasToggledMode, setHasToggledMode] = useState(false);130 const [contextFiles, setContextFiles] = useState<string[]>([131 ...defaultContextFiles,132 ]);133
134 const [contextOpen, setContextOpen] = useState(false);135 const [modelOpen, setModelOpen] = useState(false);136 const [modelHighlightIndex, setModelHighlightIndex] = useState(() =>137 Math.max(0, models.indexOf(defaultModel))138 );139
140 const [isStreaming, setIsStreaming] = useState(false);141 const [statusMessage, setStatusMessage] = useState<string | null>(null);142
143 const trimmedPrompt = prompt.trim();144 const canSubmit = trimmedPrompt.length > 0;145
146 function setPromptValue(next: string) {147 if (value === undefined) {148 setInternalPrompt(next);149 }150 onValueChange?.(next);151 }152
153 // Auto-resize textarea smoothly between 44px and 160px154 useEffect(() => {155 const el = textareaNodeRef.current;156 if (!el) return;157 el.style.height = "auto";158 const nextHeight = Math.min(160, Math.max(44, el.scrollHeight));159 el.style.height = `${nextHeight}px`;160 }, [prompt, textareaNodeRef]);161
162 // Close popovers on outside pointer down or Escape & track keyboard vs pointer modality163 useEffect(() => {164 function handlePointerDown(event: PointerEvent) {165 lastInputWasKeyboardRef.current = false;166 if (!containerRef.current?.contains(event.target as Node)) {167 setContextOpen(false);168 setModelOpen(false);169 }170 }171
172 function handleKeyDown(event: KeyboardEvent) {173 lastInputWasKeyboardRef.current = true;174 if (event.key === "Escape") {175 setContextOpen(false);176 setModelOpen(false);177 }178 }179
180 window.addEventListener("pointerdown", handlePointerDown);181 window.addEventListener("keydown", handleKeyDown);182 return () => {183 window.removeEventListener("pointerdown", handlePointerDown);184 window.removeEventListener("keydown", handleKeyDown);185 };186 }, []);187
188 useEffect(() => {189 return () => {190 if (streamTimerRef.current !== null) {191 window.clearTimeout(streamTimerRef.current);192 }193 if (dismissTimerRef.current !== null) {194 window.clearTimeout(dismissTimerRef.current);195 }196 };197 }, []);198
199 function clearDismissTimer() {200 if (dismissTimerRef.current !== null) {201 window.clearTimeout(dismissTimerRef.current);202 dismissTimerRef.current = null;203 }204 }205
206 function scheduleStatusDismiss() {207 clearDismissTimer();208 dismissTimerRef.current = window.setTimeout(() => {209 setStatusMessage(null);210 dismissTimerRef.current = null;211 }, 3500);212 }213
214 function clearStatus() {215 clearDismissTimer();216 if (streamTimerRef.current !== null) {217 window.clearTimeout(streamTimerRef.current);218 streamTimerRef.current = null;219 }220 setIsStreaming(false);221 setStatusMessage(null);222 }223
224 function addContextFile(file: string) {225 setContextFiles((prev) => (prev.includes(file) ? prev : [...prev, file]));226 setContextOpen(false);227 textareaNodeRef.current?.focus();228 }229
230 function removeContextFile(file: string) {231 setContextFiles((prev) => prev.filter((item) => item !== file));232 }233
234 function handleStop() {235 clearStatus();236 onStop?.();237 }238
239 async function handleSubmit() {240 if (isStreaming) {241 handleStop();242 return;243 }244 if (!canSubmit) return;245
246 setContextOpen(false);247 setModelOpen(false);248 clearDismissTimer();249
250 const payload: AiPromptComposerPayload = {251 prompt: trimmedPrompt,252 model: selectedModel,253 mode,254 contextFiles,255 };256
257 if (onSubmit) {258 setIsStreaming(true);259 setStatusMessage("Thinking...");260 try {261 await onSubmit(payload);262 setIsStreaming(false);263 setStatusMessage(simulatedStatusText);264 scheduleStatusDismiss();265 } catch {266 setIsStreaming(false);267 setStatusMessage(null);268 }269 return;270 }271
272 if (streamTimerRef.current !== null) {273 window.clearTimeout(streamTimerRef.current);274 }275 setIsStreaming(true);276 setStatusMessage("Thinking...");277 streamTimerRef.current = window.setTimeout(() => {278 setIsStreaming(false);279 setStatusMessage(simulatedStatusText);280 streamTimerRef.current = null;281 scheduleStatusDismiss();282 }, 1800);283 }284
285 function handleTextareaKeyDown(286 event: React.KeyboardEvent<HTMLTextAreaElement>287 ) {288 if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {289 event.preventDefault();290 void handleSubmit();291 }292 }293
294 function handleModelTriggerKeyDown(295 event: React.KeyboardEvent<HTMLButtonElement>296 ) {297 if (event.key === "ArrowDown") {298 event.preventDefault();299 if (!modelOpen) {300 setContextOpen(false);301 setModelHighlightIndex(Math.max(0, models.indexOf(selectedModel)));302 setModelOpen(true);303 return;304 }305 setModelHighlightIndex((prev) => (prev + 1) % models.length);306 return;307 }308
309 if (event.key === "ArrowUp") {310 event.preventDefault();311 if (!modelOpen) {312 setContextOpen(false);313 setModelHighlightIndex(Math.max(0, models.indexOf(selectedModel)));314 setModelOpen(true);315 return;316 }317 setModelHighlightIndex((prev) => (prev - 1 + models.length) % models.length);318 return;319 }320
321 if (event.key === "Enter" && modelOpen) {322 event.preventDefault();323 const chosen = models[modelHighlightIndex];324 if (chosen) {325 setSelectedModel(chosen);326 }327 setModelOpen(false);328 return;329 }330
331 if (event.key === "Escape" && modelOpen) {332 event.preventDefault();333 setModelOpen(false);334 }335 }336
337 const transitionEase = prefersReducedMotion338 ? { duration: 0 }339 : { duration: 0.18, ease: APPLE_EASE };340
341 const statusTransition = prefersReducedMotion342 ? { duration: 0 }343 : { duration: 0.2, ease: APPLE_EASE };344
345 return (346 <div ref={containerRef} className={`w-full max-w-[640px] min-w-0 ${className}`}>347 {/* Minimal Floating Status Strip Above Composer */}348 <AnimatePresence initial={false}>349 {statusMessage && (350 <motion.div351 key={isStreaming ? "streaming" : statusMessage}352 initial={prefersReducedMotion ? false : { opacity: 0, y: 6 }}353 animate={{ opacity: 1, y: 0 }}354 exit={prefersReducedMotion ? undefined : { opacity: 0, y: 4 }}355 transition={statusTransition}356 aria-live="polite"357 className="mb-2 flex min-w-0 items-center justify-between gap-2 rounded-[12px] border-0 bg-white px-3 py-2 shadow-[0_4px_16px_rgba(0,0,0,0.05)] dark:border-0 dark:bg-[#111113] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_8px_24px_rgba(0,0,0,0.45)]"358 >359 <div className="flex min-w-0 items-center gap-2 font-mono text-xs leading-4 tracking-normal text-[#0A0A0A]/80 dark:text-white/80">360 {isStreaming ? (361 <Loader2362 className={`h-3.5 w-3.5 shrink-0 text-black/50 dark:text-white/50 ${363 prefersReducedMotion ? "" : "animate-spin"364 }`}365 strokeWidth={1.75}366 aria-hidden="true"367 />368 ) : (369 <Check370 className="h-3.5 w-3.5 shrink-0 text-emerald-500"371 strokeWidth={1.75}372 aria-hidden="true"373 />374 )}375 <span className="truncate">{statusMessage}</span>376 </div>377
378 <button379 type="button"380 onClick={clearStatus}381 aria-label="Dismiss status"382 className="inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-[4px] text-black/40 transition-colors duration-150 hover:text-black active:scale-[0.97] dark:text-white/40 dark:hover:text-white"383 >384 <X385 className="h-3.5 w-3.5"386 strokeWidth={1.75}387 aria-hidden="true"388 />389 </button>390 </motion.div>391 )}392 </AnimatePresence>393
394 {/* Concentric Shell: rounded-[20px] (12px p-3 + 8px inner radius = 20px) */}395 <div className="cosrim-prompt-shell relative rounded-[20px] border-0 bg-white p-3 shadow-[0_4px_20px_rgba(0,0,0,0.05)] transition-shadow duration-200 focus-within:shadow-[0_12px_32px_-4px_rgba(0,0,0,0.09)] dark:border-0 dark:bg-[#111113] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_12px_32px_rgba(0,0,0,0.55)] dark:focus-within:shadow-[inset_0_1px_0_rgba(255,255,255,0.18),0_16px_40px_rgba(0,0,0,0.65)]">396 {/* Attached Context Chips Tray */}397 <AnimatePresence initial={false}>398 {contextFiles.length > 0 && (399 <motion.div400 layout={!prefersReducedMotion}401 initial={prefersReducedMotion ? false : { opacity: 0, y: -4 }}402 animate={{ opacity: 1, y: 0 }}403 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -4 }}404 transition={405 prefersReducedMotion406 ? { duration: 0 }407 : { duration: 0.2, ease: APPLE_EASE }408 }409 className="mb-2 flex flex-wrap items-center gap-2"410 >411 <AnimatePresence initial={false}>412 {contextFiles.map((file) => (413 <motion.span414 key={file}415 layout={!prefersReducedMotion}416 initial={417 prefersReducedMotion418 ? false419 : { opacity: 0, scale: 0.94, y: -4 }420 }421 animate={{ opacity: 1, scale: 1, y: 0 }}422 exit={423 prefersReducedMotion424 ? undefined425 : { opacity: 0, scale: 0.94 }426 }427 transition={428 prefersReducedMotion429 ? { duration: 0 }430 : { duration: 0.2, ease: APPLE_EASE }431 }432 className="inline-flex min-h-8 max-w-full min-w-0 items-center gap-2 rounded-[8px] bg-[#F3F3F5] px-2 font-mono text-xs leading-4 tracking-normal tabular-nums text-[#0A0A0A] transition-colors duration-150 hover:bg-[#EAEAEF] dark:bg-[#1A1A1E] dark:text-white dark:hover:bg-[#222227]"433 >434 <FileText435 className="h-3.5 w-3.5 shrink-0 text-black/50 dark:text-white/50"436 strokeWidth={1.75}437 aria-hidden="true"438 />439 <span className="truncate">{file}</span>440 <button441 type="button"442 onClick={() => removeContextFile(file)}443 aria-label={`Remove ${file} from context`}444 className="inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-[4px] text-black/45 transition-colors duration-150 hover:text-black active:scale-[0.97] dark:text-white/45 dark:hover:text-white"445 >446 <X447 className="h-3.5 w-3.5"448 strokeWidth={1.75}449 aria-hidden="true"450 />451 </button>452 </motion.span>453 ))}454 </AnimatePresence>455 </motion.div>456 )}457 </AnimatePresence>458
459 {/* Auto-Expanding Prompt Textarea */}460 <div className="px-1 py-1">461 <textarea462 ref={textareaNodeRef}463 data-prompt-input=""464 data-keyboard-focus={isKeyboardFocus ? "true" : undefined}465 rows={1}466 value={prompt}467 onPointerDown={() => {468 lastInputWasKeyboardRef.current = false;469 setIsKeyboardFocus(false);470 }}471 onFocus={(e) => {472 setIsKeyboardFocus(473 lastInputWasKeyboardRef.current &&474 e.currentTarget.matches(":focus-visible")475 );476 }}477 onBlur={() => setIsKeyboardFocus(false)}478 onChange={(e) => {479 if (!isStreaming && statusMessage) {480 clearDismissTimer();481 setStatusMessage(null);482 }483 setPromptValue(e.target.value);484 }}485 onKeyDown={handleTextareaKeyDown}486 placeholder={placeholder}487 aria-label="AI prompt"488 data-lenis-prevent="true"489 className="cosrim-code-scroll min-h-[44px] max-h-[160px] w-full resize-none overflow-y-auto bg-transparent text-sm leading-[22px] font-normal tracking-[-0.01em] text-[#0A0A0A] placeholder:text-black/35 dark:text-white dark:placeholder:text-white/35"490 />491 </div>492
493 {/* Bottom Action Bar — strictly pinned right Submit (↑) with wrapping left control cluster */}494 <div className="mt-3 flex flex-nowrap items-end justify-between gap-2 sm:items-center sm:gap-3">495 {/* Left Cluster: + Context, Model Switcher, Mode Toggle */}496 <div className="flex min-w-0 flex-1 flex-wrap items-center gap-1.5 sm:gap-2">497 {/* + Context Button & Anchored Popover */}498 <div className="relative">499 <button500 type="button"501 aria-haspopup="listbox"502 aria-expanded={contextOpen}503 aria-controls={contextListboxId}504 onClick={() => {505 setModelOpen(false);506 setContextOpen((prev) => !prev);507 }}508 className="inline-flex min-h-8 items-center justify-center gap-2 rounded-[8px] bg-[#F3F3F5] px-2 text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] transition-colors duration-150 hover:bg-[#EAEAEF] active:scale-[0.97] dark:bg-[#1A1A1E] dark:text-white dark:hover:bg-[#222227]"509 >510 <Plus511 className="h-3.5 w-3.5 shrink-0"512 strokeWidth={1.75}513 aria-hidden="true"514 />515 <span>Context</span>516 </button>517
518 <AnimatePresence>519 {contextOpen && (520 <motion.div521 id={contextListboxId}522 role="listbox"523 aria-label="Attach context file"524 initial={525 prefersReducedMotion526 ? { opacity: 0 }527 : { opacity: 0, scale: 0.96, y: 4 }528 }529 animate={{ opacity: 1, scale: 1, y: 0 }}530 exit={531 prefersReducedMotion532 ? { opacity: 0 }533 : { opacity: 0, scale: 0.96, y: 4 }534 }535 transition={transitionEase}536 className="absolute bottom-full left-0 z-30 mb-2 w-52 max-w-[calc(100dvw-3rem)] origin-bottom-left rounded-[12px] bg-white p-1 shadow-[0_12px_32px_-4px_rgba(0,0,0,0.12)] dark:bg-[#141417] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_16px_40px_rgba(0,0,0,0.65)]"537 >538 {availableFiles.map((file) => {539 const attached = contextFiles.includes(file);540 return (541 <button542 key={file}543 type="button"544 role="option"545 aria-selected={attached}546 onClick={() => addContextFile(file)}547 className="flex min-h-8 w-full items-center justify-between gap-2 rounded-[8px] px-2 font-mono text-xs leading-4 text-[#0A0A0A] transition-colors duration-150 hover:bg-[#F3F3F5] dark:text-white dark:hover:bg-[#1E1E24]"548 >549 <span className="inline-flex items-center gap-2 truncate">550 <FileText551 className="h-3.5 w-3.5 shrink-0 text-black/45 dark:text-white/45"552 strokeWidth={1.75}553 aria-hidden="true"554 />555 <span className="truncate">{file}</span>556 </span>557 {attached && (558 <Check559 className="h-3.5 w-3.5 shrink-0 text-emerald-500"560 strokeWidth={1.75}561 aria-hidden="true"562 />563 )}564 </button>565 );566 })}567 </motion.div>568 )}569 </AnimatePresence>570 </div>571
572 {/* Anchored Model Switcher */}573 <div className="relative">574 <button575 type="button"576 aria-haspopup="listbox"577 aria-expanded={modelOpen}578 aria-controls={modelListboxId}579 onKeyDown={handleModelTriggerKeyDown}580 onClick={() => {581 setContextOpen(false);582 setModelHighlightIndex(583 Math.max(0, models.indexOf(selectedModel))584 );585 setModelOpen((prev) => !prev);586 }}587 className="inline-flex min-h-8 items-center justify-center gap-2 rounded-[8px] bg-[#F3F3F5] px-2 text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] transition-colors duration-150 hover:bg-[#EAEAEF] active:scale-[0.97] dark:bg-[#1A1A1E] dark:text-white dark:hover:bg-[#222227]"588 >589 <span>{selectedModel}</span>590 <ChevronDown591 className={`h-3.5 w-3.5 shrink-0 text-black/50 transition-transform duration-200 dark:text-white/50 ${592 modelOpen ? "rotate-180" : "rotate-0"593 }`}594 strokeWidth={1.75}595 aria-hidden="true"596 />597 </button>598
599 <AnimatePresence>600 {modelOpen && (601 <motion.div602 id={modelListboxId}603 role="listbox"604 aria-label="Select AI model"605 initial={606 prefersReducedMotion607 ? { opacity: 0 }608 : { opacity: 0, scale: 0.96, y: 4 }609 }610 animate={{ opacity: 1, scale: 1, y: 0 }}611 exit={612 prefersReducedMotion613 ? { opacity: 0 }614 : { opacity: 0, scale: 0.96, y: 4 }615 }616 transition={transitionEase}617 className="absolute bottom-full left-0 z-30 mb-2 w-44 max-w-[calc(100dvw-3rem)] origin-bottom-left rounded-[12px] bg-white p-1 shadow-[0_12px_32px_-4px_rgba(0,0,0,0.12)] dark:bg-[#141417] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_16px_40px_rgba(0,0,0,0.65)]"618 >619 {models.map((item, idx) => {620 const active = selectedModel === item;621 const highlighted = modelHighlightIndex === idx;622 return (623 <button624 key={item}625 type="button"626 role="option"627 aria-selected={active}628 onMouseEnter={() => setModelHighlightIndex(idx)}629 onClick={() => {630 setSelectedModel(item);631 setModelOpen(false);632 textareaNodeRef.current?.focus();633 }}634 className={`flex min-h-8 w-full items-center justify-between gap-2 rounded-[8px] px-2 text-xs leading-4 font-medium tracking-[-0.01em] transition-colors duration-150 ${635 highlighted636 ? "bg-[#F3F3F5] text-[#0A0A0A] dark:bg-[#1E1E24] dark:text-white"637 : "text-black/75 hover:bg-[#F3F3F5] dark:text-white/75 dark:hover:bg-[#1E1E24]"638 }`}639 >640 <span>{item}</span>641 {active && (642 <Check643 className="h-3.5 w-3.5 shrink-0 text-emerald-500"644 strokeWidth={1.75}645 aria-hidden="true"646 />647 )}648 </button>649 );650 })}651 </motion.div>652 )}653 </AnimatePresence>654 </div>655
656 {/* Segmented Mode Switch (Chat | Agent): h-8 p-1 rounded-[8px] -> h-6 rounded-[4px] */}657 <div658 role="group"659 aria-label="Composer mode"660 className="inline-flex min-h-8 items-center rounded-[8px] bg-[#F3F3F5] p-1 dark:bg-[#1A1A1E]"661 >662 {MODES.map((item) => {663 const active = mode === item;664 return (665 <button666 key={item}667 type="button"668 aria-pressed={active}669 onClick={() => {670 if (item !== mode) setHasToggledMode(true);671 setMode(item);672 }}673 className="relative inline-flex min-h-6 items-center justify-center rounded-[4px] px-2 text-xs leading-4 font-medium tracking-[-0.01em] active:scale-[0.97]"674 >675 {active && (676 <motion.span677 layoutId={`${modeGroupId}-mode-thumb`}678 {...getLiquidToggleMotion(679 prefersReducedMotion,680 hasToggledMode681 )}682 aria-hidden="true"683 className="absolute inset-0 rounded-[4px] bg-white shadow-[0_1px_3px_rgba(0,0,0,0.06)] dark:bg-[#2A2A30] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.08)]"684 />685 )}686 <span687 className={`relative z-10 transition-colors duration-150 ${688 active689 ? "text-[#0A0A0A] dark:text-white"690 : "text-black/50 hover:text-black dark:text-white/50 dark:hover:text-white"691 }`}692 >693 {item}694 </span>695 </button>696 );697 })}698 </div>699 </div>700
701 {/* Right Cluster: Live Readout, Keyboard Hint, Submit Morph */}702 <div className="flex shrink-0 items-center gap-2 self-end sm:self-center">703 <AnimatePresence initial={false}>704 {prompt.length > 0 && (705 <motion.span706 initial={prefersReducedMotion ? false : { opacity: 0 }}707 animate={{ opacity: 1 }}708 exit={prefersReducedMotion ? undefined : { opacity: 0 }}709 transition={transitionEase}710 className="hidden font-mono text-xs leading-4 tabular-nums text-black/35 dark:text-white/35 sm:inline-block"711 >712 {prompt.length} chars713 </motion.span>714 )}715 </AnimatePresence>716
717 <span718 aria-hidden="true"719 className="hidden h-8 items-center px-1 font-mono text-xs leading-4 tracking-normal tabular-nums text-black/35 dark:text-white/35 sm:inline-flex"720 >721 ⌘↵722 </span>723
724 <button725 type="button"726 onClick={() => void handleSubmit()}727 disabled={!canSubmit && !isStreaming}728 aria-label={isStreaming ? "Stop generating" : "Submit prompt"}729 title={isStreaming ? "Stop generating" : "Submit prompt"}730 className={`inline-flex h-8 min-h-8 w-8 min-w-8 shrink-0 items-center justify-center rounded-[8px] transition-all duration-150 active:scale-[0.97] ${731 isStreaming732 ? "bg-emerald-500/15 text-emerald-600 shadow-[0_0_0_1px_rgba(16,185,129,0.35)] dark:bg-emerald-500/20 dark:text-emerald-400"733 : canSubmit734 ? "bg-black text-white hover:bg-black/90 dark:bg-white dark:text-black dark:hover:bg-white/90"735 : "cursor-not-allowed bg-[#EFEFF2] text-black/30 dark:bg-[#1A1A1E] dark:text-white/25"736 }`}737 >738 <AnimatePresence mode="popLayout" initial={false}>739 {isStreaming ? (740 <motion.span741 key="stop"742 initial={743 prefersReducedMotion744 ? false745 : { opacity: 0, scale: 0.85 }746 }747 animate={{ opacity: 1, scale: 1 }}748 exit={749 prefersReducedMotion750 ? undefined751 : { opacity: 0, scale: 0.85 }752 }753 transition={transitionEase}754 className="inline-flex items-center justify-center"755 >756 <Square757 className="h-3.5 w-3.5 fill-current"758 strokeWidth={1.75}759 aria-hidden="true"760 />761 </motion.span>762 ) : (763 <motion.span764 key="submit"765 initial={766 prefersReducedMotion767 ? false768 : { opacity: 0, scale: 0.85 }769 }770 animate={{ opacity: 1, scale: 1 }}771 exit={772 prefersReducedMotion773 ? undefined774 : { opacity: 0, scale: 0.85 }775 }776 transition={transitionEase}777 className="inline-flex items-center justify-center"778 >779 <ArrowUp780 className="h-3.5 w-3.5"781 strokeWidth={1.75}782 aria-hidden="true"783 />784 </motion.span>785 )}786 </AnimatePresence>787 </button>788 </div>789 </div>790 </div>791 </div>792 );793}Usage Example
import { AiPromptComposer } from "@/components/ui/AiPromptComposer";
export default function PromptStage() { return ( <AiPromptComposer defaultModel="Claude 4.5" defaultMode="Agent" defaultContextFiles={["Navbar.tsx"]} /> );}3. Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| models | readonly string[] | ["Claude 4.5", "GPT-5.1", "Gemini 2.5"] | Models listed in the anchored model switcher popover. |
| defaultModel | string | "Claude 4.5" | Initially active model label. |
| defaultMode | "Chat" | "Agent" | "Agent" | Initial segmented mode switch state. |
| availableFiles | readonly string[] | ["design-system.md", "Navbar.tsx", "globals.css"] | Attachable file options inside the + Context popover. |
| defaultContextFiles | readonly string[] | ["Navbar.tsx"] | Context chips displayed on initial mount. |
| value | string | undefined | Optional controlled prompt string. |
| onValueChange | (value: string) => void | undefined | Change handler called when the prompt text updates. |
| onSubmit | (payload: AiPromptComposerPayload) => Promise<void> | void | undefined | Async or sync submit callback receiving prompt, model, mode, and contextFiles. |