Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

chat-input.tsx

174 lines4.6 KB

1import { useRef, useState } from 'react'
2import { useChatContext } from './chat'
3import type { ReactNode, RefObject } from 'react'
4​
5export interface ChatInputRenderProps {
6 /** Current input value */
7 value: string
8 /** Set input value */
9 onChange: (value: string) => void
10 /** Submit the message */
11 onSubmit: () => void
12 /** Is the chat currently loading */
13 isLoading: boolean
14 /** Is input disabled */
15 disabled: boolean
16 /** Ref to the input element */
17 inputRef: RefObject<HTMLInputElement | HTMLTextAreaElement | null>
19​
20export interface ChatInputProps {
21 /** Render prop for full control */
22 children?: (props: ChatInputRenderProps) => ReactNode
23 /** CSS class name */
24 className?: string
25 /** Placeholder text */
26 placeholder?: string
27 /** Disable input */
28 disabled?: boolean
29 /** Submit on Enter (Shift+Enter for new line) */
30 submitOnEnter?: boolean
32​
33/**
34 * Chat input component - handles message input and submission
35 *
36 * Features:
37 * - Auto-growing textarea
38 * - Submit on Enter (Shift+Enter for new line)
39 * - Loading state management
40 * - Full render prop support for custom UIs
41 *
42 * @example
43 * ```tsx
44 * <Chat.Input placeholder="Type your message..." />
45 * ```
46 *
47 * @example Custom UI with render prop
48 * ```tsx
49 * <Chat.Input>
50 * {({ value, onChange, onSubmit, isLoading }) => (
51 * <div>
52 * <textarea value={value} onChange={(e) => onChange(e.target.value)} />
53 * <button onClick={onSubmit} disabled={isLoading}>Send</button>
54 * </div>
55 * )}
56 * </Chat.Input>
57 * ```
58 */
59export function ChatInput({
60 children,
61 className,
62 placeholder = 'Type a message...',
63 disabled: disabledProp,
64 submitOnEnter = true,
65}: ChatInputProps) {
66 const { sendMessage, isLoading } = useChatContext()
67 const [value, setValue] = useState('')
68 const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
69​
70 const disabled = disabledProp || isLoading
71​
72 const handleSubmit = () => {
73 if (!value.trim() || disabled) return
74 void sendMessage(value)
75 setValue('')
76 }
77​
78 const renderProps: ChatInputRenderProps = {
79 value,
80 onChange: setValue,
81 onSubmit: handleSubmit,
82 isLoading,
83 disabled,
84 inputRef,
85 }
86​
87 // Render prop pattern
88 if (children) {
89 return <>{children(renderProps)}</>
90 }
91​
92 // Default implementation
93 return (
94 <div
95 className={className}
96 data-chat-input
97 style={{
98 display: 'flex',
99 gap: '0.75rem',
100 alignItems: 'center',
101 width: '100%',
102 }}
103 >
104 <input
105 ref={inputRef as React.RefObject<HTMLInputElement>}
106 type="text"
107 value={value}
108 onChange={(e) => setValue(e.target.value)}
109 onKeyDown={(e) => {
110 if (submitOnEnter && e.key === 'Enter') {
111 e.preventDefault()
112 handleSubmit()
113 }
114 }}
115 placeholder={placeholder}
116 disabled={disabled}
117 data-chat-textarea
118 style={{
119 flex: 1,
120 padding: '0.75rem 1rem',
121 fontSize: '0.875rem',
122 border: '1px solid rgba(255, 255, 255, 0.1)',
123 borderRadius: '0.75rem',
124 backgroundColor: 'rgba(31, 41, 55, 0.5)',
125 color: 'white',
126 outline: 'none',
127 transition: 'all 0.2s',
128 }}
129 onFocus={(e) => {
130 e.target.style.borderColor = 'rgba(249, 115, 22, 0.4)'
131 e.target.style.boxShadow = '0 0 0 2px rgba(249, 115, 22, 0.2)'
132 }}
133 onBlur={(e) => {
134 e.target.style.borderColor = 'rgba(255, 255, 255, 0.1)'
135 e.target.style.boxShadow = 'none'
136 }}
137 />
138 <button
139 onClick={handleSubmit}
140 disabled={disabled || !value.trim()}
141 data-chat-submit
142 style={{
143 padding: '0.75rem 1.5rem',
144 fontSize: '0.875rem',
145 fontWeight: 500,
146 color: 'white',
147 backgroundColor:
148 disabled || !value.trim()
149 ? 'rgba(107, 114, 128, 0.5)'
150 : 'rgb(249, 115, 22)',
151 border: 'none',
152 borderRadius: '0.75rem',
153 cursor: disabled || !value.trim() ? 'not-allowed' : 'pointer',
154 transition: 'all 0.2s',
155 whiteSpace: 'nowrap',
156 }}
157 onMouseEnter={(e) => {
158 if (!disabled && value.trim()) {
159 ;(e.target as HTMLButtonElement).style.backgroundColor =
160 'rgb(234, 88, 12)'
161 }
162 }}
163 onMouseLeave={(e) => {
164 if (!disabled && value.trim()) {
165 ;(e.target as HTMLButtonElement).style.backgroundColor =
166 'rgb(249, 115, 22)'
167 }
168 }}
169 >
170 {isLoading ? 'Sending...' : 'Send'}
171 </button>
172 </div>
173 )