Skip to content

Commit ec78c2c

Browse files
matt2eclaude
andcommitted
perf(panels): measure the resize handle only when its width can change
ResizeHandle ran `useLayoutEffect(measure)` with no dependency list, so every commit of the handle called getBoundingClientRect() on its panel and forced a layout of the tree React had just committed. Both mounted handles re-render with every channel-list replacement, which made this a per-message cost (measured 37 forced layouts / 127 ms per 40 incoming messages). The only consumer of the measured width is aria-valuenow. Observe the panel with a ResizeObserver instead, replacing both the per-commit layout effect and the window resize listener. The observer fires after layout, so the read is free, and it also covers container-driven CSS clamps (`max-width: calc(100% - 336px)`) that a window listener never saw. aria-valuenow still tracks the rendered width after drag, keyboard step, reset and window or container resize. Add ResizeHandle.test.tsx: five parent commits produce no rect reads, the observer notification updates aria-valuenow, and the width prop alone does not move the value. Scope usePanelSplit's observer stub to the container now that the handle observes its own panel. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 03b73ec commit ec78c2c

3 files changed

Lines changed: 117 additions & 11 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// @vitest-environment jsdom
2+
import "@testing-library/jest-dom/vitest";
3+
import { StrictMode, useState } from "react";
4+
import {
5+
act,
6+
cleanup,
7+
fireEvent,
8+
render,
9+
screen,
10+
} from "@testing-library/react";
11+
import { afterEach, expect, test, vi } from "vitest";
12+
import { ResizeHandle } from "./ResizeHandle";
13+
14+
afterEach(() => {
15+
cleanup();
16+
vi.restoreAllMocks();
17+
vi.unstubAllGlobals();
18+
});
19+
20+
// jsdom has no layout: a stub observer stands in for the browser and a
21+
// controlled getBoundingClientRect supplies the panel's rendered width. The
22+
// forced-layout count in a real browser is the perf harness's measurement.
23+
test.each([1, -1] as const)(
24+
"direction %s measures the panel from its observer, not on every commit",
25+
(direction) => {
26+
let panelWidth = 260;
27+
const observed: Element[] = [];
28+
let notify = () => {};
29+
const disconnect = vi.fn();
30+
vi.stubGlobal(
31+
"ResizeObserver",
32+
class {
33+
constructor(callback: () => void) {
34+
notify = callback;
35+
}
36+
observe(element: Element) {
37+
observed.push(element);
38+
}
39+
disconnect = disconnect;
40+
},
41+
);
42+
const rect = vi
43+
.spyOn(HTMLElement.prototype, "getBoundingClientRect")
44+
.mockImplementation(() => ({ width: panelWidth }) as DOMRect);
45+
let commit = () => {};
46+
function Workspace() {
47+
const [, bump] = useState(0);
48+
const [width, setWidth] = useState(260);
49+
commit = () => bump((count) => count + 1);
50+
const handle = (
51+
<ResizeHandle
52+
width={width}
53+
setWidth={setWidth}
54+
min={220}
55+
max={520}
56+
reset={() => setWidth(260)}
57+
label="Resize"
58+
direction={direction}
59+
/>
60+
);
61+
return direction === 1 ? (
62+
<>
63+
<div data-testid="panel" />
64+
{handle}
65+
</>
66+
) : (
67+
<section data-testid="panel">{handle}</section>
68+
);
69+
}
70+
const view = render(
71+
<StrictMode>
72+
<Workspace />
73+
</StrictMode>,
74+
);
75+
const handle = screen.getByRole("separator");
76+
expect(new Set(observed)).toEqual(new Set([screen.getByTestId("panel")]));
77+
expect(handle).toHaveAttribute("aria-valuenow", "260");
78+
for (let i = 0; i < 5; i++) act(commit);
79+
expect(rect).not.toHaveBeenCalled();
80+
81+
panelWidth = 300;
82+
act(notify);
83+
expect(rect).toHaveBeenCalledTimes(1);
84+
expect(handle).toHaveAttribute("aria-valuenow", "300");
85+
86+
// The width prop alone does not move the value; the rendered width does.
87+
fireEvent.keyDown(handle, { key: "End" });
88+
expect(handle).toHaveAttribute("aria-valuenow", "300");
89+
panelWidth = 520;
90+
act(notify);
91+
expect(handle).toHaveAttribute("aria-valuenow", "520");
92+
93+
view.unmount();
94+
expect(disconnect).toHaveBeenCalledTimes(2);
95+
},
96+
);

‎src/features/panels/ResizeHandle.tsx‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,24 @@ export function ResizeHandle({
5454
document.documentElement.style.removeProperty("cursor");
5555
document.body.style.removeProperty("user-select");
5656
}, [move]);
57-
const measure = useCallback(() => {
57+
// Only aria-valuenow reads the rendered width, and it changes only when the
58+
// panel is laid out differently (drag, keyboard step, reset, CSS clamp,
59+
// container or window resize). Observe the panel rather than measuring on
60+
// every commit: the handle re-renders with each channel-list replacement and
61+
// a getBoundingClientRect there forces layout of the whole committed tree.
62+
useLayoutEffect(() => {
5863
const panel =
5964
direction === 1
6065
? handle.current?.previousElementSibling
6166
: handle.current?.parentElement;
6267
if (!(panel instanceof HTMLElement)) return;
63-
const next = Math.round(panel.getBoundingClientRect().width);
64-
setRenderedWidth((current) => (current === next ? current : next));
68+
const observer = new ResizeObserver(() => {
69+
const next = Math.round(panel.getBoundingClientRect().width);
70+
setRenderedWidth((current) => (current === next ? current : next));
71+
});
72+
observer.observe(panel);
73+
return () => observer.disconnect();
6574
}, [direction]);
66-
useLayoutEffect(measure);
67-
useEffect(() => {
68-
window.addEventListener("resize", measure);
69-
return () => window.removeEventListener("resize", measure);
70-
}, [measure]);
7175
useEffect(() => () => finish(), [finish]);
7276

7377
return (

‎src/features/panels/usePanelSplit.test.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ test.each([undefined, { defaultWidth: 420, maxRatio: 0.5 }])(
3131
vi.stubGlobal(
3232
"ResizeObserver",
3333
class {
34+
callback: () => void;
3435
constructor(callback: () => void) {
35-
measure = callback;
36+
this.callback = callback;
3637
}
37-
observe() {}
38-
disconnect = disconnect;
38+
// The handle observes its own panel; track only the container.
39+
observe(element: HTMLElement) {
40+
if (element.dataset.testid !== "workspace") return;
41+
measure = this.callback;
42+
this.disconnect = disconnect;
43+
}
44+
disconnect() {}
3945
},
4046
);
4147
function Workspace() {

0 commit comments

Comments
 (0)