react-zoom-pan-pinch 3.1.1 → 3.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.
@@ -3,6 +3,8 @@ import { ComponentsSizesType } from "./bounds.types";
3
3
  export declare function getComponentsSizes(wrapperComponent: HTMLDivElement, contentComponent: HTMLDivElement, newScale: number): ComponentsSizesType;
4
4
  export declare const getBounds: (wrapperWidth: number, newContentWidth: number, diffWidth: number, wrapperHeight: number, newContentHeight: number, diffHeight: number, centerZoomedOut: boolean) => BoundsType;
5
5
  export declare const calculateBounds: (contextInstance: ReactZoomPanPinchContext, newScale: number) => BoundsType;
6
+ export declare function clamp(v: number, min: number, max: number): number;
7
+ export declare function rubberbandIfOutOfBounds(position: number, min: number, max: number, constant?: number): number;
6
8
  /**
7
9
  * Keeps value between given bounds, used for limiting view to given boundaries
8
10
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -62,6 +62,9 @@ export type ReactZoomPanPinchProps = {
62
62
  velocityDisabled?: boolean;
63
63
  lockAxisX?: boolean;
64
64
  lockAxisY?: boolean;
65
+ allowLeftClickPan?: boolean;
66
+ allowMiddleClickPan?: boolean;
67
+ allowRightClickPan?: boolean;
65
68
  activationKeys?: string[];
66
69
  excluded?: string[];
67
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-zoom-pan-pinch",
3
- "version": "3.1.1",
3
+ "version": "3.3.0",
4
4
  "description": "Zoom and pan html elements in easy way",
5
5
  "author": "prc5",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@ export const TransformComponent: React.FC<Props> = ({
24
24
  wrapperProps = {},
25
25
  contentProps = {},
26
26
  }: Props) => {
27
- const { init } = useContext(Context);
27
+ const { init, cleanupWindowEvents } = useContext(Context);
28
28
 
29
29
  const wrapperRef = useRef<HTMLDivElement | null>(null);
30
30
  const contentRef = useRef<HTMLDivElement | null>(null);
@@ -33,9 +33,12 @@ export const TransformComponent: React.FC<Props> = ({
33
33
  const wrapper = wrapperRef.current;
34
34
  const content = contentRef.current;
35
35
  if (wrapper !== null && content !== null && init) {
36
- init(wrapper, content);
36
+ init?.(wrapper, content);
37
37
  }
38
38
  // eslint-disable-next-line react-hooks/exhaustive-deps
39
+ return () => {
40
+ cleanupWindowEvents?.();
41
+ };
39
42
  }, []);
40
43
 
41
44
  return (
@@ -32,9 +32,6 @@ export const TransformWrapper = React.forwardRef(
32
32
 
33
33
  useEffect(() => {
34
34
  instance.update(props);
35
- return () => {
36
- instance.cleanupWindowEvents();
37
- };
38
35
  }, [instance, props]);
39
36
 
40
37
  return <Context.Provider value={instance}>{content}</Context.Provider>;
@@ -34,6 +34,9 @@ export const initialSetup: LibrarySetup = {
34
34
  velocityDisabled: false,
35
35
  lockAxisX: false,
36
36
  lockAxisY: false,
37
+ allowLeftClickPan: true,
38
+ allowMiddleClickPan: true,
39
+ allowRightClickPan: true,
37
40
  activationKeys: [],
38
41
  excluded: [],
39
42
  },
@@ -91,6 +91,36 @@ export const calculateBounds = (
91
91
  return bounds;
92
92
  };
93
93
 
94
+ export function clamp(v: number, min: number, max: number) {
95
+ return Math.max(min, Math.min(v, max));
96
+ }
97
+
98
+ // Based on @aholachek ;)
99
+ // https://twitter.com/chpwn/status/285540192096497664
100
+ // iOS constant = 0.55
101
+
102
+ // https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
103
+
104
+ function rubberband(distance: number, dimension: number, constant: number) {
105
+ if (dimension === 0 || Math.abs(dimension) === Infinity)
106
+ return distance ** (constant * 5);
107
+ return (distance * dimension * constant) / (dimension + constant * distance);
108
+ }
109
+
110
+ export function rubberbandIfOutOfBounds(
111
+ position: number,
112
+ min: number,
113
+ max: number,
114
+ constant = 0.15,
115
+ ) {
116
+ if (constant === 0) return clamp(position, min, max);
117
+ if (position < min)
118
+ return -rubberband(min - position, max - min, constant) + min;
119
+ if (position > max)
120
+ return +rubberband(position - max, max - min, constant) + max;
121
+ return position;
122
+ }
123
+
94
124
  /**
95
125
  * Keeps value between given bounds, used for limiting view to given boundaries
96
126
  * 1# eg. boundLimiter(2, 0, 3, true) => 2
@@ -208,6 +208,10 @@ export class ZoomPanPinch {
208
208
  const keysPressed = this.isPressingKeys(this.setup.panning.activationKeys);
209
209
  if (!keysPressed) return;
210
210
 
211
+ if (event.button === 0 && !this.setup.panning.allowLeftClickPan) return;
212
+ if (event.button === 1 && !this.setup.panning.allowMiddleClickPan) return;
213
+ if (event.button === 2 && !this.setup.panning.allowRightClickPan) return;
214
+
211
215
  event.preventDefault();
212
216
  event.stopPropagation();
213
217
 
@@ -81,6 +81,9 @@ export type ReactZoomPanPinchProps = {
81
81
  velocityDisabled?: boolean;
82
82
  lockAxisX?: boolean;
83
83
  lockAxisY?: boolean;
84
+ allowLeftClickPan?: boolean;
85
+ allowMiddleClickPan?: boolean;
86
+ allowRightClickPan?: boolean;
84
87
  activationKeys?: string[];
85
88
  excluded?: string[];
86
89
  };
@@ -248,6 +248,24 @@ export const wrapperPropsTable: ComponentProps = {
248
248
  description:
249
249
  "Disable the panning feature for the Y axis (prevents vertical panning).",
250
250
  },
251
+ allowLeftClickPan: {
252
+ type: ["boolean"],
253
+ defaultValue: String(initialSetup.panning.allowLeftClickPan),
254
+ description:
255
+ "Allow left click to initiate panning",
256
+ },
257
+ allowMiddleClickPan: {
258
+ type: ["boolean"],
259
+ defaultValue: String(initialSetup.panning.allowMiddleClickPan),
260
+ description:
261
+ "Allow middle click to initiate panning",
262
+ },
263
+ allowRightClickPan: {
264
+ type: ["boolean"],
265
+ defaultValue: String(initialSetup.panning.allowRightClickPan),
266
+ description:
267
+ "Allow right click to initiate panning",
268
+ },
251
269
  activationKeys: {
252
270
  type: ["string[]"],
253
271
  defaultValue: String(initialSetup.panning.activationKeys),