Skip to content

Commit de59cca

Browse files
fix(web): collapse only CSS whitespace in middle-truncated labels
\s and trim() also match em and no-break spaces, which CSS preserves, so branch names and filenames containing them rendered differently from main. Collapse and trim only space, tab, and line-break characters. AI-assisted; verified with the tests and tools listed above.
1 parent 5e5323d commit de59cca

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ describe("MiddleTruncate", () => {
2424
expect(label()).toHaveStyle({ clipPath: "none" });
2525
});
2626

27-
it("keeps the space where the head meets the tail", async () => {
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) => {
2831
await using screen = await render(
2932
<div>
30-
<MiddleTruncate value="Fix the sidebar" tail={7} data-testid="split" />
33+
<MiddleTruncate value={value} tail={tail} data-testid="split" />
3134
<span data-testid="whole" className="whitespace-nowrap">
32-
Fix the sidebar
35+
{value}
3336
</span>
3437
</div>,
3538
);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export function MiddleTruncate({
1616
showTitle?: boolean;
1717
variant?: "default" | "cap-aligned";
1818
}) {
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);
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);
2123
const headRef = useRef<HTMLSpanElement>(null);
2224
const head = split?.head;
2325
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fits the head span's DOM text and refits it through a ResizeObserver

0 commit comments

Comments
 (0)