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,1442 @@
1
+ import React, {
2
+ useCallback,
3
+ useEffect,
4
+ useMemo,
5
+ useRef,
6
+ useState,
7
+ } from "react";
8
+
9
+ import { TransformWrapper, TransformComponent } from "../../../components";
10
+ import { useTransformContext } from "../../../hooks";
11
+ import { normalizeArgs, viewerChrome } from "../../utils";
12
+ import heroImg from "../../assets/medium-image.jpg";
13
+
14
+ const font =
15
+ 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
16
+
17
+ const DISPLAY_W = 920;
18
+
19
+ const LABEL_PRESETS = [
20
+ { name: "Person", color: "#ef4444" },
21
+ { name: "Vehicle", color: "#3b82f6" },
22
+ { name: "Building", color: "#22c55e" },
23
+ { name: "Animal", color: "#f59e0b" },
24
+ { name: "Text", color: "#ec4899" },
25
+ { name: "Defect", color: "#f97316" },
26
+ { name: "Other", color: "#a78bfa" },
27
+ ];
28
+
29
+ interface Annotation {
30
+ id: string;
31
+ kind: "pin" | "rect";
32
+ title: string;
33
+ description: string;
34
+ label: string;
35
+ color: string;
36
+ x: number;
37
+ y: number;
38
+ w: number;
39
+ h: number;
40
+ }
41
+
42
+ type PlaceMode = "navigate" | "rect" | "pin";
43
+
44
+ type DraftShape = {
45
+ kind: "pin" | "rect";
46
+ x: number;
47
+ y: number;
48
+ w: number;
49
+ h: number;
50
+ };
51
+ type DrawingState = {
52
+ startX: number;
53
+ startY: number;
54
+ endX: number;
55
+ endY: number;
56
+ };
57
+
58
+ type ZoomToElement = (target: string | HTMLElement, scale?: number) => void;
59
+
60
+ function clientToContent(
61
+ clientX: number,
62
+ clientY: number,
63
+ wrapper: HTMLDivElement | null,
64
+ state: { positionX: number; positionY: number; scale: number },
65
+ ): { x: number; y: number } | null {
66
+ if (!wrapper) return null;
67
+ const r = wrapper.getBoundingClientRect();
68
+ return {
69
+ x: (clientX - r.left - state.positionX) / state.scale,
70
+ y: (clientY - r.top - state.positionY) / state.scale,
71
+ };
72
+ }
73
+
74
+ function newId(): string {
75
+ return `a-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
76
+ }
77
+
78
+ function normalizeRect(d: DrawingState) {
79
+ return {
80
+ x: Math.min(d.startX, d.endX),
81
+ y: Math.min(d.startY, d.endY),
82
+ w: Math.abs(d.endX - d.startX),
83
+ h: Math.abs(d.endY - d.startY),
84
+ };
85
+ }
86
+
87
+ /* ── Mode rail ───────────────────────────────────────────────── */
88
+
89
+ function ModeRail(props: {
90
+ placeMode: PlaceMode;
91
+ onModeChange: (m: PlaceMode) => void;
92
+ }) {
93
+ const { placeMode, onModeChange } = props;
94
+ const modes: {
95
+ id: PlaceMode;
96
+ label: string;
97
+ sub: string;
98
+ icon: React.ReactNode;
99
+ }[] = [
100
+ {
101
+ id: "navigate",
102
+ label: "Navigate",
103
+ sub: "Pan & zoom",
104
+ icon: (
105
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
106
+ <path
107
+ d="M8 2V14M2 8H14"
108
+ stroke="currentColor"
109
+ strokeWidth="1.4"
110
+ strokeLinecap="round"
111
+ />
112
+ <path
113
+ d="M6.5 3.5L8 2L9.5 3.5M6.5 12.5L8 14L9.5 12.5M3.5 6.5L2 8L3.5 9.5M12.5 6.5L14 8L12.5 9.5"
114
+ stroke="currentColor"
115
+ strokeWidth="1.2"
116
+ strokeLinecap="round"
117
+ strokeLinejoin="round"
118
+ />
119
+ </svg>
120
+ ),
121
+ },
122
+ {
123
+ id: "rect",
124
+ label: "Rectangle",
125
+ sub: "Bounding box",
126
+ icon: (
127
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
128
+ <rect
129
+ x="2"
130
+ y="3"
131
+ width="12"
132
+ height="10"
133
+ rx="1.5"
134
+ stroke="currentColor"
135
+ strokeWidth="1.4"
136
+ strokeDasharray="3 2"
137
+ />
138
+ <circle cx="2" cy="3" r="1.5" fill="currentColor" />
139
+ <circle cx="14" cy="3" r="1.5" fill="currentColor" />
140
+ <circle cx="2" cy="13" r="1.5" fill="currentColor" />
141
+ <circle cx="14" cy="13" r="1.5" fill="currentColor" />
142
+ </svg>
143
+ ),
144
+ },
145
+ {
146
+ id: "pin",
147
+ label: "Pin",
148
+ sub: "Point marker",
149
+ icon: (
150
+ <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
151
+ <path
152
+ d="M8 1.5C5.5 1.5 3.5 3.5 3.5 6C3.5 9.5 8 14.5 8 14.5S12.5 9.5 12.5 6C12.5 3.5 10.5 1.5 8 1.5Z"
153
+ stroke="currentColor"
154
+ strokeWidth="1.4"
155
+ strokeLinejoin="round"
156
+ />
157
+ <circle cx="8" cy="6" r="2" fill="currentColor" />
158
+ </svg>
159
+ ),
160
+ },
161
+ ];
162
+
163
+ return (
164
+ <div
165
+ style={{ display: "flex", flexWrap: "wrap", gap: 8, marginBottom: 12 }}
166
+ >
167
+ {modes.map((m) => {
168
+ const on = placeMode === m.id;
169
+ return (
170
+ <button
171
+ key={m.id}
172
+ type="button"
173
+ onClick={() => onModeChange(m.id)}
174
+ style={{
175
+ display: "flex",
176
+ alignItems: "center",
177
+ gap: 10,
178
+ padding: "10px 16px",
179
+ borderRadius: 12,
180
+ border: on
181
+ ? "1px solid rgba(167, 139, 250, 0.45)"
182
+ : "1px solid rgba(63, 63, 70, 0.9)",
183
+ background: on
184
+ ? "rgba(109, 40, 217, 0.18)"
185
+ : "rgba(24, 24, 27, 0.65)",
186
+ cursor: "pointer",
187
+ fontFamily: "inherit",
188
+ minWidth: 140,
189
+ }}
190
+ >
191
+ <span
192
+ style={{ color: on ? "#c4b5fd" : "#71717a", display: "flex" }}
193
+ >
194
+ {m.icon}
195
+ </span>
196
+ <span style={{ textAlign: "left" }}>
197
+ <span
198
+ style={{
199
+ display: "block",
200
+ fontSize: 13,
201
+ fontWeight: 700,
202
+ color: on ? "#e9d5ff" : "#d4d4d8",
203
+ }}
204
+ >
205
+ {m.label}
206
+ </span>
207
+ <span
208
+ style={{
209
+ display: "block",
210
+ fontSize: 11,
211
+ color: "#71717a",
212
+ marginTop: 1,
213
+ }}
214
+ >
215
+ {m.sub}
216
+ </span>
217
+ </span>
218
+ </button>
219
+ );
220
+ })}
221
+ </div>
222
+ );
223
+ }
224
+
225
+ /* ── Annotation markers (rendered on image) ──────────────────── */
226
+
227
+ function AnnotationMarker(props: {
228
+ annotation: Annotation;
229
+ pinIndex: number;
230
+ isSelected: boolean;
231
+ onClick: () => void;
232
+ }) {
233
+ const { annotation: a, pinIndex, isSelected: sel, onClick } = props;
234
+
235
+ if (a.kind === "pin") {
236
+ return (
237
+ <button
238
+ id={`annotation-${a.id}`}
239
+ type="button"
240
+ onClick={(e) => {
241
+ e.stopPropagation();
242
+ onClick();
243
+ }}
244
+ style={{
245
+ position: "absolute",
246
+ left: a.x - 14,
247
+ top: a.y - 14,
248
+ width: 28,
249
+ height: 28,
250
+ borderRadius: "50%",
251
+ border: sel ? "3px solid #fbbf24" : `2px solid ${a.color}`,
252
+ background: a.color,
253
+ color: "#fff",
254
+ fontSize: 12,
255
+ fontWeight: 800,
256
+ fontFamily: "inherit",
257
+ cursor: "pointer",
258
+ display: "flex",
259
+ alignItems: "center",
260
+ justifyContent: "center",
261
+ boxShadow: sel
262
+ ? `0 0 0 4px rgba(251,191,36,0.25), 0 0 18px ${a.color}60`
263
+ : `0 4px 16px rgba(0,0,0,0.4), 0 0 8px ${a.color}40`,
264
+ zIndex: sel ? 8 : 5,
265
+ transition: "box-shadow 0.15s ease, transform 0.15s ease",
266
+ padding: 0,
267
+ }}
268
+ >
269
+ {pinIndex}
270
+ </button>
271
+ );
272
+ }
273
+
274
+ return (
275
+ <div
276
+ id={`annotation-${a.id}`}
277
+ role="button"
278
+ tabIndex={0}
279
+ onClick={(e) => {
280
+ e.stopPropagation();
281
+ onClick();
282
+ }}
283
+ onKeyDown={(e) => {
284
+ if (e.key === "Enter" || e.key === " ") {
285
+ e.preventDefault();
286
+ e.stopPropagation();
287
+ onClick();
288
+ }
289
+ }}
290
+ style={{
291
+ position: "absolute",
292
+ left: a.x,
293
+ top: a.y,
294
+ width: a.w,
295
+ height: a.h,
296
+ border: sel ? `2.5px solid #fbbf24` : `2px solid ${a.color}`,
297
+ borderRadius: 4,
298
+ background: sel ? `${a.color}18` : `${a.color}10`,
299
+ cursor: "pointer",
300
+ boxShadow: sel
301
+ ? `0 0 0 3px rgba(251,191,36,0.2), 0 0 16px ${a.color}30`
302
+ : "none",
303
+ zIndex: sel ? 8 : 5,
304
+ transition: "border-color 0.15s ease, box-shadow 0.15s ease",
305
+ }}
306
+ >
307
+ <span
308
+ style={{
309
+ position: "absolute",
310
+ top: -1,
311
+ left: -1,
312
+ padding: "2px 8px",
313
+ borderRadius: "4px 0 4px 0",
314
+ background: a.color,
315
+ color: "#fff",
316
+ fontSize: 10,
317
+ fontWeight: 700,
318
+ fontFamily: font,
319
+ whiteSpace: "nowrap",
320
+ lineHeight: "16px",
321
+ }}
322
+ >
323
+ {a.label}
324
+ </span>
325
+ </div>
326
+ );
327
+ }
328
+
329
+ /* ── Draft form ──────────────────────────────────────────────── */
330
+
331
+ function DraftForm(props: {
332
+ draft: DraftShape;
333
+ anchor: { left: number; top: number };
334
+ onCommit: (title: string, description: string, labelIdx: number) => void;
335
+ onCancel: () => void;
336
+ }) {
337
+ const { draft, anchor, onCommit, onCancel } = props;
338
+ const [title, setTitle] = useState("");
339
+ const [description, setDescription] = useState("");
340
+ const [labelIdx, setLabelIdx] = useState(0);
341
+
342
+ return (
343
+ // eslint-disable-next-line jsx-a11y/no-static-element-interactions
344
+ <div
345
+ style={{
346
+ position: "absolute",
347
+ left: anchor.left + 14,
348
+ top: anchor.top,
349
+ zIndex: 25,
350
+ width: 280,
351
+ }}
352
+ onClick={(e) => e.stopPropagation()}
353
+ onKeyDown={(e) => e.stopPropagation()}
354
+ onPointerDown={(e) => e.stopPropagation()}
355
+ onPointerMove={(e) => e.stopPropagation()}
356
+ onPointerUp={(e) => e.stopPropagation()}
357
+ onMouseDown={(e) => e.stopPropagation()}
358
+ onTouchStart={(e) => e.stopPropagation()}
359
+ >
360
+ <div
361
+ style={{
362
+ borderRadius: 14,
363
+ padding: 2,
364
+ background:
365
+ "linear-gradient(135deg, rgba(167,139,250,0.5), rgba(96,165,250,0.4))",
366
+ }}
367
+ >
368
+ <div
369
+ style={{
370
+ borderRadius: 12,
371
+ background: "rgba(9, 9, 11, 0.97)",
372
+ border: "1px solid rgba(63, 63, 70, 0.8)",
373
+ padding: "14px 14px 12px",
374
+ }}
375
+ >
376
+ <div
377
+ style={{
378
+ fontSize: 10,
379
+ fontWeight: 700,
380
+ letterSpacing: "0.1em",
381
+ textTransform: "uppercase",
382
+ color: "#a1a1aa",
383
+ marginBottom: 10,
384
+ }}
385
+ >
386
+ {draft.kind === "rect" ? "New bounding box" : "New pin"}
387
+ </div>
388
+
389
+ {/* eslint-disable jsx-a11y/no-autofocus */}
390
+ <input
391
+ autoFocus
392
+ value={title}
393
+ placeholder="Title (e.g. Main subject)"
394
+ onChange={(e) => setTitle(e.target.value)}
395
+ onKeyDown={(e) => {
396
+ if (e.key === "Escape") onCancel();
397
+ }}
398
+ style={{
399
+ width: "100%",
400
+ boxSizing: "border-box",
401
+ padding: "9px 10px",
402
+ borderRadius: 8,
403
+ border: "1px solid #3f3f46",
404
+ background: "#18181b",
405
+ color: "#fafafa",
406
+ fontSize: 13,
407
+ fontFamily: "inherit",
408
+ marginBottom: 8,
409
+ }}
410
+ />
411
+ {/* eslint-enable jsx-a11y/no-autofocus */}
412
+
413
+ <textarea
414
+ value={description}
415
+ placeholder="Description (optional)"
416
+ rows={2}
417
+ onChange={(e) => setDescription(e.target.value)}
418
+ onKeyDown={(e) => {
419
+ if (e.key === "Escape") onCancel();
420
+ }}
421
+ style={{
422
+ width: "100%",
423
+ boxSizing: "border-box",
424
+ padding: "9px 10px",
425
+ borderRadius: 8,
426
+ border: "1px solid #3f3f46",
427
+ background: "#18181b",
428
+ color: "#fafafa",
429
+ fontSize: 13,
430
+ fontFamily: "inherit",
431
+ resize: "none",
432
+ marginBottom: 10,
433
+ }}
434
+ />
435
+
436
+ <div style={{ marginBottom: 12 }}>
437
+ <div
438
+ style={{
439
+ fontSize: 10,
440
+ fontWeight: 700,
441
+ letterSpacing: "0.08em",
442
+ textTransform: "uppercase",
443
+ color: "#71717a",
444
+ marginBottom: 6,
445
+ }}
446
+ >
447
+ Label
448
+ </div>
449
+ <div style={{ display: "flex", flexWrap: "wrap", gap: 4 }}>
450
+ {LABEL_PRESETS.map((p, i) => {
451
+ const on = labelIdx === i;
452
+ return (
453
+ <button
454
+ key={p.name}
455
+ type="button"
456
+ onClick={() => setLabelIdx(i)}
457
+ style={{
458
+ padding: "4px 10px",
459
+ borderRadius: 6,
460
+ border: on
461
+ ? `1.5px solid ${p.color}`
462
+ : "1px solid #3f3f46",
463
+ background: on ? `${p.color}22` : "transparent",
464
+ color: on ? p.color : "#a1a1aa",
465
+ fontSize: 11,
466
+ fontWeight: 600,
467
+ fontFamily: "inherit",
468
+ cursor: "pointer",
469
+ }}
470
+ >
471
+ {p.name}
472
+ </button>
473
+ );
474
+ })}
475
+ </div>
476
+ </div>
477
+
478
+ <div style={{ display: "flex", gap: 8, justifyContent: "flex-end" }}>
479
+ <button
480
+ type="button"
481
+ onClick={onCancel}
482
+ style={{
483
+ padding: "7px 14px",
484
+ borderRadius: 8,
485
+ border: "1px solid #3f3f46",
486
+ background: "transparent",
487
+ color: "#a1a1aa",
488
+ fontSize: 12,
489
+ fontWeight: 600,
490
+ fontFamily: "inherit",
491
+ cursor: "pointer",
492
+ }}
493
+ >
494
+ Cancel
495
+ </button>
496
+ <button
497
+ type="button"
498
+ onClick={() =>
499
+ onCommit(title || "Untitled", description, labelIdx)
500
+ }
501
+ style={{
502
+ padding: "7px 16px",
503
+ borderRadius: 8,
504
+ border: "none",
505
+ background: "linear-gradient(135deg, #7c3aed, #6d28d9)",
506
+ color: "#fff",
507
+ fontSize: 12,
508
+ fontWeight: 700,
509
+ fontFamily: "inherit",
510
+ cursor: "pointer",
511
+ }}
512
+ >
513
+ Save
514
+ </button>
515
+ </div>
516
+ </div>
517
+ </div>
518
+ </div>
519
+ );
520
+ }
521
+
522
+ /* ── Detail popup ────────────────────────────────────────────── */
523
+
524
+ function DetailPopup(props: {
525
+ annotation: Annotation;
526
+ anchor: { left: number; top: number };
527
+ onClose: () => void;
528
+ onDelete: () => void;
529
+ }) {
530
+ const { annotation: a, anchor, onClose, onDelete } = props;
531
+
532
+ return (
533
+ // eslint-disable-next-line jsx-a11y/no-static-element-interactions
534
+ <div
535
+ style={{
536
+ position: "absolute",
537
+ left: anchor.left + 14,
538
+ top: anchor.top,
539
+ zIndex: 25,
540
+ width: 260,
541
+ }}
542
+ onClick={(e) => e.stopPropagation()}
543
+ onKeyDown={(e) => e.stopPropagation()}
544
+ onPointerDown={(e) => e.stopPropagation()}
545
+ onPointerMove={(e) => e.stopPropagation()}
546
+ onPointerUp={(e) => e.stopPropagation()}
547
+ onMouseDown={(e) => e.stopPropagation()}
548
+ onTouchStart={(e) => e.stopPropagation()}
549
+ >
550
+ <div
551
+ style={{
552
+ borderRadius: 14,
553
+ background: "rgba(9, 9, 11, 0.96)",
554
+ border: "1px solid rgba(63, 63, 70, 0.85)",
555
+ backdropFilter: "blur(12px)",
556
+ WebkitBackdropFilter: "blur(12px)",
557
+ boxShadow: "0 12px 40px rgba(0,0,0,0.5)",
558
+ overflow: "hidden",
559
+ }}
560
+ >
561
+ {/* Header bar */}
562
+ <div
563
+ style={{
564
+ display: "flex",
565
+ alignItems: "center",
566
+ justifyContent: "space-between",
567
+ padding: "10px 14px",
568
+ background: `${a.color}12`,
569
+ borderBottom: "1px solid rgba(63,63,70,0.6)",
570
+ }}
571
+ >
572
+ <span
573
+ style={{
574
+ display: "inline-flex",
575
+ alignItems: "center",
576
+ gap: 6,
577
+ padding: "3px 10px",
578
+ borderRadius: 6,
579
+ background: a.color,
580
+ color: "#fff",
581
+ fontSize: 11,
582
+ fontWeight: 700,
583
+ fontFamily: font,
584
+ }}
585
+ >
586
+ <span
587
+ style={{
588
+ width: 6,
589
+ height: 6,
590
+ borderRadius: "50%",
591
+ background: "#fff",
592
+ opacity: 0.6,
593
+ }}
594
+ />
595
+ {a.label}
596
+ </span>
597
+ <button
598
+ type="button"
599
+ onClick={onClose}
600
+ aria-label="Close"
601
+ style={{
602
+ width: 24,
603
+ height: 24,
604
+ borderRadius: 6,
605
+ border: "none",
606
+ background: "rgba(255,255,255,0.06)",
607
+ cursor: "pointer",
608
+ display: "flex",
609
+ alignItems: "center",
610
+ justifyContent: "center",
611
+ padding: 0,
612
+ }}
613
+ >
614
+ <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
615
+ <path
616
+ d="M2.5 2.5L9.5 9.5M9.5 2.5L2.5 9.5"
617
+ stroke="#a1a1aa"
618
+ strokeWidth="1.5"
619
+ strokeLinecap="round"
620
+ />
621
+ </svg>
622
+ </button>
623
+ </div>
624
+
625
+ {/* Body */}
626
+ <div style={{ padding: "12px 14px" }}>
627
+ <h3
628
+ style={{
629
+ margin: "0 0 6px",
630
+ fontSize: 15,
631
+ fontWeight: 700,
632
+ color: "#f4f4f5",
633
+ fontFamily: font,
634
+ }}
635
+ >
636
+ {a.title}
637
+ </h3>
638
+ {a.description && (
639
+ <p
640
+ style={{
641
+ margin: "0 0 12px",
642
+ fontSize: 13,
643
+ lineHeight: 1.5,
644
+ color: "#a1a1aa",
645
+ fontFamily: font,
646
+ }}
647
+ >
648
+ {a.description}
649
+ </p>
650
+ )}
651
+
652
+ <div
653
+ style={{
654
+ display: "grid",
655
+ gridTemplateColumns: "1fr 1fr",
656
+ gap: 8,
657
+ padding: "10px 12px",
658
+ borderRadius: 8,
659
+ background: "rgba(39,39,42,0.5)",
660
+ marginBottom: 12,
661
+ }}
662
+ >
663
+ <div>
664
+ <div
665
+ style={{
666
+ fontSize: 10,
667
+ fontWeight: 600,
668
+ color: "#71717a",
669
+ marginBottom: 2,
670
+ }}
671
+ >
672
+ Position
673
+ </div>
674
+ <div
675
+ style={{
676
+ fontSize: 12,
677
+ fontWeight: 600,
678
+ color: "#d4d4d8",
679
+ fontFamily: "ui-monospace, monospace",
680
+ }}
681
+ >
682
+ {Math.round(a.x)}, {Math.round(a.y)}
683
+ </div>
684
+ </div>
685
+ {a.kind === "rect" && (
686
+ <div>
687
+ <div
688
+ style={{
689
+ fontSize: 10,
690
+ fontWeight: 600,
691
+ color: "#71717a",
692
+ marginBottom: 2,
693
+ }}
694
+ >
695
+ Size
696
+ </div>
697
+ <div
698
+ style={{
699
+ fontSize: 12,
700
+ fontWeight: 600,
701
+ color: "#d4d4d8",
702
+ fontFamily: "ui-monospace, monospace",
703
+ }}
704
+ >
705
+ {Math.round(a.w)} × {Math.round(a.h)}
706
+ </div>
707
+ </div>
708
+ )}
709
+ <div>
710
+ <div
711
+ style={{
712
+ fontSize: 10,
713
+ fontWeight: 600,
714
+ color: "#71717a",
715
+ marginBottom: 2,
716
+ }}
717
+ >
718
+ Type
719
+ </div>
720
+ <div style={{ fontSize: 12, fontWeight: 600, color: "#d4d4d8" }}>
721
+ {a.kind === "rect" ? "Bounding box" : "Point marker"}
722
+ </div>
723
+ </div>
724
+ </div>
725
+
726
+ <button
727
+ type="button"
728
+ onClick={onDelete}
729
+ style={{
730
+ width: "100%",
731
+ padding: "8px",
732
+ borderRadius: 8,
733
+ border: "1px solid rgba(239,68,68,0.3)",
734
+ background: "rgba(239,68,68,0.08)",
735
+ color: "#fca5a5",
736
+ fontSize: 12,
737
+ fontWeight: 600,
738
+ fontFamily: "inherit",
739
+ cursor: "pointer",
740
+ }}
741
+ >
742
+ Delete annotation
743
+ </button>
744
+ </div>
745
+ </div>
746
+ </div>
747
+ );
748
+ }
749
+
750
+ /* ── Annotation canvas ───────────────────────────────────────── */
751
+
752
+ function AnnotationCanvas(props: {
753
+ imgSize: { w: number; h: number };
754
+ setImgSize: React.Dispatch<React.SetStateAction<{ w: number; h: number }>>;
755
+ annotations: Annotation[];
756
+ addAnnotation: (a: Annotation) => void;
757
+ draft: DraftShape | null;
758
+ setDraft: React.Dispatch<React.SetStateAction<DraftShape | null>>;
759
+ placeMode: PlaceMode;
760
+ selectedId: string | null;
761
+ setSelectedId: React.Dispatch<React.SetStateAction<string | null>>;
762
+ onDelete: (id: string) => void;
763
+ }) {
764
+ const {
765
+ imgSize,
766
+ setImgSize,
767
+ annotations,
768
+ addAnnotation,
769
+ draft,
770
+ setDraft,
771
+ placeMode,
772
+ selectedId,
773
+ setSelectedId,
774
+ onDelete,
775
+ } = props;
776
+
777
+ const instance = useTransformContext();
778
+ const [drawing, setDrawing] = useState<DrawingState | null>(null);
779
+
780
+ const pinCount = useMemo(() => {
781
+ const map = new Map<string, number>();
782
+ let n = 0;
783
+ annotations.forEach((a) => {
784
+ if (a.kind === "pin") {
785
+ n += 1;
786
+ map.set(a.id, n);
787
+ }
788
+ });
789
+ return map;
790
+ }, [annotations]);
791
+
792
+ const handlePointerDown = useCallback(
793
+ (e: React.PointerEvent) => {
794
+ if (placeMode !== "rect") return;
795
+ (e.target as HTMLElement).setPointerCapture(e.pointerId);
796
+ const pt = clientToContent(
797
+ e.clientX,
798
+ e.clientY,
799
+ instance.wrapperComponent,
800
+ instance.state,
801
+ );
802
+ if (!pt) return;
803
+ setDrawing({ startX: pt.x, startY: pt.y, endX: pt.x, endY: pt.y });
804
+ },
805
+ [placeMode, instance],
806
+ );
807
+
808
+ const handlePointerMove = useCallback(
809
+ (e: React.PointerEvent) => {
810
+ if (!drawing) return;
811
+ const pt = clientToContent(
812
+ e.clientX,
813
+ e.clientY,
814
+ instance.wrapperComponent,
815
+ instance.state,
816
+ );
817
+ if (pt)
818
+ setDrawing((prev) =>
819
+ prev ? { ...prev, endX: pt.x, endY: pt.y } : null,
820
+ );
821
+ },
822
+ [drawing, instance],
823
+ );
824
+
825
+ const handlePointerUp = useCallback(
826
+ (e: React.PointerEvent) => {
827
+ (e.target as HTMLElement).releasePointerCapture(e.pointerId);
828
+ if (!drawing) return;
829
+ const r = normalizeRect(drawing);
830
+ setDrawing(null);
831
+ if (r.w > 10 && r.h > 10) {
832
+ setDraft({ kind: "rect", ...r });
833
+ setSelectedId(null);
834
+ }
835
+ },
836
+ [drawing, setDraft, setSelectedId],
837
+ );
838
+
839
+ const handlePinClick = useCallback(
840
+ (e: React.MouseEvent) => {
841
+ if (placeMode !== "pin") return;
842
+ const pt = clientToContent(
843
+ e.clientX,
844
+ e.clientY,
845
+ instance.wrapperComponent,
846
+ instance.state,
847
+ );
848
+ if (!pt) return;
849
+ if (pt.x < 0 || pt.y < 0 || pt.x > imgSize.w || pt.y > imgSize.h) return;
850
+ setDraft({ kind: "pin", x: pt.x, y: pt.y, w: 0, h: 0 });
851
+ setSelectedId(null);
852
+ },
853
+ [placeMode, instance, imgSize, setDraft, setSelectedId],
854
+ );
855
+
856
+ const commitDraft = useCallback(
857
+ (title: string, description: string, labelIdx: number) => {
858
+ if (!draft) return;
859
+ const preset = LABEL_PRESETS[labelIdx] ?? LABEL_PRESETS[0];
860
+ const ann: Annotation = {
861
+ id: newId(),
862
+ kind: draft.kind,
863
+ title,
864
+ description,
865
+ label: preset.name,
866
+ color: preset.color,
867
+ x: draft.x,
868
+ y: draft.y,
869
+ w: draft.w,
870
+ h: draft.h,
871
+ };
872
+ addAnnotation(ann);
873
+ },
874
+ [draft, addAnnotation],
875
+ );
876
+
877
+ const selectedAnnotation =
878
+ annotations.find((a) => a.id === selectedId) ?? null;
879
+ const drawPreview = drawing ? normalizeRect(drawing) : null;
880
+
881
+ const [txState, setTxState] = useState({
882
+ positionX: instance.state.positionX,
883
+ positionY: instance.state.positionY,
884
+ scale: instance.state.scale,
885
+ });
886
+
887
+ useEffect(() => {
888
+ return instance.onChange((ref) => {
889
+ setTxState({
890
+ positionX: ref.state.positionX,
891
+ positionY: ref.state.positionY,
892
+ scale: ref.state.scale,
893
+ });
894
+ });
895
+ }, [instance]);
896
+
897
+ const contentToScreen = useCallback(
898
+ (cx: number, cy: number) => ({
899
+ left: cx * txState.scale + txState.positionX,
900
+ top: cy * txState.scale + txState.positionY,
901
+ }),
902
+ [txState],
903
+ );
904
+
905
+ const popupAnchor = useMemo(() => {
906
+ if (draft) {
907
+ const ax = draft.kind === "rect" ? draft.x + draft.w : draft.x;
908
+ const ay = draft.y;
909
+ return contentToScreen(ax, ay);
910
+ }
911
+ if (selectedAnnotation) {
912
+ const ax =
913
+ selectedAnnotation.kind === "rect"
914
+ ? selectedAnnotation.x + selectedAnnotation.w
915
+ : selectedAnnotation.x;
916
+ const ay = selectedAnnotation.y;
917
+ return contentToScreen(ax, ay);
918
+ }
919
+ return null;
920
+ }, [draft, selectedAnnotation, contentToScreen]);
921
+
922
+ return (
923
+ <div style={{ position: "relative" }}>
924
+ <TransformComponent
925
+ wrapperStyle={{
926
+ width: "min(920px, calc(100vw - 120px))",
927
+ height: "min(560px, 62vh)",
928
+ ...viewerChrome,
929
+ overflow: "hidden",
930
+ }}
931
+ contentStyle={{ width: imgSize.w, height: imgSize.h }}
932
+ >
933
+ <div
934
+ role="button"
935
+ tabIndex={0}
936
+ style={{
937
+ position: "relative",
938
+ width: imgSize.w,
939
+ height: imgSize.h,
940
+ background: "#18181b",
941
+ }}
942
+ onClick={() => {
943
+ setSelectedId(null);
944
+ setDraft(null);
945
+ }}
946
+ onKeyDown={(e) => {
947
+ if (e.key === "Enter" || e.key === " ") {
948
+ e.preventDefault();
949
+ setSelectedId(null);
950
+ setDraft(null);
951
+ }
952
+ }}
953
+ >
954
+ <img
955
+ src={heroImg}
956
+ alt="Labeling target"
957
+ draggable={false}
958
+ onLoad={(e) => {
959
+ const im = e.currentTarget;
960
+ setImgSize({
961
+ w: DISPLAY_W,
962
+ h: Math.round((im.naturalHeight / im.naturalWidth) * DISPLAY_W),
963
+ });
964
+ }}
965
+ style={{ width: DISPLAY_W, height: "auto", display: "block" }}
966
+ />
967
+
968
+ {/* Rendered annotations */}
969
+ {annotations.map((a) => (
970
+ <AnnotationMarker
971
+ key={a.id}
972
+ annotation={a}
973
+ pinIndex={pinCount.get(a.id) ?? 0}
974
+ isSelected={selectedId === a.id}
975
+ onClick={() => {
976
+ setSelectedId(a.id);
977
+ setDraft(null);
978
+ }}
979
+ />
980
+ ))}
981
+
982
+ {/* Drawing preview (live rect while dragging) */}
983
+ {drawPreview && (
984
+ <div
985
+ style={{
986
+ position: "absolute",
987
+ left: drawPreview.x,
988
+ top: drawPreview.y,
989
+ width: drawPreview.w,
990
+ height: drawPreview.h,
991
+ border: "2px dashed rgba(167, 139, 250, 0.85)",
992
+ borderRadius: 4,
993
+ background: "rgba(167, 139, 250, 0.08)",
994
+ pointerEvents: "none",
995
+ zIndex: 15,
996
+ }}
997
+ />
998
+ )}
999
+
1000
+ {/* Overlay for placement (pin click or rect drawing) */}
1001
+ {placeMode !== "navigate" && !draft && (
1002
+ <div
1003
+ role="button"
1004
+ tabIndex={-1}
1005
+ aria-label="Click to annotate"
1006
+ onClick={placeMode === "pin" ? handlePinClick : undefined}
1007
+ onKeyDown={() => {}}
1008
+ onPointerDown={
1009
+ placeMode === "rect" ? handlePointerDown : undefined
1010
+ }
1011
+ onPointerMove={
1012
+ placeMode === "rect" ? handlePointerMove : undefined
1013
+ }
1014
+ onPointerUp={placeMode === "rect" ? handlePointerUp : undefined}
1015
+ style={{
1016
+ position: "absolute",
1017
+ inset: 0,
1018
+ zIndex: 10,
1019
+ cursor: "crosshair",
1020
+ background:
1021
+ placeMode === "rect"
1022
+ ? "rgba(167, 139, 250, 0.03)"
1023
+ : "rgba(244, 63, 94, 0.03)",
1024
+ border: "none",
1025
+ padding: 0,
1026
+ outline: "none",
1027
+ touchAction: "none",
1028
+ }}
1029
+ />
1030
+ )}
1031
+
1032
+ {/* Draft marker preview (visible while form is open) */}
1033
+ {draft && draft.kind === "rect" && (
1034
+ <div
1035
+ style={{
1036
+ position: "absolute",
1037
+ left: draft.x,
1038
+ top: draft.y,
1039
+ width: draft.w,
1040
+ height: draft.h,
1041
+ border: "2px dashed rgba(167, 139, 250, 0.85)",
1042
+ borderRadius: 4,
1043
+ background: "rgba(167, 139, 250, 0.1)",
1044
+ pointerEvents: "none",
1045
+ zIndex: 15,
1046
+ }}
1047
+ />
1048
+ )}
1049
+ {draft && draft.kind === "pin" && (
1050
+ <div
1051
+ style={{
1052
+ position: "absolute",
1053
+ left: draft.x - 14,
1054
+ top: draft.y - 14,
1055
+ width: 28,
1056
+ height: 28,
1057
+ borderRadius: "50%",
1058
+ border: "2px dashed rgba(167, 139, 250, 0.85)",
1059
+ background: "rgba(167, 139, 250, 0.25)",
1060
+ pointerEvents: "none",
1061
+ zIndex: 15,
1062
+ boxShadow: "0 0 12px rgba(167, 139, 250, 0.3)",
1063
+ }}
1064
+ />
1065
+ )}
1066
+ </div>
1067
+ </TransformComponent>
1068
+
1069
+ {/* Floating popups — rendered outside the transform so they don't
1070
+ scale/clip, but anchored to the annotation's screen position. */}
1071
+ {draft && popupAnchor && (
1072
+ <DraftForm
1073
+ draft={draft}
1074
+ anchor={popupAnchor}
1075
+ onCommit={commitDraft}
1076
+ onCancel={() => setDraft(null)}
1077
+ />
1078
+ )}
1079
+
1080
+ {selectedAnnotation && !draft && popupAnchor && (
1081
+ <DetailPopup
1082
+ annotation={selectedAnnotation}
1083
+ anchor={popupAnchor}
1084
+ onClose={() => setSelectedId(null)}
1085
+ onDelete={() => onDelete(selectedAnnotation.id)}
1086
+ />
1087
+ )}
1088
+ </div>
1089
+ );
1090
+ }
1091
+
1092
+ /* ── Sidebar ─────────────────────────────────────────────────── */
1093
+
1094
+ function Sidebar(props: {
1095
+ annotations: Annotation[];
1096
+ selectedId: string | null;
1097
+ onSelect: (id: string | null) => void;
1098
+ onDelete: (id: string) => void;
1099
+ zoomToElement: ZoomToElement;
1100
+ }) {
1101
+ const { annotations, selectedId, onSelect, onDelete, zoomToElement } = props;
1102
+
1103
+ const pinCount = useRef(0);
1104
+
1105
+ return (
1106
+ <aside
1107
+ style={{
1108
+ flex: "1 1 280px",
1109
+ minWidth: 260,
1110
+ maxWidth: 360,
1111
+ borderRadius: 14,
1112
+ background: "rgba(24, 24, 27, 0.75)",
1113
+ border: "1px solid rgba(63, 63, 70, 0.75)",
1114
+ padding: "16px 16px 14px",
1115
+ maxHeight: 600,
1116
+ overflow: "auto",
1117
+ }}
1118
+ >
1119
+ <div
1120
+ style={{
1121
+ fontSize: 11,
1122
+ fontWeight: 700,
1123
+ letterSpacing: "0.1em",
1124
+ textTransform: "uppercase",
1125
+ color: "#71717a",
1126
+ marginBottom: 12,
1127
+ }}
1128
+ >
1129
+ Annotations ({annotations.length})
1130
+ </div>
1131
+
1132
+ {annotations.length === 0 && (
1133
+ <p
1134
+ style={{
1135
+ margin: 0,
1136
+ fontSize: 13,
1137
+ color: "#71717a",
1138
+ lineHeight: 1.55,
1139
+ }}
1140
+ >
1141
+ No annotations yet. Switch to{" "}
1142
+ <strong style={{ color: "#a1a1aa" }}>Rectangle</strong> or{" "}
1143
+ <strong style={{ color: "#a1a1aa" }}>Pin</strong> and draw on the
1144
+ image.
1145
+ </p>
1146
+ )}
1147
+
1148
+ <ul style={{ listStyle: "none", margin: 0, padding: 0 }}>
1149
+ {(() => {
1150
+ pinCount.current = 0;
1151
+ return null;
1152
+ })()}
1153
+ {annotations.map((a) => {
1154
+ const sel = selectedId === a.id;
1155
+ let idx: number | undefined;
1156
+ if (a.kind === "pin") {
1157
+ pinCount.current += 1;
1158
+ idx = pinCount.current;
1159
+ }
1160
+ return (
1161
+ <li
1162
+ key={a.id}
1163
+ style={{
1164
+ marginBottom: 8,
1165
+ borderRadius: 10,
1166
+ overflow: "hidden",
1167
+ border: sel
1168
+ ? "1px solid rgba(251,191,36,0.45)"
1169
+ : "1px solid transparent",
1170
+ background: sel ? "rgba(113,63,18,0.15)" : "rgba(39,39,42,0.5)",
1171
+ }}
1172
+ >
1173
+ <div style={{ padding: "10px 12px" }}>
1174
+ <div
1175
+ style={{
1176
+ display: "flex",
1177
+ alignItems: "center",
1178
+ justifyContent: "space-between",
1179
+ gap: 8,
1180
+ marginBottom: 6,
1181
+ }}
1182
+ >
1183
+ <div
1184
+ style={{ display: "flex", alignItems: "center", gap: 8 }}
1185
+ >
1186
+ <span
1187
+ style={{
1188
+ display: "inline-block",
1189
+ padding: "2px 8px",
1190
+ borderRadius: 5,
1191
+ background: `${a.color}22`,
1192
+ color: a.color,
1193
+ fontSize: 10,
1194
+ fontWeight: 700,
1195
+ fontFamily: font,
1196
+ }}
1197
+ >
1198
+ {a.label}
1199
+ </span>
1200
+ <span style={{ fontSize: 10, color: "#52525b" }}>
1201
+ {a.kind === "rect" ? "Box" : `Pin #${idx}`}
1202
+ </span>
1203
+ </div>
1204
+ <div style={{ display: "flex", gap: 4 }}>
1205
+ <button
1206
+ type="button"
1207
+ onClick={(e) => {
1208
+ e.stopPropagation();
1209
+ onSelect(a.id);
1210
+ requestAnimationFrame(() =>
1211
+ zoomToElement(`annotation-${a.id}`, 1.8),
1212
+ );
1213
+ }}
1214
+ style={{
1215
+ fontSize: 11,
1216
+ fontWeight: 600,
1217
+ color: "#a78bfa",
1218
+ background: "none",
1219
+ border: "none",
1220
+ cursor: "pointer",
1221
+ fontFamily: "inherit",
1222
+ }}
1223
+ >
1224
+ Focus
1225
+ </button>
1226
+ <button
1227
+ type="button"
1228
+ onClick={(e) => {
1229
+ e.stopPropagation();
1230
+ onDelete(a.id);
1231
+ }}
1232
+ style={{
1233
+ fontSize: 11,
1234
+ fontWeight: 600,
1235
+ color: "#71717a",
1236
+ background: "none",
1237
+ border: "none",
1238
+ cursor: "pointer",
1239
+ fontFamily: "inherit",
1240
+ }}
1241
+ >
1242
+ ×
1243
+ </button>
1244
+ </div>
1245
+ </div>
1246
+
1247
+ <button
1248
+ type="button"
1249
+ onClick={() => {
1250
+ onSelect(a.id);
1251
+ requestAnimationFrame(() =>
1252
+ zoomToElement(`annotation-${a.id}`, 1.8),
1253
+ );
1254
+ }}
1255
+ style={{
1256
+ width: "100%",
1257
+ border: "none",
1258
+ background: "none",
1259
+ padding: 0,
1260
+ cursor: "pointer",
1261
+ fontFamily: "inherit",
1262
+ textAlign: "left",
1263
+ }}
1264
+ >
1265
+ <div
1266
+ style={{
1267
+ fontSize: 13,
1268
+ fontWeight: 600,
1269
+ color: "#e4e4e7",
1270
+ marginBottom: 2,
1271
+ }}
1272
+ >
1273
+ {a.title}
1274
+ </div>
1275
+ {a.description && (
1276
+ <p
1277
+ style={{
1278
+ margin: "0 0 4px",
1279
+ fontSize: 12,
1280
+ color: "#71717a",
1281
+ lineHeight: 1.4,
1282
+ }}
1283
+ >
1284
+ {a.description}
1285
+ </p>
1286
+ )}
1287
+ <div
1288
+ style={{
1289
+ fontSize: 11,
1290
+ color: "#52525b",
1291
+ fontFamily: "ui-monospace, monospace",
1292
+ }}
1293
+ >
1294
+ {Math.round(a.x)},{Math.round(a.y)}
1295
+ {a.kind === "rect"
1296
+ ? ` · ${Math.round(a.w)}×${Math.round(a.h)}`
1297
+ : " px"}
1298
+ </div>
1299
+ </button>
1300
+ </div>
1301
+ </li>
1302
+ );
1303
+ })}
1304
+ </ul>
1305
+ </aside>
1306
+ );
1307
+ }
1308
+
1309
+ /* ── Main component ──────────────────────────────────────────── */
1310
+
1311
+ export const Example: React.FC<Record<string, unknown>> = (args) => {
1312
+ const normalized = normalizeArgs(args);
1313
+ const [annotations, setAnnotations] = useState<Annotation[]>([]);
1314
+ const [draft, setDraft] = useState<DraftShape | null>(null);
1315
+ const [placeMode, setPlaceMode] = useState<PlaceMode>("navigate");
1316
+ const [selectedId, setSelectedId] = useState<string | null>(null);
1317
+ const [imgSize, setImgSize] = useState({
1318
+ w: DISPLAY_W,
1319
+ h: Math.round(DISPLAY_W * 0.66),
1320
+ });
1321
+
1322
+ const panningDisabled = placeMode !== "navigate";
1323
+
1324
+ const addAnnotation = useCallback((ann: Annotation) => {
1325
+ setAnnotations((prev) => [...prev, ann]);
1326
+ setSelectedId(ann.id);
1327
+ setDraft(null);
1328
+ }, []);
1329
+
1330
+ const deleteAnnotation = useCallback(
1331
+ (id: string) => {
1332
+ setAnnotations((prev) => prev.filter((a) => a.id !== id));
1333
+ if (selectedId === id) setSelectedId(null);
1334
+ },
1335
+ [selectedId],
1336
+ );
1337
+
1338
+ return (
1339
+ <div
1340
+ style={{
1341
+ fontFamily: font,
1342
+ background:
1343
+ "linear-gradient(165deg, #0c0a14 0%, #151022 40%, #0a0812 100%)",
1344
+ padding: "20px 22px 28px",
1345
+ borderRadius: 16,
1346
+ boxSizing: "border-box",
1347
+ }}
1348
+ >
1349
+ <header style={{ marginBottom: 18 }}>
1350
+ <div
1351
+ style={{
1352
+ fontSize: 11,
1353
+ fontWeight: 700,
1354
+ letterSpacing: "0.12em",
1355
+ textTransform: "uppercase",
1356
+ color: "rgba(167, 139, 250, 0.85)",
1357
+ marginBottom: 6,
1358
+ }}
1359
+ >
1360
+ Image labeling tool
1361
+ </div>
1362
+ <h1
1363
+ style={{
1364
+ margin: 0,
1365
+ fontSize: "clamp(1.2rem, 2.2vw, 1.55rem)",
1366
+ fontWeight: 800,
1367
+ letterSpacing: "-0.03em",
1368
+ color: "#f4f4f5",
1369
+ }}
1370
+ >
1371
+ Annotate &amp; detect
1372
+ </h1>
1373
+ <p
1374
+ style={{
1375
+ margin: "8px 0 0",
1376
+ fontSize: 13,
1377
+ color: "rgba(161, 161, 170, 0.95)",
1378
+ maxWidth: 600,
1379
+ lineHeight: 1.5,
1380
+ }}
1381
+ >
1382
+ Use <strong style={{ color: "#e4e4e7" }}>Rectangle</strong> to draw
1383
+ bounding boxes for object detection, or{" "}
1384
+ <strong style={{ color: "#e4e4e7" }}>Pin</strong> to mark a specific
1385
+ point. Click any annotation to see its details.
1386
+ </p>
1387
+ </header>
1388
+
1389
+ <TransformWrapper
1390
+ {...normalized}
1391
+ centerOnInit
1392
+ limitToBounds
1393
+ centerZoomedOut
1394
+ doubleClick={{ disabled: true }}
1395
+ panning={{ ...normalized.panning, disabled: panningDisabled }}
1396
+ >
1397
+ {(utils) => (
1398
+ <div
1399
+ style={{
1400
+ display: "flex",
1401
+ flexWrap: "wrap",
1402
+ gap: 20,
1403
+ alignItems: "flex-start",
1404
+ }}
1405
+ >
1406
+ <div style={{ position: "relative" }}>
1407
+ <ModeRail
1408
+ placeMode={placeMode}
1409
+ onModeChange={(m) => {
1410
+ setPlaceMode(m);
1411
+ setDraft(null);
1412
+ setSelectedId(null);
1413
+ }}
1414
+ />
1415
+
1416
+ <AnnotationCanvas
1417
+ imgSize={imgSize}
1418
+ setImgSize={setImgSize}
1419
+ annotations={annotations}
1420
+ addAnnotation={addAnnotation}
1421
+ draft={draft}
1422
+ setDraft={setDraft}
1423
+ placeMode={placeMode}
1424
+ selectedId={selectedId}
1425
+ setSelectedId={setSelectedId}
1426
+ onDelete={deleteAnnotation}
1427
+ />
1428
+ </div>
1429
+
1430
+ <Sidebar
1431
+ annotations={annotations}
1432
+ selectedId={selectedId}
1433
+ onSelect={setSelectedId}
1434
+ onDelete={deleteAnnotation}
1435
+ zoomToElement={utils.zoomToElement}
1436
+ />
1437
+ </div>
1438
+ )}
1439
+ </TransformWrapper>
1440
+ </div>
1441
+ );
1442
+ };