Navigation and control
Skip and Next use the ref API, the background follows positionValue.
Snapping and animation
Section titled “Snapping and animation”snapMode |
Behavior |
|---|---|
page (default) |
One page per swipe; a fling moves to the next page |
momentum |
A hard fling glides over several pages, then snaps to the nearest |
free |
Glides and stops anywhere, like a scroll view |
animation sets how the carousel settles, after a swipe and for scrollTo, next, dots and autoplay: a spring that carries the finger’s velocity (default { damping: 26, stiffness: 220 }) or { type: 'timing', duration }.
onScrollStart fires when a swipe, a command or an autoplay step starts, onScrollEnd(index) when the carousel comes to rest. configureGesture receives the pan gesture to adjust it, for example to run together with a parent gesture:
const configureGesture = useCallback((pan) => pan.simultaneousWithExternalGesture(parentPan), [parentPan]);Tap to navigate
Section titled “Tap to navigate”Some users tap rather than swipe. With tapToNavigate, a tap changes the page:
<MorphCarousel tapToNavigate … /> // left 30% back, the rest forward<MorphCarousel tapToNavigate={{ mode: 'forward' }} … /> // any tap goes forward<MorphCarousel tapToNavigate={{ area: 'media' }} … /> // only taps on the media area| Option | Default | |
|---|---|---|
mode |
'sides' |
sides: the leading backZone goes back, the rest forward, like stories. forward: any tap goes forward |
backZone |
0.3 |
Fraction of the carousel that goes back |
area |
'all' |
media counts only taps on the media area |
The leading side is the left, the top when vertical, the right in right-to-left layouts. With pageSize smaller than the carousel, a tap on a neighbor peeking at the side goes to that neighbor. Swiping keeps working: a tap counts only when the finger does not move, and a touch on a carousel at rest does not fire onScrollEnd.
It is off by default because a tap on a button inside a page would also change the page; with buttons in your pages, use area: 'media'.
Controlling the carousel
Section titled “Controlling the carousel”const carousel = useRef<MorphCarouselRef>(null);const position = useSharedValue(0);
// Tint the screen with the current item's color while swipingconst background = useAnimatedStyle(() => ({ backgroundColor: interpolateColor(position.value, [0, 1, 2], ['#efe9ff', '#fff3df', '#e2f6f1']),}));
<MorphCarousel ref={carousel} positionValue={position} onIndexChange={setIndex} … /><Button title="Next" onPress={() => carousel.current?.next()} />next() on the last page goes back to the first when loop is off, which is what “Next” buttons and autoplay usually want. With loop, scrollTo(0) from the last page moves forward one page instead of rewinding through all of them.
Source of the demo
Section titled “Source of the demo”import { router } from 'expo-router';import { MorphCarousel, type MorphCarouselRef } from 'expo-morph-carousel';import { useRef, useState } from 'react';import { Pressable, StyleSheet, Text, View } from 'react-native';import Animated, { interpolateColor, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { products } from '../data/products';import { column, sides } from '../data/layout';
const steps = products.slice(0, 4);const tints = steps.map((s) => s.color);
/** * Full-screen onboarding. The media fills the background, the text sits in * the lower half, and the footer buttons use the ref API. The background tint * follows the swipe through `positionValue`. */export default function OnboardingScreen() { const insets = useSafeAreaInsets(); const carousel = useRef<MorphCarouselRef>(null); const position = useSharedValue(0); const [index, setIndex] = useState(0); const last = index === steps.length - 1;
const background = useAnimatedStyle(() => ({ backgroundColor: interpolateColor( position.value, steps.map((_, i) => i), tints.map((c) => `${c}22`), ), }));
return ( <Animated.View style={[ styles.screen, background, sides(insets), { paddingTop: insets.top, paddingBottom: insets.bottom }, ]} > <Pressable style={styles.skip} onPress={() => router.back()} hitSlop={12}> <Text style={styles.skipText}>Skip</Text> </Pressable> <MorphCarousel ref={carousel} testID="onboarding-carousel" data={steps} keyExtractor={(s) => s.id} getImage={(s) => s.image} getPlaceholderColor={(s) => s.color} getAccessibilityLabel={(s) => s.title} accessibilityLabel="Introduction" mediaPosition="background" mediaPadding={64} mediaStyle={styles.media} positionValue={position} onIndexChange={setIndex} style={[styles.carousel, column]} pageStyle={styles.pageFrame} pagination={{ activeColor: '#1c1c1e', inactiveColor: '#1c1c1e33' }} renderItem={({ item, index: i }) => ( <View style={styles.page}> <Text style={styles.step}> Step {i + 1} of {steps.length} </Text> <Text style={styles.title}>{item.title}</Text> <Text style={styles.description}>{item.description}</Text> </View> )} renderFooter={() => ( <Pressable testID="onboarding-next" style={styles.next} onPress={() => (last ? router.back() : carousel.current?.next())} > <Text style={styles.nextText}>{last ? 'Get started' : 'Next'}</Text> </Pressable> )} /> </Animated.View> );}
const styles = StyleSheet.create({ screen: { flex: 1, backgroundColor: '#fff' }, skip: { alignSelf: 'flex-end', padding: 16 }, skipText: { fontSize: 16, color: '#6e6e73' }, carousel: { flex: 1 }, media: { bottom: '42%' }, pageFrame: { justifyContent: 'flex-end' }, page: { paddingHorizontal: 32, paddingBottom: 16, gap: 10 }, step: { fontSize: 13, fontWeight: '600', color: '#6e6e73', textTransform: 'uppercase', letterSpacing: 1 }, title: { fontSize: 30, fontWeight: '800', color: '#1c1c1e' }, description: { fontSize: 16, lineHeight: 22, color: '#3a3a3c' }, next: { alignSelf: 'stretch', marginHorizontal: 24, paddingVertical: 16, borderRadius: 16, backgroundColor: '#1c1c1e', alignItems: 'center', }, nextText: { color: '#fff', fontSize: 16, fontWeight: '600' },});