Skip to content

Commit

Permalink
fix: sliding error with no loop
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
dohooo committed Nov 27, 2021
1 parent 094f3af commit 955b5ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,12 @@ function Carousel<T extends unknown = any>(

const offsetX = useDerivedValue(() => {
const x = handlerOffsetX.value % computedAnimResult.TOTAL_WIDTH;

if (!loop) {
return handlerOffsetX.value;
}
return isNaN(x) ? 0 : x;
}, [computedAnimResult]);
}, [computedAnimResult, loop]);

useAnimatedReaction(
() => offsetX.value,
Expand Down Expand Up @@ -323,14 +327,23 @@ function Carousel<T extends unknown = any>(
}

const page = Math.round(handlerOffsetX.value / width);

const velocityPage = Math.round(
(handlerOffsetX.value + e.velocityX) / width
);
const pageWithVelocity = Math.min(

let pageWithVelocity = Math.min(
page + 1,
Math.max(page - 1, velocityPage)
);

if (!loop) {
pageWithVelocity = Math.max(
-(data.length - 1),
Math.min(0, pageWithVelocity)
);
}

if (loop) {
handlerOffsetX.value = _withAnimationCallback(
pageWithVelocity * width
Expand Down
7 changes: 7 additions & 0 deletions src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* In worklet
* e.g. runOnJS(lop)(...);
*/
export function log(msg: any) {
console.log(msg);
}

0 comments on commit 955b5ed

Please sign in to comment.