Skip to content

Commit b620ee1

Browse files
petrkonecny2Petr Konecny
andauthored
chore: introduce compound pattern for components (#25)
* chore: introduce compound pattern * chore: updated docs * chore: added helper hook for interpolated values * chore: rename slide to item --------- Co-authored-by: Petr Konecny <[email protected]>
1 parent ba3158e commit b620ee1

24 files changed

+314
-398
lines changed

README.md

Lines changed: 190 additions & 51 deletions
Large diffs are not rendered by default.

example/components/Collapsible.tsx

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

example/components/ExternalLink.tsx

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

example/components/HapticTab.tsx

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

example/components/HelloWave.tsx

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

example/components/ParallaxScrollView.tsx

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

example/examples/AnimatedExample.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
HeroCarousel,
3-
interpolateInsideCarousel,
4-
useCarouselContext,
5-
useAutoCarouselSlideIndex,
6-
CarouselContextProvider,
7-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel, useInterpolateInsideCarousel } from '@strv/react-native-hero-carousel'
82
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
93
import { Image } from 'expo-image'
104
import { LinearGradient } from 'expo-linear-gradient'
@@ -24,17 +18,14 @@ const getRandomImageUrl = () => {
2418
const images = Array.from({ length: 5 }, getRandomImageUrl)
2519

2620
const Slide = ({ image, title, index }: { image: string; title: string; index: number }) => {
27-
const { scrollValue } = useCarouselContext()
28-
const { index: slideIndex, total } = useAutoCarouselSlideIndex()
21+
const progress = useInterpolateInsideCarousel({
22+
valueBefore: 0,
23+
thisValue: 1,
24+
valueAfter: 0,
25+
offset: 0.2,
26+
})
2927

3028
const rStyle = useAnimatedStyle(() => {
31-
const progress = interpolateInsideCarousel(scrollValue.value, slideIndex, total, {
32-
valueBefore: 0,
33-
thisValue: 1,
34-
valueAfter: 0,
35-
offset: 0.2,
36-
})
37-
3829
return {
3930
flex: 1,
4031
width: '100%',
@@ -44,13 +35,13 @@ const Slide = ({ image, title, index }: { image: string; title: string; index: n
4435
transformOrigin: 'center',
4536
transform: [
4637
{
47-
scale: interpolate(progress, [0, 1], [0.8, 1], Extrapolation.CLAMP),
38+
scale: interpolate(progress.value, [0, 1], [0.8, 1], Extrapolation.CLAMP),
4839
},
4940
{
50-
rotate: `${interpolate(progress, [0, 1], [-15, 0], Extrapolation.CLAMP)}deg`,
41+
rotate: `${interpolate(progress.value, [0, 1], [-15, 0], Extrapolation.CLAMP)}deg`,
5142
},
5243
],
53-
opacity: progress,
44+
opacity: progress.value,
5445
}
5546
})
5647

@@ -75,7 +66,7 @@ export default function AnimatedExample() {
7566
}, [])
7667

7768
return (
78-
<CarouselContextProvider>
69+
<HeroCarousel.Provider>
7970
<SafeAreaView style={styles.container}>
8071
<View style={styles.carouselContainer}>
8172
<HeroCarousel>
@@ -88,7 +79,7 @@ export default function AnimatedExample() {
8879
<Pagination total={images.length} />
8980
</View>
9081
</SafeAreaView>
91-
</CarouselContextProvider>
82+
</HeroCarousel.Provider>
9283
)
9384
}
9485

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

0 commit comments

Comments
 (0)