Skip to content

Commit 8309a40

Browse files
fix(web): measure collapsed model labels at their visible width
Do not reserve hidden text in the resting composer when its responsive label is deliberately collapsed. Keep recovering natural width for flexible labels that are merely truncated. Implemented with Codex through T3 Code.
1 parent 4268800 commit 8309a40

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import {
4+
resolveRestingComposerControlsLayout,
5+
resolveRestingComposerControlsNaturalWidth,
6+
} from "../composerFooterLayout";
7+
import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
8+
9+
function measurePicker(input: { clientWidth: number; flexGrow: string; maxWidth?: string }) {
10+
const label = { clientWidth: input.clientWidth, scrollWidth: 160 };
11+
const picker = {
12+
getBoundingClientRect: () => ({ width: 52 }),
13+
querySelector: () => label,
14+
};
15+
const controls = {
16+
querySelector: (selector: string) => {
17+
if (selector === "[data-chat-provider-model-picker]") return picker;
18+
if (selector === "[data-resting-controls-overflow]") {
19+
return { getBoundingClientRect: () => ({ width: 24 }) };
20+
}
21+
return null;
22+
},
23+
querySelectorAll: () => [{ getBoundingClientRect: () => ({ width: 140 }) }],
24+
};
25+
vi.stubGlobal("getComputedStyle", (element: unknown) => {
26+
if (element === label) return { flexGrow: input.flexGrow };
27+
if (element === picker) return { minWidth: "52px", maxWidth: input.maxWidth ?? "none" };
28+
return { columnGap: "4px" };
29+
});
30+
return measureRestingComposerControls(controls as unknown as HTMLElement)!;
31+
}
32+
33+
afterEach(() => vi.unstubAllGlobals());
34+
35+
describe("measureRestingComposerControls", () => {
36+
it("keeps controls inline when the model label is deliberately collapsed", () => {
37+
const measurement = measurePicker({ clientWidth: 0, flexGrow: "0" });
38+
39+
expect(measurement.naturalFixedWidth).toBe(52);
40+
expect(resolveRestingComposerControlsNaturalWidth(measurement)).toBe(196);
41+
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
42+
hiddenCount: 0,
43+
visible: true,
44+
});
45+
});
46+
47+
it("recovers truncated text while the model label is flexible", () => {
48+
const measurement = measurePicker({ clientWidth: 20, flexGrow: "1" });
49+
50+
expect(measurement.naturalFixedWidth).toBe(192);
51+
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
52+
hiddenCount: 1,
53+
visible: true,
54+
});
55+
});
56+
57+
it("recovers flexible text squeezed to zero instead of mistaking it for collapsed text", () => {
58+
const measurement = measurePicker({ clientWidth: 0, flexGrow: "1" });
59+
60+
expect(measurement.naturalFixedWidth).toBe(212);
61+
expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
62+
hiddenCount: 1,
63+
visible: true,
64+
});
65+
});
66+
67+
it("still caps recovered text at the model picker's maximum width", () => {
68+
expect(
69+
measurePicker({ clientWidth: 0, flexGrow: "1", maxWidth: "180px" }).naturalFixedWidth,
70+
).toBe(180);
71+
});
72+
});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ function providerModelPickerNaturalWidth(picker: HTMLElement): number {
2424
if (renderedWidth === 0) return 0;
2525
const style = getComputedStyle(picker);
2626
const label = picker.querySelector<HTMLElement>('[data-chat-provider-model-picker-label="true"]');
27-
const hiddenLabelWidth = label ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
27+
// Narrow composers deliberately collapse the label with w-0 and flex-none.
28+
// A flexible label squeezed to zero still needs its natural width recovered.
29+
const labelIsCollapsed = label?.clientWidth === 0 && getComputedStyle(label).flexGrow === "0";
30+
const hiddenLabelWidth =
31+
label && !labelIsCollapsed ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
2832
const maxWidth = Number.parseFloat(style.maxWidth);
2933
const naturalWidth = Math.min(
3034
renderedWidth + hiddenLabelWidth,

0 commit comments

Comments
 (0)