Skip to content

Build and run agents you can see, understand and trust.

fix(webui): re-measure the chat composer when its width changes (#3953)

2SinceJuly1f5df4f33ba6
diff --git a/examples/web_ui/frontend/src/components/chat/TextInput.tsx b/examples/web_ui/frontend/src/components/chat/TextInput.tsx
index 652ee4720..c78eaa090 100644
--- a/examples/web_ui/frontend/src/components/chat/TextInput.tsx
+++ b/examples/web_ui/frontend/src/components/chat/TextInput.tsx
@@ -201,8 +201,11 @@ export const TextInput = forwardRef<TextInputRef, TextInputProps>(
const textareaRef = useRef<HTMLTextAreaElement>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const measureRef = useRef<HTMLSpanElement>(null);
+ const ghostColumnRef = useRef<HTMLDivElement>(null);
/** ``true`` — textarea takes the full width, buttons drop to their own row. */
const [isStacked, setIsStacked] = useState(false);
+ /** Width of the column the ghost measures into; changes re-run both measurements. */
+ const [composerWidth, setComposerWidth] = useState(0);
const asrContextRef = useRef({ key: asrResetKey, mounted: true });
useEffect(() => {
@@ -324,7 +327,7 @@ export const TextInput = forwardRef<TextInputRef, TextInputProps>(
textarea.style.height = 'auto';
textarea.style.height = `${textarea.scrollHeight}px`;
// Stacking widens the textarea, so the line count has to be redone.
- }, [value, isStacked]);
+ }, [value, isStacked, composerWidth]);
// Measured on the ghost, not the textarea: stacking widens the textarea, so
// measuring it would flip-flop (wraps → stack → fits → unstack → wraps).
@@ -332,7 +335,21 @@ export const TextInput = forwardRef<TextInputRef, TextInputProps>(
const ghost = measureRef.current;
if (!ghost) return;
setIsStacked(ghost.scrollHeight > LINE_HEIGHT_PX);
- }, [value]);
+ }, [value, composerWidth]);
+
+ // Both measurements above are keyed on the draft, so a width change without an
+ // edit leaves the layout stale — the draft can wrap at one width and not the
+ // other. Track the measured column's width and re-run them when it moves.
+ useLayoutEffect(() => {
+ const column = ghostColumnRef.current;
+ if (!column) return;
+ const observer = new ResizeObserver((entries) => {
+ const entry = entries[0];
+ if (entry) setComposerWidth(entry.contentRect.width);
+ });
+ observer.observe(column);
+ return () => observer.disconnect();
+ }, []);
// Calculate autocomplete suggestion using useMemo
const suggestion = useMemo(() => {
@@ -586,7 +603,7 @@ export const TextInput = forwardRef<TextInputRef, TextInputProps>(
aria-hidden
className="pointer-events-none invisible absolute inset-x-0 top-0 flex h-0 items-start overflow-hidden"
>
- <div className="min-w-0 flex-1">
+ <div ref={ghostColumnRef} className="min-w-0 flex-1">
{/* Padding-x and line-height decide where text wraps, so they
match the textarea; padding-y is left off on purpose. */}
<span