react-zoom-pan-pinch 2.2.1 → 2.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.
Files changed (81) hide show
  1. package/dist/components/transform-component.d.ts +10 -0
  2. package/dist/components/transform-wrapper.d.ts +8 -0
  3. package/dist/constants/state.constants.d.ts +6 -0
  4. package/dist/core/animations/animations.constants.d.ts +16 -0
  5. package/dist/core/animations/animations.utils.d.ts +4 -0
  6. package/dist/core/bounds/bounds.types.d.ts +8 -0
  7. package/dist/core/bounds/bounds.utils.d.ts +15 -0
  8. package/dist/core/double-click/double-click.logic.d.ts +3 -0
  9. package/dist/core/handlers/handlers.logic.d.ts +8 -0
  10. package/dist/core/handlers/handlers.utils.d.ts +13 -0
  11. package/dist/core/instance.core.d.ts +68 -0
  12. package/dist/core/pan/panning.logic.d.ts +5 -0
  13. package/dist/core/pan/panning.utils.d.ts +10 -0
  14. package/dist/core/pan/velocity.logic.d.ts +5 -0
  15. package/dist/core/pan/velocity.utils.d.ts +5 -0
  16. package/dist/core/pinch/pinch.logic.d.ts +4 -0
  17. package/dist/core/pinch/pinch.utils.d.ts +6 -0
  18. package/dist/core/wheel/wheel.logic.d.ts +4 -0
  19. package/dist/core/wheel/wheel.utils.d.ts +7 -0
  20. package/dist/core/zoom/zoom.logic.d.ts +3 -0
  21. package/dist/core/zoom/zoom.utils.d.ts +3 -0
  22. package/dist/hooks/use-transform-context.d.ts +3 -0
  23. package/dist/index.cjs.js +270 -267
  24. package/dist/index.cjs.js.map +1 -1
  25. package/dist/index.d.ts +63 -57
  26. package/dist/index.esm.js +270 -267
  27. package/dist/index.esm.js.map +1 -1
  28. package/dist/models/calculations.model.d.ts +25 -0
  29. package/dist/models/context.model.d.ts +117 -0
  30. package/dist/models/helpers.model.d.ts +5 -0
  31. package/dist/models/index.d.ts +3 -0
  32. package/dist/stories/docs/handlers.d.ts +12 -0
  33. package/dist/stories/docs/props.d.ts +14 -0
  34. package/dist/stories/examples/content-rerendering/example.d.ts +2 -0
  35. package/dist/stories/examples/stress/example.d.ts +2 -0
  36. package/dist/stories/examples/zoom-to-element/example.d.ts +2 -0
  37. package/dist/stories/types/args.types.d.ts +268 -0
  38. package/dist/stories/utils/args.utils.d.ts +3 -0
  39. package/dist/stories/utils/code.utils.d.ts +6 -0
  40. package/dist/stories/utils/controls.utils.d.ts +3 -0
  41. package/dist/stories/utils/index.d.ts +3 -0
  42. package/dist/utils/calculations.utils.d.ts +11 -0
  43. package/dist/utils/callback.utils.d.ts +2 -0
  44. package/dist/utils/context.utils.d.ts +2 -0
  45. package/dist/utils/event.utils.d.ts +1 -0
  46. package/dist/utils/helpers.utils.d.ts +2 -0
  47. package/dist/utils/index.d.ts +7 -0
  48. package/dist/utils/state.utils.d.ts +3 -0
  49. package/dist/utils/styles.utils.d.ts +3 -0
  50. package/package.json +30 -25
  51. package/src/.eslintrc.json +90 -0
  52. package/src/components/transform-component.tsx +2 -1
  53. package/src/components/transform-wrapper.tsx +27 -5
  54. package/src/constants/state.constants.ts +0 -2
  55. package/src/core/animations/animations.constants.ts +14 -12
  56. package/src/core/animations/animations.utils.ts +15 -11
  57. package/src/core/bounds/bounds.utils.ts +20 -20
  58. package/src/core/double-click/double-click.logic.ts +6 -12
  59. package/src/core/handlers/handlers.logic.ts +98 -89
  60. package/src/core/handlers/handlers.utils.ts +22 -24
  61. package/src/{components/transform-context.tsx → core/instance.core.ts} +85 -118
  62. package/src/core/pan/panning.logic.ts +20 -19
  63. package/src/core/pan/panning.utils.ts +4 -7
  64. package/src/core/pan/velocity.logic.ts +1 -0
  65. package/src/core/pinch/pinch.logic.ts +4 -6
  66. package/src/core/pinch/pinch.utils.ts +2 -2
  67. package/src/core/wheel/wheel.logic.ts +1 -0
  68. package/src/core/wheel/wheel.utils.ts +12 -11
  69. package/src/core/zoom/zoom.logic.ts +29 -29
  70. package/src/core/zoom/zoom.utils.ts +2 -2
  71. package/src/hooks/use-transform-context.tsx +3 -2
  72. package/src/models/context.model.ts +6 -2
  73. package/src/stories/docs/handlers.tsx +30 -30
  74. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +3 -0
  75. package/src/stories/examples/stress/example.tsx +24 -0
  76. package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
  77. package/src/stories/examples/zoom-to-element/example.tsx +4 -0
  78. package/src/stories/utils/code.utils.tsx +1 -0
  79. package/src/stories/utils/controls.utils.tsx +21 -4
  80. package/src/stories/utils/styles.module.css +5 -0
  81. package/src/.eslintrc.js +0 -36
@@ -1,16 +1,12 @@
1
- import React, { Component } from "react";
2
-
3
1
  import {
4
2
  BoundsType,
5
3
  LibrarySetup,
6
4
  PositionType,
7
5
  VelocityType,
8
6
  AnimationType,
9
- ReactZoomPanPinchRef,
10
7
  ReactZoomPanPinchProps,
11
8
  ReactZoomPanPinchState,
12
9
  } from "../models";
13
-
14
10
  import {
15
11
  getContext,
16
12
  createSetup,
@@ -20,52 +16,39 @@ import {
20
16
  makePassiveEventOption,
21
17
  getCenterPosition,
22
18
  } from "../utils";
23
-
24
- import { contextInitialState } from "../constants/state.constants";
25
-
26
- import { handleCancelAnimation } from "../core/animations/animations.utils";
27
- import { isWheelAllowed } from "../core/wheel/wheel.utils";
28
- import { isPinchAllowed, isPinchStartAllowed } from "../core/pinch/pinch.utils";
29
- import { handleCalculateBounds } from "../core/bounds/bounds.utils";
30
-
19
+ import { handleCancelAnimation } from "./animations/animations.utils";
20
+ import { isWheelAllowed } from "./wheel/wheel.utils";
21
+ import { isPinchAllowed, isPinchStartAllowed } from "./pinch/pinch.utils";
22
+ import { handleCalculateBounds } from "./bounds/bounds.utils";
31
23
  import {
32
24
  handleWheelStart,
33
25
  handleWheelZoom,
34
26
  handleWheelStop,
35
- } from "../core/wheel/wheel.logic";
36
- import {
37
- isPanningAllowed,
38
- isPanningStartAllowed,
39
- } from "../core/pan/panning.utils";
27
+ } from "./wheel/wheel.logic";
28
+ import { isPanningAllowed, isPanningStartAllowed } from "./pan/panning.utils";
40
29
  import {
41
30
  handlePanning,
42
31
  handlePanningEnd,
43
32
  handlePanningStart,
44
- } from "../core/pan/panning.logic";
33
+ } from "./pan/panning.logic";
45
34
  import {
46
35
  handlePinchStart,
47
36
  handlePinchStop,
48
37
  handlePinchZoom,
49
- } from "../core/pinch/pinch.logic";
38
+ } from "./pinch/pinch.logic";
50
39
  import {
51
40
  handleDoubleClick,
52
41
  isDoubleClickAllowed,
53
- } from "../core/double-click/double-click.logic";
42
+ } from "./double-click/double-click.logic";
54
43
 
55
44
  type StartCoordsType = { x: number; y: number } | null;
56
45
 
57
- const Context = React.createContext(contextInitialState);
58
-
59
- class TransformContext extends Component<
60
- Omit<ReactZoomPanPinchProps, "ref"> & {
61
- setRef: (context: ReactZoomPanPinchRef) => void;
62
- }
63
- > {
46
+ export class ZoomPanPinch {
64
47
  public mounted = true;
65
48
 
66
- public transformState: ReactZoomPanPinchState = createState(this.props);
67
-
68
- public setup: LibrarySetup = createSetup(this.props);
49
+ public transformState: ReactZoomPanPinchState;
50
+ public setup: LibrarySetup;
51
+ public observer?: ResizeObserver;
69
52
 
70
53
  // Components
71
54
  public wrapperComponent: HTMLDivElement | null = null;
@@ -98,20 +81,23 @@ class TransformContext extends Component<
98
81
  // key press
99
82
  public pressedKeys: { [key: string]: boolean } = {};
100
83
 
101
- componentDidMount(): void {
102
- this.initializeWindowEvents();
84
+ constructor(public props: ReactZoomPanPinchProps) {
85
+ this.setup = createSetup(this.props);
86
+ this.transformState = createState(this.props);
103
87
  }
104
88
 
105
- componentWillUnmount(): void {
89
+ mount = () => {
90
+ this.initializeWindowEvents();
91
+ };
92
+
93
+ unmount = () => {
106
94
  this.cleanupWindowEvents();
107
- }
95
+ };
108
96
 
109
- componentDidUpdate(oldProps: ReactZoomPanPinchProps): void {
110
- if (oldProps !== this.props) {
111
- handleCalculateBounds(this, this.transformState.scale);
112
- this.setup = createSetup(this.props);
113
- }
114
- }
97
+ update = (newProps: ReactZoomPanPinchProps) => {
98
+ handleCalculateBounds(this, this.transformState.scale);
99
+ this.setup = createSetup(newProps);
100
+ };
115
101
 
116
102
  initializeWindowEvents = (): void => {
117
103
  const passive = makePassiveEventOption();
@@ -124,24 +110,31 @@ class TransformContext extends Component<
124
110
  currentDocument?.addEventListener("mouseleave", this.clearPanning, passive);
125
111
  currentWindow?.addEventListener("keyup", this.setKeyUnPressed, passive);
126
112
  currentWindow?.addEventListener("keydown", this.setKeyPressed, passive);
127
-
128
- this.handleRef();
129
- }
113
+ };
130
114
 
131
115
  cleanupWindowEvents = (): void => {
132
116
  const passive = makePassiveEventOption();
133
117
  const currentDocument = this.wrapperComponent?.ownerDocument;
134
118
  const currentWindow = currentDocument?.defaultView;
135
- currentWindow?.removeEventListener("mousedown", this.onPanningStart, passive);
119
+ currentWindow?.removeEventListener(
120
+ "mousedown",
121
+ this.onPanningStart,
122
+ passive,
123
+ );
136
124
  currentWindow?.removeEventListener("mousemove", this.onPanning, passive);
137
125
  currentWindow?.removeEventListener("mouseup", this.onPanningStop, passive);
138
- currentDocument?.removeEventListener("mouseleave", this.clearPanning, passive);
126
+ currentDocument?.removeEventListener(
127
+ "mouseleave",
128
+ this.clearPanning,
129
+ passive,
130
+ );
139
131
  currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
140
132
  currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
141
133
  document.removeEventListener("mouseleave", this.clearPanning, passive);
142
134
 
143
135
  handleCancelAnimation(this);
144
- }
136
+ this.observer?.disconnect();
137
+ };
145
138
 
146
139
  handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
147
140
  // Zooming events on wrapper
@@ -154,36 +147,25 @@ class TransformContext extends Component<
154
147
  wrapper.addEventListener("touchend", this.onTouchPanningStop, passive);
155
148
  };
156
149
 
157
- handleInitialize = (): void => {
150
+ handleInitialize = (contentComponent: HTMLDivElement): void => {
158
151
  const { centerOnInit } = this.setup;
159
-
160
152
  this.applyTransformation();
161
- this.forceUpdate();
162
153
 
163
154
  if (centerOnInit) {
164
- // this has to be redone once the right solution is found
165
- // problem is - we need to execute it after mounted component specify it's height / width, images are fetched async so it's tricky
166
- setTimeout(() => {
167
- if (this.mounted) {
168
- this.setCenter();
169
- }
170
- }, 50);
171
- setTimeout(() => {
172
- if (this.mounted) {
173
- this.setCenter();
174
- }
175
- }, 100);
176
- setTimeout(() => {
177
- if (this.mounted) {
178
- this.setCenter();
179
- }
180
- }, 200);
155
+ this.setCenter();
156
+ this.observer = new ResizeObserver(() => {
157
+ this.setCenter();
158
+ this.observer?.disconnect();
159
+ });
160
+
161
+ // Start observing the target node for configured mutations
162
+ this.observer.observe(contentComponent);
181
163
  }
182
164
  };
183
165
 
184
- //////////
166
+ /// ///////
185
167
  // Zoom
186
- //////////
168
+ /// ///////
187
169
 
188
170
  onWheelZoom = (event: WheelEvent): void => {
189
171
  const { disabled } = this.setup;
@@ -200,9 +182,9 @@ class TransformContext extends Component<
200
182
  handleWheelStop(this, event);
201
183
  };
202
184
 
203
- //////////
185
+ /// ///////
204
186
  // Pan
205
- //////////
187
+ /// ///////
206
188
 
207
189
  onPanningStart = (event: MouseEvent): void => {
208
190
  const { disabled } = this.setup;
@@ -251,9 +233,9 @@ class TransformContext extends Component<
251
233
  }
252
234
  };
253
235
 
254
- //////////
236
+ /// ///////
255
237
  // Pinch
256
- //////////
238
+ /// ///////
257
239
 
258
240
  onPinchStart = (event: TouchEvent): void => {
259
241
  const { disabled } = this.setup;
@@ -297,9 +279,9 @@ class TransformContext extends Component<
297
279
  }
298
280
  };
299
281
 
300
- //////////
282
+ /// ///////
301
283
  // Touch
302
- //////////
284
+ /// ///////
303
285
 
304
286
  onTouchPanningStart = (event: TouchEvent): void => {
305
287
  const { disabled } = this.setup;
@@ -362,9 +344,9 @@ class TransformContext extends Component<
362
344
  this.onPinchStop(event);
363
345
  };
364
346
 
365
- //////////
347
+ /// ///////
366
348
  // Double Click
367
- //////////
349
+ /// ///////
368
350
 
369
351
  onDoubleClick = (event: MouseEvent | TouchEvent): void => {
370
352
  const { disabled } = this.setup;
@@ -376,9 +358,9 @@ class TransformContext extends Component<
376
358
  handleDoubleClick(this, event);
377
359
  };
378
360
 
379
- //////////
361
+ /// ///////
380
362
  // Helpers
381
- //////////
363
+ /// ///////
382
364
 
383
365
  clearPanning = (event: MouseEvent): void => {
384
366
  if (this.isPanning) {
@@ -401,22 +383,6 @@ class TransformContext extends Component<
401
383
  return Boolean(keys.find((key) => this.pressedKeys[key]));
402
384
  };
403
385
 
404
- setComponents = (
405
- wrapperComponent: HTMLDivElement,
406
- contentComponent: HTMLDivElement,
407
- ): void => {
408
- this.cleanupWindowEvents();
409
- this.wrapperComponent = wrapperComponent;
410
- this.contentComponent = contentComponent;
411
- handleCalculateBounds(this, this.transformState.scale);
412
- this.handleInitializeWrapperEvents(wrapperComponent);
413
- this.initializeWindowEvents();
414
- this.handleInitialize();
415
- this.handleRef();
416
- this.isInitialized = true;
417
- handleCallback(getContext(this), undefined, this.props.onInit);
418
- };
419
-
420
386
  setTransformState = (
421
387
  scale: number,
422
388
  positionX: number,
@@ -424,7 +390,11 @@ class TransformContext extends Component<
424
390
  ): void => {
425
391
  const { onTransformed } = this.props;
426
392
 
427
- if (!isNaN(scale) && !isNaN(positionX) && !isNaN(positionY)) {
393
+ if (
394
+ !Number.isNaN(scale) &&
395
+ !Number.isNaN(positionX) &&
396
+ !Number.isNaN(positionY)
397
+ ) {
428
398
  if (scale !== this.transformState.scale) {
429
399
  this.transformState.previousScale = this.transformState.scale;
430
400
  this.transformState.scale = scale;
@@ -463,31 +433,28 @@ class TransformContext extends Component<
463
433
  const { scale, positionX, positionY } = this.transformState;
464
434
  const transform = getTransformStyles(positionX, positionY, scale);
465
435
  this.contentComponent.style.transform = transform;
466
-
467
- this.handleRef();
468
436
  };
469
437
 
470
- handleRef = (): void => {
471
- this.props.setRef(getContext(this));
438
+ getContext = () => {
439
+ return getContext(this);
472
440
  };
473
441
 
474
- render(): JSX.Element {
475
- const value = getContext(this);
476
- const { children } = this.props;
477
- const content = typeof children === "function" ? children(value) : children;
478
-
479
- return (
480
- <Context.Provider
481
- value={{
482
- ...this.transformState,
483
- setComponents: this.setComponents,
484
- contextInstance: this,
485
- }}
486
- >
487
- {content}
488
- </Context.Provider>
489
- );
490
- }
491
- }
442
+ /**
443
+ * Initialization
444
+ */
492
445
 
493
- export { Context, TransformContext };
446
+ init = (
447
+ wrapperComponent: HTMLDivElement,
448
+ contentComponent: HTMLDivElement,
449
+ ): void => {
450
+ this.cleanupWindowEvents();
451
+ this.wrapperComponent = wrapperComponent;
452
+ this.contentComponent = contentComponent;
453
+ handleCalculateBounds(this, this.transformState.scale);
454
+ this.handleInitializeWrapperEvents(wrapperComponent);
455
+ this.handleInitialize(contentComponent);
456
+ this.initializeWindowEvents();
457
+ this.isInitialized = true;
458
+ handleCallback(getContext(this), undefined, this.props.onInit);
459
+ };
460
+ }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { ReactZoomPanPinchContext } from "../../models/context.model";
2
3
  import { animate, handleCancelAnimation } from "../animations/animations.utils";
3
4
  import { handleCalculateBounds } from "../bounds/bounds.utils";
@@ -29,6 +30,25 @@ export function handlePanningStart(
29
30
  }
30
31
  }
31
32
 
33
+ export function handleAlignToBounds(
34
+ contextInstance: ReactZoomPanPinchContext,
35
+ ): void {
36
+ const { scale } = contextInstance.transformState;
37
+ const { minScale, alignmentAnimation } = contextInstance.setup;
38
+ const { disabled, sizeX, sizeY, animationTime, animationType } =
39
+ alignmentAnimation;
40
+
41
+ const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
42
+
43
+ if (isDisabled) return;
44
+
45
+ const targetState = handlePanToBounds(contextInstance);
46
+
47
+ if (targetState) {
48
+ animate(contextInstance, targetState, animationTime, animationType);
49
+ }
50
+ }
51
+
32
52
  export function handlePanning(
33
53
  contextInstance: ReactZoomPanPinchContext,
34
54
  clientX: number,
@@ -78,22 +98,3 @@ export function handlePanningEnd(
78
98
  }
79
99
  }
80
100
  }
81
-
82
- export function handleAlignToBounds(
83
- contextInstance: ReactZoomPanPinchContext,
84
- ): void {
85
- const { scale } = contextInstance.transformState;
86
- const { minScale, alignmentAnimation } = contextInstance.setup;
87
- const { disabled, sizeX, sizeY, animationTime, animationType } =
88
- alignmentAnimation;
89
-
90
- const isDisabled = disabled || scale < minScale || (!sizeX && !sizeY);
91
-
92
- if (isDisabled) return;
93
-
94
- const targetState = handlePanToBounds(contextInstance);
95
-
96
- if (targetState) {
97
- animate(contextInstance, targetState, animationTime, animationType);
98
- }
99
- }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import {
2
3
  PositionType,
3
4
  ReactZoomPanPinchContext,
@@ -59,7 +60,7 @@ export const handleTouchPanningSetup = (
59
60
  contextInstance: ReactZoomPanPinchContext,
60
61
  event: TouchEvent,
61
62
  ): void => {
62
- const touches = event.touches;
63
+ const { touches } = event;
63
64
  const { positionX, positionY } = contextInstance.transformState;
64
65
 
65
66
  contextInstance.isPanning = true;
@@ -81,12 +82,8 @@ export function handlePanToBounds(
81
82
 
82
83
  if (disabled || !wrapperComponent || !contextInstance.bounds) return;
83
84
 
84
- const {
85
- maxPositionX,
86
- minPositionX,
87
- maxPositionY,
88
- minPositionY,
89
- } = contextInstance.bounds;
85
+ const { maxPositionX, minPositionX, maxPositionY, minPositionY } =
86
+ contextInstance.bounds;
90
87
 
91
88
  const xChanged = positionX > maxPositionX || positionX < minPositionX;
92
89
  const yChanged = positionY > maxPositionY || positionY < minPositionY;
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { PositionType } from "../../models";
2
3
  import { ReactZoomPanPinchContext } from "../../models/context.model";
3
4
  import { animations } from "../animations/animations.constants";
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { ReactZoomPanPinchContext } from "../../models";
2
3
  import { handleCancelAnimation } from "../animations/animations.utils";
3
4
  import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
@@ -29,11 +30,8 @@ export const handlePinchZoom = (
29
30
  ): void => {
30
31
  const { contentComponent, pinchStartDistance } = contextInstance;
31
32
  const { scale } = contextInstance.transformState;
32
- const {
33
- limitToBounds,
34
- centerZoomedOut,
35
- zoomAnimation,
36
- } = contextInstance.setup;
33
+ const { limitToBounds, centerZoomedOut, zoomAnimation } =
34
+ contextInstance.setup;
37
35
  const { disabled, size } = zoomAnimation;
38
36
 
39
37
  // if one finger starts from outside of wrapper
@@ -42,7 +40,7 @@ export const handlePinchZoom = (
42
40
  const midPoint = calculateTouchMidPoint(event, scale, contentComponent);
43
41
 
44
42
  // if touches goes off of the wrapper element
45
- if (!isFinite(midPoint.x) || !isFinite(midPoint.y)) return;
43
+ if (!Number.isFinite(midPoint.x) || !Number.isFinite(midPoint.y)) return;
46
44
 
47
45
  const currentDistance = getTouchDistance(event);
48
46
  const newScale = calculatePinchZoom(contextInstance, currentDistance);
@@ -54,8 +54,8 @@ export const calculateTouchMidPoint = (
54
54
 
55
55
  export const getTouchDistance = (event: TouchEvent): number => {
56
56
  return Math.sqrt(
57
- Math.pow(event.touches[0].pageX - event.touches[1].pageX, 2) +
58
- Math.pow(event.touches[0].pageY - event.touches[1].pageY, 2),
57
+ (event.touches[0].pageX - event.touches[1].pageX) ** 2 +
58
+ (event.touches[0].pageY - event.touches[1].pageY) ** 2,
59
59
  );
60
60
  };
61
61
 
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { ReactZoomPanPinchContext } from "../../models";
2
3
  import { handleCallback } from "../../utils/callback.utils";
3
4
  import { getContext } from "../../utils/context.utils";
@@ -1,6 +1,4 @@
1
- import { ReactZoomPanPinchContext } from "../../models";
2
- import { PositionType } from "../../models";
3
-
1
+ import { ReactZoomPanPinchContext, PositionType } from "../../models";
4
2
  import { checkIsNumber, isExcludedNode, roundNumber } from "../../utils";
5
3
  import { checkZoomBounds } from "../zoom/zoom.utils";
6
4
 
@@ -8,12 +6,8 @@ export const isWheelAllowed = (
8
6
  contextInstance: ReactZoomPanPinchContext,
9
7
  event: WheelEvent,
10
8
  ): boolean => {
11
- const {
12
- disabled,
13
- wheelDisabled,
14
- touchPadDisabled,
15
- excluded,
16
- } = contextInstance.setup.wheel;
9
+ const { disabled, wheelDisabled, touchPadDisabled, excluded } =
10
+ contextInstance.setup.wheel;
17
11
  const { isInitialized, isPanning } = contextInstance;
18
12
 
19
13
  const target = event.target as HTMLElement;
@@ -31,11 +25,18 @@ export const isWheelAllowed = (
31
25
  return true;
32
26
  };
33
27
 
28
+ export const getDeltaY = (event?: WheelEvent) => {
29
+ if (event) {
30
+ return event.deltaY < 0 ? 1 : -1;
31
+ }
32
+ return 0;
33
+ };
34
+
34
35
  export function getDelta(
35
36
  event: WheelEvent,
36
37
  customDelta?: number | null,
37
38
  ): number {
38
- const deltaY = event ? (event.deltaY < 0 ? 1 : -1) : 0;
39
+ const deltaY = getDeltaY(event);
39
40
  const delta = checkIsNumber(customDelta, deltaY);
40
41
  return delta;
41
42
  }
@@ -60,7 +61,7 @@ export function getMousePosition(
60
61
  mouseY = (touch.clientY - contentRect.top) / scale;
61
62
  }
62
63
 
63
- if (isNaN(mouseX) || isNaN(mouseY))
64
+ if (Number.isNaN(mouseX) || Number.isNaN(mouseY))
64
65
  console.error("No mouse or touch offset found");
65
66
 
66
67
  return {
@@ -5,6 +5,35 @@ import { handleCalculateBounds } from "../bounds/bounds.utils";
5
5
  import { handleAlignToBounds } from "../pan/panning.logic";
6
6
  import { checkZoomBounds, handleCalculateZoomPositions } from "./zoom.utils";
7
7
 
8
+ export function handleZoomToPoint(
9
+ contextInstance: ReactZoomPanPinchContext,
10
+ scale: number,
11
+ mouseX: number,
12
+ mouseY: number,
13
+ ): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
14
+ const { minScale, maxScale, limitToBounds } = contextInstance.setup;
15
+
16
+ const newScale = checkZoomBounds(
17
+ roundNumber(scale, 2),
18
+ minScale,
19
+ maxScale,
20
+ 0,
21
+ false,
22
+ );
23
+ const bounds = handleCalculateBounds(contextInstance, newScale);
24
+
25
+ const { x, y } = handleCalculateZoomPositions(
26
+ contextInstance,
27
+ mouseX,
28
+ mouseY,
29
+ newScale,
30
+ bounds,
31
+ limitToBounds,
32
+ );
33
+
34
+ return { scale: newScale, positionX: x, positionY: y };
35
+ }
36
+
8
37
  export function handleAlignToScaleBounds(
9
38
  contextInstance: ReactZoomPanPinchContext,
10
39
  mousePositionX?: number,
@@ -38,32 +67,3 @@ export function handleAlignToScaleBounds(
38
67
  animate(contextInstance, targetState, animationTime, animationType);
39
68
  }
40
69
  }
41
-
42
- export function handleZoomToPoint(
43
- contextInstance: ReactZoomPanPinchContext,
44
- scale: number,
45
- mouseX: number,
46
- mouseY: number,
47
- ): Omit<ReactZoomPanPinchState, "previousScale"> | undefined {
48
- const { minScale, maxScale, limitToBounds } = contextInstance.setup;
49
-
50
- const newScale = checkZoomBounds(
51
- roundNumber(scale, 2),
52
- minScale,
53
- maxScale,
54
- 0,
55
- false,
56
- );
57
- const bounds = handleCalculateBounds(contextInstance, newScale);
58
-
59
- const { x, y } = handleCalculateZoomPositions(
60
- contextInstance,
61
- mouseX,
62
- mouseY,
63
- newScale,
64
- bounds,
65
- limitToBounds,
66
- );
67
-
68
- return { scale: newScale, positionX: x, positionY: y };
69
- }
@@ -51,8 +51,8 @@ export function checkZoomBounds(
51
51
  const scalePadding = enablePadding ? zoomPadding : 0;
52
52
  const minScaleWithPadding = minScale - scalePadding;
53
53
 
54
- if (!isNaN(maxScale) && zoom >= maxScale) return maxScale;
55
- if (!isNaN(minScale) && zoom <= minScaleWithPadding)
54
+ if (!Number.isNaN(maxScale) && zoom >= maxScale) return maxScale;
55
+ if (!Number.isNaN(minScale) && zoom <= minScaleWithPadding)
56
56
  return minScaleWithPadding;
57
57
  return zoom;
58
58
  }
@@ -1,6 +1,7 @@
1
1
  import { useContext } from "react";
2
- import { getContext } from "utils";
3
- import { Context } from "../components/transform-context";
2
+
3
+ import { getContext } from "../utils";
4
+ import { Context } from "../components/transform-wrapper";
4
5
  import { ReactZoomPanPinchRef } from "../models";
5
6
 
6
7
  const useTransformContext = (): ReactZoomPanPinchRef => {
@@ -1,6 +1,5 @@
1
1
  import React from "react";
2
2
 
3
- import { TransformContext } from "../components/transform-context";
4
3
  import { animations } from "../core/animations/animations.constants";
5
4
  import { DeepNonNullable } from "./helpers.model";
6
5
  import {
@@ -11,8 +10,9 @@ import {
11
10
  setTransform,
12
11
  zoomOut,
13
12
  } from "../core/handlers/handlers.logic";
13
+ import { ZoomPanPinch } from "../core/instance.core";
14
14
 
15
- export type ReactZoomPanPinchContext = typeof TransformContext.prototype;
15
+ export type ReactZoomPanPinchContext = typeof ZoomPanPinch.prototype;
16
16
 
17
17
  export type ReactZoomPanPinchRef = {
18
18
  instance: ReactZoomPanPinchContext;
@@ -35,6 +35,10 @@ export type ReactZoomPanPinchHandlers = {
35
35
  zoomToElement: ReturnType<typeof zoomToElement>;
36
36
  };
37
37
 
38
+ export type ReactZoomPanPinchRefProps = {
39
+ setRef: (context: ReactZoomPanPinchRef) => void;
40
+ } & Omit<ReactZoomPanPinchProps, "ref">;
41
+
38
42
  export type ReactZoomPanPinchProps = {
39
43
  children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
40
44
  ref?: React.Ref<ReactZoomPanPinchRef>;