Skip to content

Commit f6b5607

Browse files
petrkonecny2Petr Konecny
andauthored
fix: dynamic width support (#16)
Co-authored-by: Petr Konecny <[email protected]>
1 parent fbcd62d commit f6b5607

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

example/examples/utils.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/components/AnimatedPagedView/index.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { forwardRef, useCallback, useImperativeHandle } from 'react'
2-
import { Dimensions } from 'react-native'
32
import Animated, {
43
useSharedValue,
54
useAnimatedStyle,
@@ -11,8 +10,7 @@ import Animated, {
1110
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
1211

1312
import { styles } from './styles'
14-
15-
const { width: SCREEN_WIDTH } = Dimensions.get('window')
13+
import { useCarouselContext } from '../../context/CarouselContext'
1614

1715
export 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

src/context/CarouselContext/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { createContext, useContext, useMemo, useState } from 'react'
22
import { useSharedValue } from 'react-native-reanimated'
33

4-
import { Dimensions } from 'react-native'
4+
import { useWindowDimensions } from 'react-native'
55
import { DEFAULT_ANIMATION, useManualScroll } from '../../hooks/useManualScroll'
66
import { DEFAULT_INTERVAL } from '../../components/HeroCarousel/index.preset'
77

8-
const windowWidth = Dimensions.get('window').width
9-
108
export const CarouselContext = createContext<
119
| (ReturnType<typeof useManualScroll> &
1210
ReturnType<typeof useUserInteracted> &
13-
Omit<ContextProps, 'children'>)
11+
Omit<ContextProps, 'children'> & {
12+
slideWidth: number
13+
})
1414
| null
1515
>(null)
1616

@@ -44,13 +44,15 @@ type ContextProps = {
4444
export const CarouselContextProvider = ({
4545
children,
4646
initialIndex = 0,
47-
slideWidth = windowWidth,
4847
disableInfiniteScroll = false,
4948
interval = DEFAULT_INTERVAL,
5049
disableAutoScroll = false,
5150
autoScrollAnimation = DEFAULT_ANIMATION,
51+
...rest
5252
}: ContextProps) => {
5353
const userInteracted = useUserInteracted()
54+
const { width: windowWidth } = useWindowDimensions()
55+
const slideWidth = rest.slideWidth ?? windowWidth
5456
const manualScroll = useManualScroll({
5557
slideWidth,
5658
initialIndex: disableInfiniteScroll ? initialIndex : initialIndex + 1,

0 commit comments

Comments
 (0)