From fd6a7c4ba96cf144b3115b8cb0a15894e01a6d91 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Fri, 11 Sep 2026 00:11:11 +0200 Subject: [PATCH] Follow latest chat messages after delayed layout on refresh --- packages/viewer/src/lib/ChatPanel.svelte | 82 +++++++++++++++--------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/packages/viewer/src/lib/ChatPanel.svelte b/packages/viewer/src/lib/ChatPanel.svelte index 640f1db..11d4352 100644 --- a/packages/viewer/src/lib/ChatPanel.svelte +++ b/packages/viewer/src/lib/ChatPanel.svelte @@ -5,6 +5,8 @@ const chat = $derived(store.chat); let list: HTMLDivElement | undefined = $state(); + let contents: HTMLDivElement | undefined = $state(); + let scrollFrame: number | undefined; let pinned = true; const colors = ["#e6c780", "#83ca90", "#80c5d7", "#d999b8"]; const notice = $derived( @@ -28,6 +30,14 @@ const id = latest?.dataset.messageId; if (id) chat.markRead(id); } + function followLatest() { + if (scrollFrame !== undefined) cancelAnimationFrame(scrollFrame); + scrollFrame = requestAnimationFrame(() => { + if (list && pinned) list.scrollTop = list.scrollHeight; + scrollFrame = undefined; + visibleRead(); + }); + } $effect(() => { const element = list; chat.messages; @@ -36,15 +46,22 @@ let disposed = false; void tick().then(() => { if (!disposed) { - if (pinned) element.scrollTop = element.scrollHeight; - visibleRead(); + followLatest(); } }); const observer = new IntersectionObserver(visibleRead); observer.observe(element); + // BGS can reveal the iframe or finish loading its stylesheet after history arrives. + // Follow changes to the viewport and message heights, including late avatar/font loads. + const resize = new ResizeObserver(followLatest); + resize.observe(element); + if (contents) resize.observe(contents); return () => { disposed = true; observer.disconnect(); + resize.disconnect(); + if (scrollFrame !== undefined) cancelAnimationFrame(scrollFrame); + scrollFrame = undefined; }; }); @@ -73,37 +90,42 @@ aria-label="Chat messages" tabindex="0" onscroll={() => { - if (list) pinned = list.scrollHeight - list.scrollTop - list.clientHeight < 32; + if (list && scrollFrame === undefined) pinned = list.scrollHeight - list.scrollTop - list.clientHeight < 32; visibleRead(); }} > - {#if !chat.messages.length}

Plan your escape together.

{/if} - {#each chat.messages as message, i (message._id ?? i)} -
- {#if message.author} -
- {#if message.playerIndex !== undefined} - - {:else}{message.author}{/if} - {#if message.createdAt}{/if} - {#if message.editedAt}edited{/if} -
- {/if} -

{message.text}

-
- {/each} +
+ {#if !chat.messages.length}

Plan your escape together.

{/if} + {#each chat.messages as message, i (message._id ?? i)} +
+ {#if message.author} +
+ {#if message.playerIndex !== undefined} + + {:else}{message.author}{/if} + {#if message.createdAt}{/if} + {#if message.editedAt}edited{/if} +
+ {/if} +

{message.text}

+
+ {/each} +
{#if chat.canSend && !chat.disabled}