Lesson 8 of 9
Mini Project: Swipe Card Stack
Build a Tinder-style swipe card stack: pan gesture drives card rotation and opacity, release past threshold dismisses the card with a fling animation, new cards animate in from below, and haptics provide swipe feedback.
Complete Swipe Card Stack
import Animated, {
useSharedValue, useAnimatedStyle, withSpring, withTiming,
interpolate, Extrapolation, runOnJS,
} from 'react-native-reanimated';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import { useState, useCallback } from 'react';
import { View, Text, StyleSheet, Pressable } from 'react-native';
import { Image } from 'expo-image';
import * as Haptics from 'expo-haptics';
interface Card { id: string; name: string; age: number; imageUri: string; bio: string; }
const CARDS: Card[] = [
{ id: '1', name: 'Emma', age: 26, imageUri: 'https://i.pravatar.cc/400?img=5', bio: 'Coffee enthusiast โ Dog lover ๐' },
{ id: '2', name: 'Liam', age: 28, imageUri: 'https://i.pravatar.cc/400?img=8', bio: 'Hiker ๐๏ธ Photographer ๐ท' },
{ id: '3', name: 'Sophia', age: 24, imageUri: 'https://i.pravatar.cc/400?img=11', bio: 'Foodie ๐ World traveller โ๏ธ' },
{ id: '4', name: 'Noah', age: 30, imageUri: 'https://i.pravatar.cc/400?img=14', bio: 'Musician ๐ธ Bookworm ๐' },
];
const SWIPE_THRESHOLD = 120;
const CARD_WIDTH = 320;
function SwipeCard({ card, onSwipe, isTop }: { card: Card; onSwipe: (dir: 'left' | 'right') => void; isTop: boolean }) {
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const gesture = Gesture.Pan()
.enabled(isTop)
.onUpdate(e => {
translateX.value = e.translationX;
translateY.value = e.translationY;
})
.onEnd(e => {
if (Math.abs(e.translationX) > SWIPE_THRESHOLD) {
const dir = e.translationX > 0 ? 'right' : 'left';
translateX.value = withTiming(dir === 'right' ? 500 : -500, { duration: 300 });
runOnJS(Haptics.impactAsync)(Haptics.ImpactFeedbackStyle.Medium);
runOnJS(onSwipe)(dir);
} else {
translateX.value = withSpring(0, { damping: 15 });
translateY.value = withSpring(0, { damping: 15 });
}
});
const cardStyle = useAnimatedStyle(() => {
const rotate = interpolate(translateX.value, [-CARD_WIDTH, 0, CARD_WIDTH], [-30, 0, 30], Extrapolation.CLAMP);
return {
transform: [
{ translateX: translateX.value },
{ translateY: translateY.value },
{ rotate: `${rotate}deg` },
],
};
});
const likeOpacity = useAnimatedStyle(() => ({
opacity: interpolate(translateX.value, [20, 100], [0, 1], Extrapolation.CLAMP),
}));
const nopeOpacity = useAnimatedStyle(() => ({
opacity: interpolate(translateX.value, [-20, -100], [0, 1], Extrapolation.CLAMP),
}));
return (
<GestureDetector gesture={gesture}>
<Animated.View style={[s.card, cardStyle]}>
<Image source={{ uri: card.imageUri }} style={s.cardImage} contentFit="cover" />
{/* Like / Nope stamps */}
<Animated.View style={[s.stamp, s.likeStamp, likeOpacity]}>
<Text style={s.likeText}>LIKE</Text>
</Animated.View>
<Animated.View style={[s.stamp, s.nopeStamp, nopeOpacity]}>
<Text style={s.nopeText}>NOPE</Text>
</Animated.View>
<View style={s.cardInfo}>
<Text style={s.cardName}>{card.name}, {card.age}</Text>
<Text style={s.cardBio}>{card.bio}</Text>
</View>
</Animated.View>
</GestureDetector>
);
}
export function SwipeCardStack() {
const [cards, setCards] = useState(CARDS);
const [history, setHistory] = useState<Card[]>([]);
const handleSwipe = useCallback((dir: 'left' | 'right') => {
setHistory(h => [...h, cards[cards.length - 1]]);
setCards(c => c.slice(0, -1));
}, [cards]);
function handleUndo() {
if (!history.length) return;
setCards(c => [...c, history[history.length - 1]]);
setHistory(h => h.slice(0, -1));
}
return (
<View style={s.screen}>
{cards.length === 0 ? (
<View style={s.empty}><Text style={s.emptyText}>No more cards ๐</Text></View>
) : (
<View style={s.stack}>
{cards.map((card, i) => (
<View key={card.id} style={[s.cardWrapper, {
transform: [{ scale: 1 - (cards.length - 1 - i) * 0.04 }],
top: (cards.length - 1 - i) * 8,
zIndex: i,
}]}>
<SwipeCard card={card} onSwipe={handleSwipe} isTop={i === cards.length - 1} />
</View>
))}
</View>
)}
<View style={s.buttons}>
<Pressable onPress={() => handleSwipe('left')} style={[s.actionBtn, s.nopeBtn]}>
<Text style={{ fontSize: 28 }}>โ</Text>
</Pressable>
{history.length > 0 && (
<Pressable onPress={handleUndo} style={[s.actionBtn, s.undoBtn]}>
<Text style={{ fontSize: 22 }}>โฉ</Text>
</Pressable>
)}
<Pressable onPress={() => handleSwipe('right')} style={[s.actionBtn, s.likeBtn]}>
<Text style={{ fontSize: 28 }}>โค๏ธ</Text>
</Pressable>
</View>
</View>
);
}
const s = StyleSheet.create({
screen: { flex: 1, backgroundColor: '#0f172a', alignItems: 'center' },
stack: { width: CARD_WIDTH, height: 480, marginTop: 60 },
cardWrapper: { position: 'absolute', width: CARD_WIDTH },
card: { width: CARD_WIDTH, height: 460, borderRadius: 20, overflow: 'hidden', shadowColor: '#000', shadowOffset: { width: 0, height: 8 }, shadowOpacity: 0.3, shadowRadius: 12, elevation: 8 },
cardImage: { ...StyleSheet.absoluteFillObject },
stamp: { position: 'absolute', top: 48, borderWidth: 3, borderRadius: 8, paddingHorizontal: 10, paddingVertical: 4 },
likeStamp: { right: 20, borderColor: '#4ade80', transform: [{ rotate: '15deg' }] },
likeText: { color: '#4ade80', fontSize: 26, fontWeight: '900' },
nopeStamp: { left: 20, borderColor: '#f87171', transform: [{ rotate: '-15deg' }] },
nopeText: { color: '#f87171', fontSize: 26, fontWeight: '900' },
cardInfo: { position: 'absolute', bottom: 0, left: 0, right: 0, padding: 20, background: 'linear-gradient(transparent, black)' },
cardName: { fontSize: 26, fontWeight: '800', color: 'white' },
cardBio: { fontSize: 14, color: '#e2e8f0', marginTop: 4 },
buttons: { flexDirection: 'row', gap: 20, marginTop: 24 },
actionBtn: { width: 60, height: 60, borderRadius: 30, alignItems: 'center', justifyContent: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.15, shadowRadius: 6, elevation: 4 },
nopeBtn: { backgroundColor: '#1e293b', borderWidth: 2, borderColor: '#ef4444' },
likeBtn: { backgroundColor: '#1e293b', borderWidth: 2, borderColor: '#4ade80' },
undoBtn: { backgroundColor: '#1e293b', borderWidth: 2, borderColor: '#fbbf24' },
empty: { flex: 1, alignItems: 'center', justifyContent: 'center' },
emptyText: { color: '#94a3b8', fontSize: 20, fontWeight: '700' },
});Tip
Tip
Practice Mini Project Swipe Card Stack in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Interactive card deck with drag rotation, threshold velocity, and spring snaps
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Swipe Card Stack from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Swipe Card Stack is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?