@devries-cameron20 / t3code-1 / commits / 47f2e91210
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): batch middle truncate fits (#37001)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Rohan Godha committed Nov 14, 2026 at 21:36 UTC 47f2e91210fe80b0b0f0339d36d3a2f313de5134
2 files changed +64 -28
apps/web/src/components/ui/middle-truncate.browser.test.tsx
+19 -1
@@ -1,4 +1,4 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { describe, expect, it, vi } from "vite-plus/test";
2
3 import { render } from "../../../test/render";
4 import { MiddleTruncate } from "./middle-truncate";
@@ -24,6 +24,24 @@ describe("MiddleTruncate", () => {
24 expect(label()).toHaveStyle({ clipPath: "none" });
25 });
26
27 + it("fits every head mounted in the same commit", async () => {
28 + const values = ["t3/investigate-release-build-times", "t3/remove-stale-worktree-branches"];
29 + await using screen = await render(
30 + <>
31 + {values.map((value) => (
32 + <div key={value}>{narrow(value)}</div>
33 + ))}
34 + </>,
35 + );
36 + const heads = () => [
37 + ...screen.container.querySelectorAll<HTMLElement>("[title] > span:first-child"),
38 + ];
39 +
40 + await vi.waitFor(() =>
41 + expect(heads().filter((head) => head.hasAttribute("data-overflow"))).toHaveLength(2),
42 + );
43 + });
44 +
45 it.each([
46 ["Fix the sidebar", 7],
47 ["report\u2003\u2003draft.txt", 9],
apps/web/src/components/ui/middle-truncate.tsx
+45 -27
@@ -25,8 +25,8 @@ export function MiddleTruncate({
25 const element = headRef.current;
26 // A new head can keep the same box size, so refit here, not only on resize.
27 if (!element || head === undefined) return;
28 - fitHead(element);
29 - return observeResize(element, () => fitHead(element));
28 + scheduleFit(element);
29 + return observeResize(element, () => scheduleFit(element));
30 }, [head]);
31 return (
32 <span
@@ -60,36 +60,54 @@ export function MiddleTruncate({
60 );
61 }
62
63 +// Fitting heads one by one forces a layout per row, since each read follows the previous
64 +// head's write. Heads queued in one commit or resize delivery fit together in a microtask,
65 +// still before paint: all reads, then all writes.
66 +const pendingFits = new Set<HTMLElement>();
67 +
68 +function scheduleFit(head: HTMLElement) {
69 + if (pendingFits.size === 0) queueMicrotask(fitPendingHeads);
70 + pendingFits.add(head);
71 +}
72 +
73 // An overflowing head is cut after its last whole character: clip the partial glyph
74 // and slide the tail left over the slack. Neither changes layout, so the observed
75 // size stays the true available width.
76 +function fitPendingHeads() {
77 + const heads = [...pendingFits].filter((head) => head.isConnected);
78 + pendingFits.clear();
79 + const overflowing = heads.map(
80 + (head) => head.scrollWidth > head.clientWidth && head.firstChild instanceof Text,
81 + );
82 + heads.forEach((head, index) => head.toggleAttribute("data-overflow", overflowing[index]));
83 + const slacks = heads.map((head, index) => (overflowing[index] ? measureSlack(head) : 0));
84 + heads.forEach((head, index) => {
85 + const slack = slacks[index]!;
86 + head.style.clipPath = slack > 0 ? `inset(-0.25em ${slack}px -0.25em 0)` : "";
87 + const tail = head.nextElementSibling;
88 + if (tail instanceof HTMLElement) tail.style.translate = slack > 0 ? `-${slack}px 0` : "";
89 + });
90 +}
91
67 -function fitHead(head: HTMLElement) {
68 - const text = head.firstChild;
69 - const overflowing = head.scrollWidth > head.clientWidth && text instanceof Text;
70 - head.toggleAttribute("data-overflow", overflowing);
71 - let slack = 0;
72 - if (overflowing) {
73 - const range = document.createRange();
74 - const left = head.getBoundingClientRect().left;
75 - const widthTo = (end: number) => {
76 - range.setStart(text, 0);
77 - range.setEnd(text, end);
78 - return range.getBoundingClientRect().right - left;
79 - };
80 - let fits = 0;
81 - let over = text.length;
82 - while (over - fits > 1) {
83 - const mid = (fits + over) >> 1;
84 - if (widthTo(mid) <= head.clientWidth) fits = mid;
85 - else over = mid;
86 - }
87 - if (/[\uD800-\uDBFF]/.test(text.data[fits - 1] ?? "")) fits -= 1;
88 - slack = Math.max(0, head.clientWidth - widthTo(fits));
92 +function measureSlack(head: HTMLElement) {
93 + const text = head.firstChild as Text;
94 + const range = document.createRange();
95 + const left = head.getBoundingClientRect().left;
96 + const width = head.clientWidth;
97 + const widthTo = (end: number) => {
98 + range.setStart(text, 0);
99 + range.setEnd(text, end);
100 + return range.getBoundingClientRect().right - left;
101 + };
102 + let fits = 0;
103 + let over = text.length;
104 + while (over - fits > 1) {
105 + const mid = (fits + over) >> 1;
106 + if (widthTo(mid) <= width) fits = mid;
107 + else over = mid;
108 }
90 - head.style.clipPath = slack > 0 ? `inset(-0.25em ${slack}px -0.25em 0)` : "";
91 - const tail = head.nextElementSibling;
92 - if (tail instanceof HTMLElement) tail.style.translate = slack > 0 ? `-${slack}px 0` : "";
109 + if (/[\uD800-\uDBFF]/.test(text.data[fits - 1] ?? "")) fits -= 1;
110 + return Math.max(0, width - widthTo(fits));
111 }
112
113 // The segments render pre so the space at the cut survives, so collapse here what CSS