Skip to content

Commit c11b9b5

Browse files
committed
feat(chat): auto-expanding textarea
1 parent 98fd8e6 commit c11b9b5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

frontend/src/chat/chat.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@
5959
<!-- Input Area -->
6060
<div class="border-t p-4">
6161
<form @submit.prevent="sendMessage" :disabled="sendingMessage" class="flex gap-2">
62-
<input
62+
<textarea
6363
v-model="newMessage"
6464
placeholder="Ask something..."
65-
class="flex-1 border rounded px-4 py-2"
66-
/>
65+
class="flex-1 border rounded px-4 py-2 resize-none overflow-y-auto"
66+
rows="1"
67+
ref="messageInput"
68+
@input="adjustTextareaHeight"
69+
@keydown.enter.exact.prevent="handleEnter"
70+
></textarea>
6771
<button class="bg-blue-600 text-white px-4 py-2 rounded disabled:bg-gray-600" :disabled="sendingMessage">
6872
<svg v-if="sendingMessage" style="height: 1em" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
6973
<g>

frontend/src/chat/chat.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ module.exports = app => app.component('chat', {
4444
this.chatMessages.push(chatMessages[1]);
4545

4646
this.newMessage = '';
47+
this.$nextTick(() => {
48+
if (this.$refs.messageInput) {
49+
this.$refs.messageInput.style.height = 'auto';
50+
}
51+
});
4752

4853
this.$nextTick(() => {
4954
if (this.$refs.messagesContainer) {
@@ -54,6 +59,18 @@ module.exports = app => app.component('chat', {
5459
this.sendingMessage = false;
5560
}
5661
},
62+
handleEnter(ev) {
63+
if (!ev.shiftKey) {
64+
this.sendMessage();
65+
}
66+
},
67+
adjustTextareaHeight(ev) {
68+
const textarea = ev.target;
69+
textarea.style.height = 'auto';
70+
const lineHeight = parseInt(window.getComputedStyle(textarea).lineHeight, 10);
71+
const maxHeight = lineHeight * 5;
72+
textarea.style.height = Math.min(textarea.scrollHeight, maxHeight) + 'px';
73+
},
5774
selectThread(threadId) {
5875
this.$router.push('/chat/' + threadId);
5976
},

0 commit comments

Comments
 (0)