react-zoom-pan-pinch 4.2.0 → 4.4.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 +2 -2
- package/dist/index.cjs.js +770 -229
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +85 -4
- package/dist/index.esm.js +771 -231
- package/dist/index.esm.js.map +1 -1
- package/dist/styles.css +40 -0
- package/package.json +1 -1
- package/src/components/mini-map/mini-map.tsx +386 -171
- package/src/components/transform-component/transform-component.tsx +25 -2
- package/src/constants/state.constants.ts +3 -0
- package/src/core/bounds/bounds.utils.ts +88 -0
- package/src/core/double-click/double-click.logic.ts +18 -9
- package/src/core/handlers/handlers.utils.ts +89 -2
- package/src/core/instance.core.ts +156 -31
- package/src/core/pan/panning.logic.ts +8 -1
- package/src/core/pan/panning.utils.ts +111 -24
- package/src/core/pan/velocity.logic.ts +10 -3
- package/src/core/wheel/wheel.logic.ts +19 -2
- package/src/core/wheel/wheel.utils.ts +44 -2
- package/src/hooks/initial-layout.utils.ts +21 -0
- package/src/hooks/use-transform-component.tsx +22 -4
- package/src/hooks/use-transform-effect.tsx +20 -1
- package/src/models/context.model.ts +25 -2
- package/src/typings.d.ts +5 -0
- package/src/utils/state.utils.ts +8 -2
- package/src/utils/styles.utils.ts +37 -0
|
@@ -4,6 +4,7 @@ import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
|
4
4
|
import { animate, handleCancelAnimation } from "../animations/animations.utils";
|
|
5
5
|
import { handleCalculateBounds } from "../bounds/bounds.utils";
|
|
6
6
|
import {
|
|
7
|
+
CLICK_DISTANCE,
|
|
7
8
|
getPaddingValue,
|
|
8
9
|
getPanningClientPosition,
|
|
9
10
|
handleNewPosition,
|
|
@@ -109,7 +110,13 @@ export function handlePanningEnd(
|
|
|
109
110
|
if (start) {
|
|
110
111
|
const dx = positionX - start.x;
|
|
111
112
|
const dy = positionY - start.y;
|
|
112
|
-
|
|
113
|
+
// A click is not a fling, but it can still land on content left past
|
|
114
|
+
// the bounds — e.g. grabbed mid return animation, which the press
|
|
115
|
+
// cancelled. Finish the return (a no-op inside the bounds).
|
|
116
|
+
if (dx * dx + dy * dy <= CLICK_DISTANCE ** 2) {
|
|
117
|
+
handleAlignToBounds(contextInstance);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
113
120
|
}
|
|
114
121
|
|
|
115
122
|
contextInstance.isAnimating = false;
|
|
@@ -9,7 +9,10 @@ import {
|
|
|
9
9
|
isEditableTarget,
|
|
10
10
|
isExcludedNode,
|
|
11
11
|
} from "../../utils";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
getPanBoundedPosition,
|
|
14
|
+
getRubberBandSource,
|
|
15
|
+
} from "../bounds/bounds.utils";
|
|
13
16
|
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
14
17
|
|
|
15
18
|
export const isPanningStartAllowed = (
|
|
@@ -66,11 +69,110 @@ export const isPanningAllowed = (
|
|
|
66
69
|
return true;
|
|
67
70
|
};
|
|
68
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Pointer travel in px up to which a press still counts as a click, not a
|
|
74
|
+
* drag: a pan ending within it aligns instead of flinging, and only a pan
|
|
75
|
+
* beyond it swallows the click that follows (#452).
|
|
76
|
+
*/
|
|
77
|
+
export const CLICK_DISTANCE = 5;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Whether a pan moving by (`dx`, `dy`) would push the content further into a
|
|
81
|
+
* bound it already rests on, judged on the dominant axis. With
|
|
82
|
+
* `panning.releaseAtBounds` such a gesture is handed back to the page or the
|
|
83
|
+
* parent (a carousel, page scroll) instead of being claimed (#509).
|
|
84
|
+
*/
|
|
85
|
+
export function isPanBlockedAtBounds(
|
|
86
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
87
|
+
dx: number,
|
|
88
|
+
dy: number,
|
|
89
|
+
): boolean {
|
|
90
|
+
const { bounds, setup, state } = contextInstance;
|
|
91
|
+
if (!bounds || !setup.limitToBounds || (!dx && !dy)) return false;
|
|
92
|
+
|
|
93
|
+
const isHorizontal = Math.abs(dx) >= Math.abs(dy);
|
|
94
|
+
if (isHorizontal ? setup.panning.lockAxisX : setup.panning.lockAxisY) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const delta = isHorizontal ? dx : dy;
|
|
99
|
+
const position = isHorizontal ? state.positionX : state.positionY;
|
|
100
|
+
const min = isHorizontal ? bounds.minPositionX : bounds.minPositionY;
|
|
101
|
+
const max = isHorizontal ? bounds.maxPositionX : bounds.maxPositionY;
|
|
102
|
+
// Moving the pointer right/down moves the content towards its max bound.
|
|
103
|
+
// Half a pixel of slack: alignment animations settle on float values.
|
|
104
|
+
return delta > 0 ? position >= max - 0.5 : position <= min + 0.5;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const getPaddingValue = (
|
|
108
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
109
|
+
size: number,
|
|
110
|
+
explicitScale?: number,
|
|
111
|
+
): number => {
|
|
112
|
+
const { setup, state } = contextInstance;
|
|
113
|
+
const { minScale, disablePadding, centerZoomedOut } = setup;
|
|
114
|
+
|
|
115
|
+
const scale = explicitScale ?? state.scale;
|
|
116
|
+
|
|
117
|
+
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
118
|
+
return size;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return 0;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Dragging past the bounds stretches like a rubber band only when the content
|
|
126
|
+
* is going to return to them on release (`autoAlignment`). Without that return
|
|
127
|
+
* the padding is a plain area the content can be left in, so it follows the
|
|
128
|
+
* pointer 1:1 up to its edge.
|
|
129
|
+
*/
|
|
130
|
+
export function isRubberBandEnabled(
|
|
131
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
132
|
+
): boolean {
|
|
133
|
+
const { limitToBounds, autoAlignment } = contextInstance.setup;
|
|
134
|
+
return limitToBounds && !autoAlignment.disabled;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The position a pan continues from. Inside the bounds that is the content
|
|
139
|
+
* position; inside the padding the content trails the pointer (rubber band),
|
|
140
|
+
* so the pointer's own position is traced back from it — otherwise the first
|
|
141
|
+
* move of a pan started there would make the content jump.
|
|
142
|
+
*/
|
|
143
|
+
export function getPanSourcePosition(
|
|
144
|
+
contextInstance: ReactZoomPanPinchContext,
|
|
145
|
+
): PositionType {
|
|
146
|
+
const { bounds, setup, state } = contextInstance;
|
|
147
|
+
const { positionX, positionY } = state;
|
|
148
|
+
|
|
149
|
+
if (!bounds || !isRubberBandEnabled(contextInstance)) {
|
|
150
|
+
return { x: positionX, y: positionY };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const { sizeX, sizeY } = setup.autoAlignment;
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
x: getRubberBandSource(
|
|
157
|
+
positionX,
|
|
158
|
+
bounds.minPositionX,
|
|
159
|
+
bounds.maxPositionX,
|
|
160
|
+
getPaddingValue(contextInstance, sizeX),
|
|
161
|
+
),
|
|
162
|
+
y: getRubberBandSource(
|
|
163
|
+
positionY,
|
|
164
|
+
bounds.minPositionY,
|
|
165
|
+
bounds.maxPositionY,
|
|
166
|
+
getPaddingValue(contextInstance, sizeY),
|
|
167
|
+
),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
69
171
|
export const handlePanningSetup = (
|
|
70
172
|
contextInstance: ReactZoomPanPinchContext,
|
|
71
173
|
event: MouseEvent,
|
|
72
174
|
): void => {
|
|
73
|
-
const { positionX, positionY } = contextInstance
|
|
175
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
74
176
|
|
|
75
177
|
contextInstance.isPanning = true;
|
|
76
178
|
|
|
@@ -86,7 +188,7 @@ export const handleTouchPanningSetup = (
|
|
|
86
188
|
event: TouchEvent,
|
|
87
189
|
): void => {
|
|
88
190
|
const { touches } = event;
|
|
89
|
-
const { positionX, positionY } = contextInstance
|
|
191
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
90
192
|
|
|
91
193
|
contextInstance.isPanning = true;
|
|
92
194
|
|
|
@@ -170,14 +272,14 @@ export function handleNewPosition(
|
|
|
170
272
|
return;
|
|
171
273
|
}
|
|
172
274
|
|
|
173
|
-
const { x, y } =
|
|
275
|
+
const { x, y } = getPanBoundedPosition(
|
|
174
276
|
newPositionX,
|
|
175
277
|
newPositionY,
|
|
176
278
|
bounds,
|
|
177
279
|
limitToBounds,
|
|
178
280
|
paddingValueX,
|
|
179
281
|
paddingValueY,
|
|
180
|
-
|
|
282
|
+
isRubberBandEnabled(contextInstance),
|
|
181
283
|
);
|
|
182
284
|
|
|
183
285
|
contextInstance.setState(scale, x, y);
|
|
@@ -188,10 +290,12 @@ export const getPanningClientPosition = (
|
|
|
188
290
|
clientX: number,
|
|
189
291
|
clientY: number,
|
|
190
292
|
): PositionType => {
|
|
191
|
-
const { startCoords
|
|
293
|
+
const { startCoords } = contextInstance;
|
|
192
294
|
const { panning } = contextInstance.setup;
|
|
193
295
|
const { lockAxisX, lockAxisY } = panning;
|
|
194
|
-
|
|
296
|
+
// A locked axis keeps its place: the source maps back onto the position
|
|
297
|
+
// it has now, also when it rests inside the padding.
|
|
298
|
+
const { x: positionX, y: positionY } = getPanSourcePosition(contextInstance);
|
|
195
299
|
|
|
196
300
|
if (!startCoords) {
|
|
197
301
|
return { x: positionX, y: positionY };
|
|
@@ -204,20 +308,3 @@ export const getPanningClientPosition = (
|
|
|
204
308
|
|
|
205
309
|
return { x: newPositionX, y: newPositionY };
|
|
206
310
|
};
|
|
207
|
-
|
|
208
|
-
export const getPaddingValue = (
|
|
209
|
-
contextInstance: ReactZoomPanPinchContext,
|
|
210
|
-
size: number,
|
|
211
|
-
explicitScale?: number,
|
|
212
|
-
): number => {
|
|
213
|
-
const { setup, state } = contextInstance;
|
|
214
|
-
const { minScale, disablePadding, centerZoomedOut } = setup;
|
|
215
|
-
|
|
216
|
-
const scale = explicitScale ?? state.scale;
|
|
217
|
-
|
|
218
|
-
if (size > 0 && scale >= minScale && !disablePadding && !centerZoomedOut) {
|
|
219
|
-
return size;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return 0;
|
|
223
|
-
};
|
|
@@ -4,7 +4,7 @@ import { ReactZoomPanPinchContext } from "../../models/context.model";
|
|
|
4
4
|
import { getContext } from "../../utils";
|
|
5
5
|
import { animations } from "../animations/animations.constants";
|
|
6
6
|
import { handleSetupAnimation } from "../animations/animations.utils";
|
|
7
|
-
import { getPaddingValue } from "./panning.utils";
|
|
7
|
+
import { getPaddingValue, isRubberBandEnabled } from "./panning.utils";
|
|
8
8
|
import {
|
|
9
9
|
getVelocityPosition,
|
|
10
10
|
getVelocityMoveTime,
|
|
@@ -123,8 +123,15 @@ export function handleVelocityPanning(
|
|
|
123
123
|
|
|
124
124
|
const paddingValueX = getPaddingValue(contextInstance, sizeX);
|
|
125
125
|
const paddingValueY = getPaddingValue(contextInstance, sizeY);
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
// A fling released inside the rubber band stays within the same stretch a
|
|
127
|
+
// drag could reach (`padding` px), not a share of the wrapper size.
|
|
128
|
+
const isRubberBand = isRubberBandEnabled(contextInstance);
|
|
129
|
+
const paddingX = isRubberBand
|
|
130
|
+
? paddingValueX
|
|
131
|
+
: (paddingValueX * wrapperComponent.clientWidth) / 100;
|
|
132
|
+
const paddingY = isRubberBand
|
|
133
|
+
? paddingValueY
|
|
134
|
+
: (paddingValueY * wrapperComponent.clientHeight) / 100;
|
|
128
135
|
const maxTargetX = maxPositionX + paddingX;
|
|
129
136
|
const minTargetX = minPositionX - paddingX;
|
|
130
137
|
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
handleWheelZoomStop,
|
|
12
12
|
getMousePosition,
|
|
13
13
|
} from "./wheel.utils";
|
|
14
|
+
import { handlePanningSetup } from "../pan/panning.utils";
|
|
14
15
|
import { handleAlignToScaleBounds } from "../zoom/zoom.logic";
|
|
15
16
|
import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
|
|
16
17
|
|
|
@@ -64,13 +65,16 @@ export const handleWheelZoom = (
|
|
|
64
65
|
}
|
|
65
66
|
event.stopPropagation();
|
|
66
67
|
|
|
68
|
+
const { isPanning } = contextInstance;
|
|
67
69
|
const delta = getDelta(event, null);
|
|
68
70
|
const zoomStep = smooth ? step * Math.abs(event.deltaY) : step;
|
|
71
|
+
// Mid-drag the scale stays inside min/max: the overshoot would need the
|
|
72
|
+
// settle animation, which cannot run while the pointer owns the position.
|
|
69
73
|
const newScale = handleCalculateWheelZoom(
|
|
70
74
|
contextInstance,
|
|
71
75
|
delta,
|
|
72
76
|
zoomStep,
|
|
73
|
-
!event.ctrlKey,
|
|
77
|
+
!event.ctrlKey || isPanning,
|
|
74
78
|
);
|
|
75
79
|
|
|
76
80
|
// if scale not change
|
|
@@ -97,6 +101,16 @@ export const handleWheelZoom = (
|
|
|
97
101
|
|
|
98
102
|
contextInstance.setState(newScale, x, y);
|
|
99
103
|
|
|
104
|
+
// Zoom during a drag (#543): the pan maps pointer → position through
|
|
105
|
+
// `startCoords`, taken at the old scale. Re-anchor it at the zoomed state,
|
|
106
|
+
// or the next move snaps the content back. The velocity sample from before
|
|
107
|
+
// the zoom would read the jump as a fling, so drop it too.
|
|
108
|
+
if (isPanning) {
|
|
109
|
+
handlePanningSetup(contextInstance, event);
|
|
110
|
+
contextInstance.lastMousePosition = null;
|
|
111
|
+
contextInstance.velocity = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
100
114
|
handleCallback(getContext(contextInstance), event, onWheel);
|
|
101
115
|
handleCallback(getContext(contextInstance), event, onZoom);
|
|
102
116
|
};
|
|
@@ -111,8 +125,11 @@ export const handleWheelStop = (
|
|
|
111
125
|
cancelTimeout(contextInstance.wheelAnimationTimer);
|
|
112
126
|
contextInstance.wheelAnimationTimer = setTimeout(() => {
|
|
113
127
|
if (!contextInstance.mounted) return;
|
|
114
|
-
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
115
128
|
contextInstance.wheelAnimationTimer = null;
|
|
129
|
+
// Mid-drag the pan end aligns (and flushes a pending resize) instead:
|
|
130
|
+
// an animation now would fight the pointer (#543).
|
|
131
|
+
if (contextInstance.isPanning) return;
|
|
132
|
+
handleAlignToScaleBounds(contextInstance, event.x, event.y);
|
|
116
133
|
contextInstance.flushResizeAlignment();
|
|
117
134
|
}, wheelAnimationTime);
|
|
118
135
|
|
|
@@ -8,10 +8,11 @@ export const isWheelAllowed = (
|
|
|
8
8
|
): boolean => {
|
|
9
9
|
const { disabled, wheelDisabled, touchPadDisabled, excluded } =
|
|
10
10
|
contextInstance.setup.wheel;
|
|
11
|
-
const { isInitialized
|
|
11
|
+
const { isInitialized } = contextInstance;
|
|
12
12
|
|
|
13
13
|
const target = event.target as HTMLElement;
|
|
14
|
-
|
|
14
|
+
// Zooming mid-drag is allowed (#543): `handleWheelZoom` re-anchors the pan.
|
|
15
|
+
const isAllowed = isInitialized && !disabled && target;
|
|
15
16
|
|
|
16
17
|
if (!isAllowed) return false;
|
|
17
18
|
// A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
|
|
@@ -50,6 +51,10 @@ export const isWheelPanningAllowed = (
|
|
|
50
51
|
return false;
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
// A drag owns the position; a wheel pan on top of it would be undone by
|
|
55
|
+
// the next pointer move.
|
|
56
|
+
if (contextInstance.isPanning) return false;
|
|
57
|
+
|
|
53
58
|
const isAllowed = isWheelAllowed(contextInstance, event);
|
|
54
59
|
// Cannot execute at the same time as wheel zoom
|
|
55
60
|
if (isAllowed) return false;
|
|
@@ -64,6 +69,43 @@ export const isWheelPanningAllowed = (
|
|
|
64
69
|
return true;
|
|
65
70
|
};
|
|
66
71
|
|
|
72
|
+
// px per line for `deltaMode === 1` (Firefox with a mouse wheel), close to
|
|
73
|
+
// what browsers scroll a page per line.
|
|
74
|
+
const WHEEL_LINE_HEIGHT = 16;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The distance in px a wheel event pans by (#500):
|
|
78
|
+
* - line and page deltas (`deltaMode` 1/2) become pixels, so a wheel that
|
|
79
|
+
* reports "3 lines" moves 48px instead of 3px;
|
|
80
|
+
* - Shift turns a vertical wheel horizontal where the browser does not do it
|
|
81
|
+
* already (a browser that does reports it as `deltaX`);
|
|
82
|
+
* - `speed` scales the result.
|
|
83
|
+
*/
|
|
84
|
+
export function getWheelPanDelta(
|
|
85
|
+
event: WheelEvent,
|
|
86
|
+
wrapperComponent: HTMLDivElement,
|
|
87
|
+
speed: number,
|
|
88
|
+
): PositionType {
|
|
89
|
+
let unitX = 1;
|
|
90
|
+
let unitY = 1;
|
|
91
|
+
if (event.deltaMode === 1) {
|
|
92
|
+
unitX = WHEEL_LINE_HEIGHT;
|
|
93
|
+
unitY = WHEEL_LINE_HEIGHT;
|
|
94
|
+
} else if (event.deltaMode === 2) {
|
|
95
|
+
unitX = wrapperComponent.clientWidth;
|
|
96
|
+
unitY = wrapperComponent.clientHeight;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let x = event.deltaX * unitX;
|
|
100
|
+
let y = event.deltaY * unitY;
|
|
101
|
+
if (event.shiftKey && !x) {
|
|
102
|
+
x = y;
|
|
103
|
+
y = 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { x: x * speed, y: y * speed };
|
|
107
|
+
}
|
|
108
|
+
|
|
67
109
|
export const getDeltaY = (event?: WheelEvent) => {
|
|
68
110
|
if (event) {
|
|
69
111
|
return event.deltaY < 0 ? 1 : -1;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ReactZoomPanPinchState } from "../models";
|
|
2
|
+
|
|
3
|
+
export type StateSnapshot = Pick<
|
|
4
|
+
ReactZoomPanPinchState,
|
|
5
|
+
"scale" | "positionX" | "positionY"
|
|
6
|
+
>;
|
|
7
|
+
|
|
8
|
+
export const takeSnapshot = (state: ReactZoomPanPinchState): StateSnapshot => ({
|
|
9
|
+
scale: state.scale,
|
|
10
|
+
positionX: state.positionX,
|
|
11
|
+
positionY: state.positionY,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const isSameSnapshot = (
|
|
15
|
+
snapshot: StateSnapshot | null,
|
|
16
|
+
state: ReactZoomPanPinchState,
|
|
17
|
+
): boolean =>
|
|
18
|
+
snapshot !== null &&
|
|
19
|
+
snapshot.scale === state.scale &&
|
|
20
|
+
snapshot.positionX === state.positionX &&
|
|
21
|
+
snapshot.positionY === state.positionY;
|
|
@@ -1,25 +1,43 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import { useTransformContext } from "./use-transform-context";
|
|
4
4
|
import { getState } from "utils";
|
|
5
5
|
import { ReactZoomPanPinchContextState } from "../models";
|
|
6
|
+
import {
|
|
7
|
+
StateSnapshot,
|
|
8
|
+
isSameSnapshot,
|
|
9
|
+
takeSnapshot,
|
|
10
|
+
} from "./initial-layout.utils";
|
|
6
11
|
|
|
7
12
|
export function useTransformComponent<T>(
|
|
8
13
|
callback: (state: ReactZoomPanPinchContextState) => T,
|
|
9
14
|
): T {
|
|
10
15
|
const libraryContext = useTransformContext();
|
|
16
|
+
const lastSeen = useRef<StateSnapshot | null>(null);
|
|
11
17
|
|
|
12
|
-
const [transformRender, setTransformRender] = useState<T>(() =>
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
const [transformRender, setTransformRender] = useState<T>(() => {
|
|
19
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
20
|
+
return callback(getState(libraryContext));
|
|
21
|
+
});
|
|
15
22
|
|
|
16
23
|
useEffect(() => {
|
|
17
24
|
let mounted = true;
|
|
18
25
|
const unmount = libraryContext.onChange((ref) => {
|
|
19
26
|
if (mounted) {
|
|
27
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
20
28
|
setTransformRender(callback(getState(ref.instance)));
|
|
21
29
|
}
|
|
22
30
|
});
|
|
31
|
+
|
|
32
|
+
// The instance initializes in a layout effect, so a transform applied on
|
|
33
|
+
// mount (initialPosition*, centerOnInit, fitOnInit) can land before this
|
|
34
|
+
// subscription exists. Catch up when the state moved since the last
|
|
35
|
+
// render; the snapshot check keeps this from re-rendering in a loop.
|
|
36
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
37
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
38
|
+
setTransformRender(callback(getState(libraryContext)));
|
|
39
|
+
}
|
|
40
|
+
|
|
23
41
|
return () => {
|
|
24
42
|
unmount();
|
|
25
43
|
mounted = false;
|
|
@@ -1,19 +1,38 @@
|
|
|
1
|
-
import { useEffect } from "react";
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
|
|
3
3
|
import { getState } from "utils";
|
|
4
4
|
import { ReactZoomPanPinchContextState } from "../models";
|
|
5
5
|
import { useTransformContext } from "./use-transform-context";
|
|
6
|
+
import {
|
|
7
|
+
StateSnapshot,
|
|
8
|
+
isSameSnapshot,
|
|
9
|
+
takeSnapshot,
|
|
10
|
+
} from "./initial-layout.utils";
|
|
6
11
|
|
|
7
12
|
export const useTransformEffect = (
|
|
8
13
|
callback: (ref: ReactZoomPanPinchContextState) => void | (() => void),
|
|
9
14
|
): void => {
|
|
10
15
|
const libraryContext = useTransformContext();
|
|
16
|
+
// Starts as the state the consumer rendered with, so a transform applied
|
|
17
|
+
// during mount (initialPosition*, centerOnInit, fitOnInit) — which happens
|
|
18
|
+
// in a layout effect, before this subscription exists — is delivered once.
|
|
19
|
+
const lastSeen = useRef<StateSnapshot | null>(null);
|
|
20
|
+
if (lastSeen.current === null) {
|
|
21
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
22
|
+
}
|
|
11
23
|
|
|
12
24
|
useEffect(() => {
|
|
13
25
|
let unmountCallback: void | (() => void);
|
|
14
26
|
const unmount = libraryContext.onChange((ref) => {
|
|
27
|
+
lastSeen.current = takeSnapshot(ref.instance.state);
|
|
15
28
|
unmountCallback = callback(getState(ref.instance));
|
|
16
29
|
});
|
|
30
|
+
|
|
31
|
+
if (!isSameSnapshot(lastSeen.current, libraryContext.state)) {
|
|
32
|
+
lastSeen.current = takeSnapshot(libraryContext.state);
|
|
33
|
+
unmountCallback = callback(getState(libraryContext));
|
|
34
|
+
}
|
|
35
|
+
|
|
17
36
|
return () => {
|
|
18
37
|
unmount();
|
|
19
38
|
unmountCallback?.();
|
|
@@ -34,6 +34,9 @@ export type ZoomToElementOptions = {
|
|
|
34
34
|
|
|
35
35
|
export type FitToViewMode = "contain" | "cover";
|
|
36
36
|
|
|
37
|
+
export type InitialPositionX = number | "left" | "center" | "right";
|
|
38
|
+
export type InitialPositionY = number | "top" | "center" | "bottom";
|
|
39
|
+
|
|
37
40
|
export type FitToViewOptions = {
|
|
38
41
|
/** `contain` (default) shows the whole content, `cover` fills the viewport. */
|
|
39
42
|
mode?: FitToViewMode;
|
|
@@ -92,8 +95,14 @@ export type ReactZoomPanPinchProps = {
|
|
|
92
95
|
ref?: React.Ref<ReactZoomPanPinchRef>;
|
|
93
96
|
detached?: boolean;
|
|
94
97
|
initialScale?: number;
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Initial `positionX` in px, or where to place the content horizontally:
|
|
100
|
+
* `"left"`, `"center"` or `"right"` edge of the wrapper. A named position
|
|
101
|
+
* is resolved once the content has a size; `resetTransform` returns to it.
|
|
102
|
+
*/
|
|
103
|
+
initialPositionX?: InitialPositionX;
|
|
104
|
+
/** Like `initialPositionX`, with `"top"`, `"center"` or `"bottom"`. */
|
|
105
|
+
initialPositionY?: InitialPositionY;
|
|
97
106
|
disabled?: boolean;
|
|
98
107
|
minPositionX?: null | number;
|
|
99
108
|
maxPositionX?: null | number;
|
|
@@ -129,6 +138,18 @@ export type ReactZoomPanPinchProps = {
|
|
|
129
138
|
allowLeftClickPan?: boolean;
|
|
130
139
|
allowMiddleClickPan?: boolean;
|
|
131
140
|
allowRightClickPan?: boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Swallow the `click` that follows a mouse pan which moved the pointer
|
|
143
|
+
* more than 5px, so dragging over clickable content does not also click
|
|
144
|
+
* it. On by default.
|
|
145
|
+
*/
|
|
146
|
+
suppressClickAfterPan?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Hand a gesture back to the page when its first move heads into a bound
|
|
149
|
+
* the content already rests on: no pan, no `preventDefault`, so the page
|
|
150
|
+
* scrolls or a parent carousel takes the drag. Off by default.
|
|
151
|
+
*/
|
|
152
|
+
releaseAtBounds?: boolean;
|
|
132
153
|
activationKeys?: string[] | ((keys: string[]) => boolean);
|
|
133
154
|
excluded?: string[];
|
|
134
155
|
};
|
|
@@ -141,6 +162,8 @@ export type ReactZoomPanPinchProps = {
|
|
|
141
162
|
trackPadPanning?: {
|
|
142
163
|
disabled?: boolean;
|
|
143
164
|
velocityDisabled?: boolean;
|
|
165
|
+
/** Multiplier applied to the wheel/trackpad delta. Defaults to 1. */
|
|
166
|
+
speed?: number;
|
|
144
167
|
lockAxisX?: boolean;
|
|
145
168
|
lockAxisY?: boolean;
|
|
146
169
|
activationKeys?: string[] | ((keys: string[]) => boolean);
|
package/src/typings.d.ts
CHANGED
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
declare module "*.css" {
|
|
6
6
|
const content: { [className: string]: string };
|
|
7
7
|
export default content;
|
|
8
|
+
/**
|
|
9
|
+
* The compiled CSS, exported by rollup-plugin-postcss with `inject: false`.
|
|
10
|
+
* Undefined under bundlers that load CSS themselves (Storybook).
|
|
11
|
+
*/
|
|
12
|
+
export const stylesheet: string | undefined;
|
|
8
13
|
}
|
|
9
14
|
declare module "*.svg" {
|
|
10
15
|
import React = require("react");
|
package/src/utils/state.utils.ts
CHANGED
|
@@ -14,15 +14,21 @@ export const createState = (
|
|
|
14
14
|
const rawScale = props.initialScale ?? initialState.scale;
|
|
15
15
|
const scale = Math.min(Math.max(rawScale, minScale), maxScale);
|
|
16
16
|
|
|
17
|
+
// A named initial position ("center", "right", …) needs the sizes, so it
|
|
18
|
+
// starts at 0 and `applyNamedInitialPosition` places it once laid out.
|
|
17
19
|
const positionX = boundLimiter(
|
|
18
|
-
props.initialPositionX
|
|
20
|
+
typeof props.initialPositionX === "number"
|
|
21
|
+
? props.initialPositionX
|
|
22
|
+
: initialState.positionX,
|
|
19
23
|
props.minPositionX ?? -Infinity,
|
|
20
24
|
props.maxPositionX ?? Infinity,
|
|
21
25
|
props.minPositionX != null || props.maxPositionX != null,
|
|
22
26
|
);
|
|
23
27
|
|
|
24
28
|
const positionY = boundLimiter(
|
|
25
|
-
props.initialPositionY
|
|
29
|
+
typeof props.initialPositionY === "number"
|
|
30
|
+
? props.initialPositionY
|
|
31
|
+
: initialState.positionY,
|
|
26
32
|
props.minPositionY ?? -Infinity,
|
|
27
33
|
props.maxPositionY ?? Infinity,
|
|
28
34
|
props.minPositionY != null || props.maxPositionY != null,
|
|
@@ -4,6 +4,43 @@ import { StateType } from "models";
|
|
|
4
4
|
export const roundScaleForTransform = (scale: number): number =>
|
|
5
5
|
Number.parseFloat(scale.toFixed(8));
|
|
6
6
|
|
|
7
|
+
const STYLE_MARKER = "data-react-zoom-pan-pinch";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Adds the library stylesheet once per root the wrapper renders into: the
|
|
11
|
+
* document `<head>`, or the shadow root itself (#371), where a stylesheet in
|
|
12
|
+
* the head never applies. `nonce` is set on the `<style>` so a CSP with
|
|
13
|
+
* `style-src 'nonce-…'` accepts it (#403). A `<style>` already holding the
|
|
14
|
+
* same CSS is reused, so several viewers (or a re-mount) add one copy.
|
|
15
|
+
*/
|
|
16
|
+
export function injectStylesheet(
|
|
17
|
+
wrapper: HTMLElement,
|
|
18
|
+
css: string | undefined,
|
|
19
|
+
nonce?: string,
|
|
20
|
+
): void {
|
|
21
|
+
if (!css || typeof wrapper.getRootNode !== "function") return;
|
|
22
|
+
const doc = wrapper.ownerDocument;
|
|
23
|
+
const root = wrapper.getRootNode();
|
|
24
|
+
// Duck-typed: a ShadowRoot from another window fails `instanceof`.
|
|
25
|
+
const isShadowRoot = root.nodeType === 11 && "host" in root;
|
|
26
|
+
const container: ParentNode | null = isShadowRoot
|
|
27
|
+
? (root as ShadowRoot)
|
|
28
|
+
: doc.head;
|
|
29
|
+
if (!container) return;
|
|
30
|
+
|
|
31
|
+
const existing = container.querySelectorAll(`style[${STYLE_MARKER}]`);
|
|
32
|
+
for (let index = 0; index < existing.length; index += 1) {
|
|
33
|
+
if (existing[index].textContent === css) return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const style = doc.createElement("style");
|
|
37
|
+
style.setAttribute(STYLE_MARKER, "");
|
|
38
|
+
if (nonce) style.setAttribute("nonce", nonce);
|
|
39
|
+
style.textContent = css;
|
|
40
|
+
// Appended, like style-inject did before (the bundle used to add it at load).
|
|
41
|
+
container.appendChild(style);
|
|
42
|
+
}
|
|
43
|
+
|
|
7
44
|
export const getTransformStyles = (
|
|
8
45
|
x: number,
|
|
9
46
|
y: number,
|