react-zoom-pan-pinch 3.7.0 → 4.0.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.
Files changed (139) 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 +73 -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/stories/assets/big-image.jpeg +0 -0
  49. package/src/stories/assets/map.jpg +0 -0
  50. package/src/stories/assets/medium-image.jpg +0 -0
  51. package/src/stories/assets/pin.svg +1 -0
  52. package/src/stories/assets/small-image.jpg +0 -0
  53. package/src/stories/assets/svg-example.svg +14 -0
  54. package/src/stories/docs/DocTable.tsx +59 -0
  55. package/src/stories/docs/HandlerCards.tsx +102 -0
  56. package/src/stories/docs/Introduction.stories.mdx +135 -0
  57. package/src/stories/docs/PropTable.tsx +116 -0
  58. package/src/stories/docs/docs.module.css +370 -0
  59. package/src/stories/docs/handlers.stories.mdx +7 -0
  60. package/src/stories/docs/handlers.tsx +111 -0
  61. package/src/stories/docs/props.stories.mdx +11 -0
  62. package/src/stories/docs/props.tsx +571 -0
  63. package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
  64. package/src/stories/examples/axis-lock/example.tsx +114 -0
  65. package/src/stories/examples/bounds/bounds.data.tsx +191 -0
  66. package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
  67. package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
  68. package/src/stories/examples/cinema/cinema-data.tsx +578 -0
  69. package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
  70. package/src/stories/examples/cinema/example.tsx +378 -0
  71. package/src/stories/examples/cinema/image.png +0 -0
  72. package/src/stories/examples/content/content.stories.mdx +172 -0
  73. package/src/stories/examples/content/mixed-content.data.tsx +624 -0
  74. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
  75. package/src/stories/examples/content-rerendering/example.tsx +631 -0
  76. package/src/stories/examples/controls/controls.stories.mdx +120 -0
  77. package/src/stories/examples/image/example.tsx +149 -0
  78. package/src/stories/examples/image/image.stories.mdx +46 -0
  79. package/src/stories/examples/image-annotations/example.tsx +1442 -0
  80. package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
  81. package/src/stories/examples/image-responsive/example.tsx +152 -0
  82. package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
  83. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
  84. package/src/stories/examples/initial-transform/example.tsx +233 -0
  85. package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
  86. package/src/stories/examples/keep-scale/example.tsx +882 -0
  87. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
  88. package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
  89. package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
  90. package/src/stories/examples/medical-viewer/example.tsx +690 -0
  91. package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
  92. package/src/stories/examples/mini-map/example.tsx +122 -0
  93. package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
  94. package/src/stories/examples/miro/example.tsx +1573 -0
  95. package/src/stories/examples/miro/miro.stories.mdx +31 -0
  96. package/src/stories/examples/padding/example.tsx +147 -0
  97. package/src/stories/examples/padding/padding.stories.mdx +35 -0
  98. package/src/stories/examples/product-card/example.tsx +594 -0
  99. package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
  100. package/src/stories/examples/stadium/example.tsx +1040 -0
  101. package/src/stories/examples/stadium/stadium-data.ts +224 -0
  102. package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
  103. package/src/stories/examples/stress/example.tsx +59 -0
  104. package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
  105. package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
  106. package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
  107. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
  108. package/src/stories/examples/text-selection/example.tsx +221 -0
  109. package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
  110. package/src/stories/examples/velocity/example.tsx +908 -0
  111. package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
  112. package/src/stories/examples/virtualize/example.tsx +239 -0
  113. package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
  114. package/src/stories/examples/zoom-to-element/example.tsx +219 -0
  115. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
  116. package/src/stories/hooks/use-controls.stories.mdx +86 -0
  117. package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
  118. package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
  119. package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
  120. package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
  121. package/src/stories/types/args.types.ts +192 -0
  122. package/src/stories/utils/args.utils.ts +35 -0
  123. package/src/stories/utils/bounds-overlay.tsx +168 -0
  124. package/src/stories/utils/code.utils.tsx +22 -0
  125. package/src/stories/utils/controls.utils.tsx +347 -0
  126. package/src/stories/utils/index.ts +5 -0
  127. package/src/stories/utils/styles.module.css +223 -0
  128. package/src/stories/utils/viewer.styles.ts +34 -0
  129. package/src/styles.css +8 -0
  130. package/src/typings.d.ts +21 -0
  131. package/src/utils/calculations.utils.ts +16 -0
  132. package/src/utils/callback.utils.ts +11 -0
  133. package/src/utils/context.utils.ts +49 -0
  134. package/src/utils/event.utils.ts +38 -0
  135. package/src/utils/helpers.utils.ts +22 -0
  136. package/src/utils/index.ts +8 -0
  137. package/src/utils/ref.utils.ts +32 -0
  138. package/src/utils/state.utils.ts +77 -0
  139. package/src/utils/styles.utils.ts +49 -0
@@ -0,0 +1,223 @@
1
+ .wrapper {
2
+ width: calc(100vw - 200px);
3
+ height: calc(100vh - 200px);
4
+ }
5
+
6
+ /* ── Control bar (glassmorphism toolbar) ─────────────────────── */
7
+
8
+ .controlBar {
9
+ position: absolute;
10
+ z-index: 10;
11
+ display: flex;
12
+ align-items: center;
13
+ gap: 2px;
14
+ padding: 4px;
15
+ border-radius: 12px;
16
+ background: rgba(15, 15, 20, 0.72);
17
+ backdrop-filter: blur(16px) saturate(1.6);
18
+ -webkit-backdrop-filter: blur(16px) saturate(1.6);
19
+ border: 1px solid rgba(255, 255, 255, 0.08);
20
+ box-shadow:
21
+ 0 2px 8px rgba(0, 0, 0, 0.3),
22
+ 0 0 0 0.5px rgba(255, 255, 255, 0.04) inset;
23
+ font-family: system-ui, -apple-system, sans-serif;
24
+ }
25
+
26
+ .controlsTopLeft {
27
+ top: 25px;
28
+ left: 25px;
29
+ }
30
+
31
+ .controlsTopCenter {
32
+ top: 25px;
33
+ left: 50%;
34
+ transform: translateX(-50%);
35
+ }
36
+
37
+ .controlsTopRight {
38
+ top: 25px;
39
+ right: 25px;
40
+ }
41
+
42
+ .controlsBottomLeft {
43
+ bottom: 25px;
44
+ left: 25px;
45
+ }
46
+
47
+ .controlsBottomRight {
48
+ bottom: 25px;
49
+ right: 25px;
50
+ }
51
+
52
+ /* ── Button groups ───────────────────────────────────────────── */
53
+
54
+ .controlGroup {
55
+ display: flex;
56
+ align-items: center;
57
+ gap: 2px;
58
+ }
59
+
60
+ .controlDivider {
61
+ width: 1px;
62
+ height: 20px;
63
+ margin: 0 4px;
64
+ background: rgba(255, 255, 255, 0.1);
65
+ border-radius: 1px;
66
+ flex-shrink: 0;
67
+ }
68
+
69
+ /* ── Icon button ─────────────────────────────────────────────── */
70
+
71
+ .controlIcon {
72
+ position: relative;
73
+ display: flex;
74
+ align-items: center;
75
+ justify-content: center;
76
+ width: 32px;
77
+ height: 32px;
78
+ padding: 0;
79
+ border: none;
80
+ border-radius: 8px;
81
+ background: transparent;
82
+ color: rgba(255, 255, 255, 0.7);
83
+ cursor: pointer;
84
+ transition:
85
+ background 0.15s ease,
86
+ color 0.15s ease;
87
+ }
88
+
89
+ .controlIconActive {
90
+ background: rgba(96, 165, 250, 0.2);
91
+ color: rgba(191, 219, 254, 0.95);
92
+ box-shadow: 0 0 0 1px rgba(96, 165, 250, 0.35) inset;
93
+ }
94
+
95
+ .controlIcon:hover {
96
+ background: rgba(255, 255, 255, 0.1);
97
+ color: #fff;
98
+ }
99
+
100
+ .controlIcon:active {
101
+ background: rgba(255, 255, 255, 0.16);
102
+ color: #fff;
103
+ transform: scale(0.94);
104
+ }
105
+
106
+ .controlIcon:focus-visible {
107
+ outline: 2px solid rgba(100, 160, 255, 0.6);
108
+ outline-offset: -1px;
109
+ }
110
+
111
+ /* ── Tooltip ─────────────────────────────────────────────────── */
112
+
113
+ .controlIcon[data-tooltip]::after,
114
+ .controlText[data-tooltip]::after {
115
+ content: attr(data-tooltip);
116
+ position: absolute;
117
+ bottom: calc(100% + 8px);
118
+ left: 50%;
119
+ transform: translateX(-50%) scale(0.92);
120
+ padding: 5px 10px;
121
+ border-radius: 8px;
122
+ background: rgba(10, 10, 14, 0.92);
123
+ border: 1px solid rgba(255, 255, 255, 0.1);
124
+ backdrop-filter: blur(10px);
125
+ -webkit-backdrop-filter: blur(10px);
126
+ color: #e4e4e7;
127
+ font-size: 11px;
128
+ font-weight: 600;
129
+ font-family: inherit;
130
+ white-space: nowrap;
131
+ pointer-events: none;
132
+ opacity: 0;
133
+ transition:
134
+ opacity 0.15s ease,
135
+ transform 0.15s ease;
136
+ z-index: 100;
137
+ }
138
+
139
+ .controlIcon[data-tooltip]:hover::after,
140
+ .controlText[data-tooltip]:hover::after {
141
+ opacity: 1;
142
+ transform: translateX(-50%) scale(1);
143
+ }
144
+
145
+ /* ── Text button (for labels like "Zoom to X") ───────────────── */
146
+
147
+ .controlText {
148
+ position: relative;
149
+ display: flex;
150
+ align-items: center;
151
+ justify-content: center;
152
+ height: 32px;
153
+ padding: 0 12px;
154
+ border: none;
155
+ border-radius: 8px;
156
+ background: transparent;
157
+ color: rgba(255, 255, 255, 0.7);
158
+ font-size: 12px;
159
+ font-weight: 600;
160
+ letter-spacing: 0.02em;
161
+ white-space: nowrap;
162
+ cursor: pointer;
163
+ font-family: inherit;
164
+ transition:
165
+ background 0.15s ease,
166
+ color 0.15s ease;
167
+ }
168
+
169
+ .controlText:hover {
170
+ background: rgba(255, 255, 255, 0.1);
171
+ color: #fff;
172
+ }
173
+
174
+ .controlText:active {
175
+ background: rgba(255, 255, 255, 0.16);
176
+ color: #fff;
177
+ transform: scale(0.97);
178
+ }
179
+
180
+ .controlText:focus-visible {
181
+ outline: 2px solid rgba(100, 160, 255, 0.6);
182
+ outline-offset: -1px;
183
+ }
184
+
185
+ /* ── Legacy single button (kept for stories still using it) ─── */
186
+
187
+ .controlPanel {
188
+ position: absolute;
189
+ z-index: 2;
190
+ transform: translate(10px, 10px);
191
+ max-width: calc(100vw - 20px);
192
+ }
193
+
194
+ .controlBtn {
195
+ padding: 6px 12px;
196
+ background: white;
197
+ border: 1px solid grey;
198
+ border-radius: 5px;
199
+ margin-right: 10px;
200
+ font-size: 12px;
201
+ text-transform: uppercase;
202
+ font-weight: 600;
203
+ cursor: pointer;
204
+ }
205
+
206
+ .controlBtn:focus {
207
+ filter: brightness(90%);
208
+ }
209
+
210
+ .controlBtn:hover {
211
+ filter: brightness(120%);
212
+ }
213
+
214
+ .controlBtn:active {
215
+ opacity: 0.9;
216
+ }
217
+
218
+ /* ── Stress-test grid ────────────────────────────────────────── */
219
+
220
+ .grid {
221
+ display: grid;
222
+ grid-template-columns: repeat(100, 1fr);
223
+ }
@@ -0,0 +1,34 @@
1
+ import type React from "react";
2
+
3
+ /**
4
+ * Checkerboard "transparency grid" background — reusable across stories.
5
+ * Apply to TransformComponent's `wrapperStyle` so zoomed-out content shows
6
+ * a visible canvas boundary instead of a flat dark void.
7
+ */
8
+ export const checkerboard: React.CSSProperties = {
9
+ background: "#0a0a14",
10
+ backgroundImage: [
11
+ "linear-gradient(45deg, #0e0e1a 25%, transparent 25%)",
12
+ "linear-gradient(-45deg, #0e0e1a 25%, transparent 25%)",
13
+ "linear-gradient(45deg, transparent 75%, #0e0e1a 75%)",
14
+ "linear-gradient(-45deg, transparent 75%, #0e0e1a 75%)",
15
+ ].join(", "),
16
+ backgroundSize: "20px 20px",
17
+ backgroundPosition: "0 0, 0 10px, 10px -10px, -10px 0px",
18
+ };
19
+
20
+ /** Common glass-panel frame used by most story viewers. */
21
+ export const viewerFrame: React.CSSProperties = {
22
+ borderRadius: "12px",
23
+ border: "2px solid rgba(255,255,255,0.08)",
24
+ boxShadow: "0 8px 32px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255,255,255,0.03)",
25
+ };
26
+
27
+ /**
28
+ * Full "viewer chrome" — frame + checkerboard.
29
+ * Spread into `wrapperStyle` together with sizing overrides.
30
+ */
31
+ export const viewerChrome: React.CSSProperties = {
32
+ ...viewerFrame,
33
+ ...checkerboard,
34
+ };
package/src/styles.css ADDED
@@ -0,0 +1,8 @@
1
+ /* add css styles here (optional) */
2
+
3
+ .test {
4
+ display: inline-block;
5
+ margin: 2em auto;
6
+ border: 2px solid #000;
7
+ font-size: 2em;
8
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Default CSS definition for typescript,
3
+ * will be overridden with file-specific definitions by rollup
4
+ */
5
+ declare module "*.css" {
6
+ const content: { [className: string]: string };
7
+ export default content;
8
+ }
9
+ declare module "*.svg" {
10
+ import React = require("react");
11
+
12
+ export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
13
+ const src: string;
14
+ export default src;
15
+ }
16
+
17
+ declare module "*.jpg";
18
+ declare module "*.jpeg";
19
+ declare module "*.png";
20
+
21
+ type SvgrComponent = React.StatelessComponent<React.SVGAttributes<SVGElement>>;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Rounds number to given decimal
3
+ * eg. roundNumber(2.34343, 1) => 2.3
4
+ */
5
+ export const roundNumber = (num: number, decimal: number) => {
6
+ return Number(num.toFixed(decimal));
7
+ };
8
+
9
+ /**
10
+ * Checks if value is number, if not it returns default value
11
+ * 1# eg. checkIsNumber(2, 30) => 2
12
+ * 2# eg. checkIsNumber(null, 30) => 30
13
+ */
14
+ export const checkIsNumber = (num: any, defaultValue: number) => {
15
+ return typeof num === "number" ? num : defaultValue;
16
+ };
@@ -0,0 +1,11 @@
1
+ import { ReactZoomPanPinchRef } from "../models";
2
+
3
+ export const handleCallback = <T>(
4
+ context: ReactZoomPanPinchRef,
5
+ event: T,
6
+ callback?: (context: ReactZoomPanPinchRef, event: T) => void,
7
+ ): void => {
8
+ if (callback && typeof callback === "function") {
9
+ callback(context, event);
10
+ }
11
+ };
@@ -0,0 +1,49 @@
1
+ import {
2
+ ReactZoomPanPinchContentRef,
3
+ ReactZoomPanPinchContext,
4
+ ReactZoomPanPinchContextState,
5
+ ReactZoomPanPinchRef,
6
+ } from "../models/context.model";
7
+ import {
8
+ zoomIn,
9
+ zoomOut,
10
+ setTransform,
11
+ resetTransform,
12
+ zoomToElement,
13
+ centerView,
14
+ } from "../core/handlers/handlers.logic";
15
+
16
+ export const getControls = (
17
+ contextInstance: ReactZoomPanPinchContext,
18
+ ): ReactZoomPanPinchContentRef => {
19
+ return {
20
+ instance: contextInstance,
21
+ state: contextInstance.state,
22
+ zoomIn: zoomIn(contextInstance),
23
+ zoomOut: zoomOut(contextInstance),
24
+ setTransform: setTransform(contextInstance),
25
+ resetTransform: resetTransform(contextInstance),
26
+ centerView: centerView(contextInstance),
27
+ zoomToElement: zoomToElement(contextInstance),
28
+ };
29
+ };
30
+
31
+ export const getState = (
32
+ contextInstance: ReactZoomPanPinchContext,
33
+ ): ReactZoomPanPinchContextState => {
34
+ return {
35
+ instance: contextInstance,
36
+ state: contextInstance.state,
37
+ };
38
+ };
39
+
40
+ export const getContext = (
41
+ contextInstance: ReactZoomPanPinchContext,
42
+ ): ReactZoomPanPinchRef => {
43
+ const ref = {} as ReactZoomPanPinchRef;
44
+
45
+ Object.assign(ref, getState(contextInstance));
46
+ Object.assign(ref, getControls(contextInstance));
47
+
48
+ return ref;
49
+ };
@@ -0,0 +1,38 @@
1
+ // We want to make event listeners non-passive, and to do so have to check
2
+ // that browsers support EventListenerOptions in the first place.
3
+
4
+ import { DeviceType } from "models";
5
+
6
+ // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
7
+ let passiveSupported = false;
8
+
9
+ export function makePassiveEventOption(): any {
10
+ try {
11
+ const options = {
12
+ get passive() {
13
+ // This function will be called when the browser
14
+ // attempts to access the passive property.
15
+ passiveSupported = true;
16
+ return false;
17
+ },
18
+ };
19
+
20
+ return options;
21
+ } catch (err) {
22
+ passiveSupported = false;
23
+ return passiveSupported;
24
+ }
25
+ }
26
+
27
+ export function isTrackPad(
28
+ event: WheelEvent & { wheelDeltaY: number },
29
+ ): DeviceType.TRACK_PAD | DeviceType.MOUSE {
30
+ if (event.wheelDeltaY) {
31
+ if (Math.abs(event.wheelDeltaY) !== 120) {
32
+ return DeviceType.TRACK_PAD;
33
+ }
34
+ } else if (event.deltaMode === 0) {
35
+ return DeviceType.TRACK_PAD;
36
+ }
37
+ return DeviceType.MOUSE;
38
+ }
@@ -0,0 +1,22 @@
1
+ import { baseClasses } from "../constants/state.constants";
2
+
3
+ const matchPrefix = `.${baseClasses.wrapperClass}`;
4
+
5
+ export const isExcludedNode = (
6
+ node: HTMLElement,
7
+ excluded: string[],
8
+ ): boolean => {
9
+ return excluded.some((exclude) =>
10
+ node.matches(
11
+ `${matchPrefix} ${exclude}, ${matchPrefix} .${exclude}, ${matchPrefix} ${exclude} *, ${matchPrefix} .${exclude} *`,
12
+ ),
13
+ );
14
+ };
15
+
16
+ export const cancelTimeout = (
17
+ timeout: ReturnType<typeof setTimeout> | null,
18
+ ): void => {
19
+ if (timeout) {
20
+ clearTimeout(timeout);
21
+ }
22
+ };
@@ -0,0 +1,8 @@
1
+ export * from "./calculations.utils";
2
+ export * from "./callback.utils";
3
+ export * from "./context.utils";
4
+ export * from "./event.utils";
5
+ export * from "./helpers.utils";
6
+ export * from "./state.utils";
7
+ export * from "./styles.utils";
8
+ export * from "./ref.utils";
@@ -0,0 +1,32 @@
1
+ /* eslint-disable no-param-reassign */
2
+ import type * as React from "react";
3
+
4
+ export function assignRef<T>(
5
+ ref:
6
+ | ((value: T | null) => void)
7
+ | React.MutableRefObject<T | null>
8
+ | null
9
+ | undefined,
10
+ value: T | null,
11
+ ): void {
12
+ if (ref == null) return;
13
+ if (typeof ref === "function") {
14
+ ref(value);
15
+ } else {
16
+ ref.current = value;
17
+ }
18
+ }
19
+
20
+ export function mergeRefs<T = any>(
21
+ refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>,
22
+ ): React.RefCallback<T> {
23
+ return (value) => {
24
+ refs.forEach((ref) => {
25
+ if (typeof ref === "function") {
26
+ ref(value);
27
+ } else if (ref != null) {
28
+ (ref as React.MutableRefObject<T | null>).current = value;
29
+ }
30
+ });
31
+ };
32
+ }
@@ -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
+ };