Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/src/normal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function Index() {
{...baseOptions}
loop
autoPlay={isAutoPlay}
autoPlayInterval={isFast ? 100 : 1500}
scrollAnimationDuration={2000}
autoPlayInterval={isFast ? 100 : 2000}
data={[...new Array(6).keys()]}
renderItem={({ index }) => <SBItem key={index} index={index} />}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ function Carousel<T>(

const { run, pause } = useAutoPlay({
autoPlay,
autoPlayInterval,
autoPlayInterval: Math.max(
autoPlayInterval - scrollAnimationDuration,
0
),
autoPlayReverse,
carouselController,
});
Expand Down
23 changes: 8 additions & 15 deletions src/hooks/useAutoPlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,28 @@ export function useAutoPlay(opts: {
} = opts;

const timer = React.useRef<NodeJS.Timer>();
const stopped = React.useRef<boolean>(!autoPlay);

const pause = React.useCallback(() => {
timer.current && clearInterval(timer.current);
stopped.current = true;
}, []);

const run = React.useCallback(() => {
if (timer.current) {
pause();
}

if (!autoPlay) {
if (stopped.current) {
return;
}

timer.current = setInterval(() => {
timer.current = setTimeout(() => {
autoPlayReverse
? carouselController.prev()
: carouselController.next();
? carouselController.prev(run)
: carouselController.next(run);
}, autoPlayInterval);
}, [
pause,
autoPlay,
autoPlayReverse,
autoPlayInterval,
carouselController,
]);
}, [autoPlayReverse, autoPlayInterval, carouselController]);

React.useEffect(() => {
if (autoPlay) {
stopped.current = false;
run();
} else {
pause();
Expand Down
71 changes: 49 additions & 22 deletions src/hooks/useCarouselController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import type Animated from 'react-native-reanimated';
import { Easing } from '../constants';
import { runOnJS, useSharedValue, withTiming } from 'react-native-reanimated';

interface TCarouselActionOptions {
count?: number;
animated?: boolean;
onFinished?: () => void;
}

interface IOpts {
loop: boolean;
size: number;
Expand All @@ -22,15 +28,15 @@ export interface ICarouselController {
index: Animated.SharedValue<number>;
sharedIndex: React.MutableRefObject<number>;
sharedPreIndex: React.MutableRefObject<number>;
prev: () => void;
next: () => void;
prev: (opts?: TCarouselActionOptions) => void;
next: (opts?: TCarouselActionOptions) => void;
computedIndex: () => void;
getCurrentIndex: () => number;
to: (index: number, animated?: boolean) => void;
scrollTo: (animationValue: number, animated?: boolean) => void;
scrollTo: (opts?: TCarouselActionOptions) => void;
}

export function useCarouselController(opts: IOpts): ICarouselController {
export function useCarouselController(options: IOpts): ICarouselController {
const {
size,
loop,
Expand All @@ -40,7 +46,7 @@ export function useCarouselController(opts: IOpts): ICarouselController {
length,
onChange,
duration,
} = opts;
} = options;

const index = useSharedValue<number>(0);
// The Index displayed to the user
Expand Down Expand Up @@ -106,21 +112,22 @@ export function useCarouselController(opts: IOpts): ICarouselController {
}, [disable]);

const onScrollEnd = React.useCallback(() => {
opts.onScrollEnd?.();
}, [opts]);
options.onScrollEnd?.();
}, [options]);

const onScrollBegin = React.useCallback(() => {
opts.onScrollBegin?.();
}, [opts]);
options.onScrollBegin?.();
}, [options]);

const scrollWithTiming = React.useCallback(
(toValue: number) => {
(toValue: number, onFinished?: () => void) => {
return withTiming(
toValue,
{ duration, easing: Easing.easeOutQuart },
(isFinished: boolean) => {
if (isFinished) {
runOnJS(onScrollEnd)();
onFinished && runOnJS(onFinished)();
}
}
);
Expand All @@ -129,16 +136,24 @@ export function useCarouselController(opts: IOpts): ICarouselController {
);

const next = React.useCallback(
(count = 1, animated = true) => {
(opts: TCarouselActionOptions = {}) => {
const { count = 1, animated = true, onFinished } = opts;
if (!canSliding() || (!loop && index.value >= length - 1)) return;

onScrollBegin?.();

const nextPage = currentFixedPage() + count;
index.value = nextPage;
handlerOffsetX.value = animated
? scrollWithTiming(-nextPage * size)
: -nextPage * size;

if (animated) {
handlerOffsetX.value = scrollWithTiming(
-nextPage * size,
onFinished
);
} else {
handlerOffsetX.value = -nextPage * size;
onFinished?.();
}
},
[
canSliding,
Expand All @@ -154,16 +169,24 @@ export function useCarouselController(opts: IOpts): ICarouselController {
);

const prev = React.useCallback(
(count = 1, animated = true) => {
(opts: TCarouselActionOptions = {}) => {
const { count = 1, animated = true, onFinished } = opts;
if (!canSliding() || (!loop && index.value <= 0)) return;

onScrollBegin?.();

const prevPage = currentFixedPage() - count;
index.value = prevPage;
handlerOffsetX.value = animated
? scrollWithTiming(-prevPage * size)
: -prevPage * size;

if (animated) {
handlerOffsetX.value = scrollWithTiming(
-prevPage * size,
onFinished
);
} else {
handlerOffsetX.value = -prevPage * size;
onFinished?.();
}
},
[
canSliding,
Expand Down Expand Up @@ -207,12 +230,16 @@ export function useCarouselController(opts: IOpts): ICarouselController {
);

const scrollTo = React.useCallback(
(value: number, animated = false) => {
const n = Math.round(value);
(opts: TCarouselActionOptions = {}) => {
const { count, animated = false } = opts;
if (!count) {
return;
}
const n = Math.round(count);
if (n < 0) {
prev(Math.abs(n), animated);
prev({ count: Math.abs(n), animated });
} else {
next(n, animated);
next({ count: n, animated });
}
},
[prev, next]
Expand Down
78 changes: 0 additions & 78 deletions src/hooks/useIndexController.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/hooks/useInitProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export type TInitializeCarouselProps<T> = TCarouselProps<T> & {
loop: Required<TCarouselProps>['loop'];
width: Required<TCarouselProps>['width'];
height: Required<TCarouselProps>['height'];
scrollAnimationDuration: Required<TCarouselProps>['scrollAnimationDuration'];
autoPlayInterval: Required<TCarouselProps>['autoPlayInterval'];
};

export function useInitProps<T>(
Expand Down