react-zoom-pan-pinch 3.7.0 → 4.0.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.
Files changed (58) hide show
  1. package/dist/index.cjs.js +785 -310
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +117 -23
  4. package/dist/index.esm.js +781 -302
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +74 -62
  7. package/src/components/index.ts +5 -0
  8. package/src/components/keep-scale/keep-scale.tsx +28 -0
  9. package/src/components/mini-map/mini-map.tsx +292 -0
  10. package/src/components/mini-map/use-resize.hook.ts +74 -0
  11. package/src/components/transform-component/transform-component.module.css +43 -0
  12. package/src/components/transform-component/transform-component.tsx +96 -0
  13. package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
  14. package/src/components/virtualize/virtualize.tsx +133 -0
  15. package/src/components/virtualize/virtualize.utils.ts +104 -0
  16. package/src/constants/state.constants.ts +99 -0
  17. package/src/core/animations/animations.constants.ts +78 -0
  18. package/src/core/animations/animations.utils.ts +115 -0
  19. package/src/core/bounds/bounds.types.ts +8 -0
  20. package/src/core/bounds/bounds.utils.ts +226 -0
  21. package/src/core/double-click/double-click.logic.ts +127 -0
  22. package/src/core/handlers/handlers.logic.ts +122 -0
  23. package/src/core/handlers/handlers.utils.ts +243 -0
  24. package/src/core/instance.core.ts +704 -0
  25. package/src/core/pan/panning.logic.ts +122 -0
  26. package/src/core/pan/panning.utils.ts +216 -0
  27. package/src/core/pan/velocity.logic.ts +187 -0
  28. package/src/core/pan/velocity.utils.ts +83 -0
  29. package/src/core/pinch/pinch.logic.ts +139 -0
  30. package/src/core/pinch/pinch.utils.ts +95 -0
  31. package/src/core/wheel/wheel.logic.ts +161 -0
  32. package/src/core/wheel/wheel.utils.ts +157 -0
  33. package/src/core/zoom/zoom.logic.ts +71 -0
  34. package/src/core/zoom/zoom.utils.ts +62 -0
  35. package/src/hooks/index.ts +5 -0
  36. package/src/hooks/use-controls.tsx +9 -0
  37. package/src/hooks/use-transform-component.tsx +30 -0
  38. package/src/hooks/use-transform-context.tsx +13 -0
  39. package/src/hooks/use-transform-effect.tsx +22 -0
  40. package/src/hooks/use-transform-init.tsx +28 -0
  41. package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
  42. package/src/index.ts +6 -0
  43. package/src/models/calculations.model.ts +34 -0
  44. package/src/models/context.model.ts +218 -0
  45. package/src/models/helpers.model.ts +15 -0
  46. package/src/models/index.ts +3 -0
  47. package/src/react-app-env.d.ts +1 -0
  48. package/src/styles.css +8 -0
  49. package/src/typings.d.ts +21 -0
  50. package/src/utils/calculations.utils.ts +16 -0
  51. package/src/utils/callback.utils.ts +11 -0
  52. package/src/utils/context.utils.ts +49 -0
  53. package/src/utils/event.utils.ts +38 -0
  54. package/src/utils/helpers.utils.ts +22 -0
  55. package/src/utils/index.ts +8 -0
  56. package/src/utils/ref.utils.ts +32 -0
  57. package/src/utils/state.utils.ts +77 -0
  58. package/src/utils/styles.utils.ts +49 -0
package/dist/index.d.ts CHANGED
@@ -28,7 +28,7 @@ declare const zoomOut: (contextInstance: ReactZoomPanPinchContext) => (step?: nu
28
28
  declare const setTransform: (contextInstance: ReactZoomPanPinchContext) => (newPositionX: number, newPositionY: number, newScale: number, animationTime?: number, animationType?: keyof typeof animations) => void;
29
29
  declare const resetTransform: (contextInstance: ReactZoomPanPinchContext) => (animationTime?: number, animationType?: keyof typeof animations) => void;
30
30
  declare const centerView: (contextInstance: ReactZoomPanPinchContext) => (scale?: number, animationTime?: number, animationType?: keyof typeof animations) => void;
31
- declare const zoomToElement: (contextInstance: ReactZoomPanPinchContext) => (node: HTMLElement | string, scale?: number, animationTime?: number, animationType?: keyof typeof animations) => void;
31
+ declare const zoomToElement: (contextInstance: ReactZoomPanPinchContext) => (node: HTMLElement | string, scale?: number, animationTime?: number, animationType?: keyof typeof animations, offsetX?: number, offsetY?: number) => void;
32
32
 
33
33
  type StartCoordsType = {
34
34
  x: number;
@@ -37,13 +37,18 @@ type StartCoordsType = {
37
37
  declare class ZoomPanPinch {
38
38
  props: ReactZoomPanPinchProps;
39
39
  mounted: boolean;
40
- pinchLastCenterX: number | null;
41
- pinchLastCenterY: number | null;
42
- transformState: ReactZoomPanPinchState;
40
+ state: ReactZoomPanPinchState;
43
41
  setup: LibrarySetup;
44
42
  observer?: ResizeObserver;
45
43
  onChangeCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void>;
46
44
  onInitCallbacks: Set<(ctx: ReactZoomPanPinchRef) => void>;
45
+ onTransformCallbacks: Set<(data: {
46
+ scale: number;
47
+ positionX: number;
48
+ positionY: number;
49
+ previousScale: number;
50
+ ref: ReactZoomPanPinchRef;
51
+ }) => void>;
47
52
  wrapperComponent: HTMLDivElement | null;
48
53
  contentComponent: HTMLDivElement | null;
49
54
  isInitialized: boolean;
@@ -54,19 +59,24 @@ declare class ZoomPanPinch {
54
59
  isPanning: boolean;
55
60
  isWheelPanning: boolean;
56
61
  startCoords: StartCoordsType;
62
+ panStartPosition: {
63
+ x: number;
64
+ y: number;
65
+ } | null;
57
66
  lastTouch: number | null;
67
+ isPinching: boolean;
58
68
  distance: null | number;
59
69
  lastDistance: null | number;
60
70
  pinchStartDistance: null | number;
61
71
  pinchStartScale: null | number;
62
72
  pinchMidpoint: null | PositionType;
73
+ pinchPreviousCenter: null | PositionType;
63
74
  doubleClickStopEventTimer: ReturnType<typeof setTimeout> | null;
64
75
  velocity: VelocityType | null;
65
76
  velocityTime: number | null;
66
77
  lastMousePosition: PositionType | null;
67
- animate: boolean;
78
+ isAnimating: boolean;
68
79
  animation: AnimationType | null;
69
- maxBounds: BoundsType | null;
70
80
  pressedKeys: {
71
81
  [key: string]: boolean;
72
82
  };
@@ -77,7 +87,7 @@ declare class ZoomPanPinch {
77
87
  initializeWindowEvents: () => void;
78
88
  cleanupWindowEvents: () => void;
79
89
  handleInitializeWrapperEvents: (wrapper: HTMLDivElement) => void;
80
- handleInitialize: (wrapper: HTMLDivElement, contentComponent: HTMLDivElement) => void;
90
+ handleInitialize: (contentComponent: HTMLDivElement) => void;
81
91
  onWheelZoom: (event: WheelEvent) => void;
82
92
  onWheelPanning: (event: WheelEvent) => void;
83
93
  onPanningStart: (event: MouseEvent) => void;
@@ -91,17 +101,26 @@ declare class ZoomPanPinch {
91
101
  onTouchPanningStop: (event: TouchEvent) => void;
92
102
  onDoubleClick: (event: MouseEvent | TouchEvent) => void;
93
103
  clearPanning: (event: MouseEvent) => void;
104
+ handleWindowBlur: () => void;
105
+ syncModifierKeys: (event: MouseEvent | WheelEvent | TouchEvent) => void;
94
106
  setKeyPressed: (e: KeyboardEvent) => void;
95
107
  setKeyUnPressed: (e: KeyboardEvent) => void;
96
- isPressingKeys: (keys: string[]) => boolean;
97
- setTransformState: (scale: number, positionX: number, positionY: number) => void;
108
+ isPressingKeys: (keys: string[] | ((keys: string[]) => boolean)) => boolean;
98
109
  setCenter: () => void;
99
110
  handleTransformStyles: (x: number, y: number, scale: number) => string;
100
- applyTransformation: () => void;
101
111
  getContext: () => ReactZoomPanPinchRef;
112
+ applyTransformation: () => void;
113
+ setState: (scale: number, positionX: number, positionY: number) => void;
102
114
  /**
103
115
  * Hooks
104
116
  */
117
+ onTransform: (callback: (data: {
118
+ scale: number;
119
+ positionX: number;
120
+ positionY: number;
121
+ previousScale: number;
122
+ ref: ReactZoomPanPinchRef;
123
+ }) => void) => () => void;
105
124
  onChange: (callback: (ref: ReactZoomPanPinchRef) => void) => () => void;
106
125
  onInit: (callback: (ref: ReactZoomPanPinchRef) => void) => () => void;
107
126
  /**
@@ -117,6 +136,7 @@ type ReactZoomPanPinchContextState = {
117
136
  };
118
137
  type ReactZoomPanPinchContentRef = {
119
138
  instance: ReactZoomPanPinchContext;
139
+ state: ReactZoomPanPinchState;
120
140
  } & ReactZoomPanPinchHandlers;
121
141
  type ReactZoomPanPinchRef = ReactZoomPanPinchContextState & ReactZoomPanPinchHandlers;
122
142
  type ReactZoomPanPinchState = {
@@ -139,6 +159,7 @@ type ReactZoomPanPinchRefProps = {
139
159
  type ReactZoomPanPinchProps = {
140
160
  children?: React.ReactNode | ((ref: ReactZoomPanPinchContentRef) => React.ReactNode);
141
161
  ref?: React.Ref<ReactZoomPanPinchRef>;
162
+ detached?: boolean;
142
163
  initialScale?: number;
143
164
  initialPositionX?: number;
144
165
  initialPositionY?: number;
@@ -157,11 +178,10 @@ type ReactZoomPanPinchProps = {
157
178
  smooth?: boolean;
158
179
  wheel?: {
159
180
  step?: number;
160
- smoothStep?: number;
161
181
  disabled?: boolean;
162
182
  wheelDisabled?: boolean;
163
183
  touchPadDisabled?: boolean;
164
- activationKeys?: string[];
184
+ activationKeys?: string[] | ((keys: string[]) => boolean);
165
185
  excluded?: string[];
166
186
  };
167
187
  panning?: {
@@ -172,13 +192,21 @@ type ReactZoomPanPinchProps = {
172
192
  allowLeftClickPan?: boolean;
173
193
  allowMiddleClickPan?: boolean;
174
194
  allowRightClickPan?: boolean;
175
- activationKeys?: string[];
195
+ activationKeys?: string[] | ((keys: string[]) => boolean);
176
196
  excluded?: string[];
177
- wheelPanning?: boolean;
178
197
  };
179
198
  pinch?: {
180
199
  step?: number;
181
200
  disabled?: boolean;
201
+ allowPanning?: boolean;
202
+ excluded?: string[];
203
+ };
204
+ trackPadPanning?: {
205
+ disabled?: boolean;
206
+ velocityDisabled?: boolean;
207
+ lockAxisX?: boolean;
208
+ lockAxisY?: boolean;
209
+ activationKeys?: string[] | ((keys: string[]) => boolean);
182
210
  excluded?: string[];
183
211
  };
184
212
  doubleClick?: {
@@ -195,7 +223,7 @@ type ReactZoomPanPinchProps = {
195
223
  animationTime?: number;
196
224
  animationType?: keyof typeof animations;
197
225
  };
198
- alignmentAnimation?: {
226
+ autoAlignment?: {
199
227
  disabled?: boolean;
200
228
  sizeX?: number;
201
229
  sizeY?: number;
@@ -205,10 +233,14 @@ type ReactZoomPanPinchProps = {
205
233
  };
206
234
  velocityAnimation?: {
207
235
  disabled?: boolean;
208
- sensitivity?: number;
236
+ sensitivityTouch?: number;
237
+ sensitivityMouse?: number;
238
+ maxStrengthMouse?: number;
239
+ maxStrengthTouch?: number;
240
+ inertia?: number;
209
241
  animationTime?: number;
242
+ maxAnimationTime?: number;
210
243
  animationType?: keyof typeof animations;
211
- equalToMove?: boolean;
212
244
  };
213
245
  onWheelStart?: (ref: ReactZoomPanPinchRef, event: WheelEvent) => void;
214
246
  onWheel?: (ref: ReactZoomPanPinchRef, event: WheelEvent) => void;
@@ -216,13 +248,13 @@ type ReactZoomPanPinchProps = {
216
248
  onPanningStart?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
217
249
  onPanning?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
218
250
  onPanningStop?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
219
- onPinchingStart?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
220
- onPinching?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
221
- onPinchingStop?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
251
+ onPinchStart?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
252
+ onPinch?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
253
+ onPinchStop?: (ref: ReactZoomPanPinchRef, event: TouchEvent) => void;
222
254
  onZoomStart?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
223
255
  onZoom?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
224
256
  onZoomStop?: (ref: ReactZoomPanPinchRef, event: TouchEvent | MouseEvent) => void;
225
- onTransformed?: (ref: ReactZoomPanPinchRef, state: {
257
+ onTransform?: (ref: ReactZoomPanPinchRef, state: {
226
258
  scale: number;
227
259
  positionX: number;
228
260
  positionY: number;
@@ -232,7 +264,7 @@ type ReactZoomPanPinchProps = {
232
264
  type ReactZoomPanPinchComponentHelpers = {
233
265
  setComponents: (wrapper: HTMLDivElement, content: HTMLDivElement) => void;
234
266
  };
235
- type LibrarySetup = Pick<ReactZoomPanPinchProps, "minPositionX" | "maxPositionX" | "minPositionY" | "maxPositionY"> & DeepNonNullable<Omit<ReactZoomPanPinchProps, "ref" | "initialScale" | "initialPositionX" | "initialPositionY" | "minPositionX" | "maxPositionX" | "minPositionY" | "maxPositionY" | "children" | "defaultPositionX" | "defaultPositionY" | "defaultScale" | "wrapperClass" | "contentClass" | "onWheelStart" | "onWheel" | "onWheelStop" | "onPanningStart" | "onPanning" | "onPanningStop" | "onPinchingStart" | "onPinching" | "onPinchingStop" | "onZoomStart" | "onZoom" | "onZoomStop" | "onTransformed" | "onInit" | "customTransform">>;
267
+ type LibrarySetup = Pick<ReactZoomPanPinchProps, "minPositionX" | "maxPositionX" | "minPositionY" | "maxPositionY"> & DeepNonNullable<Omit<ReactZoomPanPinchProps, "ref" | "initialScale" | "initialPositionX" | "initialPositionY" | "minPositionX" | "maxPositionX" | "minPositionY" | "maxPositionY" | "children" | "defaultPositionX" | "defaultPositionY" | "defaultScale" | "wrapperClass" | "contentClass" | "onWheelStart" | "onWheel" | "onWheelStop" | "onPanningStart" | "onPanning" | "onPanningStop" | "onPinchStart" | "onPinch" | "onPinchStop" | "onZoomStart" | "onZoom" | "onZoomStop" | "onTransform" | "onInit" | "customTransform">>;
236
268
  type ReactZoomPanPinchBaseClasses = {
237
269
  wrapperClass: string;
238
270
  contentClass: string;
@@ -261,8 +293,15 @@ type BoundsType = {
261
293
  maxPositionX: number;
262
294
  minPositionY: number;
263
295
  maxPositionY: number;
296
+ scaleWidthFactor: number;
297
+ scaleHeightFactor: number;
264
298
  };
265
299
  type AnimationType = () => void | number;
300
+ declare enum DeviceType {
301
+ TRACK_PAD = "track_pad",
302
+ MOUSE = "mouse",
303
+ TOUCH = "touch"
304
+ }
266
305
 
267
306
  declare const useControls: () => ReactZoomPanPinchContentRef;
268
307
 
@@ -281,6 +320,13 @@ type MiniMapProps = {
281
320
  width?: number;
282
321
  height?: number;
283
322
  borderColor?: string;
323
+ /** Merged onto the viewport indicator (`.rzpp-minimap-preview`). */
324
+ previewStyle?: React.CSSProperties;
325
+ /** Extra class name(s) appended to `.rzpp-minimap-wrapper`. */
326
+ wrapperClassName?: string;
327
+ /** Extra class name(s) appended to `.rzpp-minimap-preview`. */
328
+ previewClassName?: string;
329
+ panning?: boolean;
284
330
  } & React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
285
331
  declare const MiniMap: React.FC<MiniMapProps>;
286
332
 
@@ -292,14 +338,62 @@ type Props = {
292
338
  contentStyle?: React.CSSProperties;
293
339
  wrapperProps?: React.HTMLAttributes<HTMLDivElement>;
294
340
  contentProps?: React.HTMLAttributes<HTMLDivElement>;
341
+ /**
342
+ * When true, renders an infinite dot-grid background behind the content
343
+ * that scales and pans in sync with the transform. Pair with
344
+ * `limitToBounds={false}` on TransformWrapper for full effect.
345
+ */
346
+ infinite?: boolean;
295
347
  };
296
348
  declare const TransformComponent: React.FC<Props>;
297
349
 
298
350
  declare const Context: React.Context<ZoomPanPinch>;
299
351
  declare const TransformWrapper: React.ForwardRefExoticComponent<Omit<ReactZoomPanPinchProps, "ref"> & React.RefAttributes<ReactZoomPanPinchContentRef>>;
300
352
 
353
+ type VirtualizeProps = {
354
+ /** Horizontal position of the element in content (unscaled) space. */
355
+ x: number;
356
+ /** Vertical position of the element in content (unscaled) space. */
357
+ y: number;
358
+ /** Width of the element in content (unscaled) space. */
359
+ width: number;
360
+ /** Height of the element in content (unscaled) space. */
361
+ height: number;
362
+ /**
363
+ * Extra pixels around the viewport that still count as "visible".
364
+ * Positive values mount children before they scroll into view,
365
+ * acting like `IntersectionObserver`'s `rootMargin`.
366
+ * @default 0
367
+ */
368
+ margin?: number;
369
+ /**
370
+ * Fraction of the element area (0-1) that must overlap the (expanded)
371
+ * viewport for the children to be mounted.
372
+ *
373
+ * - `0` — any overlap is enough (default).
374
+ * - `1` — the element must be fully inside the viewport.
375
+ * @default 0
376
+ */
377
+ threshold?: number;
378
+ /**
379
+ * Content rendered in place of children when the element is outside
380
+ * the viewport. Defaults to `null` (nothing rendered).
381
+ */
382
+ placeholder?: React.ReactNode;
383
+ /** Fires when the element enters the viewport and children are mounted. */
384
+ onShow?: () => void;
385
+ /** Fires when the element leaves the viewport and children are unmounted. */
386
+ onHide?: () => void;
387
+ children: React.ReactNode;
388
+ className?: string;
389
+ style?: React.CSSProperties;
390
+ };
391
+ declare const Virtualize: React.ForwardRefExoticComponent<VirtualizeProps & React.RefAttributes<HTMLDivElement>>;
392
+
393
+ /** Drop binary float noise on scale (e.g. 1.5000000000000002) without clipping real zoom values. */
394
+ declare const roundScaleForTransform: (scale: number) => number;
301
395
  declare const getTransformStyles: (x: number, y: number, scale: number) => string;
302
396
  declare const getMatrixTransformStyles: (x: number, y: number, scale: number) => string;
303
397
  declare const getCenterPosition: (scale: number, wrapperComponent: HTMLDivElement, contentComponent: HTMLDivElement) => StateType;
304
398
 
305
- export { AnimationType, BoundsType, Context, KeepScale, LibrarySetup, MiniMap, MiniMapProps, PositionType, ReactZoomPanPinchBaseClasses, ReactZoomPanPinchComponentHelpers, ReactZoomPanPinchContentRef, ReactZoomPanPinchContext, ReactZoomPanPinchContextState, ReactZoomPanPinchHandlers, ReactZoomPanPinchProps, ReactZoomPanPinchRef, ReactZoomPanPinchRefProps, ReactZoomPanPinchState, SizeType, StateType, TransformComponent, TransformWrapper, VelocityType, getCenterPosition, getMatrixTransformStyles, getTransformStyles, useControls, useTransformComponent, useTransformContext, useTransformEffect, useTransformInit };
399
+ export { type AnimationType, type BoundsType, Context, DeviceType, KeepScale, type LibrarySetup, MiniMap, type MiniMapProps, type PositionType, type ReactZoomPanPinchBaseClasses, type ReactZoomPanPinchComponentHelpers, type ReactZoomPanPinchContentRef, type ReactZoomPanPinchContext, type ReactZoomPanPinchContextState, type ReactZoomPanPinchHandlers, type ReactZoomPanPinchProps, type ReactZoomPanPinchRef, type ReactZoomPanPinchRefProps, type ReactZoomPanPinchState, type SizeType, type StateType, TransformComponent, TransformWrapper, type VelocityType, Virtualize, type VirtualizeProps, getCenterPosition, getMatrixTransformStyles, getTransformStyles, roundScaleForTransform, useControls, useTransformComponent, useTransformContext, useTransformEffect, useTransformInit };