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
12 changes: 9 additions & 3 deletions src/hooks/useInterpolateInsideCarousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ export const useInterpolateInsideCarousel = (values: {
valueAfter: number
offset?: number
}) => {
const { scrollValue } = useCarouselContext()
const { scrollValue, disableInfiniteScroll } = useCarouselContext()
const { index, total } = useAutoCarouselSlideIndex()
return useDerivedValue(() => {
return interpolateInsideCarousel(scrollValue.value, index, total, values)
}, [scrollValue, values])
return interpolateInsideCarousel(
scrollValue.value,
index,
total,
values,
!disableInfiniteScroll,
)
}, [scrollValue, values, disableInfiniteScroll])
}
26 changes: 26 additions & 0 deletions src/utils/__tests__/interpolateInsideCarousel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,30 @@ describe('interpolateInsideCarousel', () => {
})
expect(result).toBe(1)
})

describe('without infinite scroll', () => {
// Three real slides and no clones, so the ends don't mirror each other.
const values = { valueBefore: 0, thisValue: 1, valueAfter: 0 }

it('keeps the first slide at the side value while the second is current', () => {
expect(interpolateInsideCarousel(1, 0, 3, values, false)).toBe(0)
})

it('keeps the second slide at the side value while the last is current', () => {
expect(interpolateInsideCarousel(2, 1, 3, values, false)).toBe(0)
})

it('keeps the last slide at the side value while the second is current', () => {
expect(interpolateInsideCarousel(1, 2, 3, values, false)).toBe(0)
})

it('gives the current slide its own value', () => {
expect(interpolateInsideCarousel(0, 0, 3, values, false)).toBe(1)
expect(interpolateInsideCarousel(2, 2, 3, values, false)).toBe(1)
})

it('interpolates halfway between slides', () => {
expect(interpolateInsideCarousel(0.5, 0, 3, values, false)).toBe(0.5)
})
})
})
4 changes: 4 additions & 0 deletions src/utils/interpolateInsideCarousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const interpolateInsideCarousel = (
valueAfter: number
offset?: number
},
// With infinite scroll the first and last slides are clones, so a slide also counts as current
// while its clone is.
isInfinite = true,
) => {
'worklet'
const { valueBefore, thisValue, valueAfter, offset = 0 } = values
Expand All @@ -27,6 +30,7 @@ export const interpolateInsideCarousel = (
}

const getAdjustedIndex = (slideIndex: number) => {
if (!isInfinite) return slideIndex
if (slideIndex === 0) return Math.max(totalLength - 2, 0)
if (slideIndex === totalLength - 1) return 1
if (slideIndex === 1) return totalLength - 1
Expand Down
Loading