TypeScript
fix(web): measure draft project names in the DOM (#22071)
Yash Singhb912bd2da0a3
diff --git a/apps/web/src/components/chat/DraftProjectName.browser.test.tsx b/apps/web/src/components/chat/DraftProjectName.browser.test.tsxnew file mode 100644index 00000000000..fe8a2f5946e--- /dev/null+++ b/apps/web/src/components/chat/DraftProjectName.browser.test.tsx@@ -0,0 +1,103 @@+import "~/index.css";++import { act } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";++import { InlineButton } from "../ui/button";+import { DraftProjectName } from "./DraftProjectName";++let root: Root;+let host: HTMLDivElement;++beforeEach(() => {+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ host = document.body.appendChild(document.createElement("div"));+ host.style.width = "700px";+ host.style.zoom = "1.31";+ root = createRoot(host);+});++afterEach(async () => {+ await act(async () => root.unmount());+ host.remove();+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+});++async function render(value: string, placement: "headline" | "row") {+ await act(async () => {+ root.render(+ placement === "headline" ? (+ <h1 className="w-full text-center text-3xl tracking-tight">+ What should we build in{" "}+ <span className="whitespace-nowrap">+ <InlineButton tone="picker" className="max-w-[calc(100%-1em)] align-baseline">+ <DraftProjectName value={value} />+ </InlineButton>+ ?+ </span>+ </h1>+ ) : (+ <div className="flex w-full items-center gap-2 text-sm font-medium">+ <span className="size-4 shrink-0" />+ <span className="min-w-0 flex-1">+ <DraftProjectName value={value} />+ </span>+ </div>+ ),+ );+ });+}++function label() {+ return host.querySelector<HTMLSpanElement>("span[aria-label]")!;+}++it.each(["headline", "row"] as const)(+ "keeps fitting names in the %s even when canvas metrics disagree with layout",+ async (placement) => {+ host.style.zoom = "1";+ const measureText = CanvasRenderingContext2D.prototype.measureText;+ vi.spyOn(CanvasRenderingContext2D.prototype, "measureText").mockImplementation(function (+ this: CanvasRenderingContext2D,+ text,+ ) {+ const metrics = measureText.call(this, text);+ Object.defineProperty(metrics, "width", { value: metrics.width + 8 });+ return metrics;+ });++ for (const value of ["t3code", "mozart_v2", "No project", "Add project"]) {+ await render(value, placement);+ expect(label().textContent).toBe(value);+ }+ },+);++it.each(["headline", "row"] as const)(+ "fits long names in the %s and restores them when the available space grows",+ async (placement) => {+ const value = "ExampleCompany-CustomerPortal-App";+ await render(value, placement);+ expect(label().textContent).toBe(value);++ await act(async () => {+ host.style.width = "180px";+ await expect.poll(() => label().textContent).toContain("…");+ });+ const shortened = label().textContent!;+ expect(shortened.startsWith("Ex")).toBe(true);+ expect(shortened.endsWith("App")).toBe(true);+ const text = document.createRange();+ text.selectNodeContents(label());+ expect(text.getBoundingClientRect().width).toBeLessThanOrEqual(+ label().getBoundingClientRect().width,+ );++ await act(async () => {+ host.style.width = "700px";+ await expect.poll(() => label().textContent).toBe(value);+ });+ },+);diff --git a/apps/web/src/components/chat/DraftProjectName.tsx b/apps/web/src/components/chat/DraftProjectName.tsxindex 96fc218c668..01b27223bc8 100644--- a/apps/web/src/components/chat/DraftProjectName.tsx+++ b/apps/web/src/components/chat/DraftProjectName.tsx@@ -6,23 +6,22 @@ import { truncateProjectName } from "./DraftProjectName.logic";export function DraftProjectName({ value }: { value: string }) {const containerRef = useRef<HTMLSpanElement>(null);+ const measurementRef = useRef<HTMLSpanElement>(null);const [displayName, setDisplayName] = useState(value);useLayoutEffect(() => {const container = containerRef.current;- const context = document.createElement("canvas").getContext("2d");- if (!container || !context) return;+ const measurement = measurementRef.current;+ if (!container || !measurement) return;const update = () => {- const style = getComputedStyle(container);- context.font = style.font;- context.letterSpacing = style.letterSpacing === "normal" ? "0px" : style.letterSpacing;+ // Canvas text metrics can differ from layout for the same font, weight, and zoom.+ // Measure in the DOM so a name that fits never loses characters to that mismatch.setDisplayName(- truncateProjectName(- value,- container.getBoundingClientRect().width,- (text) => context.measureText(text).width,- ),+ truncateProjectName(value, container.getBoundingClientRect().width, (text) => {+ measurement.textContent = text;+ return measurement.getBoundingClientRect().width;+ }),);};update();@@ -37,7 +36,7 @@ export function DraftProjectName({ value }: { value: string }) {return (<spanref={containerRef}- className="inline-grid min-w-0 max-w-full overflow-hidden align-baseline whitespace-nowrap"+ className="relative inline-grid min-w-0 max-w-full overflow-hidden align-baseline whitespace-nowrap">{/* Keep the full name's intrinsic width so fitting the visible text cannot shrink its budget. */}<span@@ -49,6 +48,11 @@ export function DraftProjectName({ value }: { value: string }) {<span aria-label={value} className="col-start-1 row-start-1 min-w-0 overflow-hidden">{displayName}</span>+ <span+ ref={measurementRef}+ aria-hidden="true"+ className="invisible absolute top-0 left-0 w-max select-none"+ /></span>);}