@blackbelt-technology/pi-dashboard-client-utils 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/useZoomPan.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { useCallback, useRef, useState } from "react";
1
+ import type React from "react";
2
+ import { useCallback, useLayoutEffect, useRef, useState } from "react";
2
3
 
3
4
  export interface ZoomPanState {
4
5
  scale: number;
@@ -10,6 +11,13 @@ export interface UseZoomPanOptions {
10
11
  minScale?: number;
11
12
  maxScale?: number;
12
13
  zoomStep?: number;
14
+ /**
15
+ * Scale the surface starts at and that reset / double-click return to.
16
+ * Clamped into [minScale, maxScale]; defaults to 1. While the user has not
17
+ * interacted, a changed value re-seeds the view (fit may resolve async).
18
+ * See change: fix-markdown-remount-storm (D4).
19
+ */
20
+ initialScale?: number;
13
21
  }
14
22
 
15
23
  const DEFAULT_MIN = 0.5;
@@ -26,7 +34,23 @@ export function useZoomPan(options?: UseZoomPanOptions) {
26
34
  const maxScale = options?.maxScale ?? DEFAULT_MAX;
27
35
  const zoomStep = options?.zoomStep ?? DEFAULT_STEP;
28
36
 
29
- const [state, setState] = useState<ZoomPanState>({ scale: 1, translateX: 0, translateY: 0 });
37
+ const initialScale = Math.min(maxScale, Math.max(minScale, options?.initialScale ?? 1));
38
+
39
+ const [state, rawSetState] = useState<ZoomPanState>({ scale: initialScale, translateX: 0, translateY: 0 });
40
+ // True once the user zoomed/panned; a late initialScale change must not override that.
41
+ const touched = useRef(false);
42
+ const setState = useCallback((u: React.SetStateAction<ZoomPanState>) => {
43
+ touched.current = true;
44
+ rawSetState(u);
45
+ }, []);
46
+ const resetToInitial = useCallback(() => {
47
+ touched.current = false;
48
+ rawSetState({ scale: initialScale, translateX: 0, translateY: 0 });
49
+ }, [initialScale]);
50
+
51
+ useLayoutEffect(() => {
52
+ if (!touched.current) rawSetState({ scale: initialScale, translateX: 0, translateY: 0 });
53
+ }, [initialScale]);
30
54
 
31
55
  // Refs for drag tracking (avoid re-renders during drag)
32
56
  const dragging = useRef(false);
@@ -59,7 +83,7 @@ export function useZoomPan(options?: UseZoomPanOptions) {
59
83
  };
60
84
  });
61
85
  },
62
- [clampScale],
86
+ [clampScale, setState],
63
87
  );
64
88
 
65
89
  // ── Wheel handler ──────────────────────────────────────────────────
@@ -108,7 +132,7 @@ export function useZoomPan(options?: UseZoomPanOptions) {
108
132
  translateX: prev.translateX + dx,
109
133
  translateY: prev.translateY + dy,
110
134
  }));
111
- }, []);
135
+ }, [setState]);
112
136
 
113
137
  const onPointerUp = useCallback(() => {
114
138
  dragging.current = false;
@@ -142,9 +166,7 @@ export function useZoomPan(options?: UseZoomPanOptions) {
142
166
  }, []);
143
167
 
144
168
  // ── Double-click reset ─────────────────────────────────────────────
145
- const onDoubleClick = useCallback(() => {
146
- setState({ scale: 1, translateX: 0, translateY: 0 });
147
- }, []);
169
+ const onDoubleClick = resetToInitial;
148
170
 
149
171
  // ── Button controls ────────────────────────────────────────────────
150
172
  const zoomIn = useCallback(() => {
@@ -152,18 +174,16 @@ export function useZoomPan(options?: UseZoomPanOptions) {
152
174
  const newScale = clampScale(prev.scale * zoomStep);
153
175
  return { ...prev, scale: newScale };
154
176
  });
155
- }, [clampScale, zoomStep]);
177
+ }, [clampScale, zoomStep, setState]);
156
178
 
157
179
  const zoomOut = useCallback(() => {
158
180
  setState((prev) => {
159
181
  const newScale = clampScale(prev.scale / zoomStep);
160
182
  return { ...prev, scale: newScale };
161
183
  });
162
- }, [clampScale, zoomStep]);
184
+ }, [clampScale, zoomStep, setState]);
163
185
 
164
- const reset = useCallback(() => {
165
- setState({ scale: 1, translateX: 0, translateY: 0 });
166
- }, []);
186
+ const reset = resetToInitial;
167
187
 
168
188
  const handlers = {
169
189
  onWheel,
@@ -175,5 +195,5 @@ export function useZoomPan(options?: UseZoomPanOptions) {
175
195
  onDoubleClick,
176
196
  };
177
197
 
178
- return { state, handlers, zoomIn, zoomOut, reset };
198
+ return { state, handlers, zoomIn, zoomOut, reset, initialScale };
179
199
  }