react-zoom-pan-pinch 2.2.0 → 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.
- package/dist/components/transform-component.d.ts +10 -0
- package/dist/components/transform-wrapper.d.ts +8 -0
- package/dist/constants/state.constants.d.ts +6 -0
- package/dist/core/animations/animations.constants.d.ts +16 -0
- package/dist/core/animations/animations.utils.d.ts +4 -0
- package/dist/core/bounds/bounds.types.d.ts +8 -0
- package/dist/core/bounds/bounds.utils.d.ts +15 -0
- package/dist/core/double-click/double-click.logic.d.ts +3 -0
- package/dist/core/handlers/handlers.logic.d.ts +8 -0
- package/dist/core/handlers/handlers.utils.d.ts +13 -0
- package/dist/core/instance.core.d.ts +68 -0
- package/dist/core/pan/panning.logic.d.ts +5 -0
- package/dist/core/pan/panning.utils.d.ts +10 -0
- package/dist/core/pan/velocity.logic.d.ts +5 -0
- package/dist/core/pan/velocity.utils.d.ts +5 -0
- package/dist/core/pinch/pinch.logic.d.ts +4 -0
- package/dist/core/pinch/pinch.utils.d.ts +6 -0
- package/dist/core/wheel/wheel.logic.d.ts +4 -0
- package/dist/core/wheel/wheel.utils.d.ts +7 -0
- package/dist/core/zoom/zoom.logic.d.ts +3 -0
- package/dist/core/zoom/zoom.utils.d.ts +3 -0
- package/dist/hooks/use-transform-context.d.ts +3 -0
- package/dist/index.cjs.js +278 -267
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +63 -57
- package/dist/index.esm.js +278 -267
- package/dist/index.esm.js.map +1 -1
- package/dist/models/calculations.model.d.ts +25 -0
- package/dist/models/context.model.d.ts +117 -0
- package/dist/models/helpers.model.d.ts +5 -0
- package/dist/models/index.d.ts +3 -0
- package/dist/stories/docs/handlers.d.ts +12 -0
- package/dist/stories/docs/props.d.ts +14 -0
- package/dist/stories/examples/content-rerendering/example.d.ts +2 -0
- package/dist/stories/examples/stress/example.d.ts +2 -0
- package/dist/stories/examples/zoom-to-element/example.d.ts +2 -0
- package/dist/stories/types/args.types.d.ts +268 -0
- package/dist/stories/utils/args.utils.d.ts +3 -0
- package/dist/stories/utils/code.utils.d.ts +6 -0
- package/dist/stories/utils/controls.utils.d.ts +3 -0
- package/dist/stories/utils/index.d.ts +3 -0
- package/dist/utils/calculations.utils.d.ts +11 -0
- package/dist/utils/callback.utils.d.ts +2 -0
- package/dist/utils/context.utils.d.ts +2 -0
- package/dist/utils/event.utils.d.ts +1 -0
- package/dist/utils/helpers.utils.d.ts +2 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/state.utils.d.ts +3 -0
- package/dist/utils/styles.utils.d.ts +3 -0
- package/package.json +30 -25
- package/src/.eslintrc.json +90 -0
- package/src/components/transform-component.tsx +2 -1
- package/src/components/transform-wrapper.tsx +27 -5
- package/src/constants/state.constants.ts +0 -2
- package/src/core/animations/animations.constants.ts +14 -12
- package/src/core/animations/animations.utils.ts +15 -11
- package/src/core/bounds/bounds.utils.ts +20 -20
- package/src/core/double-click/double-click.logic.ts +6 -12
- package/src/core/handlers/handlers.logic.ts +98 -89
- package/src/core/handlers/handlers.utils.ts +22 -24
- package/src/{components/transform-context.tsx → core/instance.core.ts} +85 -118
- package/src/core/pan/panning.logic.ts +20 -19
- package/src/core/pan/panning.utils.ts +4 -7
- package/src/core/pan/velocity.logic.ts +1 -0
- package/src/core/pinch/pinch.logic.ts +4 -6
- package/src/core/pinch/pinch.utils.ts +2 -2
- package/src/core/wheel/wheel.logic.ts +1 -0
- package/src/core/wheel/wheel.utils.ts +12 -11
- package/src/core/zoom/zoom.logic.ts +29 -29
- package/src/core/zoom/zoom.utils.ts +2 -2
- package/src/hooks/use-transform-context.tsx +3 -2
- package/src/models/context.model.ts +6 -2
- package/src/stories/docs/handlers.tsx +30 -30
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +3 -0
- package/src/stories/examples/content-rerendering/example.tsx +1 -1
- package/src/stories/examples/stress/example.tsx +24 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/zoom-to-element/example.tsx +4 -0
- package/src/stories/utils/code.utils.tsx +1 -0
- package/src/stories/utils/controls.utils.tsx +21 -4
- package/src/stories/utils/styles.module.css +5 -0
- package/src/utils/styles.utils.ts +9 -1
- 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 "
|
|
5
|
-
import { handleZoomToPoint } from "
|
|
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 "
|
|
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
|
-
|
|
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 =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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 =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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 =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
57
|
+
if (disabled || !wrapperComponent || !contentComponent) return;
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
60
|
-
};
|
|
65
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
66
|
+
};
|
|
61
67
|
|
|
62
|
-
export const resetTransform =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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 =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
wrapperComponent,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
93
|
+
animate(contextInstance, targetState, animationTime, animationType);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
89
96
|
|
|
90
|
-
export const zoomToElement =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
107
|
+
const { wrapperComponent } = contextInstance;
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
109
|
+
const target: HTMLElement | null =
|
|
110
|
+
typeof node === "string" ? document.getElementById(node) : node;
|
|
102
111
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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 "
|
|
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
|
-
}
|
|
193
|
-
|
|
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
|
);
|