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

chat-input.tsx

168 lines4.7 KB

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