react-zoom-pan-pinch 4.2.0 → 4.3.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.
@@ -160,6 +160,61 @@ export function rubberbandIfOutOfBounds(
160
160
  return position;
161
161
  }
162
162
 
163
+ /**
164
+ * Furthest a grabbed position is traced back along the rubber band curve, as
165
+ * a fraction of the padding. The curve never reaches the padding itself, so
166
+ * a position at (or past) it has no pointer distance to map back to.
167
+ */
168
+ const MAX_RUBBER_BAND_RATIO = 0.9;
169
+
170
+ /**
171
+ * Resistance of the rubber band, the constant UIScrollView uses: past a bound
172
+ * the content moves at 0.55× the pointer and slows down from there.
173
+ */
174
+ export const RUBBER_BAND_CONSTANT = 0.55;
175
+
176
+ /**
177
+ * Where a pan that was dragged past a bound ends up: `(1 - 1 / (x * c / d + 1)) * d`
178
+ * with `d` = `padding`. It trails the pointer from the first pixel past the
179
+ * bound and approaches — never reaches — `padding` px past it.
180
+ */
181
+ export function getRubberBandPosition(
182
+ position: number,
183
+ min: number,
184
+ max: number,
185
+ padding: number,
186
+ ): number {
187
+ if (padding <= 0) return clamp(position, min, max);
188
+ if (position < min)
189
+ return min - rubberband(min - position, padding, RUBBER_BAND_CONSTANT);
190
+ if (position > max)
191
+ return max + rubberband(position - max, padding, RUBBER_BAND_CONSTANT);
192
+ return position;
193
+ }
194
+
195
+ /**
196
+ * Inverse of `getRubberBandPosition`: the pointer position that puts the
197
+ * content at `position`. Lets a pan that starts inside the padding (grabbed
198
+ * mid return animation, trackpad deltas) continue from where the content is.
199
+ */
200
+ export function getRubberBandSource(
201
+ position: number,
202
+ min: number,
203
+ max: number,
204
+ padding: number,
205
+ ): number {
206
+ if (padding <= 0) return position;
207
+
208
+ const stretch = (offset: number) => {
209
+ const limited = Math.min(offset, padding * MAX_RUBBER_BAND_RATIO);
210
+ return (limited * padding) / (RUBBER_BAND_CONSTANT * (padding - limited));
211
+ };
212
+
213
+ if (position < min) return min - stretch(min - position);
214
+ if (position > max) return max + stretch(position - max);
215
+ return position;
216
+ }
217
+
163
218
  /**
164
219
  * Keeps value between given bounds, used for limiting view to given boundaries
165
220
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -224,3 +279,36 @@ export function getMouseBoundedPosition(
224
279
  );
225
280
  return { x, y };
226
281
  }
282
+
283
+ /**
284
+ * Bounded position for a pan gesture: free inside the bounds and, inside the
285
+ * padding around them, rubber banded (`rubberBand`) or 1:1 up to its edge.
286
+ */
287
+ export function getPanBoundedPosition(
288
+ positionX: number,
289
+ positionY: number,
290
+ bounds: BoundsType,
291
+ limitToBounds: boolean,
292
+ paddingX: number,
293
+ paddingY: number,
294
+ rubberBand: boolean,
295
+ ): PositionType {
296
+ const { minPositionX, minPositionY, maxPositionX, maxPositionY } = bounds;
297
+
298
+ const limit = (
299
+ position: number,
300
+ min: number,
301
+ max: number,
302
+ padding: number,
303
+ ) => {
304
+ if (limitToBounds && rubberBand && padding > 0) {
305
+ return roundNumber(getRubberBandPosition(position, min, max, padding), 2);
306
+ }
307
+ return boundLimiter(position, min - padding, max + padding, limitToBounds);
308
+ };
309
+
310
+ return {
311
+ x: limit(positionX, minPositionX, maxPositionX, paddingX),
312
+ y: limit(positionY, minPositionY, maxPositionY, paddingY),
313
+ };
314
+ }
@@ -17,7 +17,6 @@ import {
17
17
  getTransformStyles,
18
18
  makePassiveEventOption,
19
19
  getCenterPosition,
20
- assignRef,
21
20
  } from "../utils";
22
21
  import { handleCancelAnimation } from "./animations/animations.utils";
23
22
  import { handleKeyboardNavigation } from "./keyboard/keyboard.logic";
@@ -41,6 +40,7 @@ import {
41
40
  } from "./wheel/wheel.logic";
42
41
  import {
43
42
  getPaddingValue,
43
+ getPanSourcePosition,
44
44
  handleNewPosition,
45
45
  isPanningAllowed,
46
46
  isPanningStartAllowed,
@@ -412,19 +412,23 @@ export class ZoomPanPinch {
412
412
  }
413
413
  event.stopPropagation();
414
414
 
415
- const { positionX, positionY } = this.state;
416
- const mouseX = positionX - event.deltaX;
417
- const mouseY = positionY - event.deltaY;
418
- const newPositionX = lockAxisX ? positionX : mouseX;
419
- const newPositionY = lockAxisY ? positionY : mouseY;
415
+ const deltaX = lockAxisX ? 0 : event.deltaX;
416
+ const deltaY = lockAxisY ? 0 : event.deltaY;
417
+
418
+ if (!deltaX && !deltaY) return;
420
419
 
421
420
  const { sizeX, sizeY } = this.setup.autoAlignment;
422
421
  const paddingValueX = getPaddingValue(this, sizeX);
423
422
  const paddingValueY = getPaddingValue(this, sizeY);
424
423
 
425
- if (newPositionX === positionX && newPositionY === positionY) return;
426
-
427
424
  handleWheelPanningStart(this, event);
425
+
426
+ // Deltas add up on the pointer's side of the rubber band, the same way
427
+ // a drag does, so the content slows down towards the padding limit.
428
+ const source = getPanSourcePosition(this);
429
+ const newPositionX = source.x - deltaX;
430
+ const newPositionY = source.y - deltaY;
431
+
428
432
  handleNewPosition(
429
433
  this,
430
434
  newPositionX,
@@ -933,6 +937,5 @@ export class ZoomPanPinch {
933
937
  this.isInitialized = true;
934
938
  const ctx = getContext(this);
935
939
  handleCallback(ctx, undefined, this.props.onInit);
936
- assignRef(this.props.ref, ctx);
937
940
  };
938
941
  }
@@ -109,7 +109,13 @@ export function handlePanningEnd(
109
109
  if (start) {
110
110
  const dx = positionX - start.x;
111
111
  const dy = positionY - start.y;
112
- if (dx * dx + dy * dy <= 25) return;
112
+ // A click is not a fling, but it can still land on content left past
113
+ // the bounds — e.g. grabbed mid return animation, which the press
114
+ // cancelled. Finish the return (a no-op inside the bounds).
115
+ if (dx * dx + dy * dy <= 25) {
116
+ handleAlignToBounds(contextInstance);
117
+ return;
118
+ }
113
119
  }
114
120
 
115
121
  contextInstance.isAnimating = false;
@@ -9,7 +9,10 @@ import {
9
9
  isEditableTarget,
10
10
  isExcludedNode,
11
11
  } from "../../utils";
12
- import { getMouseBoundedPosition } from "../bounds/bounds.utils";
12
+ import {
13
+ getPanBoundedPosition,
14
+ getRubberBandSource,
15
+ } from "../bounds/bounds.utils";
13
16
  import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
14
17
 
15
18
  export const isPanningStartAllowed = (
@@ -66,11 +69,75 @@ export const isPanningAllowed = (
66
69
  return true;
67
70
  };
68
71
 
72
+ export const getPaddingValue = (
73
+ contextInstance: ReactZoomPanPinchContext,
74
+ size: number,
75
+ explicitScale?: number,
76
+ ): number => {
77
+ const { setup, state } = contextInstance;
78
+ const { minScale, disablePadding, centerZoomedOut } = setup;
79
+
80
+ const scale = explicitScale ?? state.scale;
81
+
82
+ if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
83
+ return size;
84
+ }
85
+
86
+ return 0;
87
+ };
88
+
89
+ /**
90
+ * Dragging past the bounds stretches like a rubber band only when the content
91
+ * is going to return to them on release (`autoAlignment`). Without that return
92
+ * the padding is a plain area the content can be left in, so it follows the
93
+ * pointer 1:1 up to its edge.
94
+ */
95
+ export function isRubberBandEnabled(
96
+ contextInstance: ReactZoomPanPinchContext,
97
+ ): boolean {
98
+ const { limitToBounds, autoAlignment } = contextInstance.setup;
99
+ return limitToBounds && !autoAlignment.disabled;
100
+ }
101
+
102
+ /**
103
+ * The position a pan continues from. Inside the bounds that is the content
104
+ * position; inside the padding the content trails the pointer (rubber band),
105
+ * so the pointer's own position is traced back from it — otherwise the first
106
+ * move of a pan started there would make the content jump.
107
+ */
108
+ export function getPanSourcePosition(
109
+ contextInstance: ReactZoomPanPinchContext,
110
+ ): PositionType {
111
+ const { bounds, setup, state } = contextInstance;
112
+ const { positionX, positionY } = state;
113
+
114
+ if (!bounds || !isRubberBandEnabled(contextInstance)) {
115
+ return { x: positionX, y: positionY };
116
+ }
117
+
118
+ const { sizeX, sizeY } = setup.autoAlignment;
119
+
120
+ return {
121
+ x: getRubberBandSource(
122
+ positionX,
123
+ bounds.minPositionX,
124
+ bounds.maxPositionX,
125
+ getPaddingValue(contextInstance, sizeX),
126
+ ),
127
+ y: getRubberBandSource(
128
+ positionY,
129
+ bounds.minPositionY,
130
+ bounds.maxPositionY,
131
+ getPaddingValue(contextInstance, sizeY),
132
+ ),
133
+ };
134
+ }
135
+
69
136
  export const handlePanningSetup = (
70
137
  contextInstance: ReactZoomPanPinchContext,
71
138
  event: MouseEvent,
72
139
  ): void => {
73
- const { positionX, positionY } = contextInstance.state;
140
+ const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
74
141
 
75
142
  contextInstance.isPanning = true;
76
143
 
@@ -86,7 +153,7 @@ export const handleTouchPanningSetup = (
86
153
  event: TouchEvent,
87
154
  ): void => {
88
155
  const { touches } = event;
89
- const { positionX, positionY } = contextInstance.state;
156
+ const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
90
157
 
91
158
  contextInstance.isPanning = true;
92
159
 
@@ -170,14 +237,14 @@ export function handleNewPosition(
170
237
  return;
171
238
  }
172
239
 
173
- const { x, y } = getMouseBoundedPosition(
240
+ const { x, y } = getPanBoundedPosition(
174
241
  newPositionX,
175
242
  newPositionY,
176
243
  bounds,
177
244
  limitToBounds,
178
245
  paddingValueX,
179
246
  paddingValueY,
180
- wrapperComponent,
247
+ isRubberBandEnabled(contextInstance),
181
248
  );
182
249
 
183
250
  contextInstance.setState(scale, x, y);
@@ -188,10 +255,12 @@ export const getPanningClientPosition = (
188
255
  clientX: number,
189
256
  clientY: number,
190
257
  ): PositionType => {
191
- const { startCoords, state } = contextInstance;
258
+ const { startCoords } = contextInstance;
192
259
  const { panning } = contextInstance.setup;
193
260
  const { lockAxisX, lockAxisY } = panning;
194
- const { positionX, positionY } = state;
261
+ // A locked axis keeps its place: the source maps back onto the position
262
+ // it has now, also when it rests inside the padding.
263
+ const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
195
264
 
196
265
  if (!startCoords) {
197
266
  return { x: positionX, y: positionY };
@@ -204,20 +273,3 @@ export const getPanningClientPosition = (
204
273
 
205
274
  return { x: newPositionX, y: newPositionY };
206
275
  };
207
-
208
- export const getPaddingValue = (
209
- contextInstance: ReactZoomPanPinchContext,
210
- size: number,
211
- explicitScale?: number,
212
- ): number => {
213
- const { setup, state } = contextInstance;
214
- const { minScale, disablePadding, centerZoomedOut } = setup;
215
-
216
- const scale = explicitScale ?? state.scale;
217
-
218
- if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
219
- return size;
220
- }
221
-
222
- return 0;
223
- };
@@ -4,7 +4,7 @@ import { ReactZoomPanPinchContext } from "../../models/context.model";
4
4
  import { getContext } from "../../utils";
5
5
  import { animations } from "../animations/animations.constants";
6
6
  import { handleSetupAnimation } from "../animations/animations.utils";
7
- import { getPaddingValue } from "./panning.utils";
7
+ import { getPaddingValue, isRubberBandEnabled } from "./panning.utils";
8
8
  import {
9
9
  getVelocityPosition,
10
10
  getVelocityMoveTime,
@@ -123,8 +123,15 @@ export function handleVelocityPanning(
123
123
 
124
124
  const paddingValueX = getPaddingValue(contextInstance, sizeX);
125
125
  const paddingValueY = getPaddingValue(contextInstance, sizeY);
126
- const paddingX = (paddingValueX * wrapperComponent.clientWidth) / 100;
127
- const paddingY = (paddingValueY * wrapperComponent.clientHeight) / 100;
126
+ // A fling released inside the rubber band stays within the same stretch a
127
+ // drag could reach (`padding` px), not a share of the wrapper size.
128
+ const isRubberBand = isRubberBandEnabled(contextInstance);
129
+ const paddingX = isRubberBand
130
+ ? paddingValueX
131
+ : (paddingValueX * wrapperComponent.clientWidth) / 100;
132
+ const paddingY = isRubberBand
133
+ ? paddingValueY
134
+ : (paddingValueY * wrapperComponent.clientHeight) / 100;
128
135
  const maxTargetX = maxPositionX + paddingX;
129
136
  const minTargetX = minPositionX - paddingX;
130
137
 
@@ -0,0 +1,21 @@
1
+ import { ReactZoomPanPinchState } from "../models";
2
+
3
+ export type StateSnapshot = Pick<
4
+ ReactZoomPanPinchState,
5
+ "scale" | "positionX" | "positionY"
6
+ >;
7
+
8
+ export const takeSnapshot = (state: ReactZoomPanPinchState): StateSnapshot => ({
9
+ scale: state.scale,
10
+ positionX: state.positionX,
11
+ positionY: state.positionY,
12
+ });
13
+
14
+ export const isSameSnapshot = (
15
+ snapshot: StateSnapshot | null,
16
+ state: ReactZoomPanPinchState,
17
+ ): boolean =>
18
+ snapshot !== null &&
19
+ snapshot.scale === state.scale &&
20
+ snapshot.positionX === state.positionX &&
21
+ snapshot.positionY === state.positionY;
@@ -1,25 +1,43 @@
1
- import { useEffect, useState } from "react";
1
+ import { useEffect, useRef, useState } from "react";
2
2
 
3
3
  import { useTransformContext } from "./use-transform-context";
4
4
  import { getState } from "utils";
5
5
  import { ReactZoomPanPinchContextState } from "../models";
6
+ import {
7
+ StateSnapshot,
8
+ isSameSnapshot,
9
+ takeSnapshot,
10
+ } from "./initial-layout.utils";
6
11
 
7
12
  export function useTransformComponent<T>(
8
13
  callback: (state: ReactZoomPanPinchContextState) => T,
9
14
  ): T {
10
15
  const libraryContext = useTransformContext();
16
+ const lastSeen = useRef<StateSnapshot | null>(null);
11
17
 
12
- const [transformRender, setTransformRender] = useState<T>(() =>
13
- callback(getState(libraryContext)),
14
- );
18
+ const [transformRender, setTransformRender] = useState<T>(() => {
19
+ lastSeen.current = takeSnapshot(libraryContext.state);
20
+ return callback(getState(libraryContext));
21
+ });
15
22
 
16
23
  useEffect(() => {
17
24
  let mounted = true;
18
25
  const unmount = libraryContext.onChange((ref) => {
19
26
  if (mounted) {
27
+ lastSeen.current = takeSnapshot(ref.instance.state);
20
28
  setTransformRender(callback(getState(ref.instance)));
21
29
  }
22
30
  });
31
+
32
+ // The instance initializes in a layout effect, so a transform applied on
33
+ // mount (initialPosition*, centerOnInit, fitOnInit) can land before this
34
+ // subscription exists. Catch up when the state moved since the last
35
+ // render; the snapshot check keeps this from re-rendering in a loop.
36
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
37
+ lastSeen.current = takeSnapshot(libraryContext.state);
38
+ setTransformRender(callback(getState(libraryContext)));
39
+ }
40
+
23
41
  return () => {
24
42
  unmount();
25
43
  mounted = false;
@@ -1,19 +1,38 @@
1
- import { useEffect } from "react";
1
+ import { useEffect, useRef } from "react";
2
2
 
3
3
  import { getState } from "utils";
4
4
  import { ReactZoomPanPinchContextState } from "../models";
5
5
  import { useTransformContext } from "./use-transform-context";
6
+ import {
7
+ StateSnapshot,
8
+ isSameSnapshot,
9
+ takeSnapshot,
10
+ } from "./initial-layout.utils";
6
11
 
7
12
  export const useTransformEffect = (
8
13
  callback: (ref: ReactZoomPanPinchContextState) => void | (() => void),
9
14
  ): void => {
10
15
  const libraryContext = useTransformContext();
16
+ // Starts as the state the consumer rendered with, so a transform applied
17
+ // during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
18
+ // in a layout effect, before this subscription exists — is delivered once.
19
+ const lastSeen = useRef<StateSnapshot | null>(null);
20
+ if (lastSeen.current === null) {
21
+ lastSeen.current = takeSnapshot(libraryContext.state);
22
+ }
11
23
 
12
24
  useEffect(() => {
13
25
  let unmountCallback: void | (() => void);
14
26
  const unmount = libraryContext.onChange((ref) => {
27
+ lastSeen.current = takeSnapshot(ref.instance.state);
15
28
  unmountCallback = callback(getState(ref.instance));
16
29
  });
30
+
31
+ if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
32
+ lastSeen.current = takeSnapshot(libraryContext.state);
33
+ unmountCallback = callback(getState(libraryContext));
34
+ }
35
+
17
36
  return () => {
18
37
  unmount();
19
38
  unmountCallback?.();