react-zoom-pan-pinch 2.3.0 → 2.3.2

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.
@@ -1,4 +1,10 @@
1
- import React, { useImperativeHandle, useMemo, useRef } from "react";
1
+ import React, {
2
+ useEffect,
3
+ useImperativeHandle,
4
+ useMemo,
5
+ useRef,
6
+ useState,
7
+ } from "react";
2
8
 
3
9
  import { ZoomPanPinch } from "../core/instance.core";
4
10
  import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "../models";
@@ -12,8 +18,15 @@ export const TransformWrapper = React.forwardRef(
12
18
  props: Omit<ReactZoomPanPinchProps, "ref">,
13
19
  ref: React.Ref<ReactZoomPanPinchRef>,
14
20
  ) => {
15
- const { children } = props;
16
- const instance = useRef(new ZoomPanPinch(props)).current;
21
+ const [, forceUpdate] = useState(0);
22
+ const { children, performance } = props;
23
+ const instance = useRef(
24
+ new ZoomPanPinch(props, () => {
25
+ if (!performance) {
26
+ forceUpdate((prev) => prev + 1);
27
+ }
28
+ }),
29
+ ).current;
17
30
 
18
31
  const content = useMemo(() => {
19
32
  if (typeof children === "function") {
@@ -33,6 +46,10 @@ export const TransformWrapper = React.forwardRef(
33
46
 
34
47
  useImperativeHandle(ref, () => getContext(instance), [instance]);
35
48
 
49
+ useEffect(() => {
50
+ instance.update(props);
51
+ }, [instance, props]);
52
+
36
53
  return <Context.Provider value={value}>{content}</Context.Provider>;
37
54
  },
38
55
  );
@@ -23,6 +23,7 @@ export const contextInitialState: ReactZoomPanPinchComponentHelpers &
23
23
 
24
24
  export const initialSetup: LibrarySetup = {
25
25
  disabled: false,
26
+ performance: false,
26
27
  minPositionX: null,
27
28
  maxPositionX: null,
28
29
  minPositionY: null,
@@ -6,6 +6,7 @@ import {
6
6
  AnimationType,
7
7
  ReactZoomPanPinchProps,
8
8
  ReactZoomPanPinchState,
9
+ ReactZoomPanPinchRef,
9
10
  } from "../models";
10
11
  import {
11
12
  getContext,
@@ -44,6 +45,8 @@ import {
44
45
  type StartCoordsType = { x: number; y: number } | null;
45
46
 
46
47
  export class ZoomPanPinch {
48
+ public props: ReactZoomPanPinchProps;
49
+
47
50
  public mounted = true;
48
51
 
49
52
  public transformState: ReactZoomPanPinchState;
@@ -81,7 +84,11 @@ export class ZoomPanPinch {
81
84
  // key press
82
85
  public pressedKeys: { [key: string]: boolean } = {};
83
86
 
84
- constructor(public props: ReactZoomPanPinchProps) {
87
+ constructor(
88
+ props: ReactZoomPanPinchProps,
89
+ private onChange?: (ctx: ReactZoomPanPinchRef) => void,
90
+ ) {
91
+ this.props = props;
85
92
  this.setup = createSetup(this.props);
86
93
  this.transformState = createState(this.props);
87
94
  }
@@ -402,11 +409,10 @@ export class ZoomPanPinch {
402
409
  this.transformState.positionX = positionX;
403
410
  this.transformState.positionY = positionY;
404
411
 
405
- handleCallback(
406
- getContext(this),
407
- { scale, positionX, positionY },
408
- onTransformed,
409
- );
412
+ const ctx = getContext(this);
413
+ this.onChange?.(ctx);
414
+
415
+ handleCallback(ctx, { scale, positionX, positionY }, onTransformed);
410
416
  this.applyTransformation();
411
417
  } else {
412
418
  console.error("Detected NaN set state values");
@@ -42,6 +42,7 @@ export type ReactZoomPanPinchRefProps = {
42
42
  export type ReactZoomPanPinchProps = {
43
43
  children?: React.ReactNode | ((ref: ReactZoomPanPinchRef) => React.ReactNode);
44
44
  ref?: React.Ref<ReactZoomPanPinchRef>;
45
+ performance?: boolean;
45
46
  initialScale?: number;
46
47
  initialPositionX?: number;
47
48
  initialPositionY?: number;
@@ -101,6 +101,12 @@ export const wrapperPropsTable: ComponentProps = {
101
101
  description:
102
102
  "This is 'global' disable props. Setting it as a 'true' value blocks all functionalities of library. For disabling parts of it like panning, zooming, pinch - checkout the dedicated sections below.",
103
103
  },
104
+ performance: {
105
+ type: ["boolean"],
106
+ defaultValue: String(initialSetup.performance),
107
+ description:
108
+ "Enable this prop to switch to limited rendering performance mode",
109
+ },
104
110
  minPositionX: {
105
111
  type: ["number"],
106
112
  defaultValue: String(initialSetup.minPositionX),