11import React , { forwardRef , useCallback , useImperativeHandle } from 'react'
2- import { Dimensions } from 'react-native'
32import Animated , {
43 useSharedValue ,
54 useAnimatedStyle ,
@@ -11,8 +10,7 @@ import Animated, {
1110import { Gesture , GestureDetector } from 'react-native-gesture-handler'
1211
1312import { styles } from './styles'
14-
15- const { width : SCREEN_WIDTH } = Dimensions . get ( 'window' )
13+ import { useCarouselContext } from '../../context/CarouselContext'
1614
1715export type AnimatedPagedScrollViewRef = {
1816 scrollTo : ( value : number ) => void
@@ -28,14 +26,15 @@ export const AnimatedPagedView = forwardRef<AnimatedPagedScrollViewRef, Animated
2826 ( props , ref ) => {
2927 const translateX = useSharedValue ( 0 )
3028 const context = useSharedValue ( { x : 0 } )
29+ const { slideWidth } = useCarouselContext ( )
3130 const childrenArray = React . Children . toArray ( props . children )
3231
3332 const clampValue = useCallback (
3433 ( value : number ) => {
3534 'worklet'
36- return clamp ( value , 0 , ( childrenArray . length - 1 ) * SCREEN_WIDTH )
35+ return clamp ( value , 0 , ( childrenArray . length - 1 ) * slideWidth )
3736 } ,
38- [ childrenArray . length ] ,
37+ [ childrenArray . length , slideWidth ] ,
3938 )
4039
4140 const gesture = Gesture . Pan ( )
@@ -48,14 +47,14 @@ export const AnimatedPagedView = forwardRef<AnimatedPagedScrollViewRef, Animated
4847 } )
4948 . onEnd ( ( event ) => {
5049 const velocity = event . velocityX
51- const currentPage = Math . round ( translateX . value / SCREEN_WIDTH )
50+ const currentPage = Math . round ( translateX . value / slideWidth )
5251 const targetPage =
5352 velocity > 500 ? currentPage - 1 : velocity < - 500 ? currentPage + 1 : currentPage
5453 // in case the gesture overshoots, snap to the nearest page
55- if ( Math . abs ( context . value . x - translateX . value ) > SCREEN_WIDTH / 2 ) {
56- translateX . value = withTiming ( clampValue ( currentPage * SCREEN_WIDTH ) )
54+ if ( Math . abs ( context . value . x - translateX . value ) > slideWidth / 2 ) {
55+ translateX . value = withTiming ( clampValue ( currentPage * slideWidth ) )
5756 } else {
58- translateX . value = withTiming ( clampValue ( targetPage * SCREEN_WIDTH ) )
57+ translateX . value = withTiming ( clampValue ( targetPage * slideWidth ) )
5958 }
6059 } )
6160
0 commit comments