Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to loop-swipe #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
CarouselProps,
GestureEvent,
GestureState,
ScrollEvent,
} from '../types';

const { width: screenWidth } = Dimensions.get('window');
Expand Down Expand Up @@ -41,11 +40,12 @@ export default class SideSwipe extends Component<CarouselProps, State> {
onGestureRelease: () => {},
onIndexChange: () => {},
renderItem: () => null,
shouldCapture: ({ dx }: GestureState) => (dx * dx) > 1,
shouldCapture: ({ dx }: GestureState) => dx * dx > 1,
shouldRelease: () => false,
threshold: 0,
useVelocityForIndex: true,
useNativeDriver: true,
loopSwipes: false,
};

constructor(props: CarouselProps) {
Expand Down Expand Up @@ -200,19 +200,14 @@ export default class SideSwipe extends Component<CarouselProps, State> {
const absoluteVelocity: number = Math.round(Math.abs(vx));
const velocityDifference: number =
absoluteVelocity < 1 ? 0 : absoluteVelocity - 1;

newIndex =
const resolvedIndexWithVelocityOffset =
dx > 0
? Math.max(resolvedIndex - velocityDifference, 0)
: Math.min(
resolvedIndex + velocityDifference,
this.props.data.length - 1
);
? resolvedIndex - velocityDifference
: resolvedIndex + velocityDifference;

newIndex = this.calculateNewIndex(dx, resolvedIndexWithVelocityOffset);
} else {
newIndex =
dx > 0
? Math.max(resolvedIndex, 0)
: Math.min(resolvedIndex, this.props.data.length - 1);
newIndex = this.calculateNewIndex(dx, resolvedIndex);
}

this.list.scrollToIndex({
Expand All @@ -226,9 +221,25 @@ export default class SideSwipe extends Component<CarouselProps, State> {
() => {
this.props.onIndexChange(newIndex);
this.props.onGestureRelease();
},
}
);
};

/**
* Calculate the new index based on the resolved index, the swipe direction and if looping is enabled
*
* @param dx Used to determine swipe direction
* @param resolvedIndex The index resolved
*/
calculateNewIndex(dx: number, resolvedIndex: number): number {
if (this.props.loopSwipes) {
return (resolvedIndex + this.props.data.length) % this.props.data.length;
} else {
return dx > 0
? Math.max(resolvedIndex, 0)
: Math.min(resolvedIndex, this.props.data.length - 1);
}
}
}

const styles = StyleSheet.create({
Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ declare type CarouselProps<T> = {
* should we use native driver for animation.
*/
useNativeDriver?: boolean;

/**
* Wrap swipes from end to start and vice-versa
*/
loopSwipes?: boolean;
};

declare class Carousel<T> extends Component<CarouselProps<T>> {}
Expand Down
1 change: 1 addition & 0 deletions types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type CarouselDefaultProps = {
shouldRelease: GestureState => boolean,
threshold: number,
useNativeDriver: boolean,
loopSwipes: boolean,
};

export type CarouselProps = CarouselDefaultProps & {
Expand Down