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.
- package/dist/index.cjs.js +785 -310
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +117 -23
- package/dist/index.esm.js +781 -302
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -62
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/DocTable.tsx +59 -0
- package/src/stories/docs/HandlerCards.tsx +102 -0
- package/src/stories/docs/Introduction.stories.mdx +135 -0
- package/src/stories/docs/PropTable.tsx +116 -0
- package/src/stories/docs/docs.module.css +370 -0
- package/src/stories/docs/handlers.stories.mdx +7 -0
- package/src/stories/docs/handlers.tsx +111 -0
- package/src/stories/docs/props.stories.mdx +11 -0
- package/src/stories/docs/props.tsx +571 -0
- package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
- package/src/stories/examples/axis-lock/example.tsx +114 -0
- package/src/stories/examples/bounds/bounds.data.tsx +191 -0
- package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
- package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
- package/src/stories/examples/cinema/cinema-data.tsx +578 -0
- package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
- package/src/stories/examples/cinema/example.tsx +378 -0
- package/src/stories/examples/cinema/image.png +0 -0
- package/src/stories/examples/content/content.stories.mdx +172 -0
- package/src/stories/examples/content/mixed-content.data.tsx +624 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
- package/src/stories/examples/content-rerendering/example.tsx +631 -0
- package/src/stories/examples/controls/controls.stories.mdx +120 -0
- package/src/stories/examples/image/example.tsx +149 -0
- package/src/stories/examples/image/image.stories.mdx +46 -0
- package/src/stories/examples/image-annotations/example.tsx +1442 -0
- package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
- package/src/stories/examples/image-responsive/example.tsx +152 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
- package/src/stories/examples/initial-transform/example.tsx +233 -0
- package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
- package/src/stories/examples/keep-scale/example.tsx +882 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
- package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
- package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
- package/src/stories/examples/medical-viewer/example.tsx +690 -0
- package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
- package/src/stories/examples/mini-map/example.tsx +122 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
- package/src/stories/examples/miro/example.tsx +1573 -0
- package/src/stories/examples/miro/miro.stories.mdx +31 -0
- package/src/stories/examples/padding/example.tsx +147 -0
- package/src/stories/examples/padding/padding.stories.mdx +35 -0
- package/src/stories/examples/product-card/example.tsx +594 -0
- package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
- package/src/stories/examples/stadium/example.tsx +1040 -0
- package/src/stories/examples/stadium/stadium-data.ts +224 -0
- package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
- package/src/stories/examples/stress/example.tsx +59 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/text-selection/example.tsx +221 -0
- package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
- package/src/stories/examples/velocity/example.tsx +908 -0
- package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
- package/src/stories/examples/virtualize/example.tsx +239 -0
- package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
- package/src/stories/examples/zoom-to-element/example.tsx +219 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
- package/src/stories/hooks/use-controls.stories.mdx +86 -0
- package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
- package/src/stories/types/args.types.ts +192 -0
- package/src/stories/utils/args.utils.ts +35 -0
- package/src/stories/utils/bounds-overlay.tsx +168 -0
- package/src/stories/utils/code.utils.tsx +22 -0
- package/src/stories/utils/controls.utils.tsx +347 -0
- package/src/stories/utils/index.ts +5 -0
- package/src/stories/utils/styles.module.css +223 -0
- package/src/stories/utils/viewer.styles.ts +34 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
|
@@ -0,0 +1,35 @@
|
|
|
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="Advanced/Velocity"
|
|
11
|
+
component={TransformWrapper}
|
|
12
|
+
argTypes={argsTypes}
|
|
13
|
+
/>
|
|
14
|
+
|
|
15
|
+
# Pan velocity (inertia)
|
|
16
|
+
|
|
17
|
+
After a fast pan the library applies **momentum** so the canvas keeps
|
|
18
|
+
moving and **eases out** — the same physics you feel when scrolling on
|
|
19
|
+
mobile. Toggle **Momentum / Locked**, adjust **Sensitivity** and
|
|
20
|
+
**Friction**, then fling the starfield to feel the difference.
|
|
21
|
+
|
|
22
|
+
| Prop | Effect |
|
|
23
|
+
|---|---|
|
|
24
|
+
| `velocityAnimation.disabled` | Turn inertia on/off |
|
|
25
|
+
| `velocityAnimation.sensitivityMouse` | How much initial speed is captured (higher = faster fling) |
|
|
26
|
+
| `velocityAnimation.inertia` | Decay coefficient — higher = longer glide |
|
|
27
|
+
| `velocityAnimation.animationTime` | Base animation duration in ms |
|
|
28
|
+
|
|
29
|
+
<Canvas>
|
|
30
|
+
<Story name="Velocity">{(args) => <Template {...args} />}</Story>
|
|
31
|
+
</Canvas>
|
|
32
|
+
|
|
33
|
+
## Component API
|
|
34
|
+
|
|
35
|
+
<ArgsTable story="Velocity" />
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/* eslint-disable react/no-array-index-key, no-nested-ternary */
|
|
2
|
+
import React, { useCallback, useState } from "react";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
TransformWrapper,
|
|
6
|
+
TransformComponent,
|
|
7
|
+
Virtualize,
|
|
8
|
+
} from "../../../components";
|
|
9
|
+
import { useTransformComponent } from "../../../hooks";
|
|
10
|
+
import { Controls, normalizeArgs, viewerChrome } from "../../utils";
|
|
11
|
+
|
|
12
|
+
const COLS = 10;
|
|
13
|
+
const ROWS = 10;
|
|
14
|
+
const TILE_SIZE = 180;
|
|
15
|
+
const GAP = 20;
|
|
16
|
+
const TOTAL = COLS * ROWS;
|
|
17
|
+
|
|
18
|
+
const CANVAS_WIDTH = COLS * (TILE_SIZE + GAP) + GAP;
|
|
19
|
+
const CANVAS_HEIGHT = ROWS * (TILE_SIZE + GAP) + GAP;
|
|
20
|
+
|
|
21
|
+
const PALETTE = [
|
|
22
|
+
"#667eea",
|
|
23
|
+
"#764ba2",
|
|
24
|
+
"#4facfe",
|
|
25
|
+
"#00f2fe",
|
|
26
|
+
"#43e97b",
|
|
27
|
+
"#38f9d7",
|
|
28
|
+
"#fa709a",
|
|
29
|
+
"#fee140",
|
|
30
|
+
"#a18cd1",
|
|
31
|
+
"#fbc2eb",
|
|
32
|
+
"#f093fb",
|
|
33
|
+
"#f5576c",
|
|
34
|
+
"#4481eb",
|
|
35
|
+
"#04befe",
|
|
36
|
+
"#fccb90",
|
|
37
|
+
"#d57eeb",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function tileColor(index: number): string {
|
|
41
|
+
return PALETTE[index % PALETTE.length];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function MountedCounter({
|
|
45
|
+
mounted,
|
|
46
|
+
total,
|
|
47
|
+
}: {
|
|
48
|
+
mounted: number;
|
|
49
|
+
total: number;
|
|
50
|
+
}) {
|
|
51
|
+
const scale = useTransformComponent(({ state }) => state.scale);
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div
|
|
55
|
+
style={{
|
|
56
|
+
position: "absolute",
|
|
57
|
+
top: 12,
|
|
58
|
+
right: 12,
|
|
59
|
+
zIndex: 10,
|
|
60
|
+
display: "flex",
|
|
61
|
+
gap: 8,
|
|
62
|
+
alignItems: "center",
|
|
63
|
+
padding: "6px 14px",
|
|
64
|
+
borderRadius: 8,
|
|
65
|
+
background: "rgba(10, 10, 18, 0.82)",
|
|
66
|
+
backdropFilter: "blur(12px)",
|
|
67
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
68
|
+
border: "1px solid rgba(255,255,255,0.1)",
|
|
69
|
+
color: "rgba(255,255,255,0.75)",
|
|
70
|
+
fontSize: 12,
|
|
71
|
+
fontWeight: 600,
|
|
72
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
73
|
+
letterSpacing: "0.02em",
|
|
74
|
+
userSelect: "none",
|
|
75
|
+
pointerEvents: "none",
|
|
76
|
+
}}
|
|
77
|
+
>
|
|
78
|
+
<span>
|
|
79
|
+
{mounted}/{total} mounted
|
|
80
|
+
</span>
|
|
81
|
+
<span style={{ opacity: 0.4 }}>|</span>
|
|
82
|
+
<span>{scale.toFixed(2)}x</span>
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const Example: React.FC<any> = (args: any) => {
|
|
88
|
+
const [mounted, setMounted] = useState(0);
|
|
89
|
+
const [margin, setMargin] = useState(100);
|
|
90
|
+
const [threshold, setThreshold] = useState(0);
|
|
91
|
+
|
|
92
|
+
const handleShow = useCallback(() => setMounted((m) => m + 1), []);
|
|
93
|
+
const handleHide = useCallback(() => setMounted((m) => m - 1), []);
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
|
|
97
|
+
<TransformWrapper
|
|
98
|
+
{...normalizeArgs(args)}
|
|
99
|
+
limitToBounds={false}
|
|
100
|
+
minScale={0.1}
|
|
101
|
+
maxScale={4}
|
|
102
|
+
centerOnInit
|
|
103
|
+
>
|
|
104
|
+
{(utils) => (
|
|
105
|
+
<>
|
|
106
|
+
<Controls
|
|
107
|
+
{...utils}
|
|
108
|
+
extraButtons={[
|
|
109
|
+
{
|
|
110
|
+
label: `Margin: ${margin}px`,
|
|
111
|
+
onClick: () => setMargin((m) => (m === 0 ? 100 : 0)),
|
|
112
|
+
active: margin > 0,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
label: `Threshold: ${threshold}`,
|
|
116
|
+
onClick: () =>
|
|
117
|
+
setThreshold((t) => (t === 0 ? 0.5 : t === 0.5 ? 1 : 0)),
|
|
118
|
+
active: threshold > 0,
|
|
119
|
+
},
|
|
120
|
+
]}
|
|
121
|
+
/>
|
|
122
|
+
|
|
123
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
124
|
+
<TransformComponent
|
|
125
|
+
wrapperStyle={{
|
|
126
|
+
...viewerChrome,
|
|
127
|
+
width: "600px",
|
|
128
|
+
height: "500px",
|
|
129
|
+
maxWidth: "85vw",
|
|
130
|
+
maxHeight: "75vh",
|
|
131
|
+
}}
|
|
132
|
+
>
|
|
133
|
+
<div
|
|
134
|
+
style={{
|
|
135
|
+
position: "relative",
|
|
136
|
+
width: CANVAS_WIDTH,
|
|
137
|
+
height: CANVAS_HEIGHT,
|
|
138
|
+
backgroundImage:
|
|
139
|
+
"radial-gradient(circle, rgba(255,255,255,0.03) 1px, transparent 1px)",
|
|
140
|
+
backgroundSize: "24px 24px",
|
|
141
|
+
}}
|
|
142
|
+
>
|
|
143
|
+
{Array.from({ length: TOTAL }).map((_, i) => {
|
|
144
|
+
const col = i % COLS;
|
|
145
|
+
const row = Math.floor(i / COLS);
|
|
146
|
+
const tileX = GAP + col * (TILE_SIZE + GAP);
|
|
147
|
+
const tileY = GAP + row * (TILE_SIZE + GAP);
|
|
148
|
+
const color = tileColor(i);
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<Virtualize
|
|
152
|
+
key={i}
|
|
153
|
+
x={tileX}
|
|
154
|
+
y={tileY}
|
|
155
|
+
width={TILE_SIZE}
|
|
156
|
+
height={TILE_SIZE}
|
|
157
|
+
margin={margin}
|
|
158
|
+
threshold={threshold}
|
|
159
|
+
onShow={handleShow}
|
|
160
|
+
onHide={handleHide}
|
|
161
|
+
placeholder={
|
|
162
|
+
<div
|
|
163
|
+
style={{
|
|
164
|
+
position: "absolute",
|
|
165
|
+
left: tileX,
|
|
166
|
+
top: tileY,
|
|
167
|
+
width: TILE_SIZE,
|
|
168
|
+
height: TILE_SIZE,
|
|
169
|
+
borderRadius: 12,
|
|
170
|
+
border: `1px dashed ${color}33`,
|
|
171
|
+
}}
|
|
172
|
+
/>
|
|
173
|
+
}
|
|
174
|
+
style={{
|
|
175
|
+
position: "absolute",
|
|
176
|
+
left: tileX,
|
|
177
|
+
top: tileY,
|
|
178
|
+
width: TILE_SIZE,
|
|
179
|
+
height: TILE_SIZE,
|
|
180
|
+
}}
|
|
181
|
+
>
|
|
182
|
+
<div
|
|
183
|
+
style={{
|
|
184
|
+
width: "100%",
|
|
185
|
+
height: "100%",
|
|
186
|
+
borderRadius: 12,
|
|
187
|
+
background: `linear-gradient(135deg, ${color}22, ${color}08)`,
|
|
188
|
+
border: `1px solid ${color}44`,
|
|
189
|
+
display: "flex",
|
|
190
|
+
flexDirection: "column",
|
|
191
|
+
alignItems: "center",
|
|
192
|
+
justifyContent: "center",
|
|
193
|
+
gap: 8,
|
|
194
|
+
transition:
|
|
195
|
+
"border-color 0.2s ease, box-shadow 0.2s ease",
|
|
196
|
+
}}
|
|
197
|
+
>
|
|
198
|
+
<div
|
|
199
|
+
style={{
|
|
200
|
+
width: 36,
|
|
201
|
+
height: 36,
|
|
202
|
+
borderRadius: 10,
|
|
203
|
+
background: `linear-gradient(135deg, ${color}, ${color}88)`,
|
|
204
|
+
display: "flex",
|
|
205
|
+
alignItems: "center",
|
|
206
|
+
justifyContent: "center",
|
|
207
|
+
fontSize: 14,
|
|
208
|
+
fontWeight: 800,
|
|
209
|
+
color: "#fff",
|
|
210
|
+
boxShadow: `0 0 16px ${color}33`,
|
|
211
|
+
}}
|
|
212
|
+
>
|
|
213
|
+
{i + 1}
|
|
214
|
+
</div>
|
|
215
|
+
<span
|
|
216
|
+
style={{
|
|
217
|
+
fontSize: 11,
|
|
218
|
+
fontWeight: 600,
|
|
219
|
+
color: "rgba(255,255,255,0.5)",
|
|
220
|
+
letterSpacing: "0.03em",
|
|
221
|
+
}}
|
|
222
|
+
>
|
|
223
|
+
Tile {i + 1}
|
|
224
|
+
</span>
|
|
225
|
+
</div>
|
|
226
|
+
</Virtualize>
|
|
227
|
+
);
|
|
228
|
+
})}
|
|
229
|
+
</div>
|
|
230
|
+
</TransformComponent>
|
|
231
|
+
|
|
232
|
+
<MountedCounter mounted={mounted} total={TOTAL} />
|
|
233
|
+
</div>
|
|
234
|
+
</>
|
|
235
|
+
)}
|
|
236
|
+
</TransformWrapper>
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper, TransformComponent, Virtualize } from "../../../components";
|
|
4
|
+
import { argsTypes } from "../../types/args.types";
|
|
5
|
+
import { DocTable } from "../../docs/DocTable.tsx";
|
|
6
|
+
import { Example } from "./example";
|
|
7
|
+
|
|
8
|
+
export const Template = (args) => <Example {...args} />;
|
|
9
|
+
|
|
10
|
+
<Meta
|
|
11
|
+
title="Components/Virtualize"
|
|
12
|
+
component={TransformWrapper}
|
|
13
|
+
argTypes={argsTypes}
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
# Virtualize
|
|
17
|
+
|
|
18
|
+
Mount and unmount children **based on viewport visibility**. `Virtualize` wraps
|
|
19
|
+
content positioned inside the transformed canvas and only renders its children
|
|
20
|
+
when the declared bounding box overlaps the viewport. This is ideal for large
|
|
21
|
+
canvases with many elements — seat maps, tile grids, node graphs — where keeping
|
|
22
|
+
every item in the DOM would hurt performance.
|
|
23
|
+
|
|
24
|
+
## How it works
|
|
25
|
+
|
|
26
|
+
Each `Virtualize` component declares its content-space position and size via
|
|
27
|
+
`x`, `y`, `width`, and `height` props. On every transform change (pan, zoom,
|
|
28
|
+
pinch) the component checks whether the scaled and translated bounding box
|
|
29
|
+
intersects the viewport. When it does, children are mounted; when it doesn't,
|
|
30
|
+
children are removed from the DOM.
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
<TransformWrapper>
|
|
36
|
+
<TransformComponent>
|
|
37
|
+
<div style={{ position: "relative", width: 4000, height: 4000 }}>
|
|
38
|
+
<Virtualize x={100} y={200} width={300} height={150} margin={50}>
|
|
39
|
+
<MyExpensiveWidget />
|
|
40
|
+
</Virtualize>
|
|
41
|
+
</div>
|
|
42
|
+
</TransformComponent>
|
|
43
|
+
</TransformWrapper>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Props
|
|
47
|
+
|
|
48
|
+
<DocTable
|
|
49
|
+
columns={[
|
|
50
|
+
{ header: "Prop" },
|
|
51
|
+
{ header: "Type", style: "type" },
|
|
52
|
+
{ header: "Default", style: "default" },
|
|
53
|
+
{ header: "Description", style: "text" },
|
|
54
|
+
]}
|
|
55
|
+
rows={[
|
|
56
|
+
["x", "number", "—", "Horizontal position in content (unscaled) space."],
|
|
57
|
+
["y", "number", "—", "Vertical position in content (unscaled) space."],
|
|
58
|
+
["width", "number", "—", "Width in content space."],
|
|
59
|
+
["height", "number", "—", "Height in content space."],
|
|
60
|
+
["margin", "number", "0", "Extra pixels around the viewport that count as visible. Acts like IntersectionObserver's rootMargin."],
|
|
61
|
+
["threshold", "number", "0", "Fraction (0–1) of the element area that must overlap the viewport. 0 = any overlap, 1 = fully visible."],
|
|
62
|
+
["placeholder", "ReactNode", "null", "Rendered when the element is outside the viewport."],
|
|
63
|
+
["onShow", "() => void", "—", "Called when children are mounted (element enters viewport)."],
|
|
64
|
+
["onHide", "() => void", "—", "Called when children are unmounted (element leaves viewport)."],
|
|
65
|
+
["className", "string", "—", "CSS class forwarded to the wrapper div."],
|
|
66
|
+
["style", "CSSProperties", "—", "Inline styles forwarded to the wrapper div."],
|
|
67
|
+
["ref", "Ref<HTMLDivElement>", "—", "Ref forwarded to the wrapper div."],
|
|
68
|
+
]}
|
|
69
|
+
/>
|
|
70
|
+
|
|
71
|
+
## Demo
|
|
72
|
+
|
|
73
|
+
The demo below renders a 10 × 10 grid of tiles (100 total) on a large
|
|
74
|
+
canvas. Only tiles overlapping the viewport are mounted — watch the counter
|
|
75
|
+
update in real time as you pan and zoom. Toggle **Margin** and **Threshold** via
|
|
76
|
+
the control bar to experiment with appearing rules.
|
|
77
|
+
|
|
78
|
+
<Canvas>
|
|
79
|
+
<Story name="Virtualize">{(args) => <Template {...args} />}</Story>
|
|
80
|
+
</Canvas>
|
|
81
|
+
|
|
82
|
+
## Component API
|
|
83
|
+
|
|
84
|
+
<ArgsTable story="Virtualize" />
|
|
@@ -0,0 +1,219 @@
|
|
|
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 { useTransformComponent } from "../../../hooks";
|
|
11
|
+
|
|
12
|
+
function ScaleBadge() {
|
|
13
|
+
return useTransformComponent(({ state }) => (
|
|
14
|
+
<div
|
|
15
|
+
style={{
|
|
16
|
+
position: "absolute",
|
|
17
|
+
bottom: 16,
|
|
18
|
+
right: 16,
|
|
19
|
+
zIndex: 10,
|
|
20
|
+
padding: "5px 12px",
|
|
21
|
+
borderRadius: 8,
|
|
22
|
+
background: "rgba(10, 10, 18, 0.78)",
|
|
23
|
+
backdropFilter: "blur(12px)",
|
|
24
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
25
|
+
border: "1px solid rgba(255,255,255,0.1)",
|
|
26
|
+
color: "rgba(255,255,255,0.7)",
|
|
27
|
+
fontSize: 11,
|
|
28
|
+
fontWeight: 600,
|
|
29
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
30
|
+
letterSpacing: "0.02em",
|
|
31
|
+
userSelect: "none",
|
|
32
|
+
pointerEvents: "none",
|
|
33
|
+
}}
|
|
34
|
+
>
|
|
35
|
+
{state.scale.toFixed(2)}x
|
|
36
|
+
</div>
|
|
37
|
+
));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const TARGETS = [
|
|
41
|
+
{
|
|
42
|
+
id: "element1",
|
|
43
|
+
label: "Alpha",
|
|
44
|
+
color: "#667eea",
|
|
45
|
+
gradient: "linear-gradient(135deg, #667eea, #764ba2)",
|
|
46
|
+
icon: "A",
|
|
47
|
+
desc: "Primary target zone",
|
|
48
|
+
top: 60,
|
|
49
|
+
left: 40,
|
|
50
|
+
width: 200,
|
|
51
|
+
height: 160,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: "element2",
|
|
55
|
+
label: "Beta",
|
|
56
|
+
color: "#4facfe",
|
|
57
|
+
gradient: "linear-gradient(135deg, #4facfe, #00f2fe)",
|
|
58
|
+
icon: "B",
|
|
59
|
+
desc: "Secondary observation point",
|
|
60
|
+
top: 280,
|
|
61
|
+
left: 240,
|
|
62
|
+
width: 220,
|
|
63
|
+
height: 140,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "element3",
|
|
67
|
+
label: "Gamma",
|
|
68
|
+
color: "#43e97b",
|
|
69
|
+
gradient: "linear-gradient(180deg, #43e97b, #38f9d7)",
|
|
70
|
+
icon: "G",
|
|
71
|
+
desc: "Tall narrow tower — great for testing vertical zoom-to-element framing.",
|
|
72
|
+
top: 48,
|
|
73
|
+
left: 658,
|
|
74
|
+
width: 96,
|
|
75
|
+
height: 480,
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
export const Example: React.FC<any> = (args: any) => {
|
|
80
|
+
return (
|
|
81
|
+
<div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
|
|
82
|
+
<style>{`
|
|
83
|
+
.zoom-target {
|
|
84
|
+
transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
|
|
85
|
+
}
|
|
86
|
+
.zoom-target:hover {
|
|
87
|
+
background: rgba(255,255,255,0.07) !important;
|
|
88
|
+
box-shadow: 0 0 24px var(--target-glow), 0 0 0 1px var(--target-color);
|
|
89
|
+
}
|
|
90
|
+
`}</style>
|
|
91
|
+
<TransformWrapper {...normalizeArgs(args)} centerOnInit>
|
|
92
|
+
{(utils) => (
|
|
93
|
+
<>
|
|
94
|
+
<Controls
|
|
95
|
+
{...utils}
|
|
96
|
+
extraButtons={TARGETS.map((t, i) => ({
|
|
97
|
+
label: `Focus ${t.label}`,
|
|
98
|
+
icon: <NumberedTargetIcon n={i + 1} />,
|
|
99
|
+
onClick: () => utils.zoomToElement(t.id),
|
|
100
|
+
}))}
|
|
101
|
+
/>
|
|
102
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
103
|
+
<TransformComponent
|
|
104
|
+
wrapperStyle={{
|
|
105
|
+
...viewerChrome,
|
|
106
|
+
width: "500px",
|
|
107
|
+
height: "500px",
|
|
108
|
+
maxWidth: "80vw",
|
|
109
|
+
maxHeight: "75vh",
|
|
110
|
+
}}
|
|
111
|
+
>
|
|
112
|
+
<div
|
|
113
|
+
style={{
|
|
114
|
+
position: "relative",
|
|
115
|
+
width: 800,
|
|
116
|
+
height: 600,
|
|
117
|
+
backgroundImage:
|
|
118
|
+
"radial-gradient(circle, rgba(255,255,255,0.04) 1px, transparent 1px)",
|
|
119
|
+
backgroundSize: "24px 24px",
|
|
120
|
+
}}
|
|
121
|
+
>
|
|
122
|
+
{TARGETS.map((target) => {
|
|
123
|
+
const narrow = target.width < 140;
|
|
124
|
+
return (
|
|
125
|
+
<div
|
|
126
|
+
key={target.id}
|
|
127
|
+
id={target.id}
|
|
128
|
+
className="zoom-target"
|
|
129
|
+
role="button"
|
|
130
|
+
tabIndex={0}
|
|
131
|
+
style={
|
|
132
|
+
{
|
|
133
|
+
position: "absolute",
|
|
134
|
+
top: target.top,
|
|
135
|
+
left: target.left,
|
|
136
|
+
width: target.width,
|
|
137
|
+
height: target.height,
|
|
138
|
+
borderRadius: 14,
|
|
139
|
+
background: "rgba(255,255,255,0.03)",
|
|
140
|
+
border: `1px solid ${target.color}33`,
|
|
141
|
+
backdropFilter: "blur(8px)",
|
|
142
|
+
WebkitBackdropFilter: "blur(8px)",
|
|
143
|
+
padding: narrow ? 14 : 20,
|
|
144
|
+
display: "flex",
|
|
145
|
+
flexDirection: "column",
|
|
146
|
+
justifyContent: "space-between",
|
|
147
|
+
cursor: "pointer",
|
|
148
|
+
"--target-color": `${target.color}66`,
|
|
149
|
+
"--target-glow": `${target.color}33`,
|
|
150
|
+
} as React.CSSProperties
|
|
151
|
+
}
|
|
152
|
+
onClick={() => utils.zoomToElement(target.id)}
|
|
153
|
+
onKeyDown={(e) => {
|
|
154
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
155
|
+
e.preventDefault();
|
|
156
|
+
utils.zoomToElement(target.id);
|
|
157
|
+
}
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
<div
|
|
161
|
+
style={{
|
|
162
|
+
display: "flex",
|
|
163
|
+
flexDirection: narrow ? "column" : "row",
|
|
164
|
+
alignItems: narrow ? "flex-start" : "center",
|
|
165
|
+
gap: narrow ? 8 : 10,
|
|
166
|
+
}}
|
|
167
|
+
>
|
|
168
|
+
<div
|
|
169
|
+
style={{
|
|
170
|
+
width: narrow ? 28 : 32,
|
|
171
|
+
height: narrow ? 28 : 32,
|
|
172
|
+
borderRadius: 8,
|
|
173
|
+
background: target.gradient,
|
|
174
|
+
display: "flex",
|
|
175
|
+
alignItems: "center",
|
|
176
|
+
justifyContent: "center",
|
|
177
|
+
fontSize: narrow ? 12 : 14,
|
|
178
|
+
fontWeight: 800,
|
|
179
|
+
color: "#fff",
|
|
180
|
+
boxShadow: `0 0 16px ${target.color}44`,
|
|
181
|
+
flexShrink: 0,
|
|
182
|
+
}}
|
|
183
|
+
>
|
|
184
|
+
{target.icon}
|
|
185
|
+
</div>
|
|
186
|
+
<span
|
|
187
|
+
style={{
|
|
188
|
+
fontSize: narrow ? 12 : 14,
|
|
189
|
+
fontWeight: 700,
|
|
190
|
+
color: "rgba(255,255,255,0.85)",
|
|
191
|
+
lineHeight: 1.2,
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
{target.label}
|
|
195
|
+
</span>
|
|
196
|
+
</div>
|
|
197
|
+
<p
|
|
198
|
+
style={{
|
|
199
|
+
margin: 0,
|
|
200
|
+
fontSize: narrow ? 10 : 11,
|
|
201
|
+
color: "rgba(255,255,255,0.4)",
|
|
202
|
+
lineHeight: 1.45,
|
|
203
|
+
}}
|
|
204
|
+
>
|
|
205
|
+
{target.desc}
|
|
206
|
+
</p>
|
|
207
|
+
</div>
|
|
208
|
+
);
|
|
209
|
+
})}
|
|
210
|
+
</div>
|
|
211
|
+
</TransformComponent>
|
|
212
|
+
<ScaleBadge />
|
|
213
|
+
</div>
|
|
214
|
+
</>
|
|
215
|
+
)}
|
|
216
|
+
</TransformWrapper>
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
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/Zoom To Element"
|
|
11
|
+
component={TransformWrapper}
|
|
12
|
+
argTypes={argsTypes}
|
|
13
|
+
/>
|
|
14
|
+
|
|
15
|
+
# Zoom To Element
|
|
16
|
+
|
|
17
|
+
Programmatically animate the viewport to **focus on a specific element** inside
|
|
18
|
+
the transformed content. Click any card on the canvas or use the target buttons
|
|
19
|
+
in the control bar — `zoomToElement(elementId)` smoothly pans and zooms to frame
|
|
20
|
+
that element in view.
|
|
21
|
+
|
|
22
|
+
This is the core API for seat-map selectors, diagram node focus, annotation
|
|
23
|
+
highlights, and any "navigate to target" interaction pattern.
|
|
24
|
+
|
|
25
|
+
<Canvas>
|
|
26
|
+
<Story name="Zoom To Element">{(args) => <Template {...args} />}</Story>
|
|
27
|
+
</Canvas>
|
|
28
|
+
|
|
29
|
+
## Component API
|
|
30
|
+
|
|
31
|
+
<ArgsTable story="Zoom To Element" />
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Meta } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
<Meta title="Hooks/useControls" />
|
|
4
|
+
|
|
5
|
+
# useControls
|
|
6
|
+
|
|
7
|
+
Access the **transform handlers** from any component inside `TransformWrapper`
|
|
8
|
+
— no render props needed. Returns the same API you get from render props:
|
|
9
|
+
`zoomIn`, `zoomOut`, `resetTransform`, `centerView`, `zoomToElement`, and
|
|
10
|
+
`setTransform`, plus the raw `instance` ref.
|
|
11
|
+
|
|
12
|
+
## Signature
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
function useControls(): ReactZoomPanPinchContentRef
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Return Value
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
{
|
|
22
|
+
instance: ReactZoomPanPinchContext; // raw library instance
|
|
23
|
+
zoomIn: (step?, animationTime?, animationName?) => void;
|
|
24
|
+
zoomOut: (step?, animationTime?, animationName?) => void;
|
|
25
|
+
resetTransform: (animationTime?, animationName?) => void;
|
|
26
|
+
centerView: (scale?, animationTime?, animationName?) => void;
|
|
27
|
+
zoomToElement: (node, scale?, animationTime?, animationName?) => void;
|
|
28
|
+
setTransform: (x, y, scale, animationTime?, animationName?) => void;
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## When to Use
|
|
33
|
+
|
|
34
|
+
- Building a **toolbar or control panel** separate from the content area
|
|
35
|
+
- Triggering zoom/pan from **buttons, menus, or keyboard shortcuts**
|
|
36
|
+
- Any descendant component that needs to **control** the transform but not read its state
|
|
37
|
+
|
|
38
|
+
For reading transform state reactively, use `useTransformComponent` or
|
|
39
|
+
`useTransformEffect` instead.
|
|
40
|
+
|
|
41
|
+
## Example
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch";
|
|
45
|
+
import { useControls } from "react-zoom-pan-pinch";
|
|
46
|
+
|
|
47
|
+
function Toolbar() {
|
|
48
|
+
const { zoomIn, zoomOut, resetTransform, centerView } = useControls();
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div className="toolbar">
|
|
52
|
+
<button onClick={() => zoomIn()}>Zoom In</button>
|
|
53
|
+
<button onClick={() => zoomOut()}>Zoom Out</button>
|
|
54
|
+
<button onClick={() => resetTransform()}>Reset</button>
|
|
55
|
+
<button onClick={() => centerView()}>Center</button>
|
|
56
|
+
</div>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function App() {
|
|
61
|
+
return (
|
|
62
|
+
<TransformWrapper>
|
|
63
|
+
<Toolbar />
|
|
64
|
+
<TransformComponent>
|
|
65
|
+
<img src="map.jpg" alt="Map" />
|
|
66
|
+
</TransformComponent>
|
|
67
|
+
</TransformWrapper>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## vs Render Props
|
|
73
|
+
|
|
74
|
+
Render props work well for simple cases, but `useControls` is cleaner when:
|
|
75
|
+
|
|
76
|
+
- The controls live in a **deeply nested** or **separate** component
|
|
77
|
+
- You need transform actions in a **custom hook**
|
|
78
|
+
- You want to avoid **prop drilling** through multiple layers
|
|
79
|
+
|
|
80
|
+
Both patterns provide the same underlying API — `useControls` is just syntactic
|
|
81
|
+
sugar over `useTransformContext` + `getControls`.
|
|
82
|
+
|
|
83
|
+
## Requirements
|
|
84
|
+
|
|
85
|
+
Must be called inside a component that is a **descendant** of
|
|
86
|
+
`TransformWrapper`. Throws an error if used outside the transform context.
|