react-zoom-pan-pinch 3.6.1 → 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 +770 -277
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +116 -22
  4. package/dist/index.esm.js +766 -269
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +73 -60
  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,120 @@
1
+ import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
2
+
3
+ import { TransformWrapper, TransformComponent } from "../../../components";
4
+ import { argsTypes } from "../../types/args.types.ts";
5
+ import { normalizeArgs, Controls, viewerChrome } from "../../utils";
6
+ import { useTransformComponent } from "../../../hooks";
7
+
8
+ import exampleImg from "../../assets/small-image.jpg";
9
+
10
+ export const StateBadge = () => {
11
+ return useTransformComponent(({ state }) => (
12
+ <div
13
+ style={{
14
+ position: "absolute",
15
+ bottom: 16,
16
+ right: 16,
17
+ zIndex: 10,
18
+ padding: "8px 14px",
19
+ borderRadius: 10,
20
+ background: "rgba(10, 10, 18, 0.85)",
21
+ backdropFilter: "blur(12px)",
22
+ WebkitBackdropFilter: "blur(12px)",
23
+ border: "1px solid rgba(255,255,255,0.08)",
24
+ fontFamily: "'SF Mono', 'Fira Code', monospace",
25
+ fontSize: 11,
26
+ color: "rgba(255,255,255,0.6)",
27
+ lineHeight: 1.7,
28
+ userSelect: "none",
29
+ pointerEvents: "none",
30
+ }}
31
+ >
32
+ <div><span style={{ color: "#667eea" }}>scale</span>: {state.scale.toFixed(3)}</div>
33
+ <div><span style={{ color: "#43e97b" }}>posX</span>: {state.positionX.toFixed(1)}</div>
34
+ <div><span style={{ color: "#f093fb" }}>posY</span>: {state.positionY.toFixed(1)}</div>
35
+ </div>
36
+ ));
37
+ };
38
+
39
+ export const Template = (args) => (
40
+ <div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
41
+ <TransformWrapper {...normalizeArgs(args)}>
42
+ {(utils) => (
43
+ <>
44
+ <Controls {...utils} />
45
+ <div style={{ position: "relative", display: "inline-block" }}>
46
+ <TransformComponent
47
+ wrapperStyle={{
48
+ ...viewerChrome,
49
+ width: "500px",
50
+ height: "500px",
51
+ maxWidth: "80vw",
52
+ maxHeight: "75vh",
53
+ }}
54
+ >
55
+ <div style={{ padding: 32 }}>
56
+ <h2 style={{ margin: "0 0 16px", fontSize: 18, fontWeight: 700, color: "rgba(255,255,255,0.9)" }}>
57
+ Interactive Content
58
+ </h2>
59
+ <p style={{ margin: "0 0 16px", fontSize: 13, lineHeight: 1.6, color: "rgba(255,255,255,0.5)" }}>
60
+ Pan and zoom this panel using mouse, trackpad, or touch gestures.
61
+ The floating controls provide programmatic zoom, reset, and center actions.
62
+ The state badge in the corner shows real-time transform values.
63
+ </p>
64
+ <div
65
+ style={{
66
+ borderRadius: 10,
67
+ overflow: "hidden",
68
+ border: "1px solid rgba(255,255,255,0.06)",
69
+ marginBottom: 16,
70
+ }}
71
+ >
72
+ <img src={exampleImg} alt="" style={{ display: "block", width: "100%" }} />
73
+ </div>
74
+ <button
75
+ type="button"
76
+ onClick={() => alert("Click events work inside transformed content!")}
77
+ style={{
78
+ padding: "8px 20px",
79
+ borderRadius: 8,
80
+ border: "1px solid rgba(255,255,255,0.15)",
81
+ background: "rgba(255,255,255,0.06)",
82
+ color: "rgba(255,255,255,0.7)",
83
+ fontSize: 12,
84
+ fontWeight: 600,
85
+ cursor: "pointer",
86
+ fontFamily: "inherit",
87
+ }}
88
+ >
89
+ Click me
90
+ </button>
91
+ </div>
92
+ </TransformComponent>
93
+ <StateBadge />
94
+ </div>
95
+ </>
96
+ )}
97
+ </TransformWrapper>
98
+ </div>
99
+ );
100
+
101
+ <Meta
102
+ title="Components/Controls"
103
+ component={TransformWrapper}
104
+ argTypes={argsTypes}
105
+ />
106
+
107
+ # Controls
108
+
109
+ The **Controls** component provides a floating toolbar with zoom in, zoom out,
110
+ reset, and center actions. It uses the render-props pattern to access the
111
+ transform API. The state badge demonstrates `useTransformComponent` for
112
+ real-time state display.
113
+
114
+ <Canvas>
115
+ <Story name="Controls">{(args) => <Template {...args} />}</Story>
116
+ </Canvas>
117
+
118
+ ## Component API
119
+
120
+ <ArgsTable story="Controls" />
@@ -0,0 +1,149 @@
1
+ import React, { useState, useCallback } from "react";
2
+
3
+ import { TransformWrapper, TransformComponent } from "../../../components";
4
+ import { Controls, normalizeArgs, viewerChrome } from "../../utils";
5
+ import { useTransformComponent } from "../../../hooks";
6
+ import smallImg from "../../assets/small-image.jpg";
7
+ import mediumImg from "../../assets/medium-image.jpg";
8
+ import bigImg from "../../assets/big-image.jpeg";
9
+
10
+ const SIZES = [
11
+ { key: "small", label: "Small", src: smallImg },
12
+ { key: "medium", label: "Medium", src: mediumImg },
13
+ { key: "big", label: "Big", src: bigImg },
14
+ ] as const;
15
+
16
+ type SizeKey = (typeof SIZES)[number]["key"];
17
+
18
+ function DimensionsBadge() {
19
+ return useTransformComponent(({ state }) => (
20
+ <div
21
+ style={{
22
+ position: "absolute",
23
+ bottom: 16,
24
+ right: 16,
25
+ zIndex: 10,
26
+ padding: "5px 12px",
27
+ borderRadius: 8,
28
+ background: "rgba(10, 10, 18, 0.78)",
29
+ backdropFilter: "blur(12px)",
30
+ WebkitBackdropFilter: "blur(12px)",
31
+ border: "1px solid rgba(255,255,255,0.1)",
32
+ color: "rgba(255,255,255,0.7)",
33
+ fontSize: 11,
34
+ fontWeight: 600,
35
+ fontFamily: "system-ui, -apple-system, sans-serif",
36
+ letterSpacing: "0.02em",
37
+ userSelect: "none",
38
+ pointerEvents: "none",
39
+ }}
40
+ >
41
+ {state.scale.toFixed(2)}x
42
+ </div>
43
+ ));
44
+ }
45
+
46
+ export const Example: React.FC<any> = (args: any) => {
47
+ const [activeSize, setActiveSize] = useState<SizeKey>("medium");
48
+
49
+ const activeEntry = SIZES.find((s) => s.key === activeSize)!;
50
+
51
+ const handleSizeChange = useCallback((key: SizeKey) => {
52
+ setActiveSize(key);
53
+ }, []);
54
+
55
+ return (
56
+ <div
57
+ style={{
58
+ fontFamily: "system-ui, -apple-system, sans-serif",
59
+ display: "flex",
60
+ flexDirection: "column",
61
+ width: "100%",
62
+ height: "100%",
63
+ minHeight: 0,
64
+ maxWidth: "100%",
65
+ boxSizing: "border-box",
66
+ }}
67
+ >
68
+ <TransformWrapper {...normalizeArgs(args)} key={activeSize} centerOnInit>
69
+ {(utils) => (
70
+ <div
71
+ style={{
72
+ flex: 1,
73
+ minHeight: 0,
74
+ width: "100%",
75
+ position: "relative",
76
+ }}
77
+ >
78
+ <Controls {...utils} position="top-center" />
79
+ <DimensionsBadge />
80
+ <TransformComponent
81
+ wrapperStyle={{
82
+ position: "absolute",
83
+ inset: 0,
84
+ width: "100%",
85
+ height: "100%",
86
+ ...viewerChrome,
87
+ }}
88
+ >
89
+ <img
90
+ src={activeEntry.src}
91
+ alt={`${activeEntry.label} preview`}
92
+ style={{ display: "block" }}
93
+ />
94
+ </TransformComponent>
95
+ </div>
96
+ )}
97
+ </TransformWrapper>
98
+
99
+ <div
100
+ style={{
101
+ display: "inline-flex",
102
+ alignItems: "center",
103
+ alignSelf: "center",
104
+ gap: 2,
105
+ marginTop: 12,
106
+ flexShrink: 0,
107
+ padding: 4,
108
+ borderRadius: 12,
109
+ background: "rgba(15, 15, 20, 0.72)",
110
+ backdropFilter: "blur(16px) saturate(1.6)",
111
+ WebkitBackdropFilter: "blur(16px) saturate(1.6)",
112
+ border: "1px solid rgba(255,255,255,0.08)",
113
+ boxShadow:
114
+ "0 2px 8px rgba(0,0,0,0.3), 0 0 0 0.5px rgba(255,255,255,0.04) inset",
115
+ }}
116
+ >
117
+ {SIZES.map((size) => {
118
+ const isActive = activeSize === size.key;
119
+ return (
120
+ <button
121
+ key={size.key}
122
+ type="button"
123
+ onClick={() => handleSizeChange(size.key)}
124
+ style={{
125
+ display: "flex",
126
+ alignItems: "center",
127
+ justifyContent: "center",
128
+ height: 32,
129
+ padding: "0 16px",
130
+ border: "none",
131
+ borderRadius: 8,
132
+ background: isActive ? "rgba(255,255,255,0.12)" : "transparent",
133
+ color: isActive ? "#fff" : "rgba(255,255,255,0.5)",
134
+ fontSize: 12,
135
+ fontWeight: 600,
136
+ letterSpacing: "0.02em",
137
+ cursor: "pointer",
138
+ fontFamily: "inherit",
139
+ transition: "background 0.15s ease, color 0.15s ease",
140
+ }}
141
+ >
142
+ {size.label}
143
+ </button>
144
+ );
145
+ })}
146
+ </div>
147
+ </div>
148
+ );
149
+ };
@@ -0,0 +1,46 @@
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) => (
8
+ <div
9
+ style={{
10
+ width: "100%",
11
+ maxWidth: 640,
12
+ height: "min(420px, 52vh)",
13
+ margin: "0 auto",
14
+ boxSizing: "border-box",
15
+ }}
16
+ >
17
+ <Example {...args} />
18
+ </div>
19
+ );
20
+
21
+ <Meta
22
+ title="Basic/Image"
23
+ component={TransformWrapper}
24
+ argTypes={argsTypes}
25
+ />
26
+
27
+ # Image
28
+
29
+ The most common use-case: wrap an **image** in `TransformWrapper` and
30
+ `TransformComponent` to make it zoomable and pannable. The preview is sized like
31
+ **Responsive Image** so it stays inside the Storybook canvas (`max-width` 640px,
32
+ `height: min(420px, 52vh)`). The viewer fills that area; use the **tab switcher**
33
+ below to swap between three image sizes and see how the library handles
34
+ different content dimensions.
35
+
36
+ Pan by dragging, zoom with the scroll wheel or pinch gesture, and use the
37
+ floating controls to zoom in, zoom out, reset, or center the view. The scale
38
+ badge in the corner tracks the current zoom level in real time.
39
+
40
+ <Canvas>
41
+ <Story name="Image">{(args) => <Template {...args} />}</Story>
42
+ </Canvas>
43
+
44
+ ## Component API
45
+
46
+ <ArgsTable story="Image" />