Skip to content

Commit 891f599

Browse files
author
Petr Konecny
committed
chore: added helper hook for interpolated values
1 parent 35ebe55 commit 891f599

File tree

15 files changed

+123
-313
lines changed

15 files changed

+123
-313
lines changed

README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,52 @@ const { index, total, runAutoScroll, goToPage } = useAutoCarouselSlideIndex()
223223
- `runAutoScroll`: Function to manually trigger auto-scroll with custom interval
224224
- `goToPage`: Function to programmatically navigate to a specific slide from another slide
225225

226-
### Utilities
226+
#### `useInterpolateInsideCarousel()`
227227

228-
#### `interpolateInsideCarousel()`
229-
230-
Advanced interpolation utility for creating custom slide animations.
228+
Hook for creating custom slide animations with automatic interpolation based on carousel scroll position. Must be used within a slide component (inside `HeroCarousel`). Returns a `SharedValue` that you can use in animated styles.
231229

232230
```tsx
233-
import { interpolateInsideCarousel } from '@strv/react-native-hero-carousel'
231+
import { useInterpolateInsideCarousel } from '@strv/react-native-hero-carousel'
232+
import Animated, { useAnimatedStyle, interpolate, Extrapolation } from 'react-native-reanimated'
234233

235-
const animatedStyle = useAnimatedStyle(() => {
236-
const progress = interpolateInsideCarousel(scrollValue.value, slideIndex, total, {
234+
const Slide = () => {
235+
const progress = useInterpolateInsideCarousel({
237236
valueBefore: 0, // Value for slides before current
238237
thisValue: 1, // Value for current slide
239238
valueAfter: 0, // Value for slides after current
240239
offset: 0.2, // Animation offset (optional)
241240
})
242241

243-
return {
244-
opacity: progress,
245-
transform: [{ scale: progress }],
246-
}
247-
})
242+
const animatedStyle = useAnimatedStyle(() => {
243+
return {
244+
opacity: progress.value,
245+
transform: [
246+
{
247+
scale: interpolate(progress.value, [0, 1], [0.8, 1], Extrapolation.CLAMP),
248+
},
249+
],
250+
}
251+
})
252+
253+
return <Animated.View style={animatedStyle}>{/* Your content */}</Animated.View>
254+
}
248255
```
249256

257+
**Returns:**
258+
259+
A `SharedValue` (from `useDerivedValue`) representing the interpolated progress value (0-1) based on the current slide's position in the carousel. Access the value using `.value` in animated styles or worklet functions.
260+
261+
**Parameters:**
262+
263+
| Parameter | Type | Default | Description |
264+
| ------------- | -------- | -------- | ---------------------------------------------------------------- |
265+
| `valueBefore` | `number` | Required | Value to use for slides before the current slide |
266+
| `thisValue` | `number` | Required | Value to use for the current slide |
267+
| `valueAfter` | `number` | Required | Value to use for slides after the current slide |
268+
| `offset` | `number` | `0` | Animation offset (0-1) to control when the animation starts/ends |
269+
270+
**Note:** The `interpolateInsideCarousel` utility function is now internal-only. Use this hook instead for all custom animations. The hook automatically handles the slide context (index, total, scrollValue) internally.
271+
250272
## Examples
251273

252274
We provide a comprehensive example app showcasing all the carousel features. You can run the examples locally or view the source code:
@@ -276,7 +298,7 @@ Then scan the QR code with Expo Go or run on simulator. See the [example app REA
276298

277299
- **Image Carousels** with smooth transitions and auto-scrolling
278300
- **Video Integration** with `expo-video` and playback controls
279-
- **Custom Animations** using `interpolateInsideCarousel` utility
301+
- **Custom Animations** using `useInterpolateInsideCarousel` hook
280302
- **Entering/Exiting Animations** using `HeroCarousel.AnimatedView` component
281303
- **Timer-based Pagination** with visual progress bars
282304
- **Gesture Handling** with swipe navigation and user interaction detection

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: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
HeroCarousel,
3-
interpolateInsideCarousel,
4-
useCarouselContext,
5-
useAutoCarouselSlideIndex,
6-
} from '@strv/react-native-hero-carousel'
1+
import { HeroCarousel, useInterpolateInsideCarousel } from '@strv/react-native-hero-carousel'
72
import { SafeAreaView, StyleSheet, View, Text, Dimensions } from 'react-native'
83
import { Image } from 'expo-image'
94
import { LinearGradient } from 'expo-linear-gradient'
@@ -23,17 +18,14 @@ const getRandomImageUrl = () => {
2318
const images = Array.from({ length: 5 }, getRandomImageUrl)
2419

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

2928
const rStyle = useAnimatedStyle(() => {
30-
const progress = interpolateInsideCarousel(scrollValue.value, slideIndex, total, {
31-
valueBefore: 0,
32-
thisValue: 1,
33-
valueAfter: 0,
34-
offset: 0.2,
35-
})
36-
3729
return {
3830
flex: 1,
3931
width: '100%',
@@ -43,13 +35,13 @@ const Slide = ({ image, title, index }: { image: string; title: string; index: n
4335
transformOrigin: 'center',
4436
transform: [
4537
{
46-
scale: interpolate(progress, [0, 1], [0.8, 1], Extrapolation.CLAMP),
38+
scale: interpolate(progress.value, [0, 1], [0.8, 1], Extrapolation.CLAMP),
4739
},
4840
{
49-
rotate: `${interpolate(progress, [0, 1], [-15, 0], Extrapolation.CLAMP)}deg`,
41+
rotate: `${interpolate(progress.value, [0, 1], [-15, 0], Extrapolation.CLAMP)}deg`,
5042
},
5143
],
52-
opacity: progress,
44+
opacity: progress.value,
5345
}
5446
})
5547

example/examples/VideoCarouselExample.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { HeroCarousel, useAutoCarouselSlideIndex } from '@strv/react-native-hero-carousel'
1+
import {
2+
HeroCarousel,
3+
useAutoCarouselSlideIndex,
4+
useActiveItemEffect,
5+
useIsActiveItem,
6+
} from '@strv/react-native-hero-carousel'
27
import { SafeAreaView, StyleSheet, View, Text, Pressable, Dimensions, Platform } from 'react-native'
38
import { useVideoPlayer, VideoView } from 'expo-video'
49
import { LinearGradient } from 'expo-linear-gradient'
5-
import { useActiveSlideEffect, useIsActiveSlide } from '@/hooks/useActiveSlideEffect'
610
import { useEffect, useRef, useState } from 'react'
711
import { TimerPagination } from './components/TimerPagination'
812
import { useEvent, useEventListener } from 'expo'
@@ -26,9 +30,9 @@ const videoTitles = [
2630
const Slide = ({ videoUri, title, index }: { videoUri: string; title: string; index: number }) => {
2731
const player = useVideoPlayer(videoUri)
2832
const { runAutoScroll } = useAutoCarouselSlideIndex()
29-
const isActiveSlide = useIsActiveSlide()
33+
const isActiveSlide = useIsActiveItem()
3034
const [duration, setDuration] = useState(0)
31-
useActiveSlideEffect(() => {
35+
useActiveItemEffect(() => {
3236
player.currentTime = 0
3337
player.play()
3438
return () => {

0 commit comments

Comments
 (0)