Skip to content

Commit 5727773

Browse files
fix(web): split middle-truncated labels before collapsing spaces
Collapsing whitespace before the split shifted the fixed tail, so a name like 'longprefix a b.txt' kept one character less of its ending than on main and could clip in narrow trees. Split the raw value as before and collapse whitespace in the rendered segments, dropping a duplicate space when the cut lands inside a run. AI-assisted; verified with the tests and tools listed above.
1 parent de59cca commit 5727773

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ describe("MiddleTruncate", () => {
4040

4141
expect(width("split")).toBeCloseTo(width("whole"), 0);
4242
});
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+
});
4351
});

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ export function MiddleTruncate({
1616
showTitle?: boolean;
1717
variant?: "default" | "cap-aligned";
1818
}) {
19-
// The segments render pre so the space at the cut survives; collapse only what CSS
20-
// would (not \s, which also eats em and no-break spaces that CSS keeps).
21-
const collapsed = value.replace(/[ \t\n\r\f]+/g, " ").replace(/^ | $/g, "");
22-
const split = splitForMiddleTruncate(collapsed, tail);
19+
const raw = splitForMiddleTruncate(value, tail);
20+
const split = raw && collapseSegments(raw);
2321
const headRef = useRef<HTMLSpanElement>(null);
2422
const head = split?.head;
2523
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fits the head span's DOM text and refits it through a ResizeObserver
@@ -94,6 +92,16 @@ function fitHead(head: HTMLElement) {
9492
if (tail instanceof HTMLElement) tail.style.translate = slack > 0 ? `-${slack}px 0` : "";
9593
}
9694

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+
97105
const DEFAULT_TAIL = 10;
98106
const MAX_SEGMENT_TAIL = 16;
99107
const MIN_WORD_TAIL = 4;

0 commit comments

Comments
 (0)