|
2 | 2 |
|
3 | 3 | import type { PreviewViewportSetting, PreviewViewportSize } from "@t3tools/contracts"; |
4 | 4 | import { |
5 | | - useCallback, |
6 | 5 | useEffect, |
7 | 6 | useRef, |
8 | 7 | useState, |
9 | 8 | type KeyboardEvent as ReactKeyboardEvent, |
10 | 9 | type PointerEvent as ReactPointerEvent, |
11 | 10 | } from "react"; |
12 | 11 |
|
| 12 | +import { useStableCallback } from "../hooks/useStableCallback"; |
13 | 13 | import { commitBrowserViewportChange } from "./browserViewportActions"; |
14 | 14 | import { |
15 | 15 | browserViewportSettingKey, |
@@ -43,8 +43,8 @@ export function useBrowserViewportResize(options: { |
43 | 43 | const keyboardViewportRef = useRef<ViewportDrag | null>(null); |
44 | 44 | const [dragViewport, setDragViewport] = useState<ViewportDrag | null>(null); |
45 | 45 | const sourceViewportKey = browserViewportSettingKey(viewport); |
46 | | - const sourceViewportKeyRef = useRef(sourceViewportKey); |
47 | | - sourceViewportKeyRef.current = sourceViewportKey; |
| 46 | + // Drag listeners and the keyboard commit timer outlive the render that started them. |
| 47 | + const isCurrentSource = useStableCallback((key: string) => key === sourceViewportKey); |
48 | 48 | const activeDrag = dragViewport?.sourceKey === sourceViewportKey ? dragViewport : null; |
49 | 49 | const effectiveViewport = activeDrag |
50 | 50 | ? ({ |
@@ -85,20 +85,17 @@ export function useBrowserViewportResize(options: { |
85 | 85 | keyboardViewportRef.current = null; |
86 | 86 | }, [sourceViewportKey]); |
87 | 87 |
|
88 | | - const commitViewportChange = useCallback( |
89 | | - (next: PreviewViewportSetting) => { |
90 | | - dragVersionRef.current += 1; |
91 | | - dragCleanupRef.current?.(); |
92 | | - if (keyboardCommitTimerRef.current !== null) { |
93 | | - clearTimeout(keyboardCommitTimerRef.current); |
94 | | - keyboardCommitTimerRef.current = null; |
95 | | - } |
96 | | - keyboardViewportRef.current = null; |
97 | | - setDragViewport(null); |
98 | | - return commitBrowserViewportChange(tabId, next); |
99 | | - }, |
100 | | - [tabId], |
101 | | - ); |
| 88 | + const commitViewportChange = (next: PreviewViewportSetting) => { |
| 89 | + dragVersionRef.current += 1; |
| 90 | + dragCleanupRef.current?.(); |
| 91 | + if (keyboardCommitTimerRef.current !== null) { |
| 92 | + clearTimeout(keyboardCommitTimerRef.current); |
| 93 | + keyboardCommitTimerRef.current = null; |
| 94 | + } |
| 95 | + keyboardViewportRef.current = null; |
| 96 | + setDragViewport(null); |
| 97 | + return commitBrowserViewportChange(tabId, next); |
| 98 | + }; |
102 | 99 |
|
103 | 100 | const clearDrag = () => setDragViewport(null); |
104 | 101 | const commitDrag = (next: PreviewViewportSetting) => { |
@@ -149,7 +146,7 @@ export function useBrowserViewportResize(options: { |
149 | 146 | keyboardCommitTimerRef.current = setTimeout(() => { |
150 | 147 | keyboardCommitTimerRef.current = null; |
151 | 148 | const latest = keyboardViewportRef.current; |
152 | | - if (!latest || latest.sourceKey !== sourceViewportKeyRef.current) return; |
| 149 | + if (!latest || !isCurrentSource(latest.sourceKey)) return; |
153 | 150 | keyboardViewportRef.current = null; |
154 | 151 | commitDrag({ _tag: "freeform", width: latest.width, height: latest.height }); |
155 | 152 | }, KEYBOARD_RESIZE_COMMIT_DELAY_MS); |
@@ -187,7 +184,7 @@ export function useBrowserViewportResize(options: { |
187 | 184 | target.setPointerCapture(pointerId); |
188 | 185 | } catch {} |
189 | 186 |
|
190 | | - const sourceChanged = () => sourceViewportKeyRef.current !== sourceViewportKey; |
| 187 | + const sourceChanged = () => !isCurrentSource(sourceViewportKey); |
191 | 188 | const move = (moveEvent: PointerEvent) => { |
192 | 189 | if (moveEvent.pointerId !== pointerId) return; |
193 | 190 | if (sourceChanged()) { |
|
0 commit comments