react-zoom-pan-pinch 4.3.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/dist/index.cjs.js +335 -67
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +53 -3
- package/dist/index.esm.js +335 -68
- package/dist/index.esm.js.map +1 -1
- package/dist/styles.css +40 -0
- package/package.json +1 -1
- package/src/components/transform-component/transform-component.tsx +25 -2
- package/src/constants/state.constants.ts +3 -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 +146 -24
- package/src/core/pan/panning.logic.ts +2 -1
- package/src/core/pan/panning.utils.ts +35 -0
- package/src/core/wheel/wheel.logic.ts +19 -2
- package/src/core/wheel/wheel.utils.ts +44 -2
- 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
|
@@ -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;
|
|
@@ -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,
|