Skip to content

Commit 02b899b

Browse files
authored
fix(web): keep draft project names readable (pingdotgg#22177)
1 parent 1efe762 commit 02b899b

6 files changed

Lines changed: 224 additions & 189 deletions

File tree

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
4242
import { InlineButton } from "../ui/button";
4343
import { DraftProjectName } from "./DraftProjectName";
44+
import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading";
4445
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
4546

4647
const ADD_PROJECT_VALUE = "add-project";
@@ -254,13 +255,13 @@ export function DraftHeroHeadline({
254255
<ComboboxTrigger
255256
render={<InlineButton tone="picker" />}
256257
data-draft-project-trigger=""
257-
// Leaves room for the trailing "?" so a truncated name keeps it on its line.
258-
className="pointer-events-auto max-w-[calc(100%-1em)] align-baseline"
258+
className="pointer-events-auto min-w-0 max-w-full align-baseline"
259259
/>
260260
}
261261
>
262262
<DraftProjectName
263263
value={isScratchDraft ? "No project" : (activeProjectDisplayName ?? "Choose a project")}
264+
singleLine
264265
/>
265266
</TooltipTrigger>
266267
{activeProjectDisplayName && !isScratchDraft ? (
@@ -308,9 +309,9 @@ export function DraftHeroHeadline({
308309
<button
309310
type="button"
310311
onClick={openAddProject}
311-
className="pointer-events-auto inline cursor-pointer border-muted-foreground/35 border-b border-dotted text-muted-foreground/60 transition-colors hover:border-muted-foreground/60 hover:text-muted-foreground/80 focus-visible:rounded-sm focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
312+
className="pointer-events-auto inline min-w-0 max-w-full cursor-pointer border-muted-foreground/35 border-b border-dotted text-muted-foreground/60 transition-colors hover:border-muted-foreground/60 hover:text-muted-foreground/80 focus-visible:rounded-sm focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
312313
>
313-
{activeProjectTitle ?? "Add a project"}
314+
<DraftProjectName value={activeProjectTitle ?? "Add a project"} singleLine />
314315
</button>
315316
);
316317

@@ -341,21 +342,21 @@ export function DraftHeroHeadline({
341342

342343
return (
343344
<div className="mx-auto flex w-full max-w-5xl flex-col items-center">
344-
{/* An explicit heading label hides the inline project picker in NVDA browse mode. */}
345-
<h1 className="w-full text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl">
346-
{isScratchDraft ? (
347-
<>What should we work on?</>
348-
) : hasResolvedProject ? (
349-
<>
350-
What should we build in <span className="whitespace-nowrap">{projectSelector}?</span>
351-
</>
352-
) : canChooseProject ? (
353-
<>{projectSelector} to start</>
354-
) : (
355-
<>Add a project to start</>
356-
)}
357-
</h1>
358-
{}
345+
{hasResolvedProject && !isScratchDraft ? (
346+
<DraftHeroProjectHeading projectName={activeProjectDisplayName ?? "Choose a project"}>
347+
{projectSelector}
348+
</DraftHeroProjectHeading>
349+
) : (
350+
<h1 className="w-full text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl">
351+
{isScratchDraft ? (
352+
<>What should we work on?</>
353+
) : canChooseProject ? (
354+
<>{projectSelector} to start</>
355+
) : (
356+
<>Add a project to start</>
357+
)}
358+
</h1>
359+
)}
359360
{scratchWorkspaceRoot === null ? null : (
360361
<p className="mt-2 flex h-6 w-full items-center justify-center text-sm">
361362
{isScratchDraft ? projectSelector : orStartWithoutProject}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useLayoutEffect, useRef, useState, type ReactNode } from "react";
2+
3+
import { observeResize } from "~/lib/observeResize";
4+
5+
export function DraftHeroProjectHeading({
6+
children,
7+
projectName,
8+
}: {
9+
children: ReactNode;
10+
projectName: string;
11+
}) {
12+
const containerRef = useRef<HTMLDivElement>(null);
13+
const inlineRef = useRef<HTMLSpanElement>(null);
14+
const [fitsInline, setFitsInline] = useState(false);
15+
16+
useLayoutEffect(() => {
17+
const container = containerRef.current;
18+
const inline = inlineRef.current;
19+
if (!container || !inline) return;
20+
21+
// Measure the full inline heading independently of its current layout, so
22+
// moving the name below it cannot make the fit check oscillate.
23+
const update = () =>
24+
setFitsInline(
25+
inline.getBoundingClientRect().width <= container.getBoundingClientRect().width,
26+
);
27+
update();
28+
return observeResize([container, inline], update);
29+
}, []);
30+
31+
return (
32+
<div ref={containerRef} className="group/draft-hero relative w-full" data-inline={fitsInline}>
33+
<span
34+
aria-hidden="true"
35+
className="pointer-events-none invisible absolute inset-x-0 top-0 h-0 overflow-hidden"
36+
>
37+
<span
38+
ref={inlineRef}
39+
className="flex w-max items-baseline gap-2 whitespace-nowrap font-normal text-3xl tracking-tight"
40+
>
41+
<span>What should we build in</span>
42+
<span>
43+
<span className="font-medium">{projectName}</span>?
44+
</span>
45+
</span>
46+
</span>
47+
<h1 className="flex w-full flex-col items-center justify-center gap-2 text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl lg:group-data-[inline=true]/draft-hero:flex-row">
48+
<span className="max-w-full lg:group-data-[inline=true]/draft-hero:shrink-0">
49+
What should we build
50+
<span className="lg:group-data-[inline=true]/draft-hero:hidden">?</span>
51+
<span className="hidden lg:group-data-[inline=true]/draft-hero:inline"> in</span>
52+
</span>
53+
<span className="flex min-w-0 max-w-full items-baseline justify-center text-base tracking-normal lg:group-data-[inline=true]/draft-hero:text-3xl lg:group-data-[inline=true]/draft-hero:tracking-tight">
54+
<span className="min-w-0">{children}</span>
55+
<span className="hidden shrink-0 lg:group-data-[inline=true]/draft-hero:inline">?</span>
56+
</span>
57+
</h1>
58+
</div>
59+
);
60+
}

‎apps/web/src/components/chat/DraftProjectName.browser.test.tsx‎

Lines changed: 134 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import "~/index.css";
33
import { act } from "react";
44
import { createRoot, type Root } from "react-dom/client";
55
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6+
import { page } from "vite-plus/test/browser";
67

78
import { InlineButton } from "../ui/button";
9+
import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading";
810
import { DraftProjectName } from "./DraftProjectName";
911

1012
let root: Root;
@@ -14,30 +16,29 @@ beforeEach(() => {
1416
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1517
host = document.body.appendChild(document.createElement("div"));
1618
host.style.width = "700px";
17-
host.style.zoom = "1.31";
19+
host.style.maxWidth = "100%";
1820
root = createRoot(host);
1921
});
2022

2123
afterEach(async () => {
2224
await act(async () => root.unmount());
2325
host.remove();
24-
vi.restoreAllMocks();
2526
vi.unstubAllGlobals();
2627
});
2728

28-
async function render(value: string, placement: "headline" | "row") {
29+
async function render(value: string, placement: "headline" | "row" = "headline") {
2930
await act(async () => {
3031
root.render(
3132
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>
33+
<div data-testid="composer" className="relative mt-64 h-32 w-full">
34+
<div className="absolute inset-x-0 bottom-full pb-4">
35+
<DraftHeroProjectHeading projectName={value}>
36+
<InlineButton tone="picker" className="min-w-0 max-w-full align-baseline">
37+
<DraftProjectName value={value} singleLine />
38+
</InlineButton>
39+
</DraftHeroProjectHeading>
40+
</div>
41+
</div>
4142
) : (
4243
<div className="flex w-full items-center gap-2 text-sm font-medium">
4344
<span className="size-4 shrink-0" />
@@ -50,54 +51,135 @@ async function render(value: string, placement: "headline" | "row") {
5051
});
5152
}
5253

53-
function label() {
54-
return host.querySelector<HTMLSpanElement>("span[aria-label]")!;
54+
const label = () => host.querySelector<HTMLSpanElement>("span[aria-label]")!;
55+
const title = () => host.querySelector<HTMLSpanElement>("h1 > span")!;
56+
57+
function expectWithinHost(element: Element) {
58+
const bounds = element.getBoundingClientRect();
59+
const hostBounds = host.getBoundingClientRect();
60+
expect(bounds.left).toBeGreaterThanOrEqual(hostBounds.left);
61+
expect(bounds.right).toBeLessThanOrEqual(hostBounds.right);
62+
}
63+
64+
function expectAboveComposer() {
65+
const heading = host.querySelector("h1")!;
66+
const composer = host.querySelector('[data-testid="composer"]')!;
67+
expectWithinHost(heading);
68+
expectWithinHost(label());
69+
expect(heading.getBoundingClientRect().bottom).toBeLessThan(composer.getBoundingClientRect().top);
5570
}
5671

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);
72+
it.each([320, 390, 822])(
73+
"places the full short project name below the title in smaller text at %ipx",
74+
async (width) => {
75+
await page.viewport(width, 800);
76+
host.style.width = `${width - 32}px`;
77+
for (const value of ["t3code", "obsidian-arr", "No project"]) {
78+
await render(value);
79+
expect(title().innerText).toBe("What should we build?");
7380
expect(label().textContent).toBe(value);
81+
expect(label().scrollWidth).toBeLessThanOrEqual(label().clientWidth);
82+
expect(label().scrollHeight).toBeLessThanOrEqual(label().clientHeight);
83+
expect(label().getBoundingClientRect().top).toBeGreaterThanOrEqual(
84+
title().getBoundingClientRect().bottom,
85+
);
86+
expect(Number.parseFloat(getComputedStyle(label()).fontSize)).toBeLessThan(
87+
Number.parseFloat(getComputedStyle(title()).fontSize),
88+
);
89+
expectAboveComposer();
7490
}
7591
},
7692
);
7793

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);
94+
it("uses the inline heading only when the viewport and chat lane both have room", async () => {
95+
await page.viewport(1440, 900);
96+
await render("obsidian-arr");
97+
expect(title().innerText).toBe("What should we build in");
98+
expect(label().getBoundingClientRect().top).toBeLessThan(title().getBoundingClientRect().bottom);
99+
expectAboveComposer();
84100

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-
);
101+
host.style.width = "350px";
102+
await expect.poll(() => title().innerText).toBe("What should we build?");
103+
expect(label().getBoundingClientRect().top).toBeGreaterThanOrEqual(
104+
title().getBoundingClientRect().bottom,
105+
);
106+
expectAboveComposer();
107+
});
97108

98-
await act(async () => {
99-
host.style.width = "700px";
100-
await expect.poll(() => label().textContent).toBe(value);
101-
});
109+
it.each([390, 1440])(
110+
"uses a single smaller row and truncates only its remaining overflow at %ipx",
111+
async (width) => {
112+
await page.viewport(width, 900);
113+
host.style.width = `${Math.min(width - 32, 700)}px`;
114+
const value = "ExampleCompany-CustomerPortal-Production-".repeat(5);
115+
await render(value);
116+
expect(label().textContent).toBe(value);
117+
expect(label().getAttribute("aria-label")).toBe(value);
118+
expect(title().innerText).toBe("What should we build?");
119+
expect(label().getBoundingClientRect().top).toBeGreaterThanOrEqual(
120+
title().getBoundingClientRect().bottom,
121+
);
122+
expect(Number.parseFloat(getComputedStyle(label()).fontSize)).toBeLessThan(
123+
Number.parseFloat(getComputedStyle(title()).fontSize),
124+
);
125+
expect(label().scrollWidth).toBeGreaterThan(label().clientWidth);
126+
expect(label().clientHeight).toBe(Number.parseFloat(getComputedStyle(label()).lineHeight));
127+
expect(getComputedStyle(label()).textOverflow).toBe("ellipsis");
128+
expect(document.documentElement.scrollWidth).toBeLessThanOrEqual(window.innerWidth);
129+
expectAboveComposer();
102130
},
103131
);
132+
133+
it("keeps short picker rows intact and bounds long rows at fractional zoom", async () => {
134+
await page.viewport(1280, 800);
135+
host.style.width = "240px";
136+
host.style.zoom = "1.31";
137+
for (const value of ["t3code", "obsidian-arr", "Add project", "LongProjectName-".repeat(10)]) {
138+
await render(value, "row");
139+
expect(label().textContent).toBe(value);
140+
expectWithinHost(label());
141+
expect(label().clientHeight).toBeLessThanOrEqual(
142+
Number.parseFloat(getComputedStyle(label()).lineHeight) * 2,
143+
);
144+
}
145+
});
146+
147+
it("moves a name below the heading when it stops fitting and returns it inline when space recovers", async () => {
148+
await page.viewport(1440, 900);
149+
host.style.width = "900px";
150+
const value = "CustomerPortal-Production";
151+
await render(value);
152+
expect(title().innerText).toBe("What should we build in");
153+
154+
await act(async () => {
155+
host.style.width = "650px";
156+
await expect.poll(() => title().innerText).toBe("What should we build?");
157+
});
158+
expect(label().textContent).toBe(value);
159+
expect(label().scrollWidth).toBeLessThanOrEqual(label().clientWidth);
160+
expect(label().scrollHeight).toBeLessThanOrEqual(label().clientHeight);
161+
expectAboveComposer();
162+
163+
await act(async () => {
164+
host.style.width = "900px";
165+
await expect.poll(() => title().innerText).toBe("What should we build in");
166+
});
167+
expect(label().textContent).toBe(value);
168+
expectAboveComposer();
169+
});
170+
171+
it("rechecks inline fit when the selected project changes", async () => {
172+
await page.viewport(1440, 900);
173+
await render("ExampleCompany-CustomerPortal-Production-Environment");
174+
expect(title().innerText).toBe("What should we build?");
175+
176+
await render("obsidian-arr");
177+
await expect.poll(() => title().innerText).toBe("What should we build in");
178+
expect(label().textContent).toBe("obsidian-arr");
179+
180+
await render("ExampleCompany-CustomerPortal-Production-Environment");
181+
await expect.poll(() => title().innerText).toBe("What should we build?");
182+
expect(label().scrollWidth).toBeLessThanOrEqual(label().clientWidth);
183+
expect(label().scrollHeight).toBeLessThanOrEqual(label().clientHeight);
184+
expectAboveComposer();
185+
});

0 commit comments

Comments
 (0)