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): size the composer to its text, including text set by code

Manas Raghuwanshi6b2e674efac5
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
new file mode 100644
index 000000000..51ca63e5a
--- /dev/null
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.test.tsx
@@ -0,0 +1,140 @@
+// screenpipe — AI that knows everything you've seen, said, or heard
+// https://screenpipe.com
+
+import React, { createRef } from "react";
+import { render } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { ComposerInputBox } from "./composer-input-box";
+import type { ComposerInputProps, ComposerMentionsProps } from "./composer-types";
+
+const LINE_HEIGHT = 22;
+const PADDING = 16;
+const MAX_HEIGHT = 208;
+
+const mentions = {
+ show: false,
+ suggestions: [],
+ dropdownRef: createRef(),
+ selectedIndex: 0,
+ onInsertMention: vi.fn(),
+ isLoadingSpeakers: false,
+ isLoadingTagSearch: false,
+} as unknown as ComposerMentionsProps;
+
+function composer(
+ value: string,
+ { chip = false, chipWidth = 0, placeholder }: { chip?: boolean; chipWidth?: number; placeholder?: string } = {},
+) {
+ const input = {
+ value,
+ placeholder,
+ inputRef: createRef(),
+ chipPrefixRef: createRef(),
+ connectionChip: chip ? { id: "slack", name: "Slack", icon: "slack" } : null,
+ chipPrefixWidth: chipWidth,
+ chipScrollTop: 0,
+ disabledReason: null,
+ canChat: true,
+ onChange: vi.fn(),
+ onKeyDown: vi.fn(),
+ } as unknown as ComposerInputProps;
+ return <ComposerInputBox input={input} mentions={mentions} />;
+}
+
+// jsdom has no layout. Model 60 characters per line, the placeholder filling an
+// empty box (as Chromium and WebKit measure it), one more line when the chip indent pushes
+// the text over, a box held at its CSS max-height once the text is taller, and
+// zero width while hidden.
+let visible = true;
+const layout: Record<string, (this: HTMLTextAreaElement) => number> = {
+ scrollHeight() {
+ const text = this.value || this.placeholder;
+ const rows = text.split("\n").reduce((sum, line) => sum + Math.max(1, Math.ceil(line.length / 60)), 0);
+ return (rows + (this.style.textIndent ? 1 : 0)) * LINE_HEIGHT + PADDING;
+ },
+ clientHeight() {
+ return Math.min(parseFloat(this.style.height) || 0, MAX_HEIGHT);
+ },
+ offsetWidth() {
+ return visible ? 600 : 0;
+ },
+};
+
+beforeEach(() => {
+ visible = true;
+ for (const [name, get] of Object.entries(layout)) {
+ Object.defineProperty(HTMLTextAreaElement.prototype, name, { configurable: true, get });
+ }
+});
+
+afterEach(() => {
+ for (const name of Object.keys(layout)) {
+ delete (HTMLTextAreaElement.prototype as unknown as Record<string, unknown>)[name];
+ }
+});
+
+describe("ComposerInputBox", () => {
+ it("fits text that is set without typing, then shrinks when cleared", () => {
+ const { container, rerender } = render(composer(""));
+ const textarea = container.querySelector("textarea")!;
+ expect(textarea.style.height).toBe("auto");
+
+ // e.g. a suggestion prompt or a failed send restored into the composer.
+ rerender(composer("one\ntwo\nthree"));
+ expect(textarea.style.height).toBe("82px");
+ expect(textarea.style.overflowY).toBe("hidden");
+
+ rerender(composer(""));
+ expect(textarea.style.height).toBe("auto");
+ });
+
+ it("scrolls only once the text is taller than the max height", () => {
+ const { container, rerender } = render(composer("short"));
+ const textarea = container.querySelector("textarea")!;
+ expect(textarea.style.overflowY).toBe("hidden");
+
+ rerender(composer(Array.from({ length: 20 }, (_, i) => `line ${i}`).join("\n")));
+ expect(textarea.style.overflowY).toBe("auto");
+ });
+
+ it("keeps its height while hidden instead of collapsing", () => {
+ const { container, rerender } = render(composer("one\ntwo\nthree"));
+ const textarea = container.querySelector("textarea")!;
+ expect(textarea.style.height).toBe("82px");
+
+ // Chat stays mounted but hidden behind another page.
+ visible = false;
+ rerender(composer("one\ntwo\nthree\nfour"));
+ expect(textarea.style.height).toBe("82px");
+ });
+
+ it("refits when a connection chip rewraps the same text", () => {
+ const { container, rerender } = render(composer("summarize my unread threads"));
+ const textarea = container.querySelector("textarea")!;
+ expect(textarea.style.height).toBe("38px");
+
+ // As in the app: the chip renders first, and its measured width (which
+ // sets the indent) arrives one render later with the text unchanged.
+ rerender(composer("summarize my unread threads", { chip: true }));
+ expect(textarea.style.height).toBe("38px");
+ rerender(composer("summarize my unread threads", { chip: true, chipWidth: 64 }));
+ expect(textarea.style.height).toBe("60px");
+
+ rerender(composer("summarize my unread threads"));
+ expect(textarea.style.height).toBe("38px");
+ });
+
+ 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.";
+ const { container, rerender } = render(composer("", { placeholder: longPlaceholder }));
+ const textarea = container.querySelector("textarea")!;
+ expect(textarea.style.height).toBe("auto");
+
+ rerender(composer("typed text", { placeholder: longPlaceholder }));
+ expect(textarea.style.height).toBe("38px");
+
+ rerender(composer("", { placeholder: longPlaceholder }));
+ expect(textarea.style.height).toBe("auto");
+ });
+});
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.tsx b/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.tsx
index 5fdc1f5bf..24f4ccc5e 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.tsx
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/composer-input-box.tsx
@@ -14,6 +14,9 @@ import type {
import { cn } from "@/lib/utils";
import { useGT } from "gt-react";
+// Text scrolling into the padding fades out and never reaches the border.
+const SCROLL_FADE =
+ "[mask-image:linear-gradient(to_bottom,transparent_0.25rem,#000_0.5rem,#000_calc(100%_-_0.5rem),transparent_calc(100%_-_0.25rem))]";
export function ComposerInputBox({
input,
@@ -26,18 +29,18 @@ export function ComposerInputBox({
return (
<div
className={cn(
- "flex flex-col rounded-lg border bg-input ring-offset-background transition-colors duration-150 focus-within:border-signal focus-within:ring-signal/15 focus-within:ring-1 motion-reduce:transition-none",
- "border-border bg-surface shadow-[0_4px_18px_rgba(0,0,0,0.08)] dark:shadow-[0_4px_18px_rgba(0,0,0,0.28)]",
+ "flex flex-col rounded-lg border border-border bg-surface shadow-[0_4px_18px_rgba(0,0,0,0.08)] dark:shadow-[0_4px_18px_rgba(0,0,0,0.28)] transition-colors duration-150 focus-within:border-signal/40 motion-reduce:transition-none",
input.disabledReason && "border-muted-foreground/30",
)}
>
<div className="relative flex-1 min-w-0">
{input.connectionChip && (
<>
- <div className="pointer-events-none absolute left-3 right-7 top-2.5 bottom-2.5 z-10 overflow-hidden">
+ <div className={cn("pointer-events-none absolute inset-y-0 left-3 right-7 z-10 overflow-hidden", SCROLL_FADE)}>
+ {/* Sits on the textarea's first line: py-2 top, 22px line height. */}
<div
ref={input.chipPrefixRef}
- className="absolute left-0 top-0 flex h-5 items-center gap-1.5"
+ className="absolute left-0 top-2 flex h-[22px] items-center gap-1.5"
style={{ transform: `translateY(${-input.chipScrollTop}px)` }}
>
<IntegrationIcon
@@ -54,7 +57,7 @@ export function ComposerInputBox({
type="button"
aria-label={ui("Remove connection context")}
onClick={input.onClearConnectionChip}
- className="absolute right-2.5 top-2 z-10 text-muted-foreground/60 hover:text-foreground transition-colors shrink-0"
+ className="absolute right-2.5 top-3 z-10 text-muted-foreground/60 hover:text-foreground transition-colors shrink-0"
>
<X className="w-3.5 h-3.5" />
</button>
@@ -62,7 +65,6 @@ export function ComposerInputBox({
)}
<ComposerTextArea
aria-label={input.ariaLabel}
- autoGrow={false}
ref={input.inputRef}
value={input.value}
onChange={input.onChange}
@@ -80,11 +82,11 @@ export function ComposerInputBox({
autoCorrect="off"
rows={1}
className={cn(
- "w-full min-h-[38px] border-0 bg-transparent px-3 text-sm font-mono placeholder:text-muted-foreground focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50 caret-signal resize-none overflow-y-auto scrollbar-minimal py-2",
+ "block w-full max-h-52 border-0 bg-transparent px-3 text-sm leading-[22px] font-mono placeholder:text-muted-foreground focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50 caret-signal resize-none overscroll-contain scrollbar-minimal py-2",
+ SCROLL_FADE,
input.connectionChip ? "pr-7" : "pr-3",
)}
style={{
- maxHeight: "150px",
textIndent:
input.connectionChip && input.chipPrefixWidth
? `${input.chipPrefixWidth + 8}px`
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-attachments.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-attachments.ts
index 1d7a6cd42..8ec205f92 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-attachments.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-attachments.ts
@@ -245,13 +245,7 @@ export function useChatAttachments({
setAttachedDocs((prev) => prev.filter((_, idx) => idx !== index));
setShowMentionDropdown(false);
setMentionFilter("");
- window.setTimeout(() => {
- inputRef.current?.focus();
- if (inputRef.current) {
- inputRef.current.style.height = "auto";
- inputRef.current.style.height = `${Math.min(inputRef.current.scrollHeight, 150)}px`;
- }
- }, 0);
+ window.setTimeout(() => inputRef.current?.focus(), 0);
}, [inputRef, setInput, setMentionFilter, setShowMentionDropdown, setAttachedDocs]);
const handleFilePicker = useCallback(async () => {
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-mentions.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-mentions.ts
index 02f48b2fa..9c07b7ccb 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-mentions.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-mentions.ts
@@ -635,10 +635,7 @@ export function useChatMentions({
const value = event.target.value;
setInput(value);
- const textarea = event.target;
- textarea.style.height = "auto";
- textarea.style.height = `${Math.min(textarea.scrollHeight, 150)}px`;
- if (hasConnectionChip) setChipScrollTop(textarea.scrollTop);
+ if (hasConnectionChip) setChipScrollTop(event.target.scrollTop);
const cursorPos = event.target.selectionStart || 0;
const textBeforeCursor = value.slice(0, cursorPos);
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts
index 1dac46031..8dd188e18 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-chat-window-events.ts
@@ -215,7 +215,6 @@ export function useChatPrefillListener({
if (sendMessageRef.current) {
await sendMessageRef.current(fullMessage, visiblePrompt, prefillImages);
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
}
} finally {
autoSendBypassRef.current = false;
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-message-queue-transport.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-message-queue-transport.ts
index 6cce0094f..4f16a022a 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-message-queue-transport.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-message-queue-transport.ts
@@ -21,7 +21,6 @@ export function createPiMessageQueueTransport(
activePreset,
consumePendingAttachments,
input,
- inputRef,
messages,
pastedImages,
piInfo,
@@ -55,7 +54,6 @@ export function createPiMessageQueueTransport(
const queuedAttachments = consumePendingAttachments();
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
if (hadPastedImages) setPastedImages([]);
// Same recovery contract as normal sends. Rust strips the wrapper for a
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
index 85d0bfbce..e21748594 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-send-transport.ts
@@ -428,7 +428,6 @@ export function usePiSendTransport(options: PiSendTransportOptions) {
// the send aborts.
setPendingSend({ sessionId: attemptSessionId, text: userMessage, displayLabel });
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
// A selector change may still be in flight when the user submits. Wait for
// it here as the authoritative boundary. Rejections
@@ -744,7 +743,6 @@ export function usePiSendTransport(options: PiSendTransportOptions) {
setPendingSend(null);
if (isAttemptForeground()) {
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
setIsLoading(true);
setIsStreaming(true);
}
diff --git a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-steering-transport.ts b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-steering-transport.ts
index 570c598b1..ab0019bca 100644
--- a/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-steering-transport.ts
+++ b/apps/screenpipe-app-tauri/components/chat/standalone/hooks/use-pi-steering-transport.ts
@@ -51,7 +51,6 @@ export function usePiSteeringTransport(
currentQueueSessionId,
finishQueuedAction,
flushStreamingMessageRender,
- inputRef,
isLoading,
isStreaming,
lastUserMessageRef,
@@ -452,7 +451,6 @@ export function usePiSteeringTransport(
useChatStore.getState().actions.setMessages(sidNow, nextRowsAfterOptimisticAppend as any);
}
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
if (shouldClearPastedImages) setPastedImages([]);
diff --git a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
index dd382056a..d3d509bdb 100644
--- a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
+++ b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts
@@ -10,7 +10,6 @@ import {
useCallback,
type Dispatch,
type SetStateAction,
- type RefObject,
type MutableRefObject,
} from "react";
import { emit, listen } from "@tauri-apps/api/event";
@@ -69,7 +68,6 @@ interface UseChatConversationsOpts {
conversationId: string | null;
setConversationId: Dispatch<SetStateAction<string | null>>;
setInput: Dispatch<SetStateAction<string>>;
- inputRef: RefObject<HTMLTextAreaElement | null>;
isLoading: boolean;
isStreaming: boolean;
piStreamingTextRef: MutableRefObject<string>;
@@ -152,7 +150,6 @@ export function useChatConversations(opts: UseChatConversationsOpts) {
conversationId,
setConversationId,
setInput,
- inputRef,
isLoading,
isStreaming,
piStreamingTextRef,
@@ -1560,7 +1557,6 @@ export function useChatConversations(opts: UseChatConversationsOpts) {
// Keep the outgoing draft visible during async restoration. Replace it
// atomically with the incoming transcript/draft once this request wins.
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
setPastedImages([]);
setAttachedDocs?.([]);
setPendingDocs?.([]);
@@ -1809,7 +1805,6 @@ export function useChatConversations(opts: UseChatConversationsOpts) {
setMessages([]);
setConversationId(null);
setInput("");
- if (inputRef.current) inputRef.current.style.height = "auto";
setShowHistory(false);
setPastedImages([]);
// Clear any in-progress doc attachments — they belong to the chat the
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 9be26e300..8986282ff 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -974,7 +974,6 @@ export function StandaloneChat({
conversationId,
setConversationId,
setInput,
- inputRef,
isLoading,
isStreaming,
piStreamingTextRef,
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/load-conversation-hydration.test.tsx b/apps/screenpipe-app-tauri/lib/__tests__/load-conversation-hydration.test.tsx
index f75af9c3c..8e195cfa1 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/load-conversation-hydration.test.tsx
+++ b/apps/screenpipe-app-tauri/lib/__tests__/load-conversation-hydration.test.tsx
@@ -115,7 +115,6 @@ function useHarness() {
setInput,
inputValueRef,
pastedImagesRef: useRef([]),
- inputRef: useRef<HTMLTextAreaElement | null>(null),
isLoading: false,
isStreaming: false,
piStreamingTextRef,
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx b/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx
index b0edc2077..6b0291802 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx
+++ b/apps/screenpipe-app-tauri/lib/__tests__/save-conversation-race.test.tsx
@@ -126,7 +126,6 @@ function useHarness(args: {
const piStreamingTextRef = useRef("");
const piMessageIdRef = useRef<string | null>(null);
const piContentBlocksRef = useRef<any[]>([]);
- const inputRef = useRef<HTMLTextAreaElement | null>(null);
const hook = useChatConversations({
messages: messagesRef.current as any,
@@ -138,7 +137,6 @@ function useHarness(args: {
conversationIdRef.current = typeof updater === "function" ? updater(conversationIdRef.current) : updater;
}) as any,
setInput: vi.fn() as any,
- inputRef,
isLoading: false,
isStreaming: false,
piStreamingTextRef,
diff --git a/packages/workflows-ui/src/chat-primitives.tsx b/packages/workflows-ui/src/chat-primitives.tsx
index eb6675466..df3b33087 100644
--- a/packages/workflows-ui/src/chat-primitives.tsx
+++ b/packages/workflows-ui/src/chat-primitives.tsx
@@ -3,7 +3,7 @@
"use client";
-import React, { forwardRef, memo, useLayoutEffect, useRef } from "react";
+import React, { forwardRef, memo, useEffect, useLayoutEffect, useRef } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { ChevronDown } from "lucide-react";
@@ -94,23 +94,52 @@ export const ChatMarkdown = memo(function ChatMarkdown({ text, streaming = false
});
type ComposerTextAreaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {
- autoGrow?: boolean;
onSend?: () => void;
};
+// Height follows the text up to the stylesheet's max-height, and only a box held
+// there scrolls. Measuring with overflow hidden keeps a passing scrollbar from
+// narrowing the wrap width.
+function fitToContent(element: HTMLTextAreaElement) {
+ // A hidden field measures zero; keep the last height until it is shown again.
+ if (!element.offsetWidth) return;
+ 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";
+}
+
/** The same input primitive used by the standalone Chat composer and its embeds. */
export const ComposerTextArea = forwardRef<HTMLTextAreaElement, ComposerTextAreaProps>(function ComposerTextArea({
- autoGrow = true, onSend, onKeyDown, onCompositionStart, onCompositionEnd, ...props
+ onSend, onKeyDown, onCompositionStart, onCompositionEnd, ...props
}, forwardedRef) {
const input = useRef<HTMLTextAreaElement | null>(null);
const composing = useRef(false);
+ // Runs for every value, including text set by code rather than typed, and
+ // when an indent or padding change rewraps the same text.
useLayoutEffect(() => {
- if (!autoGrow || !input.current) return;
+ if (input.current) fitToContent(input.current);
+ }, [props.value, props.className, props.style?.textIndent]);
+ // A new width rewraps the text. Refit a frame later so the height change
+ // happens outside the observer callback.
+ useEffect(() => {
const element = input.current;
- element.style.height = "auto";
- element.style.height = Math.max(42, Math.min(element.scrollHeight, 160)) + "px";
- element.style.overflowY = element.scrollHeight > 160 ? "auto" : "hidden";
- }, [props.value, autoGrow]);
+ if (!element) return;
+ let width = element.offsetWidth;
+ let frame = 0;
+ const observer = new ResizeObserver(() => {
+ if (element.offsetWidth === width) return;
+ width = element.offsetWidth;
+ cancelAnimationFrame(frame);
+ frame = requestAnimationFrame(() => fitToContent(element));
+ });
+ observer.observe(element);
+ return () => { observer.disconnect(); cancelAnimationFrame(frame); };
+ }, []);
return <textarea {...props}
ref={(element) => { input.current = element; if (typeof forwardedRef === "function") forwardedRef(element); else if (forwardedRef) forwardedRef.current = element; }}
onCompositionStart={(event) => { composing.current = true; onCompositionStart?.(event); }}