Skip to content

Commit c699562

Browse files
guryanov-amaria-rcksjuliusmarminge
authored
fix(web): branch labels keep the space before a middle-truncated name (pingdotgg#23627)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 14978e3 commit c699562

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,29 @@ describe("MiddleTruncate", () => {
2323
expect(label()).not.toHaveAttribute("data-overflow");
2424
expect(label()).toHaveStyle({ clipPath: "none" });
2525
});
26+
27+
it.each([
28+
["Fix the sidebar", 7],
29+
["report\u2003\u2003draft.txt", 9],
30+
])("renders %j split as wide as whole", async (value, tail) => {
31+
await using screen = await render(
32+
<div>
33+
<MiddleTruncate value={value} tail={tail} data-testid="split" />
34+
<span data-testid="whole" className="whitespace-nowrap">
35+
{value}
36+
</span>
37+
</div>,
38+
);
39+
const width = (id: string) => screen.getByTestId(id).element().getBoundingClientRect().width;
40+
41+
expect(width("split")).toBeCloseTo(width("whole"), 0);
42+
});
43+
44+
it("keeps the raw value's tail when collapsing its spaces", async () => {
45+
await using screen = await render(<MiddleTruncate value={"longprefix a b.txt"} />);
46+
47+
expect(screen.container.querySelector("[title] > span:last-child")?.textContent).toBe(
48+
"x a b.txt",
49+
);
50+
});
2651
});

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

Lines changed: 16 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+
const raw = splitForMiddleTruncate(value, tail);
20+
const split = raw && collapseSegments(raw);
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
@@ -42,10 +43,12 @@ export function MiddleTruncate({
4243
{split ? (
4344
<>
4445
{/* The tail carries the ellipsis so it sits flush; a CSS ellipsis leaves a ragged gap. */}
45-
<span ref={headRef} className="peer min-w-0 overflow-hidden">
46+
<span ref={headRef} className="peer min-w-0 overflow-hidden whitespace-pre">
4647
{split.head}
4748
</span>
48-
<span className="shrink-0 peer-data-overflow:before:content-['…']">{split.tail}</span>
49+
<span className="shrink-0 whitespace-pre peer-data-overflow:before:content-['…']">
50+
{split.tail}
51+
</span>
4952
</>
5053
) : (
5154
// Keyed so React does not reuse the head node and its imperative clip.
@@ -89,6 +92,16 @@ function fitHead(head: HTMLElement) {
8992
if (tail instanceof HTMLElement) tail.style.translate = slack > 0 ? `-${slack}px 0` : "";
9093
}
9194

95+
// The segments render pre so the space at the cut survives, so collapse here what CSS
96+
// would have (not \s: em and no-break spaces are kept). Split first, so the tail length
97+
// still counts the raw value.
98+
function collapseSegments(split: { head: string; tail: string }) {
99+
const collapse = (text: string) => text.replace(/[ \t\n\r\f]+/g, " ");
100+
const head = collapse(split.head).replace(/^ /, "");
101+
const tail = collapse(split.tail).replace(/ $/, "");
102+
return { head, tail: head.endsWith(" ") ? tail.replace(/^ /, "") : tail };
103+
}
104+
92105
const DEFAULT_TAIL = 10;
93106
const MAX_SEGMENT_TAIL = 16;
94107
const MIN_WORD_TAIL = 4;

0 commit comments

Comments
 (0)