Open History to see the last commit
1import { type PointerEvent, useCallback, useEffect, useRef } from "react";
2
3interface ResizeSession {
4 width: number;
5 edge: "left" | "right";
6 resize: (width: number) => number;
7 finish: (width: number, moved: boolean) => void;
8 cleanup?: () => void;
9}
10
11/** Shared pointer lifecycle for side panels, including interrupted and sub-frame drags. */
12export function useResizeDrag<T extends HTMLElement>(
13 start: (event: PointerEvent<T>) => ResizeSession | null,
14) {
15 const drag = useRef<{
16 session: ResizeSession;
17 target: T;
18 pointerId: number;
19 startX: number;
20 pendingX: number;
21 width: number;
22 moved: boolean;
23 frame: number | null;
24 } | null>(null);
25
26 const flush = useCallback(() => {
27 const active = drag.current;
28 if (!active) return;
29 const delta = (active.pendingX - active.startX) * (active.session.edge === "left" ? -1 : 1);
30 active.moved ||= Math.abs(delta) > 2;
31 active.width = active.session.resize(active.session.width + delta);
32 }, []);
33
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 // Capture may already have been released by the browser.
48 }
49 document.body.style.removeProperty("cursor");
50 document.body.style.removeProperty("user-select");
51 active.session.cleanup?.();
52 if (commit) active.session.finish(active.width, active.moved);
53 },
54 [flush],
55 );
56
57 useEffect(() => {
58 const onBlur = () => finish();
59 window.addEventListener("blur", onBlur);
60 return () => {
61 window.removeEventListener("blur", onBlur);
62 finish(false);
63 };
64 }, [finish]);
65
66 const end = (event: PointerEvent<T>, usePosition: boolean) => {
67 const active = drag.current;
68 if (!active || active.pointerId !== event.pointerId) return;
69 event.preventDefault();
70 if (usePosition) active.pendingX = event.clientX;
71 finish();
72 };
73
74 return {
75 onPointerDown(event: PointerEvent<T>) {
76 if (event.button !== 0 || drag.current) return;
77 const session = start(event);
78 if (!session) return;
79 try {
80 event.currentTarget.setPointerCapture(event.pointerId);
81 } catch {
82 session.cleanup?.();
83 return;
84 }
85 event.preventDefault();
86 event.stopPropagation();
87 drag.current = {
88 session,
89 target: event.currentTarget,
90 pointerId: event.pointerId,
91 startX: event.clientX,
92 pendingX: event.clientX,
93 width: session.width,
94 moved: false,
95 frame: null,
96 };
97 document.body.style.cursor = "col-resize";
98 document.body.style.userSelect = "none";
99 },
100 onPointerMove(event: PointerEvent<T>) {
101 const active = drag.current;
102 if (!active || active.pointerId !== event.pointerId) return;
103 event.preventDefault();
104 active.pendingX = event.clientX;
105 active.moved ||= Math.abs(event.clientX - active.startX) > 2;
106 if (active.frame !== null) return;
107 active.frame = requestAnimationFrame(() => {
108 active.frame = null;
109 flush();
110 });
111 },
112 onPointerUp: (event: PointerEvent<T>) => end(event, true),
113 onPointerCancel: (event: PointerEvent<T>) => end(event, false),
114 onLostPointerCapture: (event: PointerEvent<T>) => end(event, false),
115 };
116}