Skip to content

Commit 0597a14

Browse files
committed
fix: sliding error with no loop
Fixes #24
1 parent 97c627b commit 0597a14

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Carousel.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ function Carousel<T extends unknown = any>(
248248

249249
const offsetX = useDerivedValue(() => {
250250
const x = handlerOffsetX.value % computedAnimResult.TOTAL_WIDTH;
251+
252+
if (!loop) {
253+
return handlerOffsetX.value;
254+
}
251255
return isNaN(x) ? 0 : x;
252-
}, [computedAnimResult]);
256+
}, [computedAnimResult, loop]);
253257

254258
useAnimatedReaction(
255259
() => offsetX.value,
@@ -323,14 +327,23 @@ function Carousel<T extends unknown = any>(
323327
}
324328

325329
const page = Math.round(handlerOffsetX.value / width);
330+
326331
const velocityPage = Math.round(
327332
(handlerOffsetX.value + e.velocityX) / width
328333
);
329-
const pageWithVelocity = Math.min(
334+
335+
let pageWithVelocity = Math.min(
330336
page + 1,
331337
Math.max(page - 1, velocityPage)
332338
);
333339

340+
if (!loop) {
341+
pageWithVelocity = Math.max(
342+
-(data.length - 1),
343+
Math.min(0, pageWithVelocity)
344+
);
345+
}
346+
334347
if (loop) {
335348
handlerOffsetX.value = _withAnimationCallback(
336349
pageWithVelocity * width

src/utils/log.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* In worklet
3+
* e.g. runOnJS(lop)(...);
4+
*/
5+
export function log(msg: any) {
6+
console.log(msg);
7+
}

0 commit comments

Comments
 (0)