The best way to control Claude Code, Codex and more from any computer or phone
fix(web): measure draft project names in the DOM (#22071)
Yash Singh committed
Oct 19, 2026 at 21:45 UTC
b912bd2da0a311e7b0934163b399c07e6c53ebb9
2 files changed
+118
-11
apps/web/src/components/chat/DraftProjectName.browser.test.tsx
new
+103
@@ -0,0 +1,103 @@
1
+import "~/index.css";
2
+
3
+import { act } from "react";
4
+import { createRoot, type Root } from "react-dom/client";
5
+import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6
+
7
+import { InlineButton } from "../ui/button";
8
+import { DraftProjectName } from "./DraftProjectName";
9
+
10
+let root: Root;
11
+let host: HTMLDivElement;
12
+
13
+beforeEach(() => {
14
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15
+ host = document.body.appendChild(document.createElement("div"));
16
+ host.style.width = "700px";
17
+ host.style.zoom = "1.31";
18
+ root = createRoot(host);
19
+});
20
+
21
+afterEach(async () => {
22
+ await act(async () => root.unmount());
23
+ host.remove();
24
+ vi.restoreAllMocks();
25
+ vi.unstubAllGlobals();
26
+});
27
+
28
+async function render(value: string, placement: "headline" | "row") {
29
+ await act(async () => {
30
+ root.render(
31
+ placement === "headline" ? (
32
+ <h1 className="w-full text-center text-3xl tracking-tight">
33
+ What should we build in{" "}
34
+ <span className="whitespace-nowrap">
35
+ <InlineButton tone="picker" className="max-w-[calc(100%-1em)] align-baseline">
36
+ <DraftProjectName value={value} />
37
+ </InlineButton>
38
+ ?
39
+ </span>
40
+ </h1>
41
+ ) : (
42
+ <div className="flex w-full items-center gap-2 text-sm font-medium">
43
+ <span className="size-4 shrink-0" />
44
+ <span className="min-w-0 flex-1">
45
+ <DraftProjectName value={value} />
46
+ </span>
47
+ </div>
48
+ ),
49
+ );
50
+ });
51
+}
52
+
53
+function label() {
54
+ return host.querySelector<HTMLSpanElement>("span[aria-label]")!;
55
+}
56
+
57
+it.each(["headline", "row"] as const)(
58
+ "keeps fitting names in the %s even when canvas metrics disagree with layout",
59
+ async (placement) => {
60
+ host.style.zoom = "1";
61
+ const measureText = CanvasRenderingContext2D.prototype.measureText;
62
+ vi.spyOn(CanvasRenderingContext2D.prototype, "measureText").mockImplementation(function (
63
+ this: CanvasRenderingContext2D,
64
+ text,
65
+ ) {
66
+ const metrics = measureText.call(this, text);
67
+ Object.defineProperty(metrics, "width", { value: metrics.width + 8 });
68
+ return metrics;
69
+ });
70
+
71
+ for (const value of ["t3code", "mozart_v2", "No project", "Add project"]) {
72
+ await render(value, placement);
73
+ expect(label().textContent).toBe(value);
74
+ }
75
+ },
76
+);
77
+
78
+it.each(["headline", "row"] as const)(
79
+ "fits long names in the %s and restores them when the available space grows",
80
+ async (placement) => {
81
+ const value = "ExampleCompany-CustomerPortal-App";
82
+ await render(value, placement);
83
+ expect(label().textContent).toBe(value);
84
+
85
+ await act(async () => {
86
+ host.style.width = "180px";
87
+ await expect.poll(() => label().textContent).toContain("…");
88
+ });
89
+ const shortened = label().textContent!;
90
+ expect(shortened.startsWith("Ex")).toBe(true);
91
+ expect(shortened.endsWith("App")).toBe(true);
92
+ const text = document.createRange();
93
+ text.selectNodeContents(label());
94
+ expect(text.getBoundingClientRect().width).toBeLessThanOrEqual(
95
+ label().getBoundingClientRect().width,
96
+ );
97
+
98
+ await act(async () => {
99
+ host.style.width = "700px";
100
+ await expect.poll(() => label().textContent).toBe(value);
101
+ });
102
+ },
103
+);
apps/web/src/components/chat/DraftProjectName.tsx
+15
-11
@@ -6,23 +6,22 @@ import { truncateProjectName } from "./DraftProjectName.logic";
6
7
export function DraftProjectName({ value }: { value: string }) {
8
const containerRef = useRef<HTMLSpanElement>(null);
9
+ const measurementRef = useRef<HTMLSpanElement>(null);
10
const [displayName, setDisplayName] = useState(value);
11
12
useLayoutEffect(() => {
13
const container = containerRef.current;
13
- const context = document.createElement("canvas").getContext("2d");
14
- if (!container || !context) return;
14
+ const measurement = measurementRef.current;
15
+ if (!container || !measurement) return;
16
17
const update = () => {
17
- const style = getComputedStyle(container);
18
- context.font = style.font;
19
- context.letterSpacing = style.letterSpacing === "normal" ? "0px" : style.letterSpacing;
18
+ // Canvas text metrics can differ from layout for the same font, weight, and zoom.
19
+ // Measure in the DOM so a name that fits never loses characters to that mismatch.
20
setDisplayName(
21
- truncateProjectName(
22
- value,
23
- container.getBoundingClientRect().width,
24
- (text) => context.measureText(text).width,
25
- ),
21
+ truncateProjectName(value, container.getBoundingClientRect().width, (text) => {
22
+ measurement.textContent = text;
23
+ return measurement.getBoundingClientRect().width;
24
+ }),
25
);
26
};
27
update();
@@ -37,7 +36,7 @@ export function DraftProjectName({ value }: { value: string }) {
36
return (
37
<span
38
ref={containerRef}
40
- className="inline-grid min-w-0 max-w-full overflow-hidden align-baseline whitespace-nowrap"
39
+ className="relative inline-grid min-w-0 max-w-full overflow-hidden align-baseline whitespace-nowrap"
40
>
41
{/* Keep the full name's intrinsic width so fitting the visible text cannot shrink its budget. */}
42
<span
@@ -49,6 +48,11 @@ export function DraftProjectName({ value }: { value: string }) {
48
<span aria-label={value} className="col-start-1 row-start-1 min-w-0 overflow-hidden">
49
{displayName}
50
</span>
51
+ <span
52
+ ref={measurementRef}
53
+ aria-hidden="true"
54
+ className="invisible absolute top-0 left-0 w-max select-none"
55
+ />
56
</span>
57
);
58
}