Spaces:
Running
Running
File size: 1,494 Bytes
ff6ab6d | 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 | import { useRef, useEffect } from 'preact/hooks';
export default function ChatInput({ onSend, disabled }) {
const textareaRef = useRef(null);
useEffect(() => {
if (!disabled) textareaRef.current?.focus();
}, [disabled]);
function handleSend() {
const text = textareaRef.current?.value.trim();
if (!text || disabled) return;
onSend(text);
textareaRef.current.value = '';
textareaRef.current.style.height = '38px';
}
function handleKeyDown(e) {
if (e.key === 'Enter' && !e.shiftKey && !disabled) {
e.preventDefault();
handleSend();
}
}
function handleInput() {
const el = textareaRef.current;
el.style.height = '38px';
if (el.scrollHeight > 38) {
el.style.height = el.scrollHeight + 'px';
}
}
return (
<div class="chat-input">
<div class="input-container">
<textarea
ref={textareaRef}
placeholder="Type your message..."
onKeyDown={handleKeyDown}
onInput={handleInput}
disabled={disabled}
/>
</div>
<button
class="send-button"
onClick={handleSend}
disabled={disabled}
aria-label="Send message"
>
➤
</button>
</div>
);
}
|