react-zoom-pan-pinch 3.7.0 → 4.0.1

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.
Files changed (58) hide show
  1. package/dist/index.cjs.js +785 -310
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +117 -23
  4. package/dist/index.esm.js +781 -302
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +74 -62
  7. package/src/components/index.ts +5 -0
  8. package/src/components/keep-scale/keep-scale.tsx +28 -0
  9. package/src/components/mini-map/mini-map.tsx +292 -0
  10. package/src/components/mini-map/use-resize.hook.ts +74 -0
  11. package/src/components/transform-component/transform-component.module.css +43 -0
  12. package/src/components/transform-component/transform-component.tsx +96 -0
  13. package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
  14. package/src/components/virtualize/virtualize.tsx +133 -0
  15. package/src/components/virtualize/virtualize.utils.ts +104 -0
  16. package/src/constants/state.constants.ts +99 -0
  17. package/src/core/animations/animations.constants.ts +78 -0
  18. package/src/core/animations/animations.utils.ts +115 -0
  19. package/src/core/bounds/bounds.types.ts +8 -0
  20. package/src/core/bounds/bounds.utils.ts +226 -0
  21. package/src/core/double-click/double-click.logic.ts +127 -0
  22. package/src/core/handlers/handlers.logic.ts +122 -0
  23. package/src/core/handlers/handlers.utils.ts +243 -0
  24. package/src/core/instance.core.ts +704 -0
  25. package/src/core/pan/panning.logic.ts +122 -0
  26. package/src/core/pan/panning.utils.ts +216 -0
  27. package/src/core/pan/velocity.logic.ts +187 -0
  28. package/src/core/pan/velocity.utils.ts +83 -0
  29. package/src/core/pinch/pinch.logic.ts +139 -0
  30. package/src/core/pinch/pinch.utils.ts +95 -0
  31. package/src/core/wheel/wheel.logic.ts +161 -0
  32. package/src/core/wheel/wheel.utils.ts +157 -0
  33. package/src/core/zoom/zoom.logic.ts +71 -0
  34. package/src/core/zoom/zoom.utils.ts +62 -0
  35. package/src/hooks/index.ts +5 -0
  36. package/src/hooks/use-controls.tsx +9 -0
  37. package/src/hooks/use-transform-component.tsx +30 -0
  38. package/src/hooks/use-transform-context.tsx +13 -0
  39. package/src/hooks/use-transform-effect.tsx +22 -0
  40. package/src/hooks/use-transform-init.tsx +28 -0
  41. package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
  42. package/src/index.ts +6 -0
  43. package/src/models/calculations.model.ts +34 -0
  44. package/src/models/context.model.ts +218 -0
  45. package/src/models/helpers.model.ts +15 -0
  46. package/src/models/index.ts +3 -0
  47. package/src/react-app-env.d.ts +1 -0
  48. package/src/styles.css +8 -0
  49. package/src/typings.d.ts +21 -0
  50. package/src/utils/calculations.utils.ts +16 -0
  51. package/src/utils/callback.utils.ts +11 -0
  52. package/src/utils/context.utils.ts +49 -0
  53. package/src/utils/event.utils.ts +38 -0
  54. package/src/utils/helpers.utils.ts +22 -0
  55. package/src/utils/index.ts +8 -0
  56. package/src/utils/ref.utils.ts +32 -0
  57. package/src/utils/state.utils.ts +77 -0
  58. package/src/utils/styles.utils.ts +49 -0
@@ -0,0 +1,77 @@
1
+ import {
2
+ LibrarySetup,
3
+ ReactZoomPanPinchProps,
4
+ ReactZoomPanPinchState,
5
+ } from "../models/context.model";
6
+ import { initialSetup, initialState } from "../constants/state.constants";
7
+ import { boundLimiter } from "core/bounds/bounds.utils";
8
+
9
+ export const createState = (
10
+ props: ReactZoomPanPinchProps,
11
+ ): ReactZoomPanPinchState => {
12
+ const minScale = Math.max(props.minScale ?? initialSetup.minScale, 1e-7);
13
+ const maxScale = props.maxScale ?? initialSetup.maxScale;
14
+ const rawScale = props.initialScale ?? initialState.scale;
15
+ const scale = Math.min(Math.max(rawScale, minScale), maxScale);
16
+
17
+ const positionX = boundLimiter(
18
+ props.initialPositionX ?? initialState.positionX,
19
+ props.minPositionX ?? -Infinity,
20
+ props.maxPositionX ?? Infinity,
21
+ props.minPositionX != null || props.maxPositionX != null,
22
+ );
23
+
24
+ const positionY = boundLimiter(
25
+ props.initialPositionY ?? initialState.positionY,
26
+ props.minPositionY ?? -Infinity,
27
+ props.maxPositionY ?? Infinity,
28
+ props.minPositionY != null || props.maxPositionY != null,
29
+ );
30
+
31
+ return {
32
+ previousScale: scale,
33
+ scale,
34
+ positionX,
35
+ positionY,
36
+ };
37
+ };
38
+
39
+ export const createSetup = (props: ReactZoomPanPinchProps): LibrarySetup => {
40
+ const newSetup = { ...initialSetup };
41
+
42
+ Object.keys(props).forEach((key: string) => {
43
+ const k = key as keyof ReactZoomPanPinchProps & keyof LibrarySetup;
44
+ const validValue =
45
+ typeof (props as Record<string, unknown>)[k] !== "undefined";
46
+ const validParameter =
47
+ typeof (initialSetup as Record<string, unknown>)[k] !== "undefined";
48
+ if (validParameter && validValue) {
49
+ const dataType = Object.prototype.toString.call(
50
+ (initialSetup as Record<string, unknown>)[k],
51
+ );
52
+ const isObject = dataType === "[object Object]";
53
+ const isArray = dataType === "[object Array]";
54
+ if (isObject) {
55
+ (newSetup as Record<string, unknown>)[k] = {
56
+ ...((initialSetup as Record<string, unknown>)[k] as object),
57
+ ...((props as Record<string, unknown>)[k] as object),
58
+ };
59
+ } else if (isArray) {
60
+ (newSetup as Record<string, unknown>)[k] = [
61
+ ...((initialSetup as Record<string, unknown>)[k] as unknown[]),
62
+ ...((props as Record<string, unknown>)[k] as unknown[]),
63
+ ];
64
+ } else {
65
+ (newSetup as Record<string, unknown>)[k] = (
66
+ props as Record<string, unknown>
67
+ )[k];
68
+ }
69
+ }
70
+ });
71
+
72
+ if (newSetup.minScale <= 0) {
73
+ newSetup.minScale = 1e-7;
74
+ }
75
+
76
+ return newSetup;
77
+ };
@@ -0,0 +1,49 @@
1
+ import { StateType } from "models";
2
+
3
+ /** Drop binary float noise on scale (e.g. 1.5000000000000002) without clipping real zoom values. */
4
+ export const roundScaleForTransform = (scale: number): number =>
5
+ Number.parseFloat(scale.toFixed(8));
6
+
7
+ export const getTransformStyles = (
8
+ x: number,
9
+ y: number,
10
+ scale: number,
11
+ ): string => {
12
+ // Standard translate prevents blurry svg on the safari
13
+ const s = roundScaleForTransform(scale);
14
+ return `translate(${x}px, ${y}px) scale(${s})`;
15
+ };
16
+
17
+ export const getMatrixTransformStyles = (
18
+ x: number,
19
+ y: number,
20
+ scale: number,
21
+ ): string => {
22
+ // The shorthand for matrix does not work for Safari hence the need to explicitly use matrix3d
23
+ // Refer to https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
24
+ const a = scale;
25
+ const b = 0;
26
+ const c = 0;
27
+ const d = scale;
28
+ const tx = x;
29
+ const ty = y;
30
+ return `matrix3d(${a}, ${b}, 0, 0, ${c}, ${d}, 0, 0, 0, 0, 1, 0, ${tx}, ${ty}, 0, 1)`;
31
+ };
32
+
33
+ export const getCenterPosition = (
34
+ scale: number,
35
+ wrapperComponent: HTMLDivElement,
36
+ contentComponent: HTMLDivElement,
37
+ ): StateType => {
38
+ const contentWidth = contentComponent.offsetWidth * scale;
39
+ const contentHeight = contentComponent.offsetHeight * scale;
40
+
41
+ const centerPositionX = (wrapperComponent.offsetWidth - contentWidth) / 2;
42
+ const centerPositionY = (wrapperComponent.offsetHeight - contentHeight) / 2;
43
+
44
+ return {
45
+ scale,
46
+ positionX: centerPositionX,
47
+ positionY: centerPositionY,
48
+ };
49
+ };