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.
This commit is contained in:
1 parent
f5087220da
commit
ca333509a2
3 files changed
+78
-6
No files matched your search
@@ -1,25 +1,42 @@
|
||||
<script setup lang="ts">
|
||||
import { nextTick, onBeforeUnmount, ref } from 'vue'
|
||||
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] }>()
|
||||
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
|
||||
element.style.height = 'auto'
|
||||
element.style.height = `${element.scrollHeight}px`
|
||||
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) {
|
||||
@@ -86,8 +103,23 @@ async function send() {
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
@@ -105,6 +137,16 @@ onBeforeUnmount(() => {
|
||||
@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">
|
||||
|
||||
Reference in new issue
Block a user