react-zoom-pan-pinch 4.0.7 → 4.1.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/README.md +6 -1
- package/dist/index.cjs.js +290 -72
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +64 -7
- package/dist/index.esm.js +290 -73
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/transform-component/transform-component.tsx +3 -0
- package/src/core/animations/animations.utils.ts +34 -5
- package/src/core/handlers/handlers.logic.ts +160 -36
- package/src/core/handlers/handlers.utils.ts +177 -51
- package/src/core/instance.core.ts +2 -0
- package/src/core/pan/panning.logic.ts +10 -1
- package/src/core/wheel/wheel.logic.ts +10 -2
- package/src/hooks/index.ts +1 -0
- package/src/models/context.model.ts +21 -0
- package/src/utils/context.utils.ts +6 -0
package/package.json
CHANGED
|
@@ -85,6 +85,9 @@ export const TransformComponent: React.FC<Props> = ({
|
|
|
85
85
|
ref={contentRef}
|
|
86
86
|
className={`${baseClasses.contentClass} ${styles.content} ${contentClass}`}
|
|
87
87
|
style={{
|
|
88
|
+
// Inline so the zoom anchor survives environments where the
|
|
89
|
+
// stylesheet is not applied, e.g. a shadow root (#371, #444).
|
|
90
|
+
transformOrigin: "0 0",
|
|
88
91
|
...contentStyle,
|
|
89
92
|
transform: getTransformStyles(
|
|
90
93
|
instance.state.positionX,
|
|
@@ -6,6 +6,12 @@ import {
|
|
|
6
6
|
StateType,
|
|
7
7
|
} from "../../models";
|
|
8
8
|
|
|
9
|
+
const settleAnimation = (contextInstance: ReactZoomPanPinchContext): void => {
|
|
10
|
+
const resolve = contextInstance.animationResolve;
|
|
11
|
+
contextInstance.animationResolve = null;
|
|
12
|
+
resolve?.();
|
|
13
|
+
};
|
|
14
|
+
|
|
9
15
|
export const handleCancelAnimation = (
|
|
10
16
|
contextInstance: ReactZoomPanPinchContext,
|
|
11
17
|
): void => {
|
|
@@ -17,6 +23,9 @@ export const handleCancelAnimation = (
|
|
|
17
23
|
contextInstance.isAnimating = false;
|
|
18
24
|
contextInstance.animation = null;
|
|
19
25
|
contextInstance.velocity = null;
|
|
26
|
+
// An interrupted animation still settles its promise so callers awaiting
|
|
27
|
+
// `zoomIn()` & co. are never left hanging (#214).
|
|
28
|
+
settleAnimation(contextInstance);
|
|
20
29
|
};
|
|
21
30
|
|
|
22
31
|
export function handleSetupAnimation(
|
|
@@ -24,8 +33,12 @@ export function handleSetupAnimation(
|
|
|
24
33
|
animationName: string,
|
|
25
34
|
animationTime: number,
|
|
26
35
|
callback: (step: number) => void,
|
|
36
|
+
onFinish?: () => void,
|
|
27
37
|
): void {
|
|
28
|
-
if (!contextInstance.mounted)
|
|
38
|
+
if (!contextInstance.mounted) {
|
|
39
|
+
onFinish?.();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
29
42
|
const startTime = new Date().getTime();
|
|
30
43
|
const lastStep = 1;
|
|
31
44
|
|
|
@@ -52,7 +65,10 @@ export function handleSetupAnimation(
|
|
|
52
65
|
// animation (e.g. from onTransform) is not wiped out afterwards.
|
|
53
66
|
contextInstance.animation = null;
|
|
54
67
|
contextInstance.animationFrame = null;
|
|
68
|
+
const resolve = contextInstance.animationResolve;
|
|
69
|
+
contextInstance.animationResolve = null;
|
|
55
70
|
callback(lastStep);
|
|
71
|
+
resolve?.();
|
|
56
72
|
} else {
|
|
57
73
|
callback(step);
|
|
58
74
|
contextInstance.animationFrame = requestAnimationFrame(animation);
|
|
@@ -60,6 +76,7 @@ export function handleSetupAnimation(
|
|
|
60
76
|
};
|
|
61
77
|
|
|
62
78
|
contextInstance.animation = animation;
|
|
79
|
+
contextInstance.animationResolve = onFinish ?? null;
|
|
63
80
|
contextInstance.animationFrame = requestAnimationFrame(animation);
|
|
64
81
|
}
|
|
65
82
|
|
|
@@ -77,14 +94,22 @@ function isValidTargetState(targetState: StateType): boolean {
|
|
|
77
94
|
return true;
|
|
78
95
|
}
|
|
79
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Animates the transform to `targetState`.
|
|
99
|
+
*
|
|
100
|
+
* Resolves once the final frame has been applied. It also resolves — never
|
|
101
|
+
* rejects — when the animation is interrupted by another animation or a user
|
|
102
|
+
* gesture, when the instance unmounts, or when the target is invalid, so the
|
|
103
|
+
* returned promise is always safe to await (#214).
|
|
104
|
+
*/
|
|
80
105
|
export function animate(
|
|
81
106
|
contextInstance: ReactZoomPanPinchContext,
|
|
82
107
|
targetState: StateType,
|
|
83
108
|
animationTime: number,
|
|
84
109
|
animationName: string,
|
|
85
|
-
): void {
|
|
110
|
+
): Promise<void> {
|
|
86
111
|
const isValid = isValidTargetState(targetState);
|
|
87
|
-
if (!contextInstance.mounted || !isValid) return;
|
|
112
|
+
if (!contextInstance.mounted || !isValid) return Promise.resolve();
|
|
88
113
|
|
|
89
114
|
const { setState } = contextInstance;
|
|
90
115
|
const { scale, positionX, positionY } = contextInstance.state;
|
|
@@ -95,7 +120,10 @@ export function animate(
|
|
|
95
120
|
|
|
96
121
|
if (animationTime === 0) {
|
|
97
122
|
setState(targetState.scale, targetState.positionX, targetState.positionY);
|
|
98
|
-
|
|
123
|
+
return Promise.resolve();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return new Promise<void>((resolve) => {
|
|
99
127
|
// animation start timestamp
|
|
100
128
|
handleSetupAnimation(
|
|
101
129
|
contextInstance,
|
|
@@ -113,6 +141,7 @@ export function animate(
|
|
|
113
141
|
|
|
114
142
|
setState(newScale, newPositionX, newPositionY);
|
|
115
143
|
},
|
|
144
|
+
resolve,
|
|
116
145
|
);
|
|
117
|
-
}
|
|
146
|
+
});
|
|
118
147
|
}
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import { ReactZoomPanPinchContext } from "../../models";
|
|
2
1
|
import {
|
|
3
|
-
|
|
2
|
+
PositionType,
|
|
3
|
+
ReactZoomPanPinchContext,
|
|
4
|
+
ZoomToElementOptions,
|
|
5
|
+
ZoomToElementTarget,
|
|
6
|
+
} from "../../models";
|
|
7
|
+
import {
|
|
8
|
+
calculateZoomToRect,
|
|
9
|
+
clientToContentPoint,
|
|
10
|
+
contentToClientPoint,
|
|
11
|
+
getUnionRect,
|
|
4
12
|
handleZoomToViewCenter,
|
|
5
13
|
resetTransformations,
|
|
14
|
+
runZoomAnimation,
|
|
6
15
|
} from "./handlers.utils";
|
|
7
16
|
import { animations } from "../animations/animations.constants";
|
|
8
17
|
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
18
|
+
import { handleZoomToPoint } from "../zoom/zoom.logic";
|
|
9
19
|
import { getCenterPosition } from "../../utils";
|
|
10
20
|
|
|
11
21
|
export const zoomIn =
|
|
@@ -14,8 +24,8 @@ export const zoomIn =
|
|
|
14
24
|
step = 0.5,
|
|
15
25
|
animationTime = 300,
|
|
16
26
|
animationType: keyof typeof animations = "easeOut",
|
|
17
|
-
): void => {
|
|
18
|
-
handleZoomToViewCenter(
|
|
27
|
+
): Promise<void> => {
|
|
28
|
+
return handleZoomToViewCenter(
|
|
19
29
|
contextInstance,
|
|
20
30
|
1,
|
|
21
31
|
step,
|
|
@@ -30,8 +40,8 @@ export const zoomOut =
|
|
|
30
40
|
step = 0.5,
|
|
31
41
|
animationTime = 300,
|
|
32
42
|
animationType: keyof typeof animations = "easeOut",
|
|
33
|
-
): void => {
|
|
34
|
-
handleZoomToViewCenter(
|
|
43
|
+
): Promise<void> => {
|
|
44
|
+
return handleZoomToViewCenter(
|
|
35
45
|
contextInstance,
|
|
36
46
|
-1,
|
|
37
47
|
step,
|
|
@@ -48,12 +58,14 @@ export const setTransform =
|
|
|
48
58
|
newScale: number,
|
|
49
59
|
animationTime = 300,
|
|
50
60
|
animationType: keyof typeof animations = "easeOut",
|
|
51
|
-
): void => {
|
|
61
|
+
): Promise<void> => {
|
|
52
62
|
const { positionX, positionY, scale } = contextInstance.state;
|
|
53
63
|
const { wrapperComponent, contentComponent } = contextInstance;
|
|
54
64
|
const { disabled } = contextInstance.setup;
|
|
55
65
|
|
|
56
|
-
if (disabled || !wrapperComponent || !contentComponent)
|
|
66
|
+
if (disabled || !wrapperComponent || !contentComponent) {
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
}
|
|
57
69
|
|
|
58
70
|
const targetState = {
|
|
59
71
|
positionX: Number.isNaN(newPositionX) ? positionX : newPositionX,
|
|
@@ -61,7 +73,7 @@ export const setTransform =
|
|
|
61
73
|
scale: Number.isNaN(newScale) ? scale : newScale,
|
|
62
74
|
};
|
|
63
75
|
|
|
64
|
-
animate(contextInstance, targetState, animationTime, animationType);
|
|
76
|
+
return animate(contextInstance, targetState, animationTime, animationType);
|
|
65
77
|
};
|
|
66
78
|
|
|
67
79
|
export const resetTransform =
|
|
@@ -69,8 +81,8 @@ export const resetTransform =
|
|
|
69
81
|
(
|
|
70
82
|
animationTime = 200,
|
|
71
83
|
animationType: keyof typeof animations = "easeOut",
|
|
72
|
-
): void => {
|
|
73
|
-
resetTransformations(contextInstance, animationTime, animationType);
|
|
84
|
+
): Promise<void> => {
|
|
85
|
+
return resetTransformations(contextInstance, animationTime, animationType);
|
|
74
86
|
};
|
|
75
87
|
|
|
76
88
|
export const centerView =
|
|
@@ -79,7 +91,7 @@ export const centerView =
|
|
|
79
91
|
scale?: number,
|
|
80
92
|
animationTime = 200,
|
|
81
93
|
animationType: keyof typeof animations = "easeOut",
|
|
82
|
-
): void => {
|
|
94
|
+
): Promise<void> => {
|
|
83
95
|
const { state, wrapperComponent, contentComponent } = contextInstance;
|
|
84
96
|
if (wrapperComponent && contentComponent) {
|
|
85
97
|
const targetState = getCenterPosition(
|
|
@@ -88,43 +100,155 @@ export const centerView =
|
|
|
88
100
|
contentComponent,
|
|
89
101
|
);
|
|
90
102
|
|
|
91
|
-
animate(
|
|
103
|
+
return animate(
|
|
104
|
+
contextInstance,
|
|
105
|
+
targetState,
|
|
106
|
+
animationTime,
|
|
107
|
+
animationType,
|
|
108
|
+
);
|
|
92
109
|
}
|
|
110
|
+
return Promise.resolve();
|
|
93
111
|
};
|
|
94
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Zooms so that the content point currently under the client (viewport)
|
|
115
|
+
* coordinates `clientX`/`clientY` stays fixed on screen — the same anchoring a
|
|
116
|
+
* wheel zoom does at the cursor. Useful for click-to-zoom and focal zooms
|
|
117
|
+
* (#353).
|
|
118
|
+
*/
|
|
119
|
+
export const zoomToPoint =
|
|
120
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
121
|
+
(
|
|
122
|
+
scale: number,
|
|
123
|
+
clientX: number,
|
|
124
|
+
clientY: number,
|
|
125
|
+
animationTime = 300,
|
|
126
|
+
animationType: keyof typeof animations = "easeOut",
|
|
127
|
+
): Promise<void> => {
|
|
128
|
+
const { wrapperComponent, contentComponent, setup } = contextInstance;
|
|
129
|
+
if (setup.disabled || !wrapperComponent || !contentComponent) {
|
|
130
|
+
return Promise.resolve();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const point = clientToContentPoint(contextInstance, clientX, clientY);
|
|
134
|
+
const targetState = handleZoomToPoint(
|
|
135
|
+
contextInstance,
|
|
136
|
+
scale,
|
|
137
|
+
point.x,
|
|
138
|
+
point.y,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (!targetState) return Promise.resolve();
|
|
142
|
+
|
|
143
|
+
const effectiveAnimationTime = setup.zoomAnimation.disabled
|
|
144
|
+
? 0
|
|
145
|
+
: animationTime;
|
|
146
|
+
|
|
147
|
+
return runZoomAnimation(
|
|
148
|
+
contextInstance,
|
|
149
|
+
targetState,
|
|
150
|
+
effectiveAnimationTime,
|
|
151
|
+
animationType,
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Converts client (viewport) coordinates — e.g. `event.clientX/Y` — into
|
|
157
|
+
* content coordinates: the unscaled position inside `TransformComponent`
|
|
158
|
+
* regardless of the current pan and zoom (#378).
|
|
159
|
+
*/
|
|
160
|
+
export const clientToContent =
|
|
161
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
162
|
+
(clientX: number, clientY: number): PositionType => {
|
|
163
|
+
return clientToContentPoint(contextInstance, clientX, clientY);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Inverse of `clientToContent`: converts unscaled content coordinates into
|
|
168
|
+
* client (viewport) coordinates for the current transform.
|
|
169
|
+
*/
|
|
170
|
+
export const contentToClient =
|
|
171
|
+
(contextInstance: ReactZoomPanPinchContext) =>
|
|
172
|
+
(x: number, y: number): PositionType => {
|
|
173
|
+
return contentToClientPoint(contextInstance, x, y);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const resolveTargets = (
|
|
177
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
178
|
+
node: ZoomToElementTarget | ZoomToElementTarget[],
|
|
179
|
+
): HTMLElement[] => {
|
|
180
|
+
const { wrapperComponent } = contextInstance;
|
|
181
|
+
if (!wrapperComponent) return [];
|
|
182
|
+
|
|
183
|
+
// Ids are resolved in the wrapper's own root so this works inside portal
|
|
184
|
+
// windows and iframes (#290) as well as shadow roots (#371).
|
|
185
|
+
const rootNode = wrapperComponent.getRootNode();
|
|
186
|
+
const root: { getElementById(id: string): Element | null } | null =
|
|
187
|
+
rootNode && "getElementById" in rootNode
|
|
188
|
+
? (rootNode as unknown as { getElementById(id: string): Element | null })
|
|
189
|
+
: wrapperComponent.ownerDocument ??
|
|
190
|
+
(typeof document !== "undefined" ? document : null);
|
|
191
|
+
|
|
192
|
+
const list = Array.isArray(node) ? node : [node];
|
|
193
|
+
|
|
194
|
+
return list
|
|
195
|
+
.map((item) =>
|
|
196
|
+
typeof item === "string"
|
|
197
|
+
? (root?.getElementById(item) as HTMLElement | null)
|
|
198
|
+
: item,
|
|
199
|
+
)
|
|
200
|
+
.filter(
|
|
201
|
+
(element): element is HTMLElement =>
|
|
202
|
+
!!element && wrapperComponent.contains(element),
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Zooms and pans so the target element(s) fill the viewport.
|
|
208
|
+
*
|
|
209
|
+
* - `node` may be an element, an element id, or an array of either; with
|
|
210
|
+
* several targets the union of their boxes is framed (#388).
|
|
211
|
+
* - The second argument is either the target `scale` (legacy positional
|
|
212
|
+
* form) or an options object. `minScale`/`maxScale` cap the automatically
|
|
213
|
+
* computed fit scale (#515).
|
|
214
|
+
*/
|
|
95
215
|
export const zoomToElement =
|
|
96
216
|
(contextInstance: ReactZoomPanPinchContext) =>
|
|
97
217
|
(
|
|
98
|
-
node:
|
|
99
|
-
|
|
218
|
+
node: ZoomToElementTarget | ZoomToElementTarget[],
|
|
219
|
+
scaleOrOptions?: number | ZoomToElementOptions,
|
|
100
220
|
animationTime = 600,
|
|
101
221
|
animationType: keyof typeof animations = "easeOut",
|
|
102
222
|
offsetX = 0,
|
|
103
223
|
offsetY = 0,
|
|
104
|
-
): void => {
|
|
224
|
+
): Promise<void> => {
|
|
105
225
|
handleCancelAnimation(contextInstance);
|
|
106
226
|
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const ownerDocument =
|
|
112
|
-
wrapperComponent?.ownerDocument ??
|
|
113
|
-
(typeof document !== "undefined" ? document : null);
|
|
227
|
+
const options: ZoomToElementOptions =
|
|
228
|
+
typeof scaleOrOptions === "object" && scaleOrOptions !== null
|
|
229
|
+
? scaleOrOptions
|
|
230
|
+
: { scale: scaleOrOptions };
|
|
114
231
|
|
|
115
|
-
const
|
|
116
|
-
typeof node === "string"
|
|
117
|
-
? ownerDocument?.getElementById(node) ?? null
|
|
118
|
-
: node;
|
|
232
|
+
const targets = resolveTargets(contextInstance, node);
|
|
119
233
|
|
|
120
|
-
if (wrapperComponent
|
|
121
|
-
|
|
122
|
-
contextInstance,
|
|
123
|
-
target,
|
|
124
|
-
scale,
|
|
125
|
-
offsetX,
|
|
126
|
-
offsetY,
|
|
127
|
-
);
|
|
128
|
-
animate(contextInstance, targetState, animationTime, animationType);
|
|
234
|
+
if (!contextInstance.wrapperComponent || targets.length === 0) {
|
|
235
|
+
return Promise.resolve();
|
|
129
236
|
}
|
|
237
|
+
|
|
238
|
+
const targetState = calculateZoomToRect(
|
|
239
|
+
contextInstance,
|
|
240
|
+
getUnionRect(targets),
|
|
241
|
+
options.scale,
|
|
242
|
+
options.offsetX ?? offsetX,
|
|
243
|
+
options.offsetY ?? offsetY,
|
|
244
|
+
options.minScale,
|
|
245
|
+
options.maxScale,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
return animate(
|
|
249
|
+
contextInstance,
|
|
250
|
+
targetState,
|
|
251
|
+
options.animationTime ?? animationTime,
|
|
252
|
+
options.animationType ?? animationType,
|
|
253
|
+
);
|
|
130
254
|
};
|