Open History to see the last commit
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
18}
19
20export interface ChatInputProps {
21 /** Render prop for full control */
22 children?: (props: ChatInputRenderProps) => JSX.Element
23 /** CSS class name */
24 class?: 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
31}
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} onInput={(e) => onChange(e.target.value)} />
53 * <button onClick={onSubmit} disabled={isLoading}>Send</button>
54 * </div>
55 * )}
56 * </Chat.Input>
57 * ```
58 */
59export function ChatInput(props: ChatInputProps) {
60 const { sendMessage, isLoading } = useChatContext()
61 const [value, setValue] = createSignal('')
62
63 const disabled = () => props.disabled || isLoading()
64
65 const handleSubmit = () => {
66 if (!value().trim() || disabled()) return
67 void sendMessage(value())
68 setValue('')
69 }
70
71 const renderProps = (): ChatInputRenderProps => ({
72 value: value(),
73 onChange: setValue,
74 onSubmit: handleSubmit,
75 isLoading: isLoading(),
76 disabled: disabled(),
77
78 ref: () => {},
79 })
80
81 // Render prop pattern
82 if (props.children) {
83 return <>{props.children(renderProps())}</>
84 }
85
86 // Default implementation
87 return (
88 <div
89 class={props.class}
90 data-chat-input
91 style={{
92 display: 'flex',
93 gap: '0.75rem',
94 'align-items': 'center',
95 width: '100%',
96 }}
97 >
98 <input
99 type="text"
100 value={value()}
101 onInput={(e) => setValue(e.currentTarget.value)}
102 onKeyDown={(e) => {
103 if ((props.submitOnEnter ?? true) && e.key === 'Enter') {
104 e.preventDefault()
105 handleSubmit()
106 }
107 }}
108 placeholder={props.placeholder ?? 'Type a message...'}
109 disabled={disabled()}
110 data-chat-textarea
111 style={{
112 flex: 1,
113 padding: '0.75rem 1rem',
114 'font-size': '0.875rem',
115 border: '1px solid rgba(255, 255, 255, 0.1)',
116 'border-radius': '0.75rem',
117 'background-color': 'rgba(31, 41, 55, 0.5)',
118 color: 'white',
119 outline: 'none',
120 transition: 'all 0.2s',
121 }}
122 onFocus={(e) => {
123 e.currentTarget.style.borderColor = 'rgba(249, 115, 22, 0.4)'
124 e.currentTarget.style.boxShadow = '0 0 0 2px rgba(249, 115, 22, 0.2)'
125 }}
126 onBlur={(e) => {
127 e.currentTarget.style.borderColor = 'rgba(255, 255, 255, 0.1)'
128 e.currentTarget.style.boxShadow = 'none'
129 }}
130 />
131 <button
132 onClick={handleSubmit}
133 disabled={disabled() || !value().trim()}
134 data-chat-submit
135 style={{
136 padding: '0.75rem 1.5rem',
137 'font-size': '0.875rem',
138 'font-weight': 500,
139 color: 'white',
140 'background-color':
141 disabled() || !value().trim()
142 ? 'rgba(107, 114, 128, 0.5)'
143 : 'rgb(249, 115, 22)',
144 border: 'none',
145 'border-radius': '0.75rem',
146 cursor: disabled() || !value().trim() ? 'not-allowed' : 'pointer',
147 transition: 'all 0.2s',
148 'white-space': 'nowrap',
149 }}
150 onMouseEnter={(e) => {
151 if (!disabled() && value().trim()) {
152 e.currentTarget.style.backgroundColor = 'rgb(234, 88, 12)'
153 }
154 }}
155 onMouseLeave={(e) => {
156 if (!disabled() && value().trim()) {
157 e.currentTarget.style.backgroundColor = 'rgb(249, 115, 22)'
158 }
159 }}
160 >
161 {isLoading() ? 'Sending...' : 'Send'}
162 </button>
163 </div>
164 )
165}