Skip to content

Commit a678bd8

Browse files
perf(web): the composer measures its prompt once per frame
The composer checked whether its prompt wraps from a MutationObserver on the whole composer body, so every DOM write there, from keystrokes to a thread switch rebuilding the editor, forced a layout right away. It now measures at most once per animation frame, after the frame's writes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8272bf9 commit a678bd8

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, it } from "vite-plus/test";
2+
3+
import { render } from "../../../test/render";
4+
import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt";
5+
6+
function Composer({ text }: { readonly text: string }) {
7+
const [multiline, observeBody] = useComposerMultilinePrompt();
8+
return (
9+
<div ref={observeBody} style={{ width: 200, lineHeight: "20px" }}>
10+
<div data-testid="composer-editor" contentEditable suppressContentEditableWarning>
11+
{text}
12+
</div>
13+
<output>{multiline ? "multiline" : "single line"}</output>
14+
</div>
15+
);
16+
}
17+
18+
it("notices when typing makes the prompt wrap", async () => {
19+
await using screen = await render(<Composer text="short" />, { router: false });
20+
await expect.element(screen.getByText("single line")).toBeVisible();
21+
await screen.rerender(<Composer text={"a long prompt that wraps ".repeat(6)} />);
22+
await expect.element(screen.getByText("multiline")).toBeVisible();
23+
});

‎apps/web/src/components/chat/useComposerMultilinePrompt.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@ export function useComposerMultilinePrompt(): readonly [
2727
const [isMultiline, setIsMultiline] = useState(false);
2828
const observeBody = (body: HTMLElement | null) => {
2929
if (!body) return;
30+
// Measuring reads layout, so one measurement per frame runs after all of the frame's DOM
31+
// writes instead of forcing a layout after each mutation.
32+
let frame: number | null = null;
3033
const measure = () => {
31-
const next = measureComposerMultilinePrompt(body);
32-
if (next !== null) setIsMultiline(next);
34+
if (frame !== null) return;
35+
frame = requestAnimationFrame(() => {
36+
frame = null;
37+
const next = measureComposerMultilinePrompt(body);
38+
if (next !== null) setIsMultiline(next);
39+
});
3340
};
3441
measure();
3542
const editor = body.querySelector<HTMLElement>('[data-testid="composer-editor"]');
3643
const stopObserving = observeResize(editor ? [body, editor] : body, measure);
3744
const mutationObserver = new MutationObserver(measure);
3845
mutationObserver.observe(body, { childList: true, characterData: true, subtree: true });
3946
return () => {
47+
if (frame !== null) cancelAnimationFrame(frame);
4048
stopObserving();
4149
mutationObserver.disconnect();
4250
};

0 commit comments

Comments
 (0)