Skip to content

Commit 2ab0bff

Browse files
committed
fix: can't override the carousel styles. e.g. "overflow: visible;"
fix #260
1 parent 3b03c5c commit 2ab0bff

File tree

2 files changed

+36
-64
lines changed

2 files changed

+36
-64
lines changed

src/Carousel.tsx

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import Animated, { runOnJS, useDerivedValue } from 'react-native-reanimated';
2+
import { runOnJS, useDerivedValue } from 'react-native-reanimated';
33

44
import { useCarouselController } from './hooks/useCarouselController';
55
import { useAutoPlay } from './hooks/useAutoPlay';
@@ -8,7 +8,7 @@ import { ScrollViewGesture } from './ScrollViewGesture';
88
import { useVisibleRanges } from './hooks/useVisibleRanges';
99

1010
import type { ICarouselInstance, TCarouselProps } from './types';
11-
import { StyleSheet, View } from 'react-native';
11+
import { StyleSheet } from 'react-native';
1212
import { BaseLayout } from './layouts/BaseLayout';
1313
import { useLayoutConfig } from './hooks/useLayoutConfig';
1414
import { useInitProps } from './hooks/useInitProps';
@@ -198,40 +198,29 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
198198

199199
return (
200200
<CTX.Provider value={{ props, common: commonVariables }}>
201-
<View
201+
<ScrollViewGesture
202+
key={mode}
203+
size={size}
204+
translation={handlerOffsetX}
202205
style={[
203206
styles.container,
204-
{ width: width || '100%', height: height || '100%' },
207+
{
208+
width: width || '100%',
209+
height: height || '100%',
210+
},
205211
style,
212+
vertical
213+
? styles.itemsVertical
214+
: styles.itemsHorizontal,
206215
]}
207216
testID={testID}
217+
onScrollBegin={scrollViewGestureOnScrollBegin}
218+
onScrollEnd={scrollViewGestureOnScrollEnd}
219+
onTouchBegin={scrollViewGestureOnTouchBegin}
220+
onTouchEnd={scrollViewGestureOnTouchEnd}
208221
>
209-
<ScrollViewGesture
210-
size={size}
211-
translation={handlerOffsetX}
212-
onScrollBegin={scrollViewGestureOnScrollBegin}
213-
onScrollEnd={scrollViewGestureOnScrollEnd}
214-
onTouchBegin={scrollViewGestureOnTouchBegin}
215-
onTouchEnd={scrollViewGestureOnTouchEnd}
216-
>
217-
<Animated.View
218-
key={mode}
219-
style={[
220-
styles.container,
221-
{
222-
width: width || '100%',
223-
height: height || '100%',
224-
},
225-
style,
226-
vertical
227-
? styles.itemsVertical
228-
: styles.itemsHorizontal,
229-
]}
230-
>
231-
{data.map(renderLayout)}
232-
</Animated.View>
233-
</ScrollViewGesture>
234-
</View>
222+
{data.map(renderLayout)}
223+
</ScrollViewGesture>
235224
</CTX.Provider>
236225
);
237226
}

src/ScrollViewGesture.tsx

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StyleProp, StyleSheet, ViewStyle } from 'react-native';
2+
import type { StyleProp, ViewStyle } from 'react-native';
33
import {
44
PanGestureHandler,
55
PanGestureHandlerGestureEvent,
@@ -27,11 +27,12 @@ type GestureContext = {
2727
interface Props {
2828
size: number;
2929
infinite?: boolean;
30+
testID?: string;
31+
style?: StyleProp<ViewStyle>;
3032
onScrollBegin?: () => void;
3133
onScrollEnd?: () => void;
3234
onTouchBegin?: () => void;
3335
onTouchEnd?: () => void;
34-
style?: StyleProp<ViewStyle>;
3536
translation: Animated.SharedValue<number>;
3637
}
3738

@@ -51,8 +52,10 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
5152
} = React.useContext(CTX);
5253

5354
const {
54-
translation,
5555
size,
56+
translation,
57+
testID,
58+
style = {},
5659
onScrollBegin,
5760
onScrollEnd,
5861
onTouchBegin,
@@ -269,42 +272,22 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
269272
]
270273
);
271274

272-
const directionStyle = React.useMemo(() => {
273-
return vertical ? styles.contentHorizontal : styles.contentVertical;
274-
}, [vertical]);
275-
276275
return (
277-
<Animated.View
278-
style={[
279-
styles.container,
280-
directionStyle,
281-
{ width: '100%', height: '100%' },
282-
]}
283-
onTouchStart={onTouchBegin}
284-
onTouchEnd={onTouchEnd}
276+
<PanGestureHandler
277+
{...panGestureHandlerProps}
278+
enabled={enabled}
279+
onGestureEvent={panGestureEventHandler}
285280
>
286-
<PanGestureHandler
287-
{...panGestureHandlerProps}
288-
enabled={enabled}
289-
onGestureEvent={panGestureEventHandler}
281+
<Animated.View
282+
testID={testID}
283+
style={style}
284+
onTouchStart={onTouchBegin}
285+
onTouchEnd={onTouchEnd}
290286
>
291287
{props.children}
292-
</PanGestureHandler>
293-
</Animated.View>
288+
</Animated.View>
289+
</PanGestureHandler>
294290
);
295291
};
296292

297293
export const ScrollViewGesture = IScrollViewGesture;
298-
299-
const styles = StyleSheet.create({
300-
container: {
301-
flex: 1,
302-
overflow: 'hidden',
303-
},
304-
contentVertical: {
305-
flexDirection: 'column',
306-
},
307-
contentHorizontal: {
308-
flexDirection: 'row',
309-
},
310-
});

0 commit comments

Comments
 (0)