react-native-gesture-image-viewer 1.3.0 → 1.4.1

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/src/types.ts CHANGED
@@ -17,7 +17,7 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
17
17
  /**
18
18
  * When you want to efficiently manage multiple `GestureViewer` instances, you can use the `id` prop to use multiple `GestureViewer` components.
19
19
  * @remark `GestureViewer` automatically removes instances from memory when components are unmounted, so no manual memory management is required.
20
- * @default 'default'
20
+ * @defaultValue 'default'
21
21
  */
22
22
  id?: string;
23
23
  /**
@@ -26,7 +26,7 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
26
26
  data: T[];
27
27
  /**
28
28
  * The index of the item to display in the `GestureViewer` when the component is mounted.
29
- * @default 0
29
+ * @defaultValue 0
30
30
  */
31
31
  initialIndex?: number;
32
32
  /**
@@ -52,7 +52,7 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
52
52
  /**
53
53
  * The width of the `GestureViewer`.
54
54
  * @remark If you don't set this prop, the width of the `GestureViewer` will be the same as the width of the screen.
55
- * @default screen width
55
+ * @defaultValue screen width
56
56
  */
57
57
  width?: number;
58
58
  /**
@@ -65,13 +65,13 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
65
65
  * **`true`**: Snap mode (`snapToInterval` auto-calculated)
66
66
  * - `snapToInterval` is automatically calculated based on `width` and `itemSpacing` values
67
67
  * - Use this option when you need item spacing
68
- * @default false
68
+ * @defaultValue false
69
69
  *
70
70
  */
71
71
  useSnap?: boolean;
72
72
  /**
73
73
  * `dismissThreshold` controls when `onDismiss` is called by applying a threshold value during vertical gestures.
74
- * @default 80
74
+ * @defaultValue 80
75
75
  */
76
76
  dismissThreshold?: number;
77
77
  // swipeThreshold?: number;
@@ -79,18 +79,18 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
79
79
  /**
80
80
  * Calls `onDismiss` function when swiping down.
81
81
  * @remark Useful for closing modals with downward swipe gestures.
82
- * @default true
82
+ * @defaultValue true
83
83
  */
84
84
  enableDismissGesture?: boolean;
85
85
  /**
86
86
  * Controls left/right swipe gestures.
87
87
  * @remark When `false`, horizontal gestures are disabled.
88
- * @default true
88
+ * @defaultValue true
89
89
  */
90
90
  enableSwipeGesture?: boolean;
91
91
  /**
92
92
  * `resistance` controls the range of vertical movement by applying resistance during vertical gestures.
93
- * @default 2
93
+ * @defaultValue 2
94
94
  */
95
95
  resistance?: number;
96
96
  /**
@@ -109,36 +109,175 @@ export interface GestureViewerProps<T = any, LC = typeof RNFlatList> {
109
109
  /**
110
110
  * By default, the background `opacity` gradually decreases from 1 to 0 during downward swipe gestures.
111
111
  * @remark When `false`, this animation is disabled.
112
- * @default true
112
+ * @defaultValue true
113
113
  */
114
114
  animateBackdrop?: boolean;
115
115
  /**
116
116
  * Only works when zoom is active, allows moving item position when zoomed.
117
117
  * @remark When `false`, gesture movement is disabled during zoom.
118
- * @default true
118
+ * @defaultValue true
119
119
  */
120
120
  enableZoomPanGesture?: boolean;
121
121
  /**
122
122
  * Controls two-finger pinch gestures.
123
123
  * @remark When `false`, two-finger zoom gestures are disabled.
124
- * @default true
124
+ * @defaultValue true
125
125
  */
126
126
  enableZoomGesture?: boolean;
127
127
  /**
128
128
  * Controls double-tap zoom gestures.
129
129
  * @remark When `false`, double-tap zoom gestures are disabled.
130
- * @default true
130
+ * @defaultValue true
131
131
  */
132
132
  enableDoubleTapGesture?: boolean;
133
133
  /**
134
134
  * The maximum zoom scale.
135
- * @default 2
135
+ * @defaultValue 2
136
136
  */
137
137
  maxZoomScale?: number;
138
138
  /**
139
139
  * The spacing between items in pixels.
140
140
  * @remark Only applied when `useSnap` is `true`.
141
- * @default 0
141
+ * @defaultValue 0
142
142
  */
143
143
  itemSpacing?: number;
144
144
  }
145
+
146
+ /**
147
+ * Supported rotation angles in degrees.
148
+ */
149
+ export type RotationAngle = 0 | 90 | 180 | 270 | 360;
150
+
151
+ /**
152
+ * Controller for managing gesture-based image/content viewer interactions.
153
+ * Provides navigation, zoom, and rotation capabilities with state management.
154
+ */
155
+ export type GestureViewerController = {
156
+ /**
157
+ * Navigates to the specified index in the viewer.
158
+ * Updates the currentIndex in the controller state.
159
+ *
160
+ * @param index - The target index (must be between 0 and totalCount - 1)
161
+ * @throws Will throw an error if index is out of bounds
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * controller.goToIndex(0); // Go to first item
166
+ * controller.goToIndex(controller.totalCount - 1); // Go to last item
167
+ * ```
168
+ */
169
+ goToIndex: (index: number) => void;
170
+
171
+ /**
172
+ * Navigates to the previous item in the sequence.
173
+ * If already at the first item, behavior depends on implementation (may wrap or do nothing).
174
+ * Updates currentIndex in the controller state.
175
+ */
176
+ goToPrevious: () => void;
177
+
178
+ /**
179
+ * Navigates to the next item in the sequence.
180
+ * If already at the last item, behavior depends on implementation (may wrap or do nothing).
181
+ * Updates currentIndex in the controller state.
182
+ */
183
+ goToNext: () => void;
184
+
185
+ /**
186
+ * Zooms in by the specified multiplier.
187
+ *
188
+ * @param multiplier - The zoom multiplier (0.01 - 1.0). Higher values zoom in more.
189
+ * @defaultValue 0.25
190
+ *
191
+ * @example
192
+ * ```typescript
193
+ * controller.zoomIn(); // Zoom in by 25%
194
+ * controller.zoomIn(0.5); // Zoom in by 50%
195
+ * ```
196
+ */
197
+ zoomIn: (multiplier?: number) => void;
198
+
199
+ /**
200
+ * Zooms out by the specified multiplier.
201
+ *
202
+ * @param multiplier - The zoom multiplier (0.01 - 1.0). Higher values zoom out more.
203
+ * @defaultValue 0.25
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * controller.zoomOut(); // Zoom out by 25%
208
+ * controller.zoomOut(0.1); // Zoom out by 10%
209
+ * ```
210
+ */
211
+ zoomOut: (multiplier?: number) => void;
212
+
213
+ /**
214
+ * Resets the zoom level to the specified scale.
215
+ *
216
+ * @param scale - The scale to reset to (1.0 = original size)
217
+ * @defaultValue 1.0
218
+ *
219
+ * @example
220
+ * ```typescript
221
+ * controller.resetZoom(); // Reset to original size
222
+ * controller.resetZoom(0.5); // Reset to 50% of original size
223
+ * ```
224
+ */
225
+ resetZoom: (scale?: number) => void;
226
+
227
+ /**
228
+ * Rotates the content by the specified angle.
229
+ *
230
+ * @param angle - Rotation angle in degrees. Must be one of: 0, 90, 180, 270, 360
231
+ * @param clockwise - Direction of rotation when angle is not 0 or 360
232
+ * @defaultValue angle: `90`, clockwise: `true`
233
+ *
234
+ * @remarks
235
+ * - Angle 0 or 360 resets rotation regardless of clockwise parameter
236
+ * - The clockwise parameter only affects rotation when angle is 90, 180, or 270
237
+ * - Rotation is cumulative and affects the current orientation
238
+ *
239
+ * @example
240
+ * ```typescript
241
+ * controller.rotate(); // Rotate 90 degrees clockwise
242
+ * controller.rotate(0); // Reset rotation
243
+ * controller.rotate(90, false); // Rotate 90 degrees counter-clockwise
244
+ * controller.rotate(180); // Rotate 180 degrees (clockwise by default)
245
+ * controller.rotate(270, false); // Rotate 270 degrees counter-clockwise
246
+ * controller.rotate(360); // Reset rotation (same as 0)
247
+ * ```
248
+ */
249
+ rotate: (angle?: RotationAngle, clockwise?: boolean) => void;
250
+ } & GestureViewerControllerState;
251
+
252
+ /**
253
+ * State information for the gesture viewer controller.
254
+ * Contains read-only properties that reflect the current state.
255
+ */
256
+ export type GestureViewerControllerState = {
257
+ /**
258
+ * The current index of the active item in the viewer.
259
+ *
260
+ * @remarks
261
+ * This value is automatically updated when navigation methods are called.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * console.log(`Currently viewing item ${controller.currentIndex + 1} of ${controller.totalCount}`);
266
+ * ```
267
+ */
268
+ readonly currentIndex: number;
269
+
270
+ /**
271
+ * The total number of items available in the viewer.
272
+ *
273
+ * @remarks
274
+ * This value determines the valid range for currentIndex (0 to totalCount - 1).
275
+ *
276
+ * @example
277
+ * ```typescript
278
+ * const hasNext = controller.currentIndex < controller.totalCount - 1;
279
+ * const hasPrevious = controller.currentIndex > 0;
280
+ * ```
281
+ */
282
+ readonly totalCount: number;
283
+ };
@@ -51,7 +51,7 @@ export const useGestureViewer = <T = any>({
51
51
  const width = useSnap ? customWidth || screenWidth : screenWidth;
52
52
 
53
53
  const [isZoomed, setIsZoomed] = useState(false);
54
-
54
+ const [isRotated, setIsRotated] = useState(false);
55
55
  const [currentIndex, setCurrentIndex] = useState(initialIndex);
56
56
  const [manager, setManager] = useState<GestureViewerManager | null>(null);
57
57
 
@@ -65,6 +65,7 @@ export const useGestureViewer = <T = any>({
65
65
  const translateX = useSharedValue(0);
66
66
  const scale = useSharedValue(1);
67
67
  const backdropOpacity = useSharedValue(1);
68
+ const rotation = useSharedValue(0);
68
69
 
69
70
  const listRef = useRef<any>(null);
70
71
 
@@ -82,6 +83,13 @@ export const useGestureViewer = <T = any>({
82
83
  },
83
84
  );
84
85
 
86
+ useAnimatedReaction(
87
+ () => rotation.value,
88
+ (currentRotation) => {
89
+ runOnJS(setIsRotated)(currentRotation % 360 !== 0);
90
+ },
91
+ );
92
+
85
93
  useEffect(() => {
86
94
  const handleManagerChange = (manager: GestureViewerManager | null) => {
87
95
  unsubscribeRef.current?.();
@@ -119,6 +127,7 @@ export const useGestureViewer = <T = any>({
119
127
  manager.setWidth(width + itemSpacing);
120
128
  manager.setHeight(screenHeight);
121
129
  manager.setZoomSharedValues(scale, translateX, translateY, maxZoomScale);
130
+ manager.setRotation(rotation);
122
131
  manager.notifyStateChange();
123
132
  }, [
124
133
  dataLength,
@@ -132,6 +141,7 @@ export const useGestureViewer = <T = any>({
132
141
  screenHeight,
133
142
  translateX,
134
143
  translateY,
144
+ rotation,
135
145
  ]);
136
146
 
137
147
  useEffect(() => {
@@ -152,6 +162,7 @@ export const useGestureViewer = <T = any>({
152
162
  scale.value = 1;
153
163
  backdropOpacity.value = 1;
154
164
  startScale.value = 1;
165
+ rotation.value = 0;
155
166
 
156
167
  if (initialIndex <= 0 || !listRef.current) {
157
168
  return;
@@ -174,7 +185,7 @@ export const useGestureViewer = <T = any>({
174
185
  return () => {
175
186
  runAfterInteractions?.cancel();
176
187
  };
177
- }, [initialIndex, translateY, backdropOpacity, translateX, scale, startScale, width, itemSpacing]);
188
+ }, [initialIndex, translateY, backdropOpacity, translateX, scale, startScale, width, itemSpacing, rotation]);
178
189
 
179
190
  const onMomentumScrollEnd = useCallback(
180
191
  (event: NativeSyntheticEvent<NativeScrollEvent>) => {
@@ -198,6 +209,7 @@ export const useGestureViewer = <T = any>({
198
209
  initialTranslateY.value = withTiming(0);
199
210
  startScale.value = withTiming(1);
200
211
  scale.value = withTiming(1);
212
+ rotation.value = 0;
201
213
  }
202
214
  },
203
215
  [
@@ -213,6 +225,7 @@ export const useGestureViewer = <T = any>({
213
225
  initialTranslateX,
214
226
  initialTranslateY,
215
227
  startScale,
228
+ rotation,
216
229
  ],
217
230
  );
218
231
 
@@ -409,7 +422,12 @@ export const useGestureViewer = <T = any>({
409
422
 
410
423
  const animatedStyle = useAnimatedStyle(() => {
411
424
  return {
412
- transform: [{ translateY: translateY.value }, { translateX: translateX.value }, { scale: scale.value }],
425
+ transform: [
426
+ { translateY: translateY.value },
427
+ { translateX: translateX.value },
428
+ { scale: scale.value },
429
+ { rotate: `${rotation.value}deg` },
430
+ ],
413
431
  };
414
432
  });
415
433
 
@@ -429,7 +447,7 @@ export const useGestureViewer = <T = any>({
429
447
  translateY,
430
448
  listRef,
431
449
  isZoomed,
432
-
450
+ isRotated,
433
451
  dismissGesture,
434
452
  zoomGesture,
435
453
 
@@ -1,12 +1,64 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import type GestureViewerManager from './GestureViewerManager';
3
- import type { GestureViewerManagerState } from './GestureViewerManager';
4
3
  import { registry } from './GestureViewerRegistry';
4
+ import type { GestureViewerController, GestureViewerControllerState } from './types';
5
5
 
6
- export const useGestureViewerController = (id = 'default') => {
7
- const [state, setState] = useState<GestureViewerManagerState>({
6
+ /**
7
+ * Hook to control the gesture viewer programmatically.
8
+ *
9
+ * @param id - Viewer instance identifier (default: 'default')
10
+ * @returns Methods and state for controlling the viewer
11
+ *
12
+ * **Available methods:**
13
+ * - `goToIndex(index: number)` - Navigate to specific index (0 to totalCount-1)
14
+ * - `goToPrevious()` - Navigate to previous item
15
+ * - `goToNext()` - Navigate to next item
16
+ * - `zoomIn(multiplier?: number)` - Zoom in (default: 0.25)
17
+ * - `zoomOut(multiplier?: number)` - Zoom out (default: 0.25)
18
+ * - `resetZoom(scale?: number)` - Reset zoom level (default: 1.0)
19
+ * - `rotate(angle?: 0|90|180|270|360, clockwise?: boolean)` - Rotate content (default: 90° clockwise)
20
+ *
21
+ * **Available state:**
22
+ * - `currentIndex: number` - Current active index (read-only)
23
+ * - `totalCount: number` - Total number of items (read-only)
24
+ *
25
+ * @example
26
+ * ```tsx
27
+ * const { goToIndex, goToNext, goToPrevious, zoomIn, zoomOut, resetZoom, rotate, currentIndex, totalCount } = useGestureViewerController();
28
+ *
29
+ * // Navigate to specific index
30
+ * goToIndex(2);
31
+ *
32
+ * // Go to next image
33
+ * goToNext();
34
+ *
35
+ * // Zoom in by 25%
36
+ * zoomIn();
37
+ *
38
+ * // Zoom out by 50%
39
+ * zoomOut(0.5);
40
+ *
41
+ * // Reset to original size
42
+ * resetZoom();
43
+ *
44
+ * // Rotate 90 degrees clockwise
45
+ * rotate();
46
+ *
47
+ * // Rotate 180 degrees
48
+ * rotate(180);
49
+ *
50
+ * // Check current state
51
+ * console.log(`Image ${currentIndex + 1} of ${totalCount}`);
52
+ *
53
+ * // Check navigation availability
54
+ * const canGoNext = currentIndex < totalCount - 1;
55
+ * const canGoPrevious = currentIndex > 0;
56
+ * ```
57
+ */
58
+ export const useGestureViewerController = (id = 'default'): GestureViewerController => {
59
+ const [state, setState] = useState<GestureViewerControllerState>({
8
60
  currentIndex: 0,
9
- dataLength: 0,
61
+ totalCount: 0,
10
62
  });
11
63
 
12
64
  const [manager, setManager] = useState<GestureViewerManager | null>(null);
@@ -25,7 +77,7 @@ export const useGestureViewerController = (id = 'default') => {
25
77
  return;
26
78
  }
27
79
 
28
- setState({ currentIndex: 0, dataLength: 0 });
80
+ setState({ currentIndex: 0, totalCount: 0 });
29
81
  };
30
82
 
31
83
  const unsubscribeFromRegistry = registry.subscribeToManager(id, handleManagerChange);
@@ -45,7 +97,7 @@ export const useGestureViewerController = (id = 'default') => {
45
97
  zoomIn: manager?.zoomIn || noopFunction,
46
98
  zoomOut: manager?.zoomOut || noopFunction,
47
99
  resetZoom: manager?.resetZoom || noopFunction,
48
- currentIndex: state.currentIndex,
49
- totalCount: state.dataLength,
100
+ rotate: manager?.rotate || noopFunction,
101
+ ...state,
50
102
  };
51
103
  };