react-native-reanimated-carousel 2.4.0 → 2.6.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 (44) hide show
  1. package/README.md +17 -62
  2. package/README.zh-CN.md +22 -60
  3. package/lib/commonjs/Carousel.js +1 -1
  4. package/lib/commonjs/Carousel.js.map +1 -1
  5. package/lib/commonjs/ScrollViewGesture.js +1 -1
  6. package/lib/commonjs/ScrollViewGesture.js.map +1 -1
  7. package/lib/commonjs/hooks/useCarouselController.js +1 -1
  8. package/lib/commonjs/hooks/useCarouselController.js.map +1 -1
  9. package/lib/commonjs/hooks/useOffsetX.js +1 -1
  10. package/lib/commonjs/hooks/useOffsetX.js.map +1 -1
  11. package/lib/commonjs/hooks/useOnProgressChange.js +1 -1
  12. package/lib/commonjs/hooks/useOnProgressChange.js.map +1 -1
  13. package/lib/commonjs/hooks/useVisibleRanges.js +1 -1
  14. package/lib/commonjs/hooks/useVisibleRanges.js.map +1 -1
  15. package/lib/commonjs/index.js +1 -1
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/commonjs/layouts/BaseLayout.js +1 -1
  18. package/lib/commonjs/layouts/BaseLayout.js.map +1 -1
  19. package/lib/commonjs/layouts/ParallaxLayout.js +1 -1
  20. package/lib/commonjs/layouts/ParallaxLayout.js.map +1 -1
  21. package/lib/commonjs/layouts/normal.js +1 -1
  22. package/lib/commonjs/layouts/normal.js.map +1 -1
  23. package/lib/commonjs/layouts/parallax.js +1 -1
  24. package/lib/commonjs/layouts/parallax.js.map +1 -1
  25. package/lib/commonjs/layouts/stack.js +1 -1
  26. package/lib/commonjs/layouts/stack.js.map +1 -1
  27. package/lib/commonjs/utils/computedWithAutoFillData.js +1 -1
  28. package/lib/commonjs/utils/computedWithAutoFillData.js.map +1 -1
  29. package/lib/commonjs/utils/dealWithAnimation.js +1 -1
  30. package/lib/commonjs/utils/dealWithAnimation.js.map +1 -1
  31. package/lib/commonjs/utils/log.js +1 -1
  32. package/lib/module/Carousel.js +11 -9
  33. package/lib/module/Carousel.js.map +1 -1
  34. package/lib/module/hooks/useCarouselController.js +24 -11
  35. package/lib/module/hooks/useCarouselController.js.map +1 -1
  36. package/lib/module/index.js +6 -0
  37. package/lib/module/index.js.map +1 -1
  38. package/lib/typescript/hooks/useCarouselController.d.ts +0 -1
  39. package/lib/typescript/types.d.ts +5 -9
  40. package/package.json +1 -1
  41. package/src/Carousel.tsx +19 -13
  42. package/src/hooks/useCarouselController.tsx +18 -12
  43. package/src/index.tsx +4 -0
  44. package/src/types.ts +5 -9
@@ -33,7 +33,6 @@ export interface ICarouselController {
33
33
  prev: (opts?: TCarouselActionOptions) => void;
34
34
  next: (opts?: TCarouselActionOptions) => void;
35
35
  getCurrentIndex: () => number;
36
- to: (index: number, animated?: boolean) => void;
37
36
  scrollTo: (opts?: TCarouselActionOptions) => void;
38
37
  }
39
38
 
@@ -225,21 +224,22 @@ export function useCarouselController(options: IOpts): ICarouselController {
225
224
  );
226
225
 
227
226
  const to = React.useCallback(
228
- (idx: number, animated: boolean = false) => {
229
- if (idx === index.value) return;
227
+ (opts: { i: number; animated: boolean; onFinished?: () => void }) => {
228
+ const { i, animated = false, onFinished } = opts;
229
+ if (i === index.value) return;
230
230
  if (!canSliding()) return;
231
231
 
232
232
  onScrollBegin?.();
233
233
 
234
- const offset = handlerOffsetX.value + (index.value - idx) * size;
234
+ const offset = handlerOffsetX.value + (index.value - i) * size;
235
235
 
236
236
  if (animated) {
237
- index.value = idx;
238
- handlerOffsetX.value = scrollWithTiming(offset);
237
+ index.value = i;
238
+ handlerOffsetX.value = scrollWithTiming(offset, onFinished);
239
239
  } else {
240
240
  handlerOffsetX.value = offset;
241
- index.value = idx;
242
- runOnJS(onScrollEnd)();
241
+ index.value = i;
242
+ onFinished?.();
243
243
  }
244
244
  },
245
245
  [
@@ -249,28 +249,34 @@ export function useCarouselController(options: IOpts): ICarouselController {
249
249
  handlerOffsetX,
250
250
  size,
251
251
  scrollWithTiming,
252
- onScrollEnd,
253
252
  ]
254
253
  );
255
254
 
256
255
  const scrollTo = React.useCallback(
257
256
  (opts: TCarouselActionOptions = {}) => {
258
- const { count, animated = false, onFinished } = opts;
257
+ const { index: i, count, animated = false, onFinished } = opts;
258
+
259
+ if (typeof i === 'number' && i > -1) {
260
+ to({ i, animated, onFinished });
261
+ return;
262
+ }
263
+
259
264
  if (!count) {
260
265
  return;
261
266
  }
267
+
262
268
  const n = Math.round(count);
269
+
263
270
  if (n < 0) {
264
271
  prev({ count: Math.abs(n), animated, onFinished });
265
272
  } else {
266
273
  next({ count: n, animated, onFinished });
267
274
  }
268
275
  },
269
- [prev, next]
276
+ [prev, next, to]
270
277
  );
271
278
 
272
279
  return {
273
- to,
274
280
  next,
275
281
  prev,
276
282
  scrollTo,
package/src/index.tsx CHANGED
@@ -1,3 +1,7 @@
1
+ if (!('__reanimatedWorkletInit' in global)) {
2
+ Object.assign(global, { __reanimatedWorkletInit: () => {} });
3
+ }
4
+
1
5
  export type { TCarouselProps, ICarouselInstance } from './types';
2
6
  import Carousel from './Carousel';
3
7
 
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ViewStyle } from 'react-native';
1
+ import type { StyleProp, ViewStyle } from 'react-native';
2
2
  import type { PanGestureHandlerProps } from 'react-native-gesture-handler';
3
3
  import type {
4
4
  AnimatedStyleProp,
@@ -104,7 +104,7 @@ export type TCarouselProps<T = any> = {
104
104
  /**
105
105
  * Carousel container style
106
106
  */
107
- style?: ViewStyle;
107
+ style?: StyleProp<ViewStyle>;
108
108
  /**
109
109
  * PanGestureHandler props
110
110
  */
@@ -189,21 +189,16 @@ export interface ICarouselInstance {
189
189
  * Scroll to previous item, it takes one optional argument (count),
190
190
  * which allows you to specify how many items to cross
191
191
  */
192
- prev: (opts?: TCarouselActionOptions) => void;
192
+ prev: (opts?: Omit<TCarouselActionOptions, 'index'>) => void;
193
193
  /**
194
194
  * Scroll to next item, it takes one optional argument (count),
195
195
  * which allows you to specify how many items to cross
196
196
  */
197
- next: (opts?: TCarouselActionOptions) => void;
197
+ next: (opts?: Omit<TCarouselActionOptions, 'index'>) => void;
198
198
  /**
199
199
  * Get current item index
200
200
  */
201
201
  getCurrentIndex: () => number;
202
- /**
203
- * Go to index
204
- * @deprecated use scrollTo instead
205
- */
206
- goToIndex: (index: number, animated?: boolean) => void;
207
202
  /**
208
203
  * Use value to scroll to a position where relative to the current position,
209
204
  * scrollTo(-2) is equivalent to prev(2), scrollTo(2) is equivalent to next(2)
@@ -222,6 +217,7 @@ export type CarouselRenderItem<ItemT> = (
222
217
  ) => React.ReactElement;
223
218
 
224
219
  export interface TCarouselActionOptions {
220
+ index?: number;
225
221
  count?: number;
226
222
  animated?: boolean;
227
223
  onFinished?: () => void;