Skip to content

Commit badf088

Browse files
committed
🤖 fix: use useLayoutEffect for chat input auto-resize
The chat input could appear way too big vertically on initial app launch, then collapse to correct size when text was entered or window was resized. Root cause: useEffect runs asynchronously after paint, so the initial scrollHeight measurement could happen before layout settled, resulting in an incorrect (too large) height that persisted until next value change. Fix: Switch to useLayoutEffect which runs synchronously after DOM mutations but before the browser paints, ensuring accurate initial height measurement.
1 parent 5874155 commit badf088

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/browser/components/VimTextArea.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useRef, useState } from "react";
1+
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
22
import type { UIMode } from "@/common/types/mode";
33
import * as vim from "@/browser/utils/vim";
44
import { TooltipWrapper, Tooltip, HelpIndicator } from "./Tooltip";
@@ -64,11 +64,15 @@ export const VimTextArea = React.forwardRef<HTMLTextAreaElement, VimTextAreaProp
6464
const yankBufferRef = useRef<string>("");
6565

6666
// Auto-resize when value changes
67-
useEffect(() => {
67+
// Uses useLayoutEffect to measure and set height synchronously before paint,
68+
// preventing flash of wrong size on initial render
69+
useLayoutEffect(() => {
6870
const el = textareaRef.current;
6971
if (!el) return;
72+
// Reset to auto to get accurate scrollHeight measurement
7073
el.style.height = "auto";
7174
const max = window.innerHeight * 0.5; // 50vh
75+
// scrollHeight gives the full content height; clamp to max
7276
el.style.height = Math.min(el.scrollHeight, max) + "px";
7377
}, [value]);
7478

0 commit comments

Comments
 (0)