-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt-input.tsx
More file actions
228 lines (206 loc) · 5.47 KB
/
prompt-input.tsx
File metadata and controls
228 lines (206 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @component PromptInput
* @source prompt-kit
* @upstream https://prompt-kit.com/docs/prompt-input
* @customized true
* @customizations
* - `autoFocus` is enabled by default on PromptInputTextarea
* - Removes redundant `TooltipProvider` wrapper in `PromptInputAction`
* - Not A Wrapper uses app-level TooltipProvider for consistency and smaller bundle
* - Upstream uses useLayoutEffect; Not A Wrapper uses standard useEffect for SSR safety
* @upgradeNotes
* - Preserve autoFocus default on PromptInputTextarea
* - Do NOT re-add TooltipProvider wrapper in PromptInputAction
* - Verify useEffect vs useLayoutEffect for textarea auto-resize
*/
"use client"
import { Textarea } from "@/components/ui/textarea"
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip"
import { cn } from "@/lib/utils"
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import React, {
createContext,
useContext,
useEffect,
useRef,
useState,
} from "react"
type PromptInputContextType = {
isLoading: boolean
value: string
setValue: (value: string) => void
maxHeight: number | string
onSubmit?: () => void
disabled?: boolean
textareaRef: React.RefObject<HTMLTextAreaElement | null>
}
const PromptInputContext = createContext<PromptInputContextType | undefined>(
undefined
)
function usePromptInput() {
const context = useContext(PromptInputContext)
if (!context) {
throw new Error("usePromptInput must be used within a PromptInput")
}
return context
}
type PromptInputProps = {
isLoading?: boolean
value?: string
onValueChange?: (value: string) => void
maxHeight?: number | string
onSubmit?: () => void
disabled?: boolean
children: React.ReactNode
className?: string
}
function PromptInput({
className,
isLoading = false,
maxHeight = 240,
value,
onValueChange,
onSubmit,
disabled = false,
children,
}: PromptInputProps) {
const [internalValue, setInternalValue] = useState(value || "")
const textareaRef = useRef<HTMLTextAreaElement>(null)
const handleChange = (newValue: string) => {
setInternalValue(newValue)
onValueChange?.(newValue)
}
return (
<PromptInputContext.Provider
value={{
isLoading,
value: value ?? internalValue,
setValue: onValueChange ?? handleChange,
maxHeight,
onSubmit,
disabled,
textareaRef,
}}
>
<div
className={cn(
"bg-[var(--composer-bg)] cursor-text rounded-[28px] p-2 shadow-composer",
className
)}
onClick={() => {
textareaRef.current?.focus()
}}
>
{children}
</div>
</PromptInputContext.Provider>
)
}
export type PromptInputTextareaProps = {
disableAutosize?: boolean
} & React.ComponentProps<typeof Textarea>
function PromptInputTextarea({
className,
onKeyDown,
disableAutosize = false,
...props
}: PromptInputTextareaProps) {
const { value, setValue, maxHeight, onSubmit, disabled, textareaRef } =
usePromptInput()
useEffect(() => {
if (disableAutosize || !textareaRef.current) return
// Reset height to auto first to properly measure scrollHeight
textareaRef.current.style.height = "auto"
// Set the height based on content
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
}, [value, disableAutosize, textareaRef])
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault()
onSubmit?.()
}
onKeyDown?.(e)
}
const maxHeightStyle =
typeof maxHeight === "number" ? `${maxHeight}px` : maxHeight
return (
<Textarea
ref={textareaRef}
autoFocus
value={value}
onChange={(e) => setValue(e.target.value)}
onKeyDown={handleKeyDown}
className={cn(
"text-primary min-h-[44px] w-full resize-none border-none bg-transparent dark:bg-transparent shadow-none outline-none focus-visible:ring-0 focus-visible:ring-offset-0",
"overflow-y-auto",
className
)}
style={{
maxHeight: maxHeightStyle,
}}
rows={1}
disabled={disabled}
{...props}
/>
)
}
type PromptInputActionsProps = React.HTMLAttributes<HTMLDivElement>
function PromptInputActions({
children,
className,
...props
}: PromptInputActionsProps) {
return (
<div className={cn("flex items-center gap-1", className)} {...props}>
{children}
</div>
)
}
type PromptInputActionProps = {
className?: string
tooltip: React.ReactNode
children: React.ReactElement
side?: "top" | "bottom" | "left" | "right"
hideArrow?: boolean
} & React.ComponentProps<typeof Tooltip>
function PromptInputAction({
tooltip,
children,
className,
side = "bottom",
hideArrow = true,
...tooltipProps
}: PromptInputActionProps) {
const { disabled } = usePromptInput()
const trigger = useRender({
defaultTagName: "button",
render: children,
props: mergeProps<"button">(
{
type: "button",
disabled,
onClick: (event) => event.stopPropagation(),
},
{}
),
})
return (
<Tooltip {...tooltipProps}>
<TooltipTrigger render={trigger} disabled={disabled} />
<TooltipContent side={side} hideArrow={hideArrow} className={className}>
{tooltip}
</TooltipContent>
</Tooltip>
)
}
export {
PromptInput,
PromptInputTextarea,
PromptInputActions,
PromptInputAction,
}