Skip to content

Commit ec1d9c1

Browse files
matheustimboclaude
andcommitted
fix(web): settle the resting composer layout with a pixel of slack
The composer re-measures its resting controls on every render, and the model picker's natural width is recovered from a truncated label whose scrollWidth is integral while the rendered box is fractional. With the host sitting exactly on a threshold, that sub-pixel difference flipped a block in and out of the overflow menu on every render until React gave up with "Maximum update depth exceeded" (error #185) while opening a thread. Hiding a block stays unconditional, or the controls would clip. Showing one again now has to clear a pixel of slack, so the noise can no longer drive the layout back and forth. Follow-up to #9393, which removed the strip-reservation feedback but left this one. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d2b6f3b commit ec1d9c1

3 files changed

Lines changed: 107 additions & 4 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,11 @@ function useRestingComposerControlsLayout(host: HTMLDivElement | null) {
876876
const hostWidth = currentHost.clientWidth;
877877

878878
setLayout((current) => {
879-
const next = resolveRestingComposerControlsLayout({ ...measurement, hostWidth });
879+
const next = resolveRestingComposerControlsLayout({
880+
...measurement,
881+
hostWidth,
882+
previous: current,
883+
});
880884
return next.hiddenCount === current.hiddenCount && next.visible === current.visible
881885
? current
882886
: next;

‎apps/web/src/components/composerFooterLayout.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,82 @@ describe("context strip labels and resting composer controls", () => {
277277
).toEqual({ hiddenCount: 2, visible: true });
278278
});
279279
});
280+
281+
describe("resolveRestingComposerControlsLayout hysteresis", () => {
282+
// Same cluster as above: picker 140 natural / 96 minimum plus a 9px
283+
// separator, traits 60, mode 140, overflow 24, gap 4.
284+
const base = {
285+
gap: 4,
286+
naturalFixedWidth: 149,
287+
minimumFixedWidth: 105,
288+
blockWidths: [60, 140],
289+
overflowWidth: 24,
290+
};
291+
292+
it("keeps a block in overflow when re-showing it would leave no slack", () => {
293+
// 149 + 60 + 140 + 4 * 2 = 357 fills the host exactly.
294+
expect(
295+
resolveRestingComposerControlsLayout({
296+
...base,
297+
hostWidth: 357,
298+
previous: { hiddenCount: 1, visible: true },
299+
}),
300+
).toEqual({ hiddenCount: 1, visible: true });
301+
});
302+
303+
it("re-shows a block once the host clears the slack margin", () => {
304+
expect(
305+
resolveRestingComposerControlsLayout({
306+
...base,
307+
hostWidth: 358,
308+
previous: { hiddenCount: 1, visible: true },
309+
}),
310+
).toEqual({ hiddenCount: 0, visible: true });
311+
});
312+
313+
it("still resolves from scratch when there is no previous layout", () => {
314+
expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
315+
hiddenCount: 0,
316+
visible: true,
317+
});
318+
});
319+
320+
it("settles when the measured picker width jitters below a pixel", () => {
321+
// Regression guard for React error #185 ("Maximum update depth
322+
// exceeded"). The composer re-measures on every render, so the picker's
323+
// recovered natural width can land a fraction of a pixel apart between
324+
// renders. Without hysteresis that flips hiddenCount forever.
325+
let layout = { hiddenCount: 0, visible: true };
326+
const settled: string[] = [];
327+
for (let index = 0; index < 10; index += 1) {
328+
layout = resolveRestingComposerControlsLayout({
329+
...base,
330+
// The picker's recovered natural width lands a half pixel apart
331+
// between renders.
332+
naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
333+
hostWidth: 357,
334+
previous: layout,
335+
});
336+
if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
337+
}
338+
expect(new Set(settled).size).toBe(1);
339+
});
340+
341+
it("keeps the cluster hidden until its minimum width clears the slack", () => {
342+
// 105 + 24 + 4 = 133 is the exact minimum for the hidden cluster.
343+
expect(
344+
resolveRestingComposerControlsLayout({
345+
...base,
346+
hostWidth: 133,
347+
previous: { hiddenCount: 2, visible: false },
348+
}),
349+
).toEqual({ hiddenCount: 2, visible: false });
350+
expect(
351+
resolveRestingComposerControlsLayout({
352+
...base,
353+
hostWidth: 134,
354+
previous: { hiddenCount: 2, visible: false },
355+
}),
356+
).toEqual({ hiddenCount: 2, visible: true });
357+
});
358+
});

‎apps/web/src/components/composerFooterLayout.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,38 @@ export function resolveRestingComposerControlsNaturalWidth(
108108
* the overflow menu, the picker may contract to its minimum readable width;
109109
* below that the whole cluster hides rather than clipping.
110110
*/
111+
const RESTING_CONTROLS_SLACK_PX = 1;
112+
111113
export function resolveRestingComposerControlsLayout(
112-
input: RestingComposerControlsMeasurement & { hostWidth: number },
114+
input: RestingComposerControlsMeasurement & {
115+
hostWidth: number;
116+
previous?: { hiddenCount: number; visible: boolean };
117+
},
113118
): { hiddenCount: number; visible: boolean } {
114-
const { blockWidths, hostWidth } = input;
119+
const { blockWidths, hostWidth, previous } = input;
115120
let hiddenCount = 0;
116121
while (
117122
hiddenCount < blockWidths.length &&
118123
restingComposerControlsWidth(input, hiddenCount) > hostWidth
119124
) {
120125
hiddenCount += 1;
121126
}
127+
// Growing the overflow menu is unconditional, or the controls would clip.
128+
// Shrinking it has to earn a pixel of slack first: the picker is flexible,
129+
// so its natural width is recovered from a truncated label whose
130+
// scrollWidth is integral while the rendered box is fractional. The
131+
// composer re-measures on every render, so without that margin a host
132+
// sitting exactly on a threshold flips a block in and out until React
133+
// gives up with "Maximum update depth exceeded".
134+
if (previous && hiddenCount < previous.hiddenCount) {
135+
if (restingComposerControlsWidth(input, hiddenCount) > hostWidth - RESTING_CONTROLS_SLACK_PX) {
136+
hiddenCount = Math.min(previous.hiddenCount, blockWidths.length);
137+
}
138+
}
139+
const minimumWidth = restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth);
122140
const visible =
123-
restingComposerControlsWidth(input, hiddenCount, input.minimumFixedWidth) <= hostWidth;
141+
previous && !previous.visible
142+
? minimumWidth <= hostWidth - RESTING_CONTROLS_SLACK_PX
143+
: minimumWidth <= hostWidth;
124144
return { hiddenCount, visible };
125145
}

0 commit comments

Comments
 (0)