Skip to content

Commit b95a7be

Browse files
author
Petr Konecny
committed
chore: introduce compound pattern
1 parent ba3158e commit b95a7be

File tree

12 files changed

+38
-49
lines changed

12 files changed

+38
-49
lines changed

example/examples/AnimatedExample.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
interpolateInsideCarousel,
44
useCarouselContext,
55
useAutoCarouselSlideIndex,
6-
CarouselContextProvider,
76
} from '@strv/react-native-hero-carousel'
87
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
98
import { Image } from 'expo-image'
@@ -75,7 +74,7 @@ export default function AnimatedExample() {
7574
}, [])
7675

7776
return (
78-
<CarouselContextProvider>
77+
<HeroCarousel.Provider>
7978
<SafeAreaView style={styles.container}>
8079
<View style={styles.carouselContainer}>
8180
<HeroCarousel>
@@ -88,7 +87,7 @@ export default function AnimatedExample() {
8887
<Pagination total={images.length} />
8988
</View>
9089
</SafeAreaView>
91-
</CarouselContextProvider>
90+
</HeroCarousel.Provider>
9291
)
9392
}
9493

example/examples/BasicExample.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HeroCarousel, CarouselContextProvider } from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel } from '@strv/react-native-hero-carousel'
22
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
33
import { Image } from 'expo-image'
44
import { LinearGradient } from 'expo-linear-gradient'
@@ -38,7 +38,7 @@ export default function BasicExample() {
3838
}, [])
3939

4040
return (
41-
<CarouselContextProvider>
41+
<HeroCarousel.Provider>
4242
<SafeAreaView style={styles.container}>
4343
<View style={styles.container}>
4444
<HeroCarousel>
@@ -48,7 +48,7 @@ export default function BasicExample() {
4848
</HeroCarousel>
4949
</View>
5050
</SafeAreaView>
51-
</CarouselContextProvider>
51+
</HeroCarousel.Provider>
5252
)
5353
}
5454

example/examples/EnteringAnimationExample.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
HeroCarousel,
3-
CarouselContextProvider,
4-
SlideAnimatedView,
5-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel } from '@strv/react-native-hero-carousel'
62
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
73
import { Image } from 'expo-image'
84
import { LinearGradient } from 'expo-linear-gradient'
@@ -36,17 +32,17 @@ const Slide = ({ image, title, index }: { image: string; title: string; index: n
3632
<Image source={{ uri: image }} style={styles.image} contentFit="cover" transition={200} />
3733
<LinearGradient colors={['transparent', 'rgba(0,0,0,0.8)']} style={styles.gradient}>
3834
<View style={styles.text}>
39-
<SlideAnimatedView style={styles.textContainer} {...animationConfig}>
35+
<HeroCarousel.AnimatedView style={styles.textContainer} {...animationConfig}>
4036
<Text style={styles.title}>{title}</Text>
41-
</SlideAnimatedView>
42-
<SlideAnimatedView
37+
</HeroCarousel.AnimatedView>
38+
<HeroCarousel.AnimatedView
4339
style={styles.textContainer}
4440
entering={FadeIn.duration(400).delay(200)}
4541
>
4642
<Text style={styles.subtitle}>
4743
Animation: {animationNames[index % animationNames.length]}
4844
</Text>
49-
</SlideAnimatedView>
45+
</HeroCarousel.AnimatedView>
5046
</View>
5147
</LinearGradient>
5248
</View>
@@ -60,7 +56,7 @@ export default function EnteringAnimationExample() {
6056
}, [])
6157

6258
return (
63-
<CarouselContextProvider>
59+
<HeroCarousel.Provider>
6460
<SafeAreaView style={styles.container}>
6561
<View style={styles.container}>
6662
<HeroCarousel>
@@ -70,7 +66,7 @@ export default function EnteringAnimationExample() {
7066
</HeroCarousel>
7167
</View>
7268
</SafeAreaView>
73-
</CarouselContextProvider>
69+
</HeroCarousel.Provider>
7470
)
7571
}
7672

example/examples/TimerPaginationExample.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
HeroCarousel,
3-
CarouselContextProvider,
4-
useAutoCarouselSlideIndex,
5-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel, useAutoCarouselSlideIndex } from '@strv/react-native-hero-carousel'
62
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
73
import { Image } from 'expo-image'
84
import { LinearGradient } from 'expo-linear-gradient'
@@ -63,7 +59,7 @@ export default function TimerPaginationExample() {
6359
}
6460

6561
return (
66-
<CarouselContextProvider interval={getInterval}>
62+
<HeroCarousel.Provider interval={getInterval}>
6763
<SafeAreaView style={styles.container}>
6864
<View style={styles.container}>
6965
<HeroCarousel>
@@ -80,7 +76,7 @@ export default function TimerPaginationExample() {
8076
<TimerPagination total={images.length} hideProgressOnInteraction />
8177
</View>
8278
</SafeAreaView>
83-
</CarouselContextProvider>
79+
</HeroCarousel.Provider>
8480
)
8581
}
8682

example/examples/VideoCarouselExample.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
HeroCarousel,
3-
CarouselContextProvider,
4-
useAutoCarouselSlideIndex,
5-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel, useAutoCarouselSlideIndex } from '@strv/react-native-hero-carousel'
62
import { SafeAreaView, StyleSheet, View, Text, Pressable, Dimensions, Platform } from 'react-native'
73
import { useVideoPlayer, VideoView } from 'expo-video'
84
import { LinearGradient } from 'expo-linear-gradient'
@@ -89,7 +85,7 @@ const Slide = ({ videoUri, title, index }: { videoUri: string; title: string; in
8985

9086
export default function VideoCarouselExample() {
9187
return (
92-
<CarouselContextProvider disableAutoScroll={true}>
88+
<HeroCarousel.Provider disableAutoScroll={true}>
9389
<SafeAreaView style={styles.container}>
9490
<View style={styles.container}>
9591
<HeroCarousel>
@@ -100,7 +96,7 @@ export default function VideoCarouselExample() {
10096
<TimerPagination total={videos.length} hideProgressOnInteraction={false} />
10197
</View>
10298
</SafeAreaView>
103-
</CarouselContextProvider>
99+
</HeroCarousel.Provider>
104100
)
105101
}
106102

example/examples/components/CarouselBase.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
import {
2-
HeroCarousel,
3-
HeroCarouselProps,
4-
CarouselContextProvider,
5-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel, HeroCarouselProps } from '@strv/react-native-hero-carousel'
62
import { SafeAreaView, StyleSheet, View } from 'react-native'
73
import { Stack } from 'expo-router'
84
import { Pagination } from '@/examples/components/Pagination'
95

106
export function CarouselBase({ children }: { children: HeroCarouselProps['children'] }) {
117
return (
12-
<CarouselContextProvider>
8+
<HeroCarousel.Provider>
139
<Stack.Screen options={{ title: 'Animated Carousel' }} />
1410
<SafeAreaView style={styles.container}>
1511
<View style={styles.container}>
1612
<HeroCarousel>{children}</HeroCarousel>
1713
<Pagination total={children.length} />
1814
</View>
1915
</SafeAreaView>
20-
</CarouselContextProvider>
16+
</HeroCarousel.Provider>
2117
)
2218
}
2319

src/components/HeroCarousel/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react'
22

3-
import { useCarouselContext } from '../../context/CarouselContext'
3+
import { CarouselContextProvider, useCarouselContext } from '../../context/CarouselContext'
44
import { HeroCarouselSlide } from '../HeroCarouselSlide'
55
import { HeroCarouselAdapter } from '../AnimatedPagedView/Adapter'
66
import { useAutoScroll } from '../../hooks/useAutoScroll'
77
import { useInfiniteScroll } from '../../hooks/useInfiniteScroll'
88
import { DEFAULT_INTERVAL } from './index.preset'
9+
import { SlideAnimatedView } from 'components/SlideAnimatedView'
910

1011
export type HeroCarouselProps = {
1112
children: React.ReactNode[]
1213
}
1314

14-
export const HeroCarousel = ({ children }: HeroCarouselProps) => {
15+
const HeroCarousel = ({ children }: HeroCarouselProps) => {
1516
const {
1617
scrollValue,
1718
userInteracted,
@@ -72,3 +73,9 @@ export const HeroCarousel = ({ children }: HeroCarouselProps) => {
7273
</>
7374
)
7475
}
76+
77+
HeroCarousel.AnimatedView = SlideAnimatedView
78+
HeroCarousel.Provider = CarouselContextProvider
79+
HeroCarousel.Item = HeroCarouselSlide
80+
81+
export { HeroCarousel }

src/components/HeroCarouselSlide/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { View } from 'react-native'
2-
import { AutoCarouselSlideContext } from '../../context/SlideContext'
2+
import { HeroCarouselSlideContext } from '../../context/SlideContext'
33
import { useAutoScroll } from '../../hooks/useAutoScroll'
44
import { useMemo } from 'react'
55
import { useManualScroll } from '../../hooks/useManualScroll'
@@ -21,14 +21,14 @@ export const HeroCarouselSlide = ({
2121
}) => {
2222
return (
2323
<View style={{ flex: 1, width, minWidth: width }}>
24-
<AutoCarouselSlideContext.Provider
24+
<HeroCarouselSlideContext.Provider
2525
value={useMemo(
2626
() => ({ index, total, runAutoScroll, goToPage }),
2727
[index, total, runAutoScroll, goToPage],
2828
)}
2929
>
3030
{children}
31-
</AutoCarouselSlideContext.Provider>
31+
</HeroCarouselSlideContext.Provider>
3232
</View>
3333
)
3434
}

src/components/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './HeroCarousel'
2-
export * from './SlideAnimatedView'
1+
export { HeroCarousel, HeroCarouselProps } from './HeroCarousel'

src/context/SlideContext/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { createContext, useContext } from 'react'
22
import { useAutoScroll } from '../../hooks/useAutoScroll'
33
import { useManualScroll } from '../../hooks/useManualScroll'
44

5-
export const AutoCarouselSlideContext = createContext<{
5+
export const HeroCarouselSlideContext = createContext<{
66
index: number
77
total: number
88
runAutoScroll: ReturnType<typeof useAutoScroll>['runAutoScroll']
99
goToPage: ReturnType<typeof useManualScroll>['goToPage']
1010
} | null>(null)
1111

1212
export const useAutoCarouselSlideIndex = () => {
13-
const context = useContext(AutoCarouselSlideContext)
13+
const context = useContext(HeroCarouselSlideContext)
1414
if (!context) {
1515
throw new Error('useAutoCarouselSlideIndex must be used within a AutoCarouselSlide')
1616
}

0 commit comments

Comments
 (0)