react-native-gesture-image-viewer 1.4.0 → 1.5.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/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,184 @@ 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
+ };
284
+
285
+ export type GestureViewerEventType = 'zoomChange' | 'rotationChange';
286
+
287
+ export type GestureViewerEventData = {
288
+ zoomChange: { scale: number; previousScale: number | null };
289
+ rotationChange: { rotation: number; previousRotation: number | null };
290
+ };
291
+
292
+ export type GestureViewerEventCallback<T extends GestureViewerEventType> = (data: GestureViewerEventData[T]) => void;
@@ -76,16 +76,38 @@ export const useGestureViewer = <T = any>({
76
76
  [width, screenHeight],
77
77
  );
78
78
 
79
+ const emitZoomChange = useCallback(
80
+ (currentScale: number, prevScale: number | null) => {
81
+ manager?.emitZoomChange(currentScale, prevScale);
82
+ },
83
+ [manager],
84
+ );
85
+
86
+ const emitRotationChange = useCallback(
87
+ (currentRotation: number, prevRotation: number | null) => {
88
+ manager?.emitRotationChange(currentRotation, prevRotation);
89
+ },
90
+ [manager],
91
+ );
92
+
79
93
  useAnimatedReaction(
80
94
  () => scale.value,
81
- (currentScale) => {
95
+ (currentScale, previousScale) => {
96
+ if (manager && currentScale !== previousScale) {
97
+ runOnJS(emitZoomChange)(currentScale, previousScale);
98
+ }
99
+
82
100
  runOnJS(setIsZoomed)(currentScale > 1);
83
101
  },
84
102
  );
85
103
 
86
104
  useAnimatedReaction(
87
105
  () => rotation.value,
88
- (currentRotation) => {
106
+ (currentRotation, previousRotation) => {
107
+ if (manager && currentRotation !== previousRotation) {
108
+ runOnJS(emitRotationChange)(currentRotation, previousRotation);
109
+ }
110
+
89
111
  runOnJS(setIsRotated)(currentRotation % 360 !== 0);
90
112
  },
91
113
  );
@@ -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);
@@ -46,7 +98,6 @@ export const useGestureViewerController = (id = 'default') => {
46
98
  zoomOut: manager?.zoomOut || noopFunction,
47
99
  resetZoom: manager?.resetZoom || noopFunction,
48
100
  rotate: manager?.rotate || noopFunction,
49
- currentIndex: state.currentIndex,
50
- totalCount: state.dataLength,
101
+ ...state,
51
102
  };
52
103
  };
@@ -0,0 +1,96 @@
1
+ import { useEffect } from 'react';
2
+ import type GestureViewerManager from './GestureViewerManager';
3
+ import { registry } from './GestureViewerRegistry';
4
+ import type { GestureViewerEventCallback, GestureViewerEventType } from './types';
5
+
6
+ /**
7
+ * Hook for subscribing to GestureViewer events on the default instance.
8
+ *
9
+ * This hook allows you to listen to specific events from the default GestureViewer instance
10
+ * (with ID 'default'), such as zoom changes or rotation changes. Events are automatically
11
+ * throttled to prevent excessive callback invocations during gestures.
12
+ *
13
+ * @param eventType - The type of event to listen for
14
+ * @param callback - Function to call when the event occurs
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * // Listen to zoom changes on the default instance (ID: 'default')
19
+ * useGestureViewerEvent('zoomChange', (data) => {
20
+ * console.log(`Zoom changed from ${data.previousScale} to ${data.scale}`);
21
+ * });
22
+ *
23
+ * // Listen to rotation changes on the default instance (ID: 'default')
24
+ * useGestureViewerEvent('rotationChange', (data) => {
25
+ * console.log(`Rotation changed from ${data.previousRotation}° to ${data.rotation}°`);
26
+ * });
27
+ * ```
28
+ */
29
+ export function useGestureViewerEvent<T extends GestureViewerEventType>(
30
+ eventType: T,
31
+ callback: GestureViewerEventCallback<T>,
32
+ ): void;
33
+
34
+ /**
35
+ * Hook for subscribing to GestureViewer events with a specific instance ID.
36
+ *
37
+ * Use this overload when you have multiple GestureViewer instances and need
38
+ * to listen to events from a specific one.
39
+ *
40
+ * @param id - The unique identifier of the GestureViewer instance
41
+ * @param eventType - The type of event to listen for
42
+ * @param callback - Function to call when the event occurs
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * // Listen to zoom changes on a specific instance
47
+ * useGestureViewerEvent('gallery', 'zoomChange', (data) => {
48
+ * console.log(`Gallery zoom: ${data.scale}x`);
49
+ * });
50
+ *
51
+ * // Listen to rotation changes on a modal viewer
52
+ * useGestureViewerEvent('modal-viewer', 'rotationChange', (data) => {
53
+ * updateRotationIndicator(data.rotation);
54
+ * });
55
+ * ```
56
+ */
57
+ export function useGestureViewerEvent<T extends GestureViewerEventType>(
58
+ id: string,
59
+ eventType: T,
60
+ callback: GestureViewerEventCallback<T>,
61
+ ): void;
62
+
63
+ /**
64
+ * Implementation of the useGestureViewerEvent hook.
65
+ *
66
+ * @internal
67
+ */
68
+ export function useGestureViewerEvent<T extends GestureViewerEventType>(
69
+ idOrEventType: string | T,
70
+ eventTypeOrCallback: T | GestureViewerEventCallback<T>,
71
+ callback?: GestureViewerEventCallback<T>,
72
+ ) {
73
+ const id = typeof idOrEventType === 'string' && callback ? idOrEventType : 'default';
74
+ const eventType = typeof idOrEventType === 'string' && callback ? (eventTypeOrCallback as T) : (idOrEventType as T);
75
+ const finalCallback = callback || (eventTypeOrCallback as GestureViewerEventCallback<T>);
76
+
77
+ useEffect(() => {
78
+ let unsubscribeFromManager: (() => void) | null = null;
79
+
80
+ const handleManagerChange = (manager: GestureViewerManager | null) => {
81
+ unsubscribeFromManager?.();
82
+ unsubscribeFromManager = null;
83
+
84
+ if (manager) {
85
+ unsubscribeFromManager = manager.addEventListener(eventType, finalCallback);
86
+ }
87
+ };
88
+
89
+ const unsubscribeFromRegistry = registry.subscribeToManager(id, handleManagerChange);
90
+
91
+ return () => {
92
+ unsubscribeFromRegistry();
93
+ unsubscribeFromManager?.();
94
+ };
95
+ }, [id, eventType, finalCallback]);
96
+ }