import { Send, Image, Sparkles } from 'lucide-react'; import ImagePreview from…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import { Send, Image, Sparkles } from 'lucide-react'; import ImagePreview from './ImagePreview';
export default function ChatInput({ input, setInput, imagePreview, handleSendMessage, handleFileSelect, handleRemoveImage, isLoading, fileInputRef }) { return ( {imagePreview && ( )}
<div className="flex items-center gap-2 rounded-full shadow-sm border-2 border-zinc-900 px-3 py-1 focus-within:border-teal-700 transition-all duration-300">
<button
onClick={() => fileInputRef.current?.click()}
className="p-2 text-gray-500 hover:text-cyan-400 hover:bg-slate-900 rounded-full transition-colors"
disabled={isLoading}
>
<Image size={18} strokeWidth={2} />
<input
type="file"
ref={fileInputRef}
onChange={handleFileSelect}
accept="image/*"
className="hidden"
disabled={isLoading}
/>
</button>
<div className="flex-1 relative">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Send a message..."
className="w-full py-2 px-3 bg-transparent border-none focus:outline-none text-white placeholder-zinc-500"
onKeyPress={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
}}
disabled={isLoading}
/>
</div>
<button
onClick={handleSendMessage}
disabled={isLoading || (!input.trim() && !imagePreview)}
className={`p-2.5 rounded-full flex items-center justify-center transition-all duration-300 ${
isLoading || (!input.trim() && !imagePreview)
? 'bg-zinc-900 text-gray-600 cursor-not-allowed'
: 'bg-gradient-to-r from-cyan-800 to-teal-900 hover:from-cyan-700 hover:to-teal-800 text-cyan-100 hover:shadow-md'
}`}
>
{isLoading ? (
<div className="h-5 w-5 rounded-full border-2 border-t-transparent border-cyan-300 animate-spin"></div>
) : (
<Send size={18} strokeWidth={2} />
)}
</button>
</div>
<div className="flex justify-center mt-2">
<div className="text-xs text-zinc-600 flex items-center gap-1">
<Sparkles size={10} className="text-cyan-600" />
<span>Press Enter to send</span>
</div>
</div>
</div>
</div>
); } the input height should be dynamic
To make the input height dynamic (so that it grows as the user types more lines), you can replace the <input> field with a textarea that automatically adjusts its height. One straightforward way to achieve this in React is by using the react-textarea-autosize package.
Below are the steps and updated code to do this.
First, install the react-textarea-autosize package:
npm install react-textarea-autosize
# or
yarn add react-textarea-autosize
Replace the <input> with <TextareaAutosize> and update any related styling. Here's the modified component:
import { Send, Image, Sparkles } from 'lucide-react';
import TextareaAutosize from 'react-textarea-autosize';
import ImagePreview from './ImagePreview';
export default function ChatInput({
input,
setInput,
imagePreview,
handleSendMessage,
handleFileSelect,
handleRemoveImage,
isLoading,
fileInputRef
}) {
return (
<div className="p-4 border-t border-zinc-900 bg-black backdrop-blur-sm shadow-md">
<div className="max-w-4xl mx-auto">
{imagePreview && (
<ImagePreview
imagePreview={imagePreview}
handleRemoveImage={handleRemoveImage}
/>
)}
<div className="flex items-center gap-2 rounded-full shadow-sm border-2 border-zinc-900 px-3 py-1 focus-within:border-teal-700 transition-all duration-300">
<button
onClick={() => fileInputRef.current?.click()}
className="p-2 text-gray-500 hover:text-cyan-400 hover:bg-slate-900 rounded-full transition-colors"
disabled={isLoading}
>
<Image size={18} strokeWidth={2} />
<input
type="file"
ref={fileInputRef}
onChange={handleFileSelect}
accept="image/*"
className="hidden"
disabled={isLoading}
/>
</button>
<div className="flex-1 relative">
<TextareaAutosize
minRows={1}
maxRows={6} // Limit the maximum rows if desired
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Send a message..."
className="w-full py-2 px-3 bg-transparent border-none focus:outline-none text-white placeholder-zinc-500 resize-none"
onKeyPress={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
}}
disabled={isLoading}
/>
</div>
<button
onClick={handleSendMessage}
disabled={isLoading || (!input.trim() && !imagePreview)}
className={`p-2.5 rounded-full flex items-center justify-center transition-all duration-300 ${
isLoading || (!input.trim() && !imagePreview)
? 'bg-zinc-900 text-gray-600 cursor-not-allowed'
: 'bg-gradient-to-r from-cyan-800 to-teal-900 hover:from-cyan-700 hover:to-teal-800 text-cyan-100 hover:shadow-md'
}`}
>
{isLoading ? (
<div className="h-5 w-5 rounded-full border-2 border-t-transparent border-cyan-300 animate-spin"></div>
) : (
<Send size={18} strokeWidth={2} />
)}
</button>
</div>
<div className="flex justify-center mt-2">
<div className="text-xs text-zinc-600 flex items-center gap-1">
<Sparkles size={10} className="text-cyan-600" />
<span>Press Enter to send</span>
</div>
</div>
</div>
</div>
);
}
Using TextareaAutosize:
The <TextareaAutosize> component automatically adjusts its height based on the content. The minRows prop ensures there's a minimum height, and the maxRows prop (optional) can limit how tall it can be before turning scrollable.
Styling Adjustments:
border-none and resize-none.KeyPress Handling:
We maintain the same onKeyPress event to send the message when the user presses Enter (without Shift).
This update should give you a dynamically sized input area that expands as the user types multiple lines, improving the UX of your chat input.