Skip to content
TypeScript

fix(web): measure collapsed model labels at their visible width (#9540)

Julius Marminge3c3e05ccfe34
diff --git a/apps/web/src/components/chat/restingComposerControlsMeasurement.test.ts b/apps/web/src/components/chat/restingComposerControlsMeasurement.test.ts
new file mode 100644
index 00000000000..052c9b1d9f4
--- /dev/null
+++ b/apps/web/src/components/chat/restingComposerControlsMeasurement.test.ts
@@ -0,0 +1,72 @@
+import { afterEach, describe, expect, it, vi } from "vite-plus/test";
+
+import {
+ resolveRestingComposerControlsLayout,
+ resolveRestingComposerControlsNaturalWidth,
+} from "../composerFooterLayout";
+import { measureRestingComposerControls } from "./restingComposerControlsMeasurement";
+
+function measurePicker(input: { clientWidth: number; flexGrow: string; maxWidth?: string }) {
+ const label = { clientWidth: input.clientWidth, scrollWidth: 160 };
+ const picker = {
+ getBoundingClientRect: () => ({ width: 52 }),
+ querySelector: () => label,
+ };
+ const controls = {
+ querySelector: (selector: string) => {
+ if (selector === "[data-chat-provider-model-picker]") return picker;
+ if (selector === "[data-resting-controls-overflow]") {
+ return { getBoundingClientRect: () => ({ width: 24 }) };
+ }
+ return null;
+ },
+ querySelectorAll: () => [{ getBoundingClientRect: () => ({ width: 140 }) }],
+ };
+ vi.stubGlobal("getComputedStyle", (element: unknown) => {
+ if (element === label) return { flexGrow: input.flexGrow };
+ if (element === picker) return { minWidth: "52px", maxWidth: input.maxWidth ?? "none" };
+ return { columnGap: "4px" };
+ });
+ return measureRestingComposerControls(controls as unknown as HTMLElement)!;
+}
+
+afterEach(() => vi.unstubAllGlobals());
+
+describe("measureRestingComposerControls", () => {
+ it("keeps controls inline when the model label is deliberately collapsed", () => {
+ const measurement = measurePicker({ clientWidth: 0, flexGrow: "0" });
+
+ expect(measurement.naturalFixedWidth).toBe(52);
+ expect(resolveRestingComposerControlsNaturalWidth(measurement)).toBe(196);
+ expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
+ hiddenCount: 0,
+ visible: true,
+ });
+ });
+
+ it("recovers truncated text while the model label is flexible", () => {
+ const measurement = measurePicker({ clientWidth: 20, flexGrow: "1" });
+
+ expect(measurement.naturalFixedWidth).toBe(192);
+ expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
+ hiddenCount: 1,
+ visible: true,
+ });
+ });
+
+ it("recovers flexible text squeezed to zero instead of mistaking it for collapsed text", () => {
+ const measurement = measurePicker({ clientWidth: 0, flexGrow: "1" });
+
+ expect(measurement.naturalFixedWidth).toBe(212);
+ expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth: 200 })).toEqual({
+ hiddenCount: 1,
+ visible: true,
+ });
+ });
+
+ it("still caps recovered text at the model picker's maximum width", () => {
+ expect(
+ measurePicker({ clientWidth: 0, flexGrow: "1", maxWidth: "180px" }).naturalFixedWidth,
+ ).toBe(180);
+ });
+});
diff --git a/apps/web/src/components/chat/restingComposerControlsMeasurement.ts b/apps/web/src/components/chat/restingComposerControlsMeasurement.ts
index 3bd4242d457..fc39951723f 100644
--- a/apps/web/src/components/chat/restingComposerControlsMeasurement.ts
+++ b/apps/web/src/components/chat/restingComposerControlsMeasurement.ts
@@ -24,7 +24,11 @@ function providerModelPickerNaturalWidth(picker: HTMLElement): number {
if (renderedWidth === 0) return 0;
const style = getComputedStyle(picker);
const label = picker.querySelector<HTMLElement>('[data-chat-provider-model-picker-label="true"]');
- const hiddenLabelWidth = label ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
+ // Narrow composers deliberately collapse the label with w-0 and flex-none.
+ // A flexible label squeezed to zero still needs its natural width recovered.
+ const labelIsCollapsed = label?.clientWidth === 0 && getComputedStyle(label).flexGrow === "0";
+ const hiddenLabelWidth =
+ label && !labelIsCollapsed ? Math.max(0, label.scrollWidth - label.clientWidth) : 0;
const maxWidth = Number.parseFloat(style.maxWidth);
const naturalWidth = Math.min(
renderedWidth + hiddenLabelWidth,