react-zoom-pan-pinch 2.2.1 → 2.3.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 (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 +273 -267
  24. package/dist/index.cjs.js.map +1 -1
  25. package/dist/index.d.ts +63 -57
  26. package/dist/index.esm.js +273 -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 +31 -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} +87 -117
  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,5 +1,5 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { animations } from "./animations.constants";
2
-
3
3
  import {
4
4
  AnimationType,
5
5
  ReactZoomPanPinchContext,
@@ -60,6 +60,20 @@ export function handleSetupAnimation(
60
60
  requestAnimationFrame(contextInstance.animation);
61
61
  }
62
62
 
63
+ function isValidTargetState(targetState: StateType): boolean {
64
+ const { scale, positionX, positionY } = targetState;
65
+
66
+ if (
67
+ Number.isNaN(scale) ||
68
+ Number.isNaN(positionX) ||
69
+ Number.isNaN(positionY)
70
+ ) {
71
+ return false;
72
+ }
73
+
74
+ return true;
75
+ }
76
+
63
77
  export function animate(
64
78
  contextInstance: ReactZoomPanPinchContext,
65
79
  targetState: StateType,
@@ -97,13 +111,3 @@ export function animate(
97
111
  );
98
112
  }
99
113
  }
100
-
101
- function isValidTargetState(targetState: StateType): boolean {
102
- const { scale, positionX, positionY } = targetState;
103
-
104
- if (isNaN(scale) || isNaN(positionX) || isNaN(positionY)) {
105
- return false;
106
- }
107
-
108
- return true;
109
- }
@@ -1,5 +1,5 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  import { roundNumber } from "../../utils";
2
-
3
3
  import {
4
4
  BoundsType,
5
5
  PositionType,
@@ -91,6 +91,25 @@ export const calculateBounds = (
91
91
  return bounds;
92
92
  };
93
93
 
94
+ /**
95
+ * Keeps value between given bounds, used for limiting view to given boundaries
96
+ * 1# eg. boundLimiter(2, 0, 3, true) => 2
97
+ * 2# eg. boundLimiter(4, 0, 3, true) => 3
98
+ * 3# eg. boundLimiter(-2, 0, 3, true) => 0
99
+ * 4# eg. boundLimiter(10, 0, 3, false) => 10
100
+ */
101
+ export const boundLimiter = (
102
+ value: number,
103
+ minBound: number,
104
+ maxBound: number,
105
+ isActive: boolean,
106
+ ): number => {
107
+ if (!isActive) return roundNumber(value, 2);
108
+ if (value < minBound) return roundNumber(minBound, 2);
109
+ if (value > maxBound) return roundNumber(maxBound, 2);
110
+ return roundNumber(value, 2);
111
+ };
112
+
94
113
  export const handleCalculateBounds = (
95
114
  contextInstance: ReactZoomPanPinchContext,
96
115
  newScale: number,
@@ -136,22 +155,3 @@ export function getMouseBoundedPosition(
136
155
  );
137
156
  return { x, y };
138
157
  }
139
-
140
- /**
141
- * Keeps value between given bounds, used for limiting view to given boundaries
142
- * 1# eg. boundLimiter(2, 0, 3, true) => 2
143
- * 2# eg. boundLimiter(4, 0, 3, true) => 3
144
- * 3# eg. boundLimiter(-2, 0, 3, true) => 0
145
- * 4# eg. boundLimiter(10, 0, 3, false) => 10
146
- */
147
- export const boundLimiter = (
148
- value: number,
149
- minBound: number,
150
- maxBound: number,
151
- isActive: boolean,
152
- ): number => {
153
- if (!isActive) return roundNumber(value, 2);
154
- if (value < minBound) return roundNumber(minBound, 2);
155
- if (value > maxBound) return roundNumber(maxBound, 2);
156
- return roundNumber(value, 2);
157
- };
@@ -1,25 +1,19 @@
1
1
  import { ReactZoomPanPinchContext } from "../../models";
2
-
3
2
  import { animate } from "../animations/animations.utils";
4
- import { getMousePosition } from "core/wheel/wheel.utils";
5
- import { handleZoomToPoint } from "core/zoom/zoom.logic";
6
- import { isExcludedNode } from "utils";
3
+ import { getMousePosition } from "../wheel/wheel.utils";
4
+ import { handleZoomToPoint } from "../zoom/zoom.logic";
5
+ import { isExcludedNode } from "../../utils";
7
6
  import {
8
7
  handleCalculateButtonZoom,
9
8
  resetTransformations,
10
- } from "core/handlers/handlers.utils";
9
+ } from "../handlers/handlers.utils";
11
10
 
12
11
  export function handleDoubleClick(
13
12
  contextInstance: ReactZoomPanPinchContext,
14
13
  event: MouseEvent | TouchEvent,
15
14
  ): void {
16
- const {
17
- disabled,
18
- mode,
19
- step,
20
- animationTime,
21
- animationType,
22
- } = contextInstance.setup.doubleClick;
15
+ const { disabled, mode, step, animationTime, animationType } =
16
+ contextInstance.setup.doubleClick;
23
17
 
24
18
  if (disabled) return;
25
19
 
@@ -9,104 +9,113 @@ import { animations } from "../animations/animations.constants";
9
9
  import { animate, handleCancelAnimation } from "../animations/animations.utils";
10
10
  import { getCenterPosition } from "../../utils";
11
11
 
12
- export const zoomIn = (contextInstance: ReactZoomPanPinchContext) => (
13
- step = 0.5,
14
- animationTime = 300,
15
- animationType: keyof typeof animations = "easeOut",
16
- ): void => {
17
- handleZoomToViewCenter(
18
- contextInstance,
19
- 1,
20
- step,
21
- animationTime,
22
- animationType,
23
- );
24
- };
12
+ export const zoomIn =
13
+ (contextInstance: ReactZoomPanPinchContext) =>
14
+ (
15
+ step = 0.5,
16
+ animationTime = 300,
17
+ animationType: keyof typeof animations = "easeOut",
18
+ ): void => {
19
+ handleZoomToViewCenter(
20
+ contextInstance,
21
+ 1,
22
+ step,
23
+ animationTime,
24
+ animationType,
25
+ );
26
+ };
25
27
 
26
- export const zoomOut = (contextInstance: ReactZoomPanPinchContext) => (
27
- step = 0.5,
28
- animationTime = 300,
29
- animationType: keyof typeof animations = "easeOut",
30
- ): void => {
31
- handleZoomToViewCenter(
32
- contextInstance,
33
- -1,
34
- step,
35
- animationTime,
36
- animationType,
37
- );
38
- };
28
+ export const zoomOut =
29
+ (contextInstance: ReactZoomPanPinchContext) =>
30
+ (
31
+ step = 0.5,
32
+ animationTime = 300,
33
+ animationType: keyof typeof animations = "easeOut",
34
+ ): void => {
35
+ handleZoomToViewCenter(
36
+ contextInstance,
37
+ -1,
38
+ step,
39
+ animationTime,
40
+ animationType,
41
+ );
42
+ };
39
43
 
40
- export const setTransform = (contextInstance: ReactZoomPanPinchContext) => (
41
- newPositionX: number,
42
- newPositionY: number,
43
- newScale: number,
44
- animationTime = 300,
45
- animationType: keyof typeof animations = "easeOut",
46
- ): void => {
47
- const { positionX, positionY, scale } = contextInstance.transformState;
48
- const { wrapperComponent, contentComponent } = contextInstance;
49
- const { disabled } = contextInstance.setup;
44
+ export const setTransform =
45
+ (contextInstance: ReactZoomPanPinchContext) =>
46
+ (
47
+ newPositionX: number,
48
+ newPositionY: number,
49
+ newScale: number,
50
+ animationTime = 300,
51
+ animationType: keyof typeof animations = "easeOut",
52
+ ): void => {
53
+ const { positionX, positionY, scale } = contextInstance.transformState;
54
+ const { wrapperComponent, contentComponent } = contextInstance;
55
+ const { disabled } = contextInstance.setup;
50
56
 
51
- if (disabled || !wrapperComponent || !contentComponent) return;
57
+ if (disabled || !wrapperComponent || !contentComponent) return;
52
58
 
53
- const targetState = {
54
- positionX: isNaN(newPositionX) ? positionX : newPositionX,
55
- positionY: isNaN(newPositionY) ? positionY : newPositionY,
56
- scale: isNaN(newScale) ? scale : newScale,
57
- };
59
+ const targetState = {
60
+ positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
61
+ positionY: Number.isNaN(newPositionY) ? positionY : newPositionY,
62
+ scale: Number.isNaN(newScale) ? scale : newScale,
63
+ };
58
64
 
59
- animate(contextInstance, targetState, animationTime, animationType);
60
- };
65
+ animate(contextInstance, targetState, animationTime, animationType);
66
+ };
61
67
 
62
- export const resetTransform = (contextInstance: ReactZoomPanPinchContext) => (
63
- animationTime = 200,
64
- animationType: keyof typeof animations = "easeOut",
65
- ): void => {
66
- resetTransformations(contextInstance, animationTime, animationType);
67
- };
68
+ export const resetTransform =
69
+ (contextInstance: ReactZoomPanPinchContext) =>
70
+ (
71
+ animationTime = 200,
72
+ animationType: keyof typeof animations = "easeOut",
73
+ ): void => {
74
+ resetTransformations(contextInstance, animationTime, animationType);
75
+ };
68
76
 
69
- export const centerView = (contextInstance: ReactZoomPanPinchContext) => (
70
- scale?: number,
71
- animationTime = 200,
72
- animationType: keyof typeof animations = "easeOut",
73
- ): void => {
74
- const {
75
- transformState,
76
- wrapperComponent,
77
- contentComponent,
78
- } = contextInstance;
79
- if (wrapperComponent && contentComponent) {
80
- const targetState = getCenterPosition(
81
- scale || transformState.scale,
82
- wrapperComponent,
83
- contentComponent,
84
- );
77
+ export const centerView =
78
+ (contextInstance: ReactZoomPanPinchContext) =>
79
+ (
80
+ scale?: number,
81
+ animationTime = 200,
82
+ animationType: keyof typeof animations = "easeOut",
83
+ ): void => {
84
+ const { transformState, wrapperComponent, contentComponent } =
85
+ contextInstance;
86
+ if (wrapperComponent && contentComponent) {
87
+ const targetState = getCenterPosition(
88
+ scale || transformState.scale,
89
+ wrapperComponent,
90
+ contentComponent,
91
+ );
85
92
 
86
- animate(contextInstance, targetState, animationTime, animationType);
87
- }
88
- };
93
+ animate(contextInstance, targetState, animationTime, animationType);
94
+ }
95
+ };
89
96
 
90
- export const zoomToElement = (contextInstance: ReactZoomPanPinchContext) => (
91
- node: HTMLElement | string,
92
- scale?: number,
93
- animationTime = 600,
94
- animationType: keyof typeof animations = "easeOut",
95
- ): void => {
96
- handleCancelAnimation(contextInstance);
97
+ export const zoomToElement =
98
+ (contextInstance: ReactZoomPanPinchContext) =>
99
+ (
100
+ node: HTMLElement | string,
101
+ scale?: number,
102
+ animationTime = 600,
103
+ animationType: keyof typeof animations = "easeOut",
104
+ ): void => {
105
+ handleCancelAnimation(contextInstance);
97
106
 
98
- const { wrapperComponent } = contextInstance;
107
+ const { wrapperComponent } = contextInstance;
99
108
 
100
- const target: HTMLElement | null =
101
- typeof node === "string" ? document.getElementById(node) : node;
109
+ const target: HTMLElement | null =
110
+ typeof node === "string" ? document.getElementById(node) : node;
102
111
 
103
- if (
104
- wrapperComponent &&
105
- isValidZoomNode(target) &&
106
- target &&
107
- wrapperComponent.contains(target)
108
- ) {
109
- const targetState = calculateZoomToNode(contextInstance, target, scale);
110
- animate(contextInstance, targetState, animationTime, animationType);
111
- }
112
- };
112
+ if (
113
+ wrapperComponent &&
114
+ isValidZoomNode(target) &&
115
+ target &&
116
+ wrapperComponent.contains(target)
117
+ ) {
118
+ const targetState = calculateZoomToNode(contextInstance, target, scale);
119
+ animate(contextInstance, targetState, animationTime, animationType);
120
+ }
121
+ };
@@ -10,7 +10,7 @@ import { PositionType } from "../../models/calculations.model";
10
10
  import {
11
11
  calculateBounds,
12
12
  getMouseBoundedPosition,
13
- } from "core/bounds/bounds.utils";
13
+ } from "../bounds/bounds.utils";
14
14
 
15
15
  export const handleCalculateButtonZoom = (
16
16
  contextInstance: ReactZoomPanPinchContext,
@@ -117,6 +117,25 @@ export function resetTransformations(
117
117
  animate(contextInstance, newState, animationTime, animationType);
118
118
  }
119
119
 
120
+ export function getOffset(element: HTMLElement): PositionType {
121
+ let el = element;
122
+
123
+ let offsetLeft = 0;
124
+ let offsetTop = 0;
125
+
126
+ while (el) {
127
+ offsetLeft += el.offsetLeft;
128
+ offsetTop += el.offsetTop;
129
+
130
+ el = el.offsetParent as HTMLElement;
131
+ }
132
+
133
+ return {
134
+ x: offsetLeft - window.scrollX,
135
+ y: offsetTop - window.scrollY,
136
+ };
137
+ }
138
+
120
139
  export function calculateZoomToNode(
121
140
  contextInstance: ReactZoomPanPinchContext,
122
141
  node: HTMLElement,
@@ -166,33 +185,12 @@ export function calculateZoomToNode(
166
185
  return { positionX: x, positionY: y, scale: newScale };
167
186
  }
168
187
 
169
- function getOffset(element: HTMLElement): PositionType {
170
- let el = element;
171
-
172
- let offsetLeft = 0;
173
- let offsetTop = 0;
174
-
175
- while (el) {
176
- offsetLeft += el.offsetLeft;
177
- offsetTop += el.offsetTop;
178
-
179
- el = el.offsetParent as HTMLElement;
180
- }
181
-
182
- return {
183
- x: offsetLeft - window.scrollX,
184
- y: offsetTop - window.scrollY,
185
- };
186
- }
187
-
188
188
  export function isValidZoomNode(node: HTMLElement | null): boolean {
189
189
  if (!node) {
190
190
  console.error("Zoom node not found");
191
191
  return false;
192
- } else if (
193
- node?.offsetWidth === undefined ||
194
- node?.offsetHeight === undefined
195
- ) {
192
+ }
193
+ if (node?.offsetWidth === undefined || node?.offsetHeight === undefined) {
196
194
  console.error(
197
195
  "Zoom node is not valid - it must contain offsetWidth and offsetHeight",
198
196
  );