@sentroy-co/client-sdk 2.13.4 → 2.13.7

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.
@@ -1,24 +1,43 @@
1
- import { useEffect } from "react"
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useLayoutEffect,
5
+ useRef,
6
+ useState,
7
+ } from "react"
2
8
  import type { Media } from "../../types"
3
9
  import { pickPresetThumbnailUrl } from "../../thumbnails"
4
10
  import { detectKind, formatBytes } from "./utils"
5
11
 
6
12
  /**
7
- * Sadeleştirilmiş lightboxtek media item için fullscreen preview.
8
- * Image/video native render, audio HTML5 player, diğerleri "download"
9
- * fallback. ESC kapat, ←/→ next/prev (caller index callback'i sağlar).
13
+ * Sentroy Lightboxfullscreen single-media preview.
10
14
  *
11
- * Headless yapı: tüm renderable HTML/inline class'lar default; consumer
12
- * className override'ı ile değiştirebilir.
15
+ * Tasarım hedefi: image viewer için "Photos.app" hissiyatı; viewport'a
16
+ * fit (resim ne kadar büyük/uzun olursa olsun ilk açılışta tamamı görünür),
17
+ * fare tekerleği veya butonlarla zoom, zoom>fit iken drag ile pan.
18
+ *
19
+ * Önceki sürüm sabit `max-h-[80vh]` ile portrait/long resimleri kırpıyor +
20
+ * caption şeridi viewport'u taşırıyordu. Yeni sürüm container size'ını
21
+ * ölçer, image natural size'ına göre `fitScale`'i hesaplar ve transform
22
+ * matrisi ile uygular — CSS `max-*` zincirlerine güvenmez.
23
+ *
24
+ * Klavye:
25
+ * Esc → close, ←/→ → prev/next, +/- → zoom, 0 → reset (fit), Space →
26
+ * 1× ↔ fit toggle.
13
27
  */
28
+
14
29
  export interface LightboxProps {
15
30
  media: Media
16
31
  onClose: () => void
17
32
  onPrev?: () => void
18
33
  onNext?: () => void
34
+ /** Outer container override — default fixed inset-0 black backdrop. */
19
35
  className?: string
20
36
  }
21
37
 
38
+ const MIN_SCALE = 0.05
39
+ const MAX_SCALE = 8
40
+
22
41
  export function Lightbox({
23
42
  media,
24
43
  onClose,
@@ -26,28 +45,212 @@ export function Lightbox({
26
45
  onNext,
27
46
  className,
28
47
  }: LightboxProps) {
48
+ const kind = detectKind(media)
49
+ const url =
50
+ kind === "image"
51
+ ? pickPresetThumbnailUrl(media, "preview") ?? media.url ?? media.downloadUrl
52
+ : media.url ?? media.downloadUrl
53
+
54
+ // Body scroll lock + key handlers — bu effect media değişse de aynı kalır.
55
+ const onCloseRef = useRef(onClose)
56
+ const onPrevRef = useRef(onPrev)
57
+ const onNextRef = useRef(onNext)
58
+ onCloseRef.current = onClose
59
+ onPrevRef.current = onPrev
60
+ onNextRef.current = onNext
61
+
29
62
  useEffect(() => {
30
- const onKey = (e: KeyboardEvent) => {
31
- if (e.key === "Escape") onClose()
32
- if (e.key === "ArrowLeft" && onPrev) onPrev()
33
- if (e.key === "ArrowRight" && onNext) onNext()
34
- }
35
- document.addEventListener("keydown", onKey)
36
63
  document.body.style.overflow = "hidden"
37
64
  return () => {
38
- document.removeEventListener("keydown", onKey)
39
65
  document.body.style.overflow = ""
40
66
  }
41
- }, [onClose, onPrev, onNext])
67
+ }, [])
42
68
 
43
- const kind = detectKind(media)
44
- // Lightbox modal büyük preview ama 4K/orijinal gerekmez. Image
45
- // için "preview" preset (~960px) kullan, video/audio/diğerlerinde
46
- // orijinal URL'i akıt.
47
- const url =
48
- kind === "image"
49
- ? pickPresetThumbnailUrl(media, "preview") ?? media.url ?? media.downloadUrl
50
- : media.url ?? media.downloadUrl
69
+ // ── Image-only zoom/pan state ──────────────────────────────────────────
70
+ const stageRef = useRef<HTMLDivElement | null>(null)
71
+ const imgRef = useRef<HTMLImageElement | null>(null)
72
+ const [natural, setNatural] = useState<{ w: number; h: number } | null>(null)
73
+ const [fitScale, setFitScale] = useState(1)
74
+ const [scale, setScale] = useState(1)
75
+ const [translate, setTranslate] = useState({ x: 0, y: 0 })
76
+ const dragStateRef = useRef<{
77
+ active: boolean
78
+ startX: number
79
+ startY: number
80
+ baseX: number
81
+ baseY: number
82
+ } | null>(null)
83
+ const [isDragging, setIsDragging] = useState(false)
84
+
85
+ // Yeni media → state sıfırla (fit recompute imgLoad'da gelir)
86
+ useEffect(() => {
87
+ setNatural(null)
88
+ setFitScale(1)
89
+ setScale(1)
90
+ setTranslate({ x: 0, y: 0 })
91
+ }, [media.id])
92
+
93
+ const recomputeFit = useCallback(() => {
94
+ const stage = stageRef.current
95
+ if (!stage || !natural) return
96
+ // Stage'in viewport içindeki boyutu — padding/border yok varsayıyoruz.
97
+ const sw = stage.clientWidth
98
+ const sh = stage.clientHeight
99
+ if (sw <= 0 || sh <= 0) return
100
+ const fit = Math.min(sw / natural.w, sh / natural.h, 1)
101
+ setFitScale(fit)
102
+ setScale(fit)
103
+ setTranslate({ x: 0, y: 0 })
104
+ }, [natural])
105
+
106
+ useLayoutEffect(() => {
107
+ recomputeFit()
108
+ }, [recomputeFit])
109
+
110
+ useEffect(() => {
111
+ const onResize = () => recomputeFit()
112
+ window.addEventListener("resize", onResize)
113
+ return () => window.removeEventListener("resize", onResize)
114
+ }, [recomputeFit])
115
+
116
+ const handleImgLoad = useCallback((e: React.SyntheticEvent<HTMLImageElement>) => {
117
+ const img = e.currentTarget
118
+ setNatural({ w: img.naturalWidth, h: img.naturalHeight })
119
+ }, [])
120
+
121
+ // Zoom around a pivot (cursor or center). Pivot stage-relative coords.
122
+ const zoomAt = useCallback(
123
+ (nextScale: number, pivot?: { x: number; y: number }) => {
124
+ const stage = stageRef.current
125
+ if (!stage) return
126
+ const clamped = Math.max(MIN_SCALE, Math.min(MAX_SCALE, nextScale))
127
+ const rect = stage.getBoundingClientRect()
128
+ const cx = pivot?.x ?? rect.width / 2
129
+ const cy = pivot?.y ?? rect.height / 2
130
+ // Image origin relative to stage center (current translate accounted)
131
+ // p = ((c - center) - t) / s → world coords. Yeni translate:
132
+ // t' = (c - center) - p * s'
133
+ const ox = cx - rect.width / 2 - translate.x
134
+ const oy = cy - rect.height / 2 - translate.y
135
+ const ratio = clamped / scale
136
+ const tx = cx - rect.width / 2 - ox * ratio
137
+ const ty = cy - rect.height / 2 - oy * ratio
138
+ setScale(clamped)
139
+ setTranslate({ x: tx, y: ty })
140
+ },
141
+ [scale, translate],
142
+ )
143
+
144
+ const zoomBy = useCallback(
145
+ (factor: number, pivot?: { x: number; y: number }) =>
146
+ zoomAt(scale * factor, pivot),
147
+ [scale, zoomAt],
148
+ )
149
+
150
+ const resetView = useCallback(() => {
151
+ setScale(fitScale)
152
+ setTranslate({ x: 0, y: 0 })
153
+ }, [fitScale])
154
+
155
+ const toggle1x = useCallback(() => {
156
+ if (Math.abs(scale - 1) < 0.001) {
157
+ resetView()
158
+ } else {
159
+ setScale(1)
160
+ setTranslate({ x: 0, y: 0 })
161
+ }
162
+ }, [scale, resetView])
163
+
164
+ // Wheel zoom (cursor-aware)
165
+ const handleWheel = useCallback(
166
+ (e: React.WheelEvent<HTMLDivElement>) => {
167
+ if (kind !== "image") return
168
+ e.preventDefault()
169
+ const stage = stageRef.current
170
+ if (!stage) return
171
+ const rect = stage.getBoundingClientRect()
172
+ const pivot = { x: e.clientX - rect.left, y: e.clientY - rect.top }
173
+ const factor = e.deltaY < 0 ? 1.12 : 1 / 1.12
174
+ zoomBy(factor, pivot)
175
+ },
176
+ [kind, zoomBy],
177
+ )
178
+
179
+ // Drag-to-pan
180
+ const onPointerDown = useCallback(
181
+ (e: React.PointerEvent<HTMLDivElement>) => {
182
+ if (kind !== "image") return
183
+ // Sadece sol tıklama (button 0) veya touch
184
+ if (e.button !== 0 && e.pointerType === "mouse") return
185
+ dragStateRef.current = {
186
+ active: true,
187
+ startX: e.clientX,
188
+ startY: e.clientY,
189
+ baseX: translate.x,
190
+ baseY: translate.y,
191
+ }
192
+ setIsDragging(true)
193
+ ;(e.target as HTMLElement).setPointerCapture?.(e.pointerId)
194
+ },
195
+ [kind, translate],
196
+ )
197
+
198
+ const onPointerMove = useCallback(
199
+ (e: React.PointerEvent<HTMLDivElement>) => {
200
+ const ds = dragStateRef.current
201
+ if (!ds?.active) return
202
+ e.preventDefault()
203
+ const dx = e.clientX - ds.startX
204
+ const dy = e.clientY - ds.startY
205
+ setTranslate({ x: ds.baseX + dx, y: ds.baseY + dy })
206
+ },
207
+ [],
208
+ )
209
+
210
+ const endDrag = useCallback(() => {
211
+ if (dragStateRef.current?.active) {
212
+ dragStateRef.current.active = false
213
+ setIsDragging(false)
214
+ }
215
+ }, [])
216
+
217
+ // Klavye
218
+ useEffect(() => {
219
+ const onKey = (e: KeyboardEvent) => {
220
+ if (
221
+ e.target instanceof HTMLElement &&
222
+ (e.target.tagName === "INPUT" || e.target.tagName === "TEXTAREA")
223
+ ) {
224
+ return
225
+ }
226
+ if (e.key === "Escape") {
227
+ e.preventDefault()
228
+ onCloseRef.current()
229
+ } else if (e.key === "ArrowLeft" && onPrevRef.current) {
230
+ e.preventDefault()
231
+ onPrevRef.current()
232
+ } else if (e.key === "ArrowRight" && onNextRef.current) {
233
+ e.preventDefault()
234
+ onNextRef.current()
235
+ } else if (e.key === "+" || e.key === "=") {
236
+ e.preventDefault()
237
+ zoomBy(1.2)
238
+ } else if (e.key === "-" || e.key === "_") {
239
+ e.preventDefault()
240
+ zoomBy(1 / 1.2)
241
+ } else if (e.key === "0") {
242
+ e.preventDefault()
243
+ resetView()
244
+ } else if (e.key === " ") {
245
+ e.preventDefault()
246
+ toggle1x()
247
+ }
248
+ }
249
+ document.addEventListener("keydown", onKey)
250
+ return () => document.removeEventListener("keydown", onKey)
251
+ }, [zoomBy, resetView, toggle1x])
252
+
253
+ const canPan = kind === "image" && scale > fitScale + 0.001
51
254
 
52
255
  return (
53
256
  <div
@@ -58,63 +261,78 @@ export function Lightbox({
58
261
  }}
59
262
  className={
60
263
  className ||
61
- "fixed inset-0 z-50 flex items-center justify-center bg-black/90 p-4 backdrop-blur-sm"
264
+ "fixed inset-0 z-50 flex flex-col bg-black/95 backdrop-blur-sm"
62
265
  }
63
266
  >
64
- {/* Close button */}
65
- <button
66
- type="button"
67
- onClick={onClose}
68
- aria-label="Close"
69
- className="absolute right-4 top-4 rounded-full bg-white/10 p-2 text-white transition-colors hover:bg-white/20"
70
- >
71
- <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
72
- <path d="M18 6L6 18M6 6l12 12" />
73
- </svg>
74
- </button>
75
-
76
- {/* Prev */}
77
- {onPrev && (
78
- <button
79
- type="button"
80
- onClick={(e) => {
81
- e.stopPropagation()
82
- onPrev()
83
- }}
84
- aria-label="Previous"
85
- className="absolute left-4 top-1/2 -translate-y-1/2 rounded-full bg-white/10 p-3 text-white transition-colors hover:bg-white/20"
86
- >
87
- <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
88
- <path d="M15 18l-6-6 6-6" />
89
- </svg>
90
- </button>
91
- )}
92
-
93
- {/* Next */}
94
- {onNext && (
267
+ {/* Top bar — close + filename */}
268
+ <div className="flex items-center justify-between gap-3 border-b border-white/10 bg-black/40 px-4 py-2.5">
269
+ <div className="flex min-w-0 items-center gap-3 text-xs text-white/70">
270
+ <span className="font-mono truncate max-w-md">
271
+ {media.fileName}
272
+ </span>
273
+ {media.size ? (
274
+ <span className="text-white/40">·</span>
275
+ ) : null}
276
+ {media.size ? <span>{formatBytes(media.size)}</span> : null}
277
+ {media.mimeType ? (
278
+ <>
279
+ <span className="text-white/40">·</span>
280
+ <span className="font-mono text-white/40">{media.mimeType}</span>
281
+ </>
282
+ ) : null}
283
+ </div>
95
284
  <button
96
285
  type="button"
97
- onClick={(e) => {
98
- e.stopPropagation()
99
- onNext()
100
- }}
101
- aria-label="Next"
102
- className="absolute right-4 top-1/2 -translate-y-1/2 rounded-full bg-white/10 p-3 text-white transition-colors hover:bg-white/20"
286
+ onClick={onClose}
287
+ aria-label="Close"
288
+ className="rounded-full bg-white/10 p-1.5 text-white transition-colors hover:bg-white/20"
103
289
  >
104
- <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
105
- <path d="M9 6l6 6-6 6" />
290
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
291
+ <path d="M18 6L6 18M6 6l12 12" />
106
292
  </svg>
107
293
  </button>
108
- )}
294
+ </div>
109
295
 
110
- {/* Content */}
111
- <div className="flex max-h-full max-w-5xl flex-col items-center gap-3">
296
+ {/* Stage */}
297
+ <div
298
+ ref={stageRef}
299
+ onClick={(e) => {
300
+ if (e.target === e.currentTarget) onClose()
301
+ }}
302
+ onWheel={handleWheel}
303
+ onPointerDown={onPointerDown}
304
+ onPointerMove={onPointerMove}
305
+ onPointerUp={endDrag}
306
+ onPointerLeave={endDrag}
307
+ className="relative flex flex-1 min-h-0 items-center justify-center overflow-hidden select-none"
308
+ style={{
309
+ cursor:
310
+ kind === "image"
311
+ ? isDragging
312
+ ? "grabbing"
313
+ : canPan
314
+ ? "grab"
315
+ : "default"
316
+ : "default",
317
+ }}
318
+ >
112
319
  {kind === "image" && url && (
113
320
  // eslint-disable-next-line @next/next/no-img-element
114
321
  <img
322
+ ref={imgRef}
115
323
  src={url}
116
324
  alt={media.alt ?? media.fileName}
117
- className="max-h-[80vh] max-w-full rounded-lg object-contain shadow-2xl"
325
+ draggable={false}
326
+ onLoad={handleImgLoad}
327
+ onDoubleClick={toggle1x}
328
+ className="origin-center select-none"
329
+ style={{
330
+ transform: `translate(${translate.x}px, ${translate.y}px) scale(${scale})`,
331
+ transition: isDragging ? "none" : "transform 0.18s ease-out",
332
+ maxWidth: "none",
333
+ maxHeight: "none",
334
+ willChange: "transform",
335
+ }}
118
336
  />
119
337
  )}
120
338
  {kind === "video" && url && (
@@ -122,7 +340,7 @@ export function Lightbox({
122
340
  src={url}
123
341
  controls
124
342
  autoPlay
125
- className="max-h-[80vh] max-w-full rounded-lg shadow-2xl"
343
+ className="max-h-full max-w-full rounded-lg shadow-2xl"
126
344
  />
127
345
  )}
128
346
  {kind === "audio" && url && (
@@ -152,18 +370,91 @@ export function Lightbox({
152
370
  </div>
153
371
  )}
154
372
 
155
- <div className="flex items-center gap-3 rounded-md bg-black/40 px-3 py-1.5 text-xs text-white/80">
156
- <span className="font-mono truncate max-w-xs">{media.fileName}</span>
157
- <span>·</span>
158
- <span>{formatBytes(media.size ?? 0)}</span>
159
- {media.mimeType && (
160
- <>
161
- <span>·</span>
162
- <span className="font-mono opacity-70">{media.mimeType}</span>
163
- </>
164
- )}
165
- </div>
373
+ {/* Prev / Next nav overlay'da, alttaki bottom bar tarafından
374
+ gizlenmesin diye nav transform-y-50% */}
375
+ {onPrev && (
376
+ <button
377
+ type="button"
378
+ onClick={(e) => {
379
+ e.stopPropagation()
380
+ onPrev()
381
+ }}
382
+ aria-label="Previous"
383
+ className="absolute left-4 top-1/2 -translate-y-1/2 rounded-full bg-white/10 p-3 text-white transition-colors hover:bg-white/20"
384
+ >
385
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
386
+ <path d="M15 18l-6-6 6-6" />
387
+ </svg>
388
+ </button>
389
+ )}
390
+ {onNext && (
391
+ <button
392
+ type="button"
393
+ onClick={(e) => {
394
+ e.stopPropagation()
395
+ onNext()
396
+ }}
397
+ aria-label="Next"
398
+ className="absolute right-4 top-1/2 -translate-y-1/2 rounded-full bg-white/10 p-3 text-white transition-colors hover:bg-white/20"
399
+ >
400
+ <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
401
+ <path d="M9 6l6 6-6 6" />
402
+ </svg>
403
+ </button>
404
+ )}
166
405
  </div>
406
+
407
+ {/* Bottom toolbar — image only (zoom controls + readout) */}
408
+ {kind === "image" && (
409
+ <div className="flex items-center justify-center gap-3 border-t border-white/10 bg-black/40 px-4 py-2.5">
410
+ <ToolbarBtn onClick={() => zoomBy(1 / 1.2)} title="Zoom out (−)">
411
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16">
412
+ <circle cx="11" cy="11" r="7" />
413
+ <path d="M8 11h6M21 21l-4.3-4.3" />
414
+ </svg>
415
+ </ToolbarBtn>
416
+ <span className="font-mono text-[11px] text-white/60 min-w-[3.5rem] text-center">
417
+ {Math.round(scale * 100)}%
418
+ </span>
419
+ <ToolbarBtn onClick={() => zoomBy(1.2)} title="Zoom in (+)">
420
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16">
421
+ <circle cx="11" cy="11" r="7" />
422
+ <path d="M8 11h6M11 8v6M21 21l-4.3-4.3" />
423
+ </svg>
424
+ </ToolbarBtn>
425
+ <span className="mx-2 h-5 w-px bg-white/15" />
426
+ <ToolbarBtn onClick={resetView} title="Fit (0)">
427
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" width="16" height="16">
428
+ <path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5" />
429
+ </svg>
430
+ </ToolbarBtn>
431
+ <ToolbarBtn onClick={toggle1x} title="100% (Space)">
432
+ <span className="font-mono text-[11px] font-medium">1:1</span>
433
+ </ToolbarBtn>
434
+ </div>
435
+ )}
167
436
  </div>
168
437
  )
169
438
  }
439
+
440
+ function ToolbarBtn({
441
+ children,
442
+ onClick,
443
+ title,
444
+ }: {
445
+ children: React.ReactNode
446
+ onClick: () => void
447
+ title: string
448
+ }) {
449
+ return (
450
+ <button
451
+ type="button"
452
+ onClick={onClick}
453
+ title={title}
454
+ aria-label={title}
455
+ className="inline-flex size-8 items-center justify-center rounded-md text-white/80 transition-colors hover:bg-white/10 hover:text-white"
456
+ >
457
+ {children}
458
+ </button>
459
+ )
460
+ }