Skip to content

Commit 8af877c

Browse files
perf(web): resize handles compile with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6a326af commit 8af877c

2 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,5 @@ export const REACT_COMPILED_FILES = [
9191
"apps/web/src/components/settings/ThemeImportDialog.tsx",
9292
"apps/web/src/components/settings/SnapShotSettings.tsx",
9393
"apps/web/src/components/settings/ConnectionsSettings.tsx",
94+
"apps/web/src/hooks/useResizeDrag.ts",
9495
] as const;

‎apps/web/src/hooks/useResizeDrag.ts‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type PointerEvent, useCallback, useEffect, useLayoutEffect, useRef } from "react";
1+
import { type PointerEvent, useEffect, useEffectEvent, useLayoutEffect, useRef } from "react";
22

33
interface ResizeSession {
44
width: number;
@@ -23,51 +23,51 @@ export function useResizeDrag<T extends HTMLElement>(
2323
frame: number | null;
2424
} | null>(null);
2525

26-
const flush = useCallback(() => {
26+
const flush = () => {
2727
const active = drag.current;
2828
if (!active) return;
2929
const delta = (active.pendingX - active.startX) * (active.session.edge === "left" ? -1 : 1);
30-
active.moved ||= Math.abs(delta) > 2;
30+
if (Math.abs(delta) > 2) active.moved = true;
3131
active.width = active.session.resize(active.session.width + delta);
32-
}, []);
32+
};
3333

34-
const finish = useCallback(
35-
(commit = true) => {
36-
const active = drag.current;
37-
if (!active) return;
38-
if (active.frame !== null) cancelAnimationFrame(active.frame);
39-
if (commit) flush();
40-
// Release can synchronously dispatch lostpointercapture.
41-
drag.current = null;
42-
try {
43-
if (active.target.hasPointerCapture(active.pointerId)) {
44-
active.target.releasePointerCapture(active.pointerId);
45-
}
46-
} catch {}
47-
document.body.style.removeProperty("cursor");
48-
document.body.style.removeProperty("user-select");
49-
active.session.cleanup?.(commit);
50-
if (commit) active.session.finish(active.width, active.moved);
51-
},
52-
[flush],
53-
);
34+
const finish = (commit = true) => {
35+
const active = drag.current;
36+
if (!active) return;
37+
if (active.frame !== null) cancelAnimationFrame(active.frame);
38+
if (commit) flush();
39+
// Release can synchronously dispatch lostpointercapture.
40+
drag.current = null;
41+
try {
42+
if (active.target.hasPointerCapture(active.pointerId)) {
43+
active.target.releasePointerCapture(active.pointerId);
44+
}
45+
} catch {}
46+
document.body.style.removeProperty("cursor");
47+
document.body.style.removeProperty("user-select");
48+
active.session.cleanup?.(commit);
49+
if (commit) active.session.finish(active.width, active.moved);
50+
};
51+
52+
// The effects must not rerun while a drag is live: their cleanup cancels it.
53+
const finishDrag = useEffectEvent((commit: boolean) => finish(commit));
5454

5555
const previousResetKey = useRef(resetKey);
5656
useLayoutEffect(() => {
5757
if (previousResetKey.current !== resetKey) {
58-
finish(false);
58+
finishDrag(false);
5959
previousResetKey.current = resetKey;
6060
}
61-
}, [finish, resetKey]);
61+
}, [resetKey]);
6262

6363
useEffect(() => {
64-
const onBlur = () => finish();
64+
const onBlur = () => finishDrag(true);
6565
window.addEventListener("blur", onBlur);
6666
return () => {
6767
window.removeEventListener("blur", onBlur);
68-
finish(false);
68+
finishDrag(false);
6969
};
70-
}, [finish]);
70+
}, []);
7171

7272
const end = (event: PointerEvent<T>, usePosition: boolean) => {
7373
const active = drag.current;
@@ -109,7 +109,7 @@ export function useResizeDrag<T extends HTMLElement>(
109109
if (!active || active.pointerId !== event.pointerId) return;
110110
event.preventDefault();
111111
active.pendingX = event.clientX;
112-
active.moved ||= Math.abs(event.clientX - active.startX) > 2;
112+
if (Math.abs(event.clientX - active.startX) > 2) active.moved = true;
113113
if (active.frame !== null) return;
114114
active.frame = requestAnimationFrame(() => {
115115
active.frame = null;

0 commit comments

Comments
 (0)