Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(chat): keep the newest messages in view while the composer resizes

Manas Raghuwanshid1f5402e26e1
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.test.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.test.tsx
index 789b5dd48..8656e17a2 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.test.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.test.tsx
@@ -167,6 +167,29 @@ describe("ComposerInputBox", () => {
expect(textarea.style.height).toBe("38px");
});
+ it("holds the surrounding layout while it measures", () => {
+ const { container, rerender } = render(composer("one\ntwo\nthree"));
+ const textarea = container.querySelector("textarea")!;
+ const parent = textarea.parentElement!;
+ parent.getBoundingClientRect = () => ({ height: 82 }) as DOMRect;
+ // The collapse to "auto" while measuring must not shrink the parent, or
+ // WebKit clamps the transcript scrolled above the composer.
+ let parentMinHeightWhileMeasuring = "";
+ const measure = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "scrollHeight")!.get!;
+ Object.defineProperty(textarea, "scrollHeight", {
+ configurable: true,
+ get() {
+ parentMinHeightWhileMeasuring = parent.style.minHeight;
+ return measure.call(this);
+ },
+ });
+
+ rerender(composer("one\ntwo\nthree!"));
+
+ expect(parentMinHeightWhileMeasuring).toBe("82px");
+ expect(parent.style.minHeight).toBe("");
+ });
+
it("never grows an empty box for a long placeholder", () => {
// e.g. a hovered Home card's prompt, or "… is running. Reply after this run finishes."
const longPlaceholder = "List every meeting I had today with its decisions, owners and deadlines, then flag follow-ups.";
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.test.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.test.tsx
index 29f07011a..1aff42e3f 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.test.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.test.tsx
@@ -4,9 +4,25 @@
import { act, renderHook, waitFor } from "@testing-library/react";
import { createRef } from "react";
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { useChatScroll } from "./use-chat-scroll";
+afterEach(() => vi.unstubAllGlobals());
+
+// The shared test ResizeObserver never fires. This one lets a test resize a
+// single element and only notifies if that element is observed.
+function stubResizeObserver() {
+ const observed = new Map<Element, ResizeObserverCallback>();
+ vi.stubGlobal("ResizeObserver", class {
+ constructor(private callback: ResizeObserverCallback) {}
+ observe(element: Element) { observed.set(element, this.callback); }
+ unobserve(element: Element) { observed.delete(element); }
+ disconnect() { observed.clear(); }
+ });
+ return (element: Element) =>
+ observed.get(element)?.([], {} as ResizeObserver);
+}
+
function defineScrollGeometry(
element: HTMLDivElement,
geometry: { scrollHeight: number; clientHeight: number; scrollTop: number },
@@ -155,6 +171,57 @@ describe("useChatScroll", () => {
expect(result.current.isUserScrolledUp).toBe(false);
});
+ it("keeps the newest message in view when a taller composer shrinks the viewport", () => {
+ const resize = stubResizeObserver();
+ const container = document.createElement("div");
+ container.append(document.createElement("div"));
+ defineScrollGeometry(container, {
+ scrollHeight: 1_400,
+ clientHeight: 600,
+ scrollTop: 800,
+ });
+ renderChatScroll(container);
+
+ // e.g. a failed send restores a long prompt and the composer grows 170px.
+ // The content is unchanged; only the viewport above the composer shrinks.
+ defineScrollGeometry(container, {
+ scrollHeight: 1_400,
+ clientHeight: 430,
+ scrollTop: 800,
+ });
+ act(() => resize(container));
+
+ expect(container.scrollTop).toBe(1_400);
+ });
+
+ it("leaves a scrolled-up reader in place when the viewport shrinks", () => {
+ const resize = stubResizeObserver();
+ const container = document.createElement("div");
+ container.append(document.createElement("div"));
+ defineScrollGeometry(container, {
+ scrollHeight: 1_400,
+ clientHeight: 600,
+ scrollTop: 100,
+ });
+ const { result } = renderChatScroll(container);
+ defineScrollGeometry(container, {
+ scrollHeight: 1_400,
+ clientHeight: 600,
+ scrollTop: 100,
+ });
+ act(() => result.current.handleMessagesScroll());
+
+ defineScrollGeometry(container, {
+ scrollHeight: 1_400,
+ clientHeight: 430,
+ scrollTop: 100,
+ });
+ act(() => resize(container));
+
+ expect(container.scrollTop).toBe(100);
+ expect(result.current.isUserScrolledUp).toBe(true);
+ });
+
it("restores scroll tracking if a programmatic jump is interrupted", async () => {
const container = document.createElement("div");
container.append(document.createElement("div"));
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.ts
index 71de8fddc..046c49ea8 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-scroll.ts
@@ -147,6 +147,8 @@ export function useChatScroll({
}
});
observer.observe(content);
+ // A taller composer shrinks the viewport without resizing the content.
+ observer.observe(container);
return () => observer.disconnect();
}, [scheduleScrollToBottom, scrollContainerRef, syncScrollState]);
diff --git a/packages/workflows-ui/src/chat-primitives.tsx b/packages/workflows-ui/src/chat-primitives.tsx
index df3b33087..3292bd48a 100644
--- a/packages/workflows-ui/src/chat-primitives.tsx
+++ b/packages/workflows-ui/src/chat-primitives.tsx
@@ -103,14 +103,23 @@ type ComposerTextAreaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> &
function fitToContent(element: HTMLTextAreaElement) {
// A hidden field measures zero; keep the last height until it is shown again.
if (!element.offsetWidth) return;
+ // Measuring collapses the box for a moment. Hold the parent's height so the
+ // layout around it does not shrink too: WebKit clamps a scrolled transcript
+ // above it during that collapse and the newest lines drop out of view. This
+ // assumes the parent does not stretch the field to its own height.
+ const parent = element.parentElement;
+ const parentMinHeight = parent?.style.minHeight ?? "";
+ if (parent) parent.style.minHeight = `${parent.getBoundingClientRect().height}px`;
element.style.overflowY = "hidden";
element.style.height = "auto";
// An empty box keeps its natural height. Browsers count placeholder text in
// scrollHeight, and a long placeholder (e.g. a hovered Home card's prompt)
// must not resize the box.
- if (!element.value) return;
- element.style.height = `${element.scrollHeight}px`;
- element.style.overflowY = element.scrollHeight > element.clientHeight ? "auto" : "hidden";
+ if (element.value) {
+ element.style.height = `${element.scrollHeight}px`;
+ element.style.overflowY = element.scrollHeight > element.clientHeight ? "auto" : "hidden";
+ }
+ if (parent) parent.style.minHeight = parentMinHeight;
}
/** The same input primitive used by the standalone Chat composer and its embeds. */