react-zoom-pan-pinch 4.0.7 → 4.1.1

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.
@@ -1,4 +1,9 @@
1
- import { ReactZoomPanPinchContext, ReactZoomPanPinchState } from "../../models";
1
+ import {
2
+ PositionType,
3
+ ReactZoomPanPinchContext,
4
+ ReactZoomPanPinchState,
5
+ StateType,
6
+ } from "../../models";
2
7
  import { animations } from "../animations/animations.constants";
3
8
  import { handleZoomToPoint } from "../zoom/zoom.logic";
4
9
  import { animate } from "../animations/animations.utils";
@@ -15,6 +20,13 @@ import {
15
20
  getMouseBoundedPosition,
16
21
  } from "../bounds/bounds.utils";
17
22
 
23
+ export type RectLike = {
24
+ x: number;
25
+ y: number;
26
+ width: number;
27
+ height: number;
28
+ };
29
+
18
30
  export const handleCalculateButtonZoom = (
19
31
  contextInstance: ReactZoomPanPinchContext,
20
32
  delta: number,
@@ -44,18 +56,56 @@ export const handleCalculateButtonZoom = (
44
56
  return newScale;
45
57
  };
46
58
 
59
+ /**
60
+ * Runs a programmatic zoom: fires `onZoomStart`/`onZoom`, animates to
61
+ * `targetState` and fires `onZoomStop` once the animation time has elapsed.
62
+ * Resolves when the animation has finished (or was interrupted).
63
+ */
64
+ export function runZoomAnimation(
65
+ contextInstance: ReactZoomPanPinchContext,
66
+ targetState: StateType,
67
+ animationTime: number,
68
+ animationType: keyof typeof animations,
69
+ ): Promise<void> {
70
+ const { wrapperComponent } = contextInstance;
71
+ const { onZoomStart, onZoom, onZoomStop } = contextInstance.props;
72
+ const event = new MouseEvent("mousemove", { bubbles: true });
73
+ const ctx = getContext(contextInstance);
74
+ handleCallback(ctx, event, onZoomStart);
75
+ handleCallback(ctx, event, onZoom);
76
+ const done = animate(
77
+ contextInstance,
78
+ targetState,
79
+ animationTime,
80
+ animationType,
81
+ );
82
+ const win =
83
+ wrapperComponent?.ownerDocument?.defaultView ??
84
+ (typeof window !== "undefined" ? window : null);
85
+ if (win) {
86
+ win.setTimeout(() => {
87
+ if (!contextInstance.mounted) return;
88
+ handleCallback(getContext(contextInstance), event, onZoomStop);
89
+ }, animationTime);
90
+ }
91
+ return done;
92
+ }
93
+
47
94
  export function handleZoomToViewCenter(
48
95
  contextInstance: ReactZoomPanPinchContext,
49
96
  delta: number,
50
97
  step: number,
51
98
  animationTime: number,
52
99
  animationType: keyof typeof animations,
53
- ): void {
100
+ ): Promise<void> {
54
101
  const { wrapperComponent } = contextInstance;
55
102
  const { scale, positionX, positionY } = contextInstance.state;
56
103
  const { zoomAnimation } = contextInstance.setup;
57
104
 
58
- if (!wrapperComponent) return console.error("No WrapperComponent found");
105
+ if (!wrapperComponent) {
106
+ console.error("No WrapperComponent found");
107
+ return Promise.resolve();
108
+ }
59
109
 
60
110
  const effectiveAnimationTime = zoomAnimation.disabled ? 0 : animationTime;
61
111
 
@@ -74,26 +124,18 @@ export function handleZoomToViewCenter(
74
124
  );
75
125
 
76
126
  if (!targetState) {
77
- return console.error(
127
+ console.error(
78
128
  "Error during zoom event. New transformation state was not calculated.",
79
129
  );
130
+ return Promise.resolve();
80
131
  }
81
132
 
82
- const { onZoomStart, onZoom, onZoomStop } = contextInstance.props;
83
- const event = new MouseEvent("mousemove", { bubbles: true });
84
- const ctx = getContext(contextInstance);
85
- handleCallback(ctx, event, onZoomStart);
86
- handleCallback(ctx, event, onZoom);
87
- animate(contextInstance, targetState, effectiveAnimationTime, animationType);
88
- const win =
89
- wrapperComponent.ownerDocument?.defaultView ??
90
- (typeof window !== "undefined" ? window : null);
91
- if (win) {
92
- win.setTimeout(() => {
93
- if (!contextInstance.mounted) return;
94
- handleCallback(getContext(contextInstance), event, onZoomStop);
95
- }, effectiveAnimationTime);
96
- }
133
+ return runZoomAnimation(
134
+ contextInstance,
135
+ targetState,
136
+ effectiveAnimationTime,
137
+ animationType,
138
+ );
97
139
  }
98
140
 
99
141
  export function resetTransformations(
@@ -101,13 +143,13 @@ export function resetTransformations(
101
143
  animationTime: number,
102
144
  animationType: keyof typeof animations,
103
145
  onResetTransformation?: () => void,
104
- ): void {
146
+ ): Promise<void> {
105
147
  const { setup, wrapperComponent, contentComponent } = contextInstance;
106
148
  const { limitToBounds, centerOnInit } = setup;
107
149
  const initialTransformation = createState(contextInstance.props);
108
150
  const { scale, positionX, positionY } = contextInstance.state;
109
151
 
110
- if (!wrapperComponent) return;
152
+ if (!wrapperComponent) return Promise.resolve();
111
153
 
112
154
  let targetPositionX = initialTransformation.positionX;
113
155
  let targetPositionY = initialTransformation.positionY;
@@ -148,35 +190,51 @@ export function resetTransformations(
148
190
  positionX === initialTransformation.positionX &&
149
191
  positionY === initialTransformation.positionY
150
192
  ) {
151
- return;
193
+ return Promise.resolve();
152
194
  }
153
195
 
154
196
  onResetTransformation?.();
155
197
 
156
- const { onZoomStart, onZoom, onZoomStop } = contextInstance.props;
157
- const event = new MouseEvent("mousemove", { bubbles: true });
158
- const ctx = getContext(contextInstance);
159
- handleCallback(ctx, event, onZoomStart);
160
- handleCallback(ctx, event, onZoom);
161
- animate(contextInstance, newState, animationTime, animationType);
162
- const win =
163
- wrapperComponent.ownerDocument?.defaultView ??
164
- (typeof window !== "undefined" ? window : null);
165
- if (win) {
166
- win.setTimeout(() => {
167
- if (!contextInstance.mounted) return;
168
- handleCallback(getContext(contextInstance), event, onZoomStop);
169
- }, animationTime);
198
+ return runZoomAnimation(
199
+ contextInstance,
200
+ newState,
201
+ animationTime,
202
+ animationType,
203
+ );
204
+ }
205
+
206
+ const toRect = (target: HTMLElement | RectLike): RectLike => {
207
+ if ("getBoundingClientRect" in target) {
208
+ const rect = target.getBoundingClientRect();
209
+ return {
210
+ x: rect.left,
211
+ y: rect.top,
212
+ width: rect.width,
213
+ height: rect.height,
214
+ };
170
215
  }
216
+ return target;
217
+ };
218
+
219
+ /**
220
+ * Smallest client rect containing every node (#388).
221
+ */
222
+ export function getUnionRect(nodes: HTMLElement[]): RectLike {
223
+ const rects = nodes.map(toRect);
224
+ const left = Math.min(...rects.map((r) => r.x));
225
+ const top = Math.min(...rects.map((r) => r.y));
226
+ const right = Math.max(...rects.map((r) => r.x + r.width));
227
+ const bottom = Math.max(...rects.map((r) => r.y + r.height));
228
+ return { x: left, y: top, width: right - left, height: bottom - top };
171
229
  }
172
230
 
173
231
  export function getOffset(
174
- element: HTMLElement,
232
+ element: HTMLElement | RectLike,
175
233
  wrapper: HTMLElement,
176
234
  content: HTMLElement,
177
235
  state: ReactZoomPanPinchState,
178
236
  ) {
179
- const offset = element.getBoundingClientRect();
237
+ const offset = toRect(element);
180
238
  const wrapperOffset = wrapper.getBoundingClientRect();
181
239
  const contentOffset = content.getBoundingClientRect();
182
240
 
@@ -189,36 +247,46 @@ export function getOffset(
189
247
  };
190
248
  }
191
249
 
192
- export function calculateZoomToNode(
250
+ /**
251
+ * Transform that frames the client-space `rect` in the viewport.
252
+ *
253
+ * `customZoom` forces the scale; otherwise the rect is fitted and the fit
254
+ * scale is capped by `minFitScale`/`maxFitScale` (#515) before the wrapper's
255
+ * own `minScale`/`maxScale` apply.
256
+ */
257
+ export function calculateZoomToRect(
193
258
  contextInstance: ReactZoomPanPinchContext,
194
- node: HTMLElement,
259
+ rect: RectLike,
195
260
  customZoom?: number,
196
261
  customOffsetX = 0,
197
262
  customOffsetY = 0,
263
+ minFitScale?: number,
264
+ maxFitScale?: number,
198
265
  ): { positionX: number; positionY: number; scale: number } {
199
266
  const { wrapperComponent, contentComponent, state } = contextInstance;
200
267
  const { limitToBounds, minScale, maxScale } = contextInstance.setup;
201
268
  if (!wrapperComponent || !contentComponent) return state;
202
269
 
203
270
  const wrapperRect = wrapperComponent.getBoundingClientRect();
204
- const nodeRect = node.getBoundingClientRect();
205
- const nodeOffset = getOffset(node, wrapperComponent, contentComponent, state);
271
+ const nodeOffset = getOffset(rect, wrapperComponent, contentComponent, state);
206
272
 
207
273
  const nodeLeft = nodeOffset.x;
208
274
  const nodeTop = nodeOffset.y;
209
- const nodeWidth = nodeRect.width / state.scale;
210
- const nodeHeight = nodeRect.height / state.scale;
275
+ const nodeWidth = rect.width / state.scale;
276
+ const nodeHeight = rect.height / state.scale;
211
277
 
212
278
  const scaleX = wrapperComponent.offsetWidth / nodeWidth;
213
279
  const scaleY = wrapperComponent.offsetHeight / nodeHeight;
214
280
 
215
- const newScale = checkZoomBounds(
216
- customZoom || Math.min(scaleX, scaleY),
217
- minScale,
218
- maxScale,
219
- 0,
220
- false,
221
- );
281
+ let targetScale = customZoom || Math.min(scaleX, scaleY);
282
+ if (maxFitScale !== undefined) {
283
+ targetScale = Math.min(targetScale, maxFitScale);
284
+ }
285
+ if (minFitScale !== undefined) {
286
+ targetScale = Math.max(targetScale, minFitScale);
287
+ }
288
+
289
+ const newScale = checkZoomBounds(targetScale, minScale, maxScale, 0, false);
222
290
 
223
291
  const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
224
292
  const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
@@ -242,3 +310,61 @@ export function calculateZoomToNode(
242
310
 
243
311
  return { positionX: x, positionY: y, scale: newScale };
244
312
  }
313
+
314
+ export function calculateZoomToNode(
315
+ contextInstance: ReactZoomPanPinchContext,
316
+ node: HTMLElement,
317
+ customZoom?: number,
318
+ customOffsetX = 0,
319
+ customOffsetY = 0,
320
+ minFitScale?: number,
321
+ maxFitScale?: number,
322
+ ): { positionX: number; positionY: number; scale: number } {
323
+ return calculateZoomToRect(
324
+ contextInstance,
325
+ toRect(node),
326
+ customZoom,
327
+ customOffsetX,
328
+ customOffsetY,
329
+ minFitScale,
330
+ maxFitScale,
331
+ );
332
+ }
333
+
334
+ /**
335
+ * Client (viewport) coordinates → unscaled content coordinates (#378).
336
+ * Returns `{ x: 0, y: 0 }` before the wrapper is mounted.
337
+ */
338
+ export function clientToContentPoint(
339
+ contextInstance: ReactZoomPanPinchContext,
340
+ clientX: number,
341
+ clientY: number,
342
+ ): PositionType {
343
+ const { wrapperComponent, state } = contextInstance;
344
+ if (!wrapperComponent) return { x: 0, y: 0 };
345
+
346
+ const rect = wrapperComponent.getBoundingClientRect();
347
+ return {
348
+ x: (clientX - rect.left - state.positionX) / state.scale,
349
+ y: (clientY - rect.top - state.positionY) / state.scale,
350
+ };
351
+ }
352
+
353
+ /**
354
+ * Unscaled content coordinates → client (viewport) coordinates.
355
+ * Returns `{ x: 0, y: 0 }` before the wrapper is mounted.
356
+ */
357
+ export function contentToClientPoint(
358
+ contextInstance: ReactZoomPanPinchContext,
359
+ x: number,
360
+ y: number,
361
+ ): PositionType {
362
+ const { wrapperComponent, state } = contextInstance;
363
+ if (!wrapperComponent) return { x: 0, y: 0 };
364
+
365
+ const rect = wrapperComponent.getBoundingClientRect();
366
+ return {
367
+ x: rect.left + state.positionX + x * state.scale,
368
+ y: rect.top + state.positionY + y * state.scale,
369
+ };
370
+ }
@@ -108,6 +108,8 @@ export class ZoomPanPinch {
108
108
  public isAnimating = false;
109
109
  public animation: AnimationType | null = null;
110
110
  public animationFrame: number | null = null;
111
+ // Settles the promise returned by the running programmatic animation.
112
+ public animationResolve: (() => void) | null = null;
111
113
  // init helpers
112
114
  public initObserverTimer: ReturnType<typeof setTimeout> | null = null;
113
115
  // key press
@@ -47,7 +47,16 @@ export function handleAlignToBounds(
47
47
 
48
48
  const targetState = handlePanToBounds(contextInstance);
49
49
 
50
- if (targetState) {
50
+ if (!targetState) return;
51
+
52
+ // Nothing to align: starting a 200 ms animation towards the current state
53
+ // would only pin the transform and swallow input that arrives meanwhile
54
+ // (#582).
55
+ const { positionX, positionY } = contextInstance.state;
56
+ const isNoop =
57
+ targetState.positionX === positionX && targetState.positionY === positionY;
58
+
59
+ if (!isNoop) {
51
60
  animate(
52
61
  contextInstance,
53
62
  targetState,
@@ -23,8 +23,14 @@ export const handleWheelStart = (
23
23
  ): void => {
24
24
  const { onWheelStart, onZoomStart } = contextInstance.props;
25
25
 
26
+ // Every wheel event must stop a running animation, not only the first one
27
+ // of a gesture. The alignment animation scheduled by `handleWheelStop`
28
+ // starts after `wheelAnimationTime` (100 ms) while `wheelStopEventTimer`
29
+ // (160 ms) is still pending; guarding the cancel on the stop timer let that
30
+ // animation overwrite the zoom of any wheel event arriving in between (#582).
31
+ handleCancelAnimation(contextInstance);
32
+
26
33
  if (!contextInstance.wheelStopEventTimer) {
27
- handleCancelAnimation(contextInstance);
28
34
  handleCallback(getContext(contextInstance), event, onWheelStart);
29
35
  handleCallback(getContext(contextInstance), event, onZoomStart);
30
36
  }
@@ -128,8 +134,10 @@ export const handleWheelPanningStart = (
128
134
  ): void => {
129
135
  const { onWheelStart, onPanningStart } = contextInstance.props;
130
136
 
137
+ // See `handleWheelStart` (#582): cancel on every event, not only the first.
138
+ handleCancelAnimation(contextInstance);
139
+
131
140
  if (!contextInstance.wheelStopEventTimer) {
132
- handleCancelAnimation(contextInstance);
133
141
  // Same as a mouse pan start: bounds must reflect the current scale and
134
142
  // content size, not the ones measured at mount (#396, #433).
135
143
  handleCalculateBounds(contextInstance, contextInstance.state.scale);
@@ -3,3 +3,4 @@ export * from "./use-transform-init";
3
3
  export * from "./use-transform-effect";
4
4
  export * from "./use-transform-context";
5
5
  export * from "./use-transform-component";
6
+ export * from "./use-zoom-pan-pinch";
@@ -4,14 +4,32 @@ import { animations } from "../core/animations/animations.constants";
4
4
  import { DeepNonNullable } from "./helpers.model";
5
5
  import {
6
6
  centerView,
7
+ clientToContent,
8
+ contentToClient,
7
9
  resetTransform,
8
10
  setTransform,
9
11
  zoomIn,
10
12
  zoomOut,
11
13
  zoomToElement,
14
+ zoomToPoint,
12
15
  } from "../core/handlers/handlers.logic";
13
16
  import { ZoomPanPinch } from "../core/instance.core";
14
17
 
18
+ export type ZoomToElementTarget = HTMLElement | string;
19
+
20
+ export type ZoomToElementOptions = {
21
+ /** Explicit target scale; when omitted the element(s) are fitted to the viewport. */
22
+ scale?: number;
23
+ /** Lower cap for the automatically computed fit scale. */
24
+ minScale?: number;
25
+ /** Upper cap for the automatically computed fit scale (e.g. stop tiny elements from over-zooming). */
26
+ maxScale?: number;
27
+ animationTime?: number;
28
+ animationType?: keyof typeof animations;
29
+ offsetX?: number;
30
+ offsetY?: number;
31
+ };
32
+
15
33
  export type ReactZoomPanPinchContext = typeof ZoomPanPinch.prototype;
16
34
 
17
35
  export type ReactZoomPanPinchContextState = {
@@ -41,6 +59,9 @@ export type ReactZoomPanPinchHandlers = {
41
59
  resetTransform: ReturnType<typeof resetTransform>;
42
60
  centerView: ReturnType<typeof centerView>;
43
61
  zoomToElement: ReturnType<typeof zoomToElement>;
62
+ zoomToPoint: ReturnType<typeof zoomToPoint>;
63
+ clientToContent: ReturnType<typeof clientToContent>;
64
+ contentToClient: ReturnType<typeof contentToClient>;
44
65
  };
45
66
 
46
67
  export type ReactZoomPanPinchRefProps = {
@@ -10,7 +10,10 @@ import {
10
10
  setTransform,
11
11
  resetTransform,
12
12
  zoomToElement,
13
+ zoomToPoint,
13
14
  centerView,
15
+ clientToContent,
16
+ contentToClient,
14
17
  } from "../core/handlers/handlers.logic";
15
18
 
16
19
  export const getControls = (
@@ -25,6 +28,9 @@ export const getControls = (
25
28
  resetTransform: resetTransform(contextInstance),
26
29
  centerView: centerView(contextInstance),
27
30
  zoomToElement: zoomToElement(contextInstance),
31
+ zoomToPoint: zoomToPoint(contextInstance),
32
+ clientToContent: clientToContent(contextInstance),
33
+ contentToClient: contentToClient(contextInstance),
28
34
  };
29
35
  };
30
36