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
@@ -1354,6 +1354,7 @@ button {
|
|||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
font-size: var(--fs-ui);
|
font-size: var(--fs-ui);
|
||||||
line-height: var(--lh-body);
|
line-height: var(--lh-body);
|
||||||
|
overflow-wrap: anywhere;
|
||||||
scrollbar-width: thin;
|
scrollbar-width: thin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1361,6 +1362,26 @@ button {
|
|||||||
color: var(--faint);
|
color: var(--faint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.composer-sizer {
|
||||||
|
position: fixed;
|
||||||
|
top: -10000px;
|
||||||
|
left: -10000px;
|
||||||
|
height: 0;
|
||||||
|
min-height: 0;
|
||||||
|
max-height: none;
|
||||||
|
overflow: hidden;
|
||||||
|
visibility: hidden;
|
||||||
|
pointer-events: none;
|
||||||
|
border: 0;
|
||||||
|
padding: var(--sp-1) var(--sp-2);
|
||||||
|
font: inherit;
|
||||||
|
font-size: var(--fs-ui);
|
||||||
|
line-height: var(--lh-body);
|
||||||
|
white-space: pre-wrap;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
resize: none;
|
||||||
|
}
|
||||||
|
|
||||||
.composer-actions {
|
.composer-actions {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -1,25 +1,42 @@
|
|||||||
<script setup lang="ts">
|
<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 { AtSign, Send } from 'lucide-vue-next'
|
||||||
import type { Message, SendMessageResponse } from '~/types'
|
import type { Message, SendMessageResponse } from '~/types'
|
||||||
import { apiErrorMessage } from '~/utils/errors'
|
import { apiErrorMessage } from '~/utils/errors'
|
||||||
|
|
||||||
const props = defineProps<{ roomId: string }>()
|
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 api = useApi()
|
||||||
const text = ref('')
|
const text = ref('')
|
||||||
const sending = ref(false)
|
const sending = ref(false)
|
||||||
const error = ref('')
|
const error = ref('')
|
||||||
const input = ref<HTMLTextAreaElement | null>(null)
|
const input = ref<HTMLTextAreaElement | null>(null)
|
||||||
|
const sizer = ref<HTMLTextAreaElement | null>(null)
|
||||||
let typingTimer: number | null = null
|
let typingTimer: number | null = null
|
||||||
let typingActive = false
|
let typingActive = false
|
||||||
|
let widthObserver: ResizeObserver | null = null
|
||||||
|
let inputWidth = 0
|
||||||
|
|
||||||
function resize() {
|
function resize() {
|
||||||
const element = input.value
|
const element = input.value
|
||||||
|
const mirror = sizer.value
|
||||||
if (!element) return
|
if (!element) return
|
||||||
element.style.height = 'auto'
|
const previous = element.getBoundingClientRect().height
|
||||||
element.style.height = `${element.scrollHeight}px`
|
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) {
|
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(() => {
|
onBeforeUnmount(() => {
|
||||||
if (typingTimer !== null) window.clearTimeout(typingTimer)
|
if (typingTimer !== null) window.clearTimeout(typingTimer)
|
||||||
|
widthObserver?.disconnect()
|
||||||
|
widthObserver = null
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -105,6 +137,16 @@ onBeforeUnmount(() => {
|
|||||||
@input="onInput"
|
@input="onInput"
|
||||||
@keydown="onKeydown"
|
@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">
|
<div class="composer-actions">
|
||||||
<span class="composer-hint">Enter sends, Shift+Enter adds a line.</span>
|
<span class="composer-hint">Enter sends, Shift+Enter adds a line.</span>
|
||||||
<div class="composer-buttons">
|
<div class="composer-buttons">
|
||||||
|
|||||||
@@ -637,7 +637,7 @@ let stickScheduled = false
|
|||||||
/// Keep the newest turn card or message in view while the log grows,
|
/// Keep the newest turn card or message in view while the log grows,
|
||||||
/// unless the reader has scrolled away from the bottom.
|
/// unless the reader has scrolled away from the bottom.
|
||||||
function scheduleStick() {
|
function scheduleStick() {
|
||||||
if (!stick.value || stickScheduled) return
|
if (!stick.value || suspendAutoScroll || stickScheduled) return
|
||||||
stickScheduled = true
|
stickScheduled = true
|
||||||
void nextTick(() => {
|
void nextTick(() => {
|
||||||
stickScheduled = false
|
stickScheduled = false
|
||||||
@@ -708,6 +708,10 @@ async function loadOlder() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onComposerResized() {
|
||||||
|
scheduleStick()
|
||||||
|
}
|
||||||
|
|
||||||
async function onSent(message: Message) {
|
async function onSent(message: Message) {
|
||||||
addMessage(message, true)
|
addMessage(message, true)
|
||||||
stick.value = true
|
stick.value = true
|
||||||
@@ -858,7 +862,12 @@ onBeforeUnmount(() => {
|
|||||||
<p v-if="typingText" class="room-status-line">{{ typingText }}</p>
|
<p v-if="typingText" class="room-status-line">{{ typingText }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ChatComposer :room-id="roomId" @sent="onSent" @typing="onTyping" />
|
<ChatComposer
|
||||||
|
:room-id="roomId"
|
||||||
|
@sent="onSent"
|
||||||
|
@typing="onTyping"
|
||||||
|
@resized="onComposerResized"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
Reference in new issue
Block a user