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="Basic/Axis lock"
|
|
11
|
+
component={TransformWrapper}
|
|
12
|
+
argTypes={argsTypes}
|
|
13
|
+
/>
|
|
14
|
+
|
|
15
|
+
# Axis lock
|
|
16
|
+
|
|
17
|
+
Use **`panning.lockAxisX`** and **`panning.lockAxisY`** when the viewport should
|
|
18
|
+
only move along one axis — for example a **timeline** (horizontal only) or a
|
|
19
|
+
**tall document strip** (vertical only).
|
|
20
|
+
|
|
21
|
+
- **`lockAxisX: true`** — horizontal panning is disabled; drag and trackpad pan
|
|
22
|
+
move the content **vertically** only.
|
|
23
|
+
- **`lockAxisY: true`** — vertical panning is disabled; movement is **horizontal**
|
|
24
|
+
only.
|
|
25
|
+
|
|
26
|
+
The buttons above the canvas switch the demo between **free** panning and each
|
|
27
|
+
locked mode. Use the control bar to reset or re-center if you get lost.
|
|
28
|
+
|
|
29
|
+
<Canvas>
|
|
30
|
+
<Story name="Axis lock">{(args) => <Template {...args} />}</Story>
|
|
31
|
+
</Canvas>
|
|
32
|
+
|
|
33
|
+
## Component API
|
|
34
|
+
|
|
35
|
+
<ArgsTable story="Axis lock" />
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import React, { useMemo, useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper, TransformComponent } from "../../../components";
|
|
4
|
+
import {
|
|
5
|
+
Controls,
|
|
6
|
+
normalizeArgs,
|
|
7
|
+
viewerChrome,
|
|
8
|
+
FreeMovementIcon,
|
|
9
|
+
LockVerticalIcon,
|
|
10
|
+
LockHorizontalIcon,
|
|
11
|
+
} from "../../utils";
|
|
12
|
+
|
|
13
|
+
type AxisMode = "free" | "vertical" | "horizontal";
|
|
14
|
+
|
|
15
|
+
const viewer: React.CSSProperties = {
|
|
16
|
+
...viewerChrome,
|
|
17
|
+
width: "560px",
|
|
18
|
+
height: "440px",
|
|
19
|
+
maxWidth: "90vw",
|
|
20
|
+
maxHeight: "78vh",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const font = "system-ui, -apple-system, sans-serif";
|
|
24
|
+
|
|
25
|
+
function GridCanvas() {
|
|
26
|
+
const cells = useMemo(() => {
|
|
27
|
+
const out: { key: string; row: number; col: number }[] = [];
|
|
28
|
+
for (let row = 0; row < 6; row += 1) {
|
|
29
|
+
for (let col = 0; col < 8; col += 1) {
|
|
30
|
+
out.push({ key: `${row}-${col}`, row, col });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div
|
|
38
|
+
style={{
|
|
39
|
+
width: 1400,
|
|
40
|
+
height: 900,
|
|
41
|
+
display: "grid",
|
|
42
|
+
gridTemplateColumns: "repeat(8, 1fr)",
|
|
43
|
+
gridTemplateRows: "repeat(6, 1fr)",
|
|
44
|
+
gap: 3,
|
|
45
|
+
boxSizing: "border-box",
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{cells.map(({ key, row, col }) => (
|
|
49
|
+
<div
|
|
50
|
+
key={key}
|
|
51
|
+
style={{
|
|
52
|
+
borderRadius: 10,
|
|
53
|
+
background: `rgba(${40 + col * 18}, ${60 + row * 12}, ${120 + (row + col) * 8}, 0.35)`,
|
|
54
|
+
border: "1px solid rgba(255,255,255,0.06)",
|
|
55
|
+
display: "flex",
|
|
56
|
+
alignItems: "center",
|
|
57
|
+
justifyContent: "center",
|
|
58
|
+
fontFamily: font,
|
|
59
|
+
fontSize: 13,
|
|
60
|
+
fontWeight: 600,
|
|
61
|
+
color: "rgba(255,255,255,0.45)",
|
|
62
|
+
}}
|
|
63
|
+
>
|
|
64
|
+
{col + 1},{row + 1}
|
|
65
|
+
</div>
|
|
66
|
+
))}
|
|
67
|
+
</div>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function axisButtons(mode: AxisMode, setMode: (m: AxisMode) => void) {
|
|
72
|
+
const btn = (id: AxisMode, label: string, Icon: React.FC) => ({
|
|
73
|
+
label,
|
|
74
|
+
icon: <Icon />,
|
|
75
|
+
onClick: () => setMode(mode === id ? "free" : id),
|
|
76
|
+
active: mode === id,
|
|
77
|
+
"data-tooltip": label,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return [
|
|
81
|
+
btn("free", "Free pan", FreeMovementIcon),
|
|
82
|
+
btn("vertical", "Vertical only", LockVerticalIcon),
|
|
83
|
+
btn("horizontal", "Horizontal only", LockHorizontalIcon),
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const Example: React.FC<Record<string, unknown>> = (args) => {
|
|
88
|
+
const normalized = normalizeArgs(args);
|
|
89
|
+
const [mode, setMode] = useState<AxisMode>("free");
|
|
90
|
+
|
|
91
|
+
const panning = {
|
|
92
|
+
...normalized.panning,
|
|
93
|
+
lockAxisX: mode === "vertical",
|
|
94
|
+
lockAxisY: mode === "horizontal",
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div style={{ fontFamily: font }}>
|
|
99
|
+
<TransformWrapper {...normalized} centerOnInit panning={panning}>
|
|
100
|
+
{(utils) => (
|
|
101
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
102
|
+
<Controls {...utils} extraButtons={axisButtons(mode, setMode)} />
|
|
103
|
+
<TransformComponent
|
|
104
|
+
wrapperStyle={viewer}
|
|
105
|
+
contentStyle={{ width: 1400, height: 900 }}
|
|
106
|
+
>
|
|
107
|
+
<GridCanvas />
|
|
108
|
+
</TransformComponent>
|
|
109
|
+
</div>
|
|
110
|
+
)}
|
|
111
|
+
</TransformWrapper>
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
const SIZE = 1200;
|
|
4
|
+
|
|
5
|
+
export const contentSize = SIZE;
|
|
6
|
+
|
|
7
|
+
function Landmark({
|
|
8
|
+
x,
|
|
9
|
+
y,
|
|
10
|
+
color,
|
|
11
|
+
size,
|
|
12
|
+
label,
|
|
13
|
+
}: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
color: string;
|
|
17
|
+
size: number;
|
|
18
|
+
label: string;
|
|
19
|
+
}) {
|
|
20
|
+
return (
|
|
21
|
+
<div
|
|
22
|
+
style={{
|
|
23
|
+
position: "absolute",
|
|
24
|
+
left: x,
|
|
25
|
+
top: y,
|
|
26
|
+
width: size,
|
|
27
|
+
height: size,
|
|
28
|
+
borderRadius: "16px",
|
|
29
|
+
background: `linear-gradient(135deg, ${color}, ${color}88)`,
|
|
30
|
+
display: "flex",
|
|
31
|
+
alignItems: "center",
|
|
32
|
+
justifyContent: "center",
|
|
33
|
+
boxShadow: `0 0 30px ${color}44`,
|
|
34
|
+
userSelect: "none",
|
|
35
|
+
}}
|
|
36
|
+
>
|
|
37
|
+
<span
|
|
38
|
+
style={{
|
|
39
|
+
color: "#fff",
|
|
40
|
+
fontSize: "11px",
|
|
41
|
+
fontWeight: 600,
|
|
42
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
43
|
+
textShadow: "0 1px 4px rgba(0,0,0,0.5)",
|
|
44
|
+
letterSpacing: "0.5px",
|
|
45
|
+
}}
|
|
46
|
+
>
|
|
47
|
+
{label}
|
|
48
|
+
</span>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function BoundsContent() {
|
|
54
|
+
return (
|
|
55
|
+
<div
|
|
56
|
+
style={{
|
|
57
|
+
position: "relative",
|
|
58
|
+
width: SIZE + 300,
|
|
59
|
+
height: SIZE + 300,
|
|
60
|
+
margin: -150,
|
|
61
|
+
background:
|
|
62
|
+
"linear-gradient(135deg, #1a1a3e 0%, #0f2027 40%, #203a43 70%, #2c5364 100%)",
|
|
63
|
+
overflow: "hidden",
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
<div
|
|
67
|
+
style={{
|
|
68
|
+
position: "absolute",
|
|
69
|
+
inset: "150px",
|
|
70
|
+
backgroundImage: `
|
|
71
|
+
linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
|
|
72
|
+
linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px)
|
|
73
|
+
`,
|
|
74
|
+
backgroundSize: "60px 60px",
|
|
75
|
+
}}
|
|
76
|
+
/>
|
|
77
|
+
<Landmark
|
|
78
|
+
x={150 + 80}
|
|
79
|
+
y={150 + 80}
|
|
80
|
+
color="#667eea"
|
|
81
|
+
size={100}
|
|
82
|
+
label="Start"
|
|
83
|
+
/>
|
|
84
|
+
<Landmark
|
|
85
|
+
x={150 + 300}
|
|
86
|
+
y={150 + 150}
|
|
87
|
+
color="#43e97b"
|
|
88
|
+
size={80}
|
|
89
|
+
label="Park"
|
|
90
|
+
/>
|
|
91
|
+
<Landmark
|
|
92
|
+
x={150 + 150}
|
|
93
|
+
y={150 + 350}
|
|
94
|
+
color="#4facfe"
|
|
95
|
+
size={90}
|
|
96
|
+
label="Lake"
|
|
97
|
+
/>
|
|
98
|
+
<Landmark
|
|
99
|
+
x={150 + 500}
|
|
100
|
+
y={150 + 100}
|
|
101
|
+
color="#f093fb"
|
|
102
|
+
size={70}
|
|
103
|
+
label="Shops"
|
|
104
|
+
/>
|
|
105
|
+
<Landmark
|
|
106
|
+
x={150 + 400}
|
|
107
|
+
y={150 + 400}
|
|
108
|
+
color="#fa709a"
|
|
109
|
+
size={110}
|
|
110
|
+
label="Plaza"
|
|
111
|
+
/>
|
|
112
|
+
<Landmark
|
|
113
|
+
x={150 + 700}
|
|
114
|
+
y={150 + 200}
|
|
115
|
+
color="#f6d365"
|
|
116
|
+
size={85}
|
|
117
|
+
label="Market"
|
|
118
|
+
/>
|
|
119
|
+
<Landmark
|
|
120
|
+
x={150 + 200}
|
|
121
|
+
y={150 + 600}
|
|
122
|
+
color="#a18cd1"
|
|
123
|
+
size={75}
|
|
124
|
+
label="Museum"
|
|
125
|
+
/>
|
|
126
|
+
<Landmark
|
|
127
|
+
x={150 + 600}
|
|
128
|
+
y={150 + 550}
|
|
129
|
+
color="#0acffe"
|
|
130
|
+
size={95}
|
|
131
|
+
label="Arena"
|
|
132
|
+
/>
|
|
133
|
+
<Landmark
|
|
134
|
+
x={150 + 850}
|
|
135
|
+
y={150 + 400}
|
|
136
|
+
color="#ff9a9e"
|
|
137
|
+
size={80}
|
|
138
|
+
label="Tower"
|
|
139
|
+
/>
|
|
140
|
+
<Landmark
|
|
141
|
+
x={150 + 900}
|
|
142
|
+
y={150 + 700}
|
|
143
|
+
color="#fccb90"
|
|
144
|
+
size={90}
|
|
145
|
+
label="Harbor"
|
|
146
|
+
/>
|
|
147
|
+
<Landmark
|
|
148
|
+
x={150 + 350}
|
|
149
|
+
y={150 + 800}
|
|
150
|
+
color="#c471f5"
|
|
151
|
+
size={85}
|
|
152
|
+
label="Temple"
|
|
153
|
+
/>
|
|
154
|
+
<Landmark
|
|
155
|
+
x={150 + 700}
|
|
156
|
+
y={150 + 850}
|
|
157
|
+
color="#48c6ef"
|
|
158
|
+
size={70}
|
|
159
|
+
label="Bridge"
|
|
160
|
+
/>
|
|
161
|
+
<Landmark
|
|
162
|
+
x={150 + 1000}
|
|
163
|
+
y={150 + 150}
|
|
164
|
+
color="#30cfd0"
|
|
165
|
+
size={75}
|
|
166
|
+
label="North"
|
|
167
|
+
/>
|
|
168
|
+
<Landmark
|
|
169
|
+
x={150 + 1000}
|
|
170
|
+
y={150 + 600}
|
|
171
|
+
color="#e0c3fc"
|
|
172
|
+
size={80}
|
|
173
|
+
label="East"
|
|
174
|
+
/>
|
|
175
|
+
<Landmark
|
|
176
|
+
x={150 + 150}
|
|
177
|
+
y={150 + 950}
|
|
178
|
+
color="#89f7fe"
|
|
179
|
+
size={70}
|
|
180
|
+
label="South"
|
|
181
|
+
/>
|
|
182
|
+
<Landmark
|
|
183
|
+
x={150 + 950}
|
|
184
|
+
y={150 + 950}
|
|
185
|
+
color="#feada6"
|
|
186
|
+
size={90}
|
|
187
|
+
label="Corner"
|
|
188
|
+
/>
|
|
189
|
+
</div>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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, BoundsOverlay, viewerChrome } from "../../utils";
|
|
6
|
+
import { BoundsContent, contentSize } from "./bounds.data";
|
|
7
|
+
|
|
8
|
+
export const minX = -(contentSize - 500);
|
|
9
|
+
export const maxX = 0;
|
|
10
|
+
export const minY = -(contentSize - 500);
|
|
11
|
+
export const maxY = 0;
|
|
12
|
+
|
|
13
|
+
export const Template = (args) => (
|
|
14
|
+
<div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
|
|
15
|
+
<TransformWrapper
|
|
16
|
+
{...normalizeArgs(args)}
|
|
17
|
+
minPositionX={minX}
|
|
18
|
+
maxPositionX={maxX}
|
|
19
|
+
minPositionY={minY}
|
|
20
|
+
maxPositionY={maxY}
|
|
21
|
+
>
|
|
22
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
23
|
+
<TransformComponent
|
|
24
|
+
wrapperStyle={{
|
|
25
|
+
...viewerChrome,
|
|
26
|
+
width: "500px",
|
|
27
|
+
height: "500px",
|
|
28
|
+
maxWidth: "80vw",
|
|
29
|
+
maxHeight: "80vh",
|
|
30
|
+
}}
|
|
31
|
+
>
|
|
32
|
+
<BoundsContent />
|
|
33
|
+
</TransformComponent>
|
|
34
|
+
<BoundsOverlay minX={minX} maxX={maxX} minY={minY} maxY={maxY} wrapperWidth={500} wrapperHeight={500} />
|
|
35
|
+
</div>
|
|
36
|
+
</TransformWrapper>
|
|
37
|
+
<p
|
|
38
|
+
style={{
|
|
39
|
+
fontSize: "13px",
|
|
40
|
+
color: "#888",
|
|
41
|
+
marginTop: "12px",
|
|
42
|
+
maxWidth: "500px",
|
|
43
|
+
lineHeight: 1.5,
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
Pan around the map. As you approach a boundary, a shadow and
|
|
47
|
+
“Limit” label fade in on that edge. The content is locked
|
|
48
|
+
within the position bounds — you cannot pan beyond them.
|
|
49
|
+
</p>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
<Meta
|
|
54
|
+
title="Basic/Bounds"
|
|
55
|
+
component={TransformWrapper}
|
|
56
|
+
argTypes={argsTypes}
|
|
57
|
+
/>
|
|
58
|
+
|
|
59
|
+
# Bounds
|
|
60
|
+
|
|
61
|
+
**Position bounds** clamp the transform’s translation: `positionX` and `positionY`
|
|
62
|
+
stay within the ranges you set. They apply while panning, during pinch or wheel
|
|
63
|
+
zoom, and when inertia or animations run—anything that would move the content
|
|
64
|
+
past a limit is stopped or corrected at that edge.
|
|
65
|
+
|
|
66
|
+
Use these props when you need a **custom panning window** (for example a large
|
|
67
|
+
map or canvas where you do not want users to drag content completely away, or
|
|
68
|
+
where min/max should **not** match the wrapper edges). For the common case
|
|
69
|
+
“keep the scaled content inside the viewport,” use **`limitToBounds`** (see the
|
|
70
|
+
controls below): the library derives limits from the wrapper, content size, and
|
|
71
|
+
current scale. When you pass a given `minPosition*` / `maxPosition*` value, it
|
|
72
|
+
**replaces** the auto-calculated bound on that axis; this story sets all four,
|
|
73
|
+
so the pan range is defined entirely by those numbers.
|
|
74
|
+
|
|
75
|
+
## What this story shows
|
|
76
|
+
|
|
77
|
+
The viewport is a fixed **500×500px** frame. The map (`BoundsContent` in
|
|
78
|
+
`bounds.data.tsx`) is much larger, so you can pan in both directions. The
|
|
79
|
+
min/max values are chosen so you can explore the map but **cannot** pan past
|
|
80
|
+
the configured limits—try dragging toward each edge until motion stops.
|
|
81
|
+
|
|
82
|
+
The **fog and “Limit” labels** are implemented by `BoundsOverlay` in the
|
|
83
|
+
story utilities (`useTransformComponent` + edge proximity). They are **not**
|
|
84
|
+
part of the published library; they only illustrate how you might reflect
|
|
85
|
+
“near a bound” in your own UI.
|
|
86
|
+
|
|
87
|
+
## Try it
|
|
88
|
+
|
|
89
|
+
<Canvas>
|
|
90
|
+
<Story name="Bounds">{(args) => <Template {...args} />}</Story>
|
|
91
|
+
</Canvas>
|
|
92
|
+
|
|
93
|
+
## Component API
|
|
94
|
+
|
|
95
|
+
Use the table for global behavior (panning, wheel, zoom limits, **`limitToBounds`**, etc.).
|
|
96
|
+
Position bounds are always set in **pixels** on `TransformWrapper`, consistent
|
|
97
|
+
with `positionX` / `positionY` in the transform state.
|
|
98
|
+
|
|
99
|
+
<ArgsTable story="Bounds" />
|
|
@@ -0,0 +1,206 @@
|
|
|
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 { DocTable } from "../../docs/DocTable.tsx";
|
|
9
|
+
import exampleImg from "../../assets/medium-image.jpg";
|
|
10
|
+
|
|
11
|
+
export const ScaleBadge = () => {
|
|
12
|
+
return useTransformComponent(({ state }) => (
|
|
13
|
+
<div
|
|
14
|
+
style={{
|
|
15
|
+
position: "absolute",
|
|
16
|
+
bottom: 14,
|
|
17
|
+
right: 14,
|
|
18
|
+
zIndex: 10,
|
|
19
|
+
padding: "4px 10px",
|
|
20
|
+
borderRadius: 8,
|
|
21
|
+
background: "rgba(10, 10, 18, 0.8)",
|
|
22
|
+
backdropFilter: "blur(12px)",
|
|
23
|
+
border: "1px solid rgba(255,255,255,0.08)",
|
|
24
|
+
color: "rgba(255,255,255,0.7)",
|
|
25
|
+
fontSize: 11,
|
|
26
|
+
fontWeight: 600,
|
|
27
|
+
fontFamily: "'SF Mono', ui-monospace, monospace",
|
|
28
|
+
fontVariantNumeric: "tabular-nums",
|
|
29
|
+
userSelect: "none",
|
|
30
|
+
pointerEvents: "none",
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
33
|
+
{state.scale.toFixed(2)}×
|
|
34
|
+
</div>
|
|
35
|
+
));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const Template = (args) => (
|
|
39
|
+
<div
|
|
40
|
+
style={{
|
|
41
|
+
fontFamily: "Inter, system-ui, -apple-system, sans-serif",
|
|
42
|
+
background:
|
|
43
|
+
"linear-gradient(165deg, #0c0a14 0%, #131025 40%, #0a0812 100%)",
|
|
44
|
+
padding: "20px 22px 28px",
|
|
45
|
+
borderRadius: 16,
|
|
46
|
+
boxSizing: "border-box",
|
|
47
|
+
}}
|
|
48
|
+
>
|
|
49
|
+
<header style={{ marginBottom: 16 }}>
|
|
50
|
+
<div
|
|
51
|
+
style={{
|
|
52
|
+
fontSize: 11,
|
|
53
|
+
fontWeight: 700,
|
|
54
|
+
letterSpacing: "0.12em",
|
|
55
|
+
textTransform: "uppercase",
|
|
56
|
+
color: "rgba(139, 92, 246, 0.85)",
|
|
57
|
+
marginBottom: 6,
|
|
58
|
+
}}
|
|
59
|
+
>
|
|
60
|
+
Basic demo
|
|
61
|
+
</div>
|
|
62
|
+
<h1
|
|
63
|
+
style={{
|
|
64
|
+
margin: 0,
|
|
65
|
+
fontSize: "clamp(1.2rem, 2.2vw, 1.55rem)",
|
|
66
|
+
fontWeight: 800,
|
|
67
|
+
letterSpacing: "-0.03em",
|
|
68
|
+
color: "#f4f4f5",
|
|
69
|
+
}}
|
|
70
|
+
>
|
|
71
|
+
Center Zoomed Out
|
|
72
|
+
</h1>
|
|
73
|
+
</header>
|
|
74
|
+
|
|
75
|
+
<TransformWrapper
|
|
76
|
+
{...normalizeArgs(args)}
|
|
77
|
+
initialScale={0.5}
|
|
78
|
+
minScale={0.2}
|
|
79
|
+
centerOnInit
|
|
80
|
+
centerZoomedOut
|
|
81
|
+
>
|
|
82
|
+
{(utils) => (
|
|
83
|
+
<div style={{ position: "relative" }}>
|
|
84
|
+
<Controls {...utils} />
|
|
85
|
+
<ScaleBadge />
|
|
86
|
+
<TransformComponent
|
|
87
|
+
wrapperStyle={{
|
|
88
|
+
...viewerChrome,
|
|
89
|
+
width: "min(600px, 90vw)",
|
|
90
|
+
height: "min(440px, 65vh)",
|
|
91
|
+
}}
|
|
92
|
+
>
|
|
93
|
+
<img src={exampleImg} alt="" style={{ display: "block" }} />
|
|
94
|
+
</TransformComponent>
|
|
95
|
+
</div>
|
|
96
|
+
)}
|
|
97
|
+
</TransformWrapper>
|
|
98
|
+
|
|
99
|
+
<div
|
|
100
|
+
style={{
|
|
101
|
+
marginTop: 16,
|
|
102
|
+
padding: "14px 18px",
|
|
103
|
+
borderRadius: 12,
|
|
104
|
+
background: "rgba(30, 27, 56, 0.7)",
|
|
105
|
+
border: "1px solid rgba(139, 92, 246, 0.2)",
|
|
106
|
+
maxWidth: 600,
|
|
107
|
+
display: "flex",
|
|
108
|
+
alignItems: "flex-start",
|
|
109
|
+
gap: 12,
|
|
110
|
+
}}
|
|
111
|
+
>
|
|
112
|
+
<span
|
|
113
|
+
style={{
|
|
114
|
+
flexShrink: 0,
|
|
115
|
+
width: 24,
|
|
116
|
+
height: 24,
|
|
117
|
+
borderRadius: 8,
|
|
118
|
+
background: "linear-gradient(135deg, #7c3aed, #6d28d9)",
|
|
119
|
+
display: "flex",
|
|
120
|
+
alignItems: "center",
|
|
121
|
+
justifyContent: "center",
|
|
122
|
+
fontSize: 12,
|
|
123
|
+
fontWeight: 800,
|
|
124
|
+
color: "#fff",
|
|
125
|
+
marginTop: 1,
|
|
126
|
+
}}
|
|
127
|
+
>
|
|
128
|
+
i
|
|
129
|
+
</span>
|
|
130
|
+
<div style={{ fontSize: 13, color: "#c4b5fd", lineHeight: 1.65 }}>
|
|
131
|
+
Try panning while zoomed out — the image{" "}
|
|
132
|
+
<strong style={{ color: "#ede9fe" }}>snaps back to center</strong>.
|
|
133
|
+
Zoom in past 1× and it pans freely.
|
|
134
|
+
<div
|
|
135
|
+
style={{
|
|
136
|
+
marginTop: 8,
|
|
137
|
+
display: "flex",
|
|
138
|
+
flexWrap: "wrap",
|
|
139
|
+
gap: 6,
|
|
140
|
+
}}
|
|
141
|
+
>
|
|
142
|
+
{[
|
|
143
|
+
{ label: "centerZoomedOut", color: "#a78bfa" },
|
|
144
|
+
{ label: "centerOnInit", color: "#a78bfa" },
|
|
145
|
+
{ label: "initialScale=0.5", color: "#38bdf8" },
|
|
146
|
+
].map((p) => (
|
|
147
|
+
<span
|
|
148
|
+
key={p.label}
|
|
149
|
+
style={{
|
|
150
|
+
padding: "3px 10px",
|
|
151
|
+
borderRadius: 6,
|
|
152
|
+
background: `${p.color}18`,
|
|
153
|
+
border: `1px solid ${p.color}35`,
|
|
154
|
+
fontSize: 11,
|
|
155
|
+
fontWeight: 600,
|
|
156
|
+
fontFamily: "'SF Mono', ui-monospace, monospace",
|
|
157
|
+
color: p.color,
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
{p.label}
|
|
161
|
+
</span>
|
|
162
|
+
))}
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
</div>
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
<Meta
|
|
170
|
+
title="Basic/Center Zoomed Out"
|
|
171
|
+
component={TransformWrapper}
|
|
172
|
+
argTypes={argsTypes}
|
|
173
|
+
/>
|
|
174
|
+
|
|
175
|
+
# Center Zoomed Out
|
|
176
|
+
|
|
177
|
+
When the image is **zoomed out** (scale below 1×), the content automatically
|
|
178
|
+
**stays centered** in the viewport. Try panning — the image snaps back. Once
|
|
179
|
+
you zoom in past 1×, free panning works normally.
|
|
180
|
+
|
|
181
|
+
This is the same pattern used in cinema / ticket-booking UIs where the layout
|
|
182
|
+
should always stay framed when the user hasn't zoomed in yet.
|
|
183
|
+
|
|
184
|
+
<DocTable
|
|
185
|
+
columns={[
|
|
186
|
+
{ header: "Prop" },
|
|
187
|
+
{ header: "Value", style: "type" },
|
|
188
|
+
{ header: "Effect", style: "text" },
|
|
189
|
+
]}
|
|
190
|
+
rows={[
|
|
191
|
+
["centerOnInit", "true", "Centers content when the component mounts"],
|
|
192
|
+
["centerZoomedOut", "true", "Keeps content centered whenever scale < 1"],
|
|
193
|
+
["initialScale", "0.5", "Starts zoomed out at 50%"],
|
|
194
|
+
["minScale", "0.2", "Allows zooming out further to 20%"],
|
|
195
|
+
]}
|
|
196
|
+
/>
|
|
197
|
+
|
|
198
|
+
<Canvas>
|
|
199
|
+
<Story name="Center Zoomed Out">
|
|
200
|
+
{(args) => <Template {...args} />}
|
|
201
|
+
</Story>
|
|
202
|
+
</Canvas>
|
|
203
|
+
|
|
204
|
+
## Component API
|
|
205
|
+
|
|
206
|
+
<ArgsTable story="Center Zoomed Out" />
|