Skip to content

Commit ba0ae76

Browse files
committed
feat: Use ResizeObserver instead of window.getComputedStyle for managing message box sizing
1 parent 45a86a5 commit ba0ae76

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

tools/server/webui/src/lib/components/app/chat/ChatMessages/ChatMessageUser.svelte

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,34 @@
5353
textareaElement = $bindable()
5454
}: Props = $props();
5555
56-
let innerWidth = $state(0);
5756
let isMultiline = $state(false);
5857
let messageElement: HTMLElement | undefined = $state();
5958
6059
$effect(() => {
61-
if (messageElement && message.content.trim() && innerWidth > 0) {
62-
if (message.content.includes('\n')) {
63-
isMultiline = true;
64-
return;
60+
if (!messageElement || !message.content.trim()) return;
61+
62+
if (message.content.includes('\n')) {
63+
isMultiline = true;
64+
return;
65+
}
66+
67+
const resizeObserver = new ResizeObserver((entries) => {
68+
for (const entry of entries) {
69+
const element = entry.target as HTMLElement;
70+
const estimatedSingleLineHeight = 24; // Typical line height for text-md
71+
72+
isMultiline = element.offsetHeight > estimatedSingleLineHeight * 1.5;
6573
}
74+
});
6675
67-
const computedStyle = window.getComputedStyle(messageElement);
68-
const lineHeight = parseFloat(computedStyle.lineHeight);
69-
const actualHeight = messageElement.scrollHeight;
76+
resizeObserver.observe(messageElement);
7077
71-
isMultiline = actualHeight > lineHeight * 1.2;
72-
}
78+
return () => {
79+
resizeObserver.disconnect();
80+
};
7381
});
7482
</script>
7583

76-
<svelte:window bind:innerWidth />
77-
7884
<div
7985
aria-label="User message with actions"
8086
class="group flex flex-col items-end gap-3 md:gap-2 {className}"

0 commit comments

Comments
 (0)