Snap Carousel
A React Native carousel that settles on one card at a time, with neighbors that shrink and fade and dots that stretch to follow.
A horizontal carousel that settles on one card at a time. The centered card is full size and opaque, its neighbors shrink and fade with the scroll position, and the dots below stretch to follow. Tap a dot or a neighboring card to go there. The dots row doubles as an adjustable control for screen readers, and on desktop the cards can be dragged with a mouse. Built for React Native and Expo with react-native only; this page previews it in the browser through react-native-web.
Installation
CLI
npx @modus-ui/cli add snap-carouselManual
- Copy the code from the component tab.
- Create a file at
components/ui/snap-carousel.tsx. - Paste the code.
It needs no packages beyond react-native, which your Expo or React Native app already has.
Usage
The carousel is as wide as its parent and as tall as its tallest card. Give the cards a background, since the carousel draws none.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | T[] | - | The items to show. |
renderItem | (item: T, index: number) => ReactNode | - | Renders a card's content. The carousel sizes and animates the card itself. |
keyExtractor | (item: T, index: number) => string | - | A stable key for each item. |
itemWidth | number | 0.72 | Card width as a fraction of the carousel's width. |
gap | number | 12 | Space between cards. |
onIndexChange | (index: number) => void | - | Called when a different card becomes the centered one. |
initialIndex | number | 0 | Index of the card centered at first. |
showDots | boolean | true | Whether to draw the dots under the cards. |
colors | Partial<SnapCarouselColors> | - | Overrides dot and activeDot. |
style | StyleProp<ViewStyle> | - | Style for the carousel container. |
Comments