Skip to content

Commit 5e5323d

Browse files
fix(web): collapse label whitespace before the middle split
Normalising before the split keeps runs of spaces and line breaks from rendering literally now that the segments use whitespace-pre, and adds a browser test for the space at the cut. AI-assisted; verified with the tests and tools listed above.
1 parent 0c28e06 commit 5e5323d

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎apps/web/src/components/ui/middle-truncate.browser.test.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ describe("MiddleTruncate", () => {
2323
expect(label()).not.toHaveAttribute("data-overflow");
2424
expect(label()).toHaveStyle({ clipPath: "none" });
2525
});
26+
27+
it("keeps the space where the head meets the tail", async () => {
28+
await using screen = await render(
29+
<div>
30+
<MiddleTruncate value="Fix the sidebar" tail={7} data-testid="split" />
31+
<span data-testid="whole" className="whitespace-nowrap">
32+
Fix the sidebar
33+
</span>
34+
</div>,
35+
);
36+
const width = (id: string) => screen.getByTestId(id).element().getBoundingClientRect().width;
37+
38+
expect(width("split")).toBeCloseTo(width("whole"), 0);
39+
});
2640
});

‎apps/web/src/components/ui/middle-truncate.tsx‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function MiddleTruncate({
1616
showTitle?: boolean;
1717
variant?: "default" | "cap-aligned";
1818
}) {
19-
const split = splitForMiddleTruncate(value, tail);
19+
// Collapse whitespace up front: the segments render pre so the space at the cut survives.
20+
const split = splitForMiddleTruncate(value.replace(/\s+/g, " ").trim(), tail);
2021
const headRef = useRef<HTMLSpanElement>(null);
2122
const head = split?.head;
2223
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fits the head span's DOM text and refits it through a ResizeObserver
@@ -43,10 +44,10 @@ export function MiddleTruncate({
4344
<>
4445
{/* The tail carries the ellipsis so it sits flush; a CSS ellipsis leaves a ragged gap. */}
4546
<span ref={headRef} className="peer min-w-0 overflow-hidden whitespace-pre">
46-
{split.head.replace(/[\r\n]/g, " ")}
47+
{split.head}
4748
</span>
4849
<span className="shrink-0 whitespace-pre peer-data-overflow:before:content-['…']">
49-
{split.tail.replace(/[\r\n]/g, " ")}
50+
{split.tail}
5051
</span>
5152
</>
5253
) : (

0 commit comments

Comments
 (0)