22
33A highly customizable, performant carousel component for React Native with advanced animations, auto-scrolling capabilities, and infinite scrolling support. Built with React Native Reanimated for smooth, native-level performance.
44
5+ ** ✨ Context-Based Configuration** - All carousel settings are configured through the context provider for a clean, centralized API.
6+
57## Features
68
79✨ ** Auto-scrolling** with customizable intervals
@@ -55,7 +57,7 @@ const Slide = ({ title, color }: { title: string; color: string }) => (
5557
5658export default function BasicCarousel() {
5759 return (
58- <CarouselContextProvider >
60+ <CarouselContextProvider interval = { 4000 } disableAutoScroll = { false } initialIndex = { 0 } >
5961 <View style = { styles .container } >
6062 <HeroCarousel >
6163 { slides .map ((slide ) => (
@@ -90,38 +92,50 @@ const styles = StyleSheet.create({
9092
9193#### ` CarouselContextProvider `
9294
93- The context provider that must wrap your carousel components.
95+ The context provider that must wrap your carousel components. ** All carousel configuration is passed here. **
9496
9597``` tsx
9698<CarouselContextProvider
97- defaultScrollValue = { 1 } // Initial scroll position (default: 1 )
99+ initialIndex = { 0 } // Initial slide index (default: 0 )
98100 slideWidth = { screenWidth } // Width of each slide (default: screen width)
101+ interval = { 3000 } // Auto-scroll interval in ms
102+ disableAutoScroll = { false } // Disable auto-scrolling
103+ disableInfiniteScroll = { false } // Disable infinite scrolling
104+ autoScrollAnimation = { (to , duration ) => withTiming (to , { duration })} // Custom animation
99105>
100106 { children }
101107</CarouselContextProvider >
102108```
103109
110+ ** Props:**
111+
112+ | Prop | Type | Default | Description |
113+ | ----------------------- | ------------------------------------------ | ------------ | ----------------------------------------------------------------------------------- |
114+ | ` initialIndex ` | ` number ` | ` 0 ` | Initial slide index to start from |
115+ | ` slideWidth ` | ` number ` | screen width | Width of each slide in pixels |
116+ | ` interval ` | ` number \| ((index: number) => number) ` | ` 3000 ` | Auto-scroll interval in milliseconds, or function returning interval for each slide |
117+ | ` disableAutoScroll ` | ` boolean ` | ` false ` | Disable automatic scrolling |
118+ | ` disableInfiniteScroll ` | ` boolean ` | ` false ` | Disable infinite scrolling (shows first/last slide boundaries) |
119+ | ` autoScrollAnimation ` | ` (to: number, duration: number) => number ` | ` withTiming ` | Custom animation function for auto-scroll transitions |
120+ | ` children ` | ` React.ReactNode ` | Required | Carousel content (should contain HeroCarousel component) |
121+
104122#### ` HeroCarousel `
105123
106- The main carousel component with auto-scrolling functionality .
124+ The main carousel component that renders slides. ** Takes no configuration props ** - all configuration is handled by the context .
107125
108126``` tsx
109- <HeroCarousel
110- interval = { 3000 } // Auto-scroll interval in ms
111- disableAutoScroll = { false } // Disable auto-scrolling
112- goToPageAnimation = { (to , duration ) => withTiming (to , { duration })} // Custom page transition animation
113- >
114- { children }
127+ <HeroCarousel >
128+ { slides .map ((slide ) => (
129+ <YourSlideComponent key = { slide .id } { ... slide } />
130+ ))}
115131</HeroCarousel >
116132```
117133
118134** Props:**
119135
120- | Prop | Type | Default | Description |
121- | ------------------- | --------------------------------------- | -------- | ----------------------------------------------------------------------------------- | --- |
122- | ` interval ` | ` number \| ((index: number) => number) ` | ` 3000 ` | Auto-scroll interval in milliseconds, or function returning interval for each slide |
123- | ` disableAutoScroll ` | ` boolean ` | ` false ` | Disable automatic scrolling | |
124- | ` children ` | ` React.ReactNode[] ` | Required | Array of slide components |
136+ | Prop | Type | Description |
137+ | ---------- | ------------------- | ------------------------- |
138+ | ` children ` | ` React.ReactNode[] ` | Array of slide components |
125139
126140### Hooks
127141
@@ -147,7 +161,7 @@ const { scrollValue, timeoutValue, slideWidth, userInteracted, setUserInteracted
147161Get the current slide information and auto-scroll controls.
148162
149163``` tsx
150- const { index, total, runAutoScroll, goToPage } = useAutoCarouselSlideIndex ()
164+ const { index, total, runAutoScroll, goToPage } = useHeroCarouselSlideIndex ()
151165```
152166
153167** Returns:**
@@ -232,32 +246,68 @@ All pagination components automatically sync with the carousel state and support
232246
233247## Advanced Usage
234248
249+ ### Configuration Examples
250+
251+ Different carousel configurations using the context provider:
252+
253+ ``` tsx
254+ // Basic auto-scrolling carousel
255+ <CarouselContextProvider interval = { 3000 } >
256+ <HeroCarousel >{ slides } </HeroCarousel >
257+ </CarouselContextProvider >
258+
259+ // Video carousel without auto-scroll
260+ <CarouselContextProvider disableAutoScroll >
261+ <HeroCarousel >{ videoSlides } </HeroCarousel >
262+ </CarouselContextProvider >
263+
264+ // Carousel with custom intervals per slide
265+ <CarouselContextProvider interval = { (index ) => (index + 1 ) * 2000 } >
266+ <HeroCarousel >{ slides } </HeroCarousel >
267+ </CarouselContextProvider >
268+
269+ // Carousel starting from specific slide
270+ <CarouselContextProvider initialIndex = { 2 } disableInfiniteScroll >
271+ <HeroCarousel >{ slides } </HeroCarousel >
272+ </CarouselContextProvider >
273+
274+ // Custom slide width and animation
275+ <CarouselContextProvider
276+ slideWidth = { 300 }
277+ autoScrollAnimation = { (to , duration ) => withSpring (to , { damping: 15 })}
278+ >
279+ <HeroCarousel >{ slides } </HeroCarousel >
280+ </CarouselContextProvider >
281+ ```
282+
235283### Programmatic Navigation
236284
237285Control the carousel programmatically using the context:
238286
239287``` tsx
240288const CarouselWithControls = () => {
241- const { scrollValue } = useCarouselContext ()
242- const { runAutoScroll } = useAutoCarouselSlideIndex ()
289+ const { scrollValue, goToPage } = useCarouselContext ()
290+ const { runAutoScroll } = useHeroCarouselSlideIndex ()
243291
244292 const goToNext = () => {
245293 runAutoScroll (0 ) // Immediate transition
246294 }
247295
248296 const goToSlide = (slideIndex : number ) => {
249- scrollValue . value = withTiming (slideIndex , { duration: 500 })
297+ goToPage (slideIndex , 500 ) // Go to slide with 500ms animation
250298 }
251299
252300 return (
253- <View >
254- <HeroCarousel disableAutoScroll >{ /* Your slides */ } </HeroCarousel >
255-
256- <View style = { styles .controls } >
257- <Button title = " Previous" onPress = { () => goToSlide (scrollValue .value - 1 )} />
258- <Button title = " Next" onPress = { goToNext } />
301+ <CarouselContextProvider disableAutoScroll >
302+ <View >
303+ <HeroCarousel >{ /* Your slides */ } </HeroCarousel >
304+
305+ <View style = { styles .controls } >
306+ <Button title = " Previous" onPress = { () => goToSlide (scrollValue .value - 1 )} />
307+ <Button title = " Next" onPress = { goToNext } />
308+ </View >
259309 </View >
260- </View >
310+ </CarouselContextProvider >
261311 )
262312}
263313```
@@ -281,6 +331,18 @@ useEffect(() => {
281331}, [])
282332```
283333
334+ ## Architecture
335+
336+ ### Context-Based Configuration
337+
338+ This library uses a ** context-based architecture** where all carousel configuration is passed to the ` CarouselContextProvider ` rather than individual components. This design provides several benefits:
339+
340+ ✅ ** Centralized Configuration** - All settings in one place
341+ ✅ ** Cleaner Component API** - Components focus on rendering, not configuration
342+ ✅ ** Easier Testing** - Mock context for isolated component testing
343+
344+ That allows for components like pagination to not be attached to the carousel component.
345+
284346## Troubleshooting
285347
286348### Common Issues
0 commit comments