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,95 @@
1
+ import React from "react";
2
+
3
+ import { TransformComponent, TransformWrapper } from "components";
4
+ import {
5
+ Controls,
6
+ NumberedTargetIcon,
7
+ normalizeArgs,
8
+ viewerChrome,
9
+ } from "../../utils";
10
+ import { ReactComponent as Creativity } from "./creativity.svg";
11
+
12
+ const ZOOMABLE_IDS = ["element1", "element2", "element3"];
13
+
14
+ function findZoomableId(el: HTMLElement | SVGElement | null): string | null {
15
+ let node = el;
16
+ while (node) {
17
+ if (node.id && ZOOMABLE_IDS.includes(node.id)) return node.id;
18
+ node = node.parentElement as HTMLElement | null;
19
+ }
20
+ return null;
21
+ }
22
+
23
+ export const Example: React.FC<any> = (args: any) => {
24
+ return (
25
+ <div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
26
+ <style>{`
27
+ #element1, #element2, #element3 {
28
+ cursor: pointer;
29
+ transition: filter 0.2s ease, opacity 0.2s ease;
30
+ }
31
+ #element1:hover, #element2:hover, #element3:hover {
32
+ filter: brightness(1.3) drop-shadow(0 0 6px rgba(99, 102, 241, 0.6));
33
+ opacity: 0.85;
34
+ }
35
+ `}</style>
36
+ <TransformWrapper {...normalizeArgs(args)} centerOnInit>
37
+ {(utils) => {
38
+ const handleSvgClick = (e: React.MouseEvent) => {
39
+ const id = findZoomableId(e.target as HTMLElement);
40
+ if (id) utils.zoomToElement(id);
41
+ };
42
+
43
+ return (
44
+ <>
45
+ <Controls
46
+ {...utils}
47
+ extraButtons={[
48
+ {
49
+ label: "Focus element 1",
50
+ icon: <NumberedTargetIcon n={1} />,
51
+ onClick: () => utils.zoomToElement("element1"),
52
+ },
53
+ {
54
+ label: "Focus element 2",
55
+ icon: <NumberedTargetIcon n={2} />,
56
+ onClick: () => utils.zoomToElement("element2"),
57
+ },
58
+ {
59
+ label: "Focus element 3",
60
+ icon: <NumberedTargetIcon n={3} />,
61
+ onClick: () => utils.zoomToElement("element3"),
62
+ },
63
+ ]}
64
+ />
65
+ <TransformComponent
66
+ wrapperStyle={{
67
+ width: "500px",
68
+ height: "500px",
69
+ maxWidth: "80vw",
70
+ maxHeight: "75vh",
71
+ ...viewerChrome,
72
+ }}
73
+ >
74
+ <div
75
+ role="button"
76
+ tabIndex={0}
77
+ aria-label="Click an SVG element to zoom to it"
78
+ onClick={handleSvgClick}
79
+ onKeyDown={(e) => {
80
+ if (e.key === "Enter" || e.key === " ") {
81
+ e.preventDefault();
82
+ handleSvgClick(e as unknown as React.MouseEvent);
83
+ }
84
+ }}
85
+ >
86
+ <Creativity style={{ width: "100%" }} />
87
+ </div>
88
+ </TransformComponent>
89
+ </>
90
+ );
91
+ }}
92
+ </TransformWrapper>
93
+ </div>
94
+ );
95
+ };
@@ -0,0 +1,28 @@
1
+ import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
2
+
3
+ import { TransformWrapper, TransformComponent } from "../../../components";
4
+ import { argsTypes } from "../../types/args.types";
5
+ import { Example } from "./example";
6
+
7
+ export const Template = (args) => <Example {...args} />;
8
+
9
+ <Meta
10
+ title="Advanced/SVG Zoom To Element"
11
+ component={TransformWrapper}
12
+ argTypes={argsTypes}
13
+ />
14
+
15
+ # SVG Zoom To Element
16
+
17
+ Target elements **inside an SVG** by their `id` attribute. The control bar
18
+ buttons zoom to three different paths within the illustration. This works the
19
+ same way as HTML element targeting — `zoomToElement` finds the element by its
20
+ `id` and calculates the bounding box to frame it in view.
21
+
22
+ <Canvas>
23
+ <Story name="SVG Zoom To Element">{(args) => <Template {...args} />}</Story>
24
+ </Canvas>
25
+
26
+ ## Component API
27
+
28
+ <ArgsTable story="SVG Zoom To Element" />
@@ -0,0 +1,221 @@
1
+ import React from "react";
2
+
3
+ import { TransformWrapper, TransformComponent } from "../../../components";
4
+ import { Controls, normalizeArgs, viewerChrome } from "../../utils";
5
+
6
+ const font = 'Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
7
+
8
+ const SELECTABLE_CLASS = "selectable";
9
+
10
+ const codeStyle: React.CSSProperties = {
11
+ padding: "2px 6px",
12
+ borderRadius: 4,
13
+ background: "#e7e5e4",
14
+ color: "#c2410c",
15
+ fontSize: 12,
16
+ fontWeight: 600,
17
+ fontFamily: "'SF Mono', 'Fira Code', ui-monospace, monospace",
18
+ };
19
+
20
+ export const Example: React.FC<Record<string, unknown>> = (args) => {
21
+ const normalized = normalizeArgs(args);
22
+
23
+ return (
24
+ <div style={{ fontFamily: font, maxWidth: 720 }}>
25
+ <TransformWrapper
26
+ {...normalized}
27
+ centerOnInit
28
+ centerZoomedOut
29
+ panning={{ ...normalized.panning, excluded: [SELECTABLE_CLASS] }}
30
+ trackPadPanning={{ disabled: false }}
31
+ >
32
+ {(utils) => (
33
+ <div style={{ position: "relative" }}>
34
+ <Controls {...utils} />
35
+ <TransformComponent
36
+ wrapperStyle={{
37
+ ...viewerChrome,
38
+ width: "100%",
39
+ height: "clamp(420px, 65vh, 600px)",
40
+ }}
41
+ contentStyle={{ width: 640 }}
42
+ >
43
+ <article
44
+ className={SELECTABLE_CLASS}
45
+ style={{
46
+ width: 640,
47
+ padding: "44px 48px",
48
+ boxSizing: "border-box",
49
+ background: "#fafaf9",
50
+ borderRadius: 12,
51
+ color: "#1c1917",
52
+ userSelect: "text",
53
+ cursor: "text",
54
+ }}
55
+ >
56
+ <div
57
+ style={{
58
+ display: "inline-block",
59
+ padding: "3px 10px",
60
+ borderRadius: 5,
61
+ background: "#e0e7ff",
62
+ color: "#4338ca",
63
+ fontSize: 10,
64
+ fontWeight: 700,
65
+ letterSpacing: "0.06em",
66
+ textTransform: "uppercase",
67
+ marginBottom: 16,
68
+ }}
69
+ >
70
+ Documentation
71
+ </div>
72
+
73
+ <h1
74
+ style={{
75
+ margin: "0 0 8px",
76
+ fontSize: 28,
77
+ fontWeight: 800,
78
+ letterSpacing: "-0.03em",
79
+ lineHeight: 1.2,
80
+ color: "#0c0a09",
81
+ }}
82
+ >
83
+ Text Selection
84
+ </h1>
85
+
86
+ <p
87
+ style={{
88
+ margin: "0 0 24px",
89
+ fontSize: 14,
90
+ lineHeight: 1.7,
91
+ color: "#57534e",
92
+ }}
93
+ >
94
+ By default the library disables text selection because
95
+ click-and-drag is used for panning. To allow text selection
96
+ inside specific areas, use the{" "}
97
+ <code style={codeStyle}>panning.excluded</code> option with a
98
+ CSS class name.
99
+ </p>
100
+
101
+ <div
102
+ style={{
103
+ padding: "16px 20px",
104
+ borderRadius: 8,
105
+ background: "#1e293b",
106
+ color: "#e2e8f0",
107
+ fontFamily:
108
+ "'SF Mono', 'Fira Code', ui-monospace, monospace",
109
+ fontSize: 12,
110
+ lineHeight: 1.7,
111
+ marginBottom: 24,
112
+ overflowX: "auto",
113
+ whiteSpace: "pre",
114
+ }}
115
+ >
116
+ {`<TransformWrapper
117
+ panning={{ excluded: ["selectable"] }}
118
+ >
119
+ <TransformComponent>
120
+ <article className="selectable">
121
+ <p>You can select this text!</p>
122
+ </article>
123
+ </TransformComponent>
124
+ </TransformWrapper>`}
125
+ </div>
126
+
127
+ <h2
128
+ style={{
129
+ margin: "0 0 12px",
130
+ fontSize: 18,
131
+ fontWeight: 700,
132
+ letterSpacing: "-0.02em",
133
+ color: "#1c1917",
134
+ }}
135
+ >
136
+ How it works
137
+ </h2>
138
+
139
+ <p
140
+ style={{
141
+ margin: "0 0 16px",
142
+ fontSize: 14,
143
+ lineHeight: 1.7,
144
+ color: "#57534e",
145
+ }}
146
+ >
147
+ The <code style={codeStyle}>excluded</code> array accepts CSS
148
+ class names. Any pointer event that originates on — or inside
149
+ — an element carrying that class is ignored by the panning
150
+ handler. This lets native browser text selection work normally
151
+ while all other areas remain pannable.
152
+ </p>
153
+
154
+ <ul
155
+ style={{
156
+ margin: "0 0 24px",
157
+ padding: "0 0 0 20px",
158
+ fontSize: 14,
159
+ lineHeight: 1.8,
160
+ color: "#57534e",
161
+ }}
162
+ >
163
+ <li>
164
+ <strong style={{ color: "#1c1917" }}>Click and drag</strong>{" "}
165
+ on this text to select it — panning is suppressed.
166
+ </li>
167
+ <li>
168
+ <strong style={{ color: "#1c1917" }}>Scroll wheel</strong>{" "}
169
+ zooms in/out, and{" "}
170
+ <strong style={{ color: "#1c1917" }}>
171
+ trackpad panning
172
+ </strong>{" "}
173
+ works normally (only click-drag is excluded).
174
+ </li>
175
+ <li>
176
+ <strong style={{ color: "#1c1917" }}>Ctrl+A / Cmd+A</strong>{" "}
177
+ selects all text inside the article.
178
+ </li>
179
+ </ul>
180
+
181
+ <div
182
+ style={{
183
+ padding: "14px 18px",
184
+ borderRadius: 8,
185
+ background: "#fef3c7",
186
+ border: "1px solid #fde68a",
187
+ fontSize: 13,
188
+ lineHeight: 1.6,
189
+ color: "#92400e",
190
+ }}
191
+ >
192
+ <strong>Tip:</strong> You can combine exclusions — add both{" "}
193
+ <code
194
+ style={{
195
+ ...codeStyle,
196
+ background: "#fde68a",
197
+ color: "#78350f",
198
+ }}
199
+ >
200
+ panningDisabled
201
+ </code>{" "}
202
+ and{" "}
203
+ <code
204
+ style={{
205
+ ...codeStyle,
206
+ background: "#fde68a",
207
+ color: "#78350f",
208
+ }}
209
+ >
210
+ wheelDisabled
211
+ </code>{" "}
212
+ to block both gestures on the same element.
213
+ </div>
214
+ </article>
215
+ </TransformComponent>
216
+ </div>
217
+ )}
218
+ </TransformWrapper>
219
+ </div>
220
+ );
221
+ };
@@ -0,0 +1,31 @@
1
+ import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
2
+
3
+ import { TransformWrapper } from "../../../components";
4
+ import { argsTypes } from "../../types/args.types.ts";
5
+ import { Example } from "./example";
6
+
7
+ export const Template = (args) => <Example {...args} />;
8
+
9
+ <Meta
10
+ title="Basic/Text Selection"
11
+ component={TransformWrapper}
12
+ argTypes={argsTypes}
13
+ />
14
+
15
+ # Text Selection
16
+
17
+ Allow users to **select and copy text** inside zoomable content. By default,
18
+ click-and-drag triggers panning, which prevents native text selection. Adding
19
+ a CSS class to the `panning.excluded` array tells the library to ignore
20
+ pointer events on that element — restoring normal browser text selection.
21
+
22
+ Try it: **click and drag** over the article text below to select it. Scroll
23
+ to zoom in, then select zoomed-in text — it works at every scale.
24
+
25
+ <Canvas>
26
+ <Story name="Text Selection">{(args) => <Template {...args} />}</Story>
27
+ </Canvas>
28
+
29
+ ## Component API
30
+
31
+ <ArgsTable story="Text Selection" />