Files
rhema-together/web/app/components/ChatComposer.vue
T
vhaudiquet ca333509a2 fix(web): stop the chat scroll bouncing on multiline input
The auto-resize reset the textarea to auto before measuring, which
collapsed it to one row for a frame. That resized the chat viewport,
the browser clamped its scroll to the old bottom, and the next input
scrolled back.

Measure the content in a hidden copy instead. The composer reports a
height change, and the room keeps the chat at the bottom only when the
reader is already there.

Typing and deleting lines now keeps the chat at the bottom without the
jump.
2026-11-28 19:00:56 +01:00

179 lines
5.1 KiB
Vue

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { AtSign, Send } from 'lucide-vue-next'
import type { Message, SendMessageResponse } from '~/types'
import { apiErrorMessage } from '~/utils/errors'
const props = defineProps<{ roomId: string }>()
const emit = defineEmits<{ sent: [message: Message]; typing: [active: boolean]; resized: [] }>()
const api = useApi()
const text = ref('')
const sending = ref(false)
const error = ref('')
const input = ref<HTMLTextAreaElement | null>(null)
const sizer = ref<HTMLTextAreaElement | null>(null)
let typingTimer: number | null = null
let typingActive = false
let widthObserver: ResizeObserver | null = null
let inputWidth = 0
function resize() {
const element = input.value
const mirror = sizer.value
if (!element) return
const previous = element.getBoundingClientRect().height
if (mirror) {
// Measure a detached copy. Resetting the real textarea to `auto`
// collapses it to one row for a frame, which resizes the chat
// viewport and moves the reader off the bottom.
mirror.style.width = `${element.clientWidth}px`
mirror.value = text.value
mirror.style.height = '0px'
element.style.height = `${mirror.scrollHeight}px`
} else {
element.style.height = 'auto'
element.style.height = `${element.scrollHeight}px`
}
const current = element.getBoundingClientRect().height
if (Math.abs(current - previous) > 0.5) emit('resized')
}
function notifyTyping(active: boolean) {
if (typingActive === active) return
typingActive = active
emit('typing', active)
}
function onInput() {
resize()
notifyTyping(true)
if (typingTimer !== null) window.clearTimeout(typingTimer)
typingTimer = window.setTimeout(() => {
typingTimer = null
notifyTyping(false)
}, 2000)
}
function insertMention() {
const element = input.value
const current = text.value
const start = element?.selectionStart ?? current.length
const end = element?.selectionEnd ?? current.length
const before = current.slice(0, start)
const after = current.slice(end)
const spacer = before.length > 0 && !/\s$/.test(before) ? ' ' : ''
const insertion = `${spacer}@rhema `
text.value = `${before}${insertion}${after}`
const cursor = before.length + insertion.length
void nextTick(() => {
element?.focus()
element?.setSelectionRange(cursor, cursor)
resize()
})
}
function onKeydown(event: KeyboardEvent) {
if (event.key !== 'Enter' || event.shiftKey || event.isComposing) return
event.preventDefault()
void send()
}
async function send() {
const value = text.value.trim()
if (!value || sending.value) return
sending.value = true
error.value = ''
try {
const response = await api.post<SendMessageResponse>(`/rooms/${props.roomId}/messages`, { text: value })
text.value = ''
if (typingTimer !== null) {
window.clearTimeout(typingTimer)
typingTimer = null
}
notifyTyping(false)
emit('sent', response.message)
await nextTick()
resize()
input.value?.focus()
} catch (err) {
error.value = apiErrorMessage(err)
} finally {
sending.value = false
}
}
onMounted(() => {
const element = input.value
if (!element) return
inputWidth = element.clientWidth
widthObserver = new ResizeObserver(() => {
const width = input.value?.clientWidth ?? 0
if (width === inputWidth) return
inputWidth = width
resize()
})
widthObserver.observe(element)
})
onBeforeUnmount(() => {
if (typingTimer !== null) window.clearTimeout(typingTimer)
widthObserver?.disconnect()
widthObserver = null
})
</script>
<template>
<div class="composer">
<div class="composer-inner">
<label class="sr-only" for="room-composer">Message the room</label>
<textarea
id="room-composer"
ref="input"
v-model="text"
class="composer-input"
rows="1"
placeholder="Message the room. Use @rhema to ask the agent."
@input="onInput"
@keydown="onKeydown"
/>
<textarea
ref="sizer"
class="composer-sizer"
rows="1"
readonly
tabindex="-1"
aria-hidden="true"
autocomplete="off"
spellcheck="false"
></textarea>
<div class="composer-actions">
<span class="composer-hint">Enter sends, Shift+Enter adds a line.</span>
<div class="composer-buttons">
<button
type="button"
class="icon-btn"
aria-label="Insert @rhema mention"
title="Insert @rhema mention"
@click="insertMention"
>
<AtSign :size="16" aria-hidden="true" />
</button>
<button
type="button"
class="btn btn-primary btn-sm"
:disabled="!text.trim() || sending"
aria-label="Send message"
title="Send"
@click="send"
>
<Send :size="14" aria-hidden="true" />
{{ sending ? 'Sending' : 'Send' }}
</button>
</div>
</div>
</div>
<p v-if="error" class="form-error composer-error" role="alert">{{ error }}</p>
</div>
</template>