ImageCarousel
A customizable image carousel component for displaying multiple images in a scrollable horizontal layout.
ImageCarousel
The ImageCarousel component provides a horizontal scrollable image gallery with pagination indicators. It's perfect for showcasing multiple images in a compact space, with support for interactivity, automatic playback, and customizable styling.
Features
- Horizontal Image Scrolling: Display multiple images in a swipeable carousel
- Pagination Indicators: Optional dot or numeric indicators for the current image
- Custom Sizing: Control width and height of the carousel
- Image Interaction: Support for handling image tap events
- Rounded Corners: Customizable border radius options
- Auto-Play Support: Optional automatic image cycling
- Looping Support: Seamless looping through images
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
images | string[] | ImageSourcePropType[] | Required | Array of image URLs or source objects |
width | number | Screen width | Width of the carousel |
height | number | 200 | Height of the carousel |
showPagination | boolean | true | Whether to show pagination indicators |
paginationStyle | 'dots' | 'numbers' | 'dots' | Style of pagination indicators |
onImagePress | (index: number) => void | — | Function to call when an image is pressed |
autoPlay | boolean | false | Whether to automatically cycle through images |
autoPlayInterval | number | 3000 | Time in milliseconds between auto-play transitions |
loop | boolean | true | Whether to loop back to the first image after the last one |
className | string | '' | Additional Tailwind classes |
rounded | 'none' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full' | 'none' | Border radius of the carousel |
Usage Examples
Basic Image Carousel
Carousel with Numeric Pagination
Rounded Carousel with Interaction
Auto-Playing Carousel
Custom Styling
Integration Examples
Product Gallery Carousel
Featured Content Slider
Property Listing Images
News Article Carousel
Custom Implementations
Carousel with Custom Overlay
Carousel with Image Thumbnails
Best Practices
Optimize Image Loading
Ensure images are properly optimized for carousel display:
Handle Image Load Errors
Implement fallback mechanisms for image loading failures:
Responsive Carousel Height
Adjust carousel height based on screen dimensions:
Implementation Details
The ImageCarousel component is built using React Native's FlatList component with horizontal scrolling and paging enabled. It handles layout measurement to adapt to container width when a specific width is not provided.
The pagination indicators are rendered as either dots (small circles that highlight the current image) or a numeric display (showing current position and total count). These are positioned absolutely at the bottom of the carousel.
The component uses the onMomentumScrollEnd event from FlatList to detect when the user has settled on a new image, which updates the active index state. This drives the pagination indicator updates.
Each image is rendered within a Pressable component to enable the onImagePress callback, allowing for interaction with individual carousel items.
Notes
- The FlatList used internally has pagingEnabled set to true for snap behavior
- For best performance, provide appropriately sized images to avoid excessive memory usage
- When using autoPlay, the carousel will pause when the user interacts with it
- The component automatically adapts to container width if no specific width is provided
- Pagination indicators are rendered on top of images using absolute positioning