Stories
Stories như các mạng xã hội: người dùng xoay như khối lập phương, ảnh của mỗi người tan vào nhau.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”- Chạm một avatar trong khay: trình xem toàn màn hình mở ra ở người đó.
- Mỗi người là một trang của carousel ngoài với
pageEffect="cube"và không có media: vuốt để sang người kế tiếp. - Ảnh của mỗi người là một carousel trong với
mediaPosition="background",mediaFit="cover"vàliquid.preserveShape: khung ảnh giữ nguyên, chỉ nội dung tan. - Carousel trong có
gestureEnabled={false}. Một Pressable phủ lên chuyển chạm ở một phần ba bên trái thànhref.prev(), phần còn lại thànhref.next(); nhấn giữ để tạm dừng. - Một timing của Reanimated làm đầy thanh tiến trình của ảnh hiện tại và gọi
next()khi hết; ảnh cuối chuyển sang người kế tiếp. - Khay tải trước mọi ảnh đúng kích thước trình xem sẽ xin (
usePrefetchImagesvớiimageTargetFor), nên story mở ra không phải chờ.
Dùng
Mã nguồn đầy đủ
Phần tiêu đề “Mã nguồn đầy đủ”Đây chính là mã đang chạy trong khung điện thoại. Chép về, đổi dữ liệu là dùng được.
import { Ionicons } from '@expo/vector-icons';import { imageTargetFor, MorphCarousel, usePrefetchImages, type MorphCarouselRef } from 'expo-morph-carousel';import { useCallback, useEffect, useRef, useState } from 'react';import { Image, Modal, PixelRatio, Pressable, ScrollView, StyleSheet, Text, useWindowDimensions, View,} from 'react-native';import { GestureHandlerRootView } from 'react-native-gesture-handler';import Animated, { cancelAnimation, Easing, runOnJS, useAnimatedStyle, useSharedValue, withTiming, type SharedValue,} from 'react-native-reanimated';import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context';
import { avatar, photo, photoFor, storyUsers, type StoryUser } from '../data/demos';import { column, sides } from '../data/layout';
/** * Stories like the big social apps have them. Two carousels: * * - the users, as the faces of a cube (`pageEffect="cube"`): swipe to the * next person, or let the last frame hand over; * - each user's frames, full screen behind the UI (`mediaPosition="background"`), * melting into each other with the liquid effect. Taps on the left and * right go back and forward, a long press pauses, and a timer drives the * progress bars and moves on through the ref API. */
const FRAME_MS = 5000;
export default function StoriesScreen() { const [open, setOpen] = useState<number | null>(null); const [seen, setSeen] = useState<Record<string, boolean>>({}); // Download every frame at the size the full-screen viewer will ask for // while the tray is on screen, so a story opens without waiting. const window = useWindowDimensions(); const target = imageTargetFor(window, PixelRatio.get(), 'cover'); usePrefetchImages(storyUsers.flatMap((u) => u.frames.map((f) => photoFor(f.id, target))));
return ( <ScrollView contentInsetAdjustmentBehavior="automatic" contentContainerStyle={[styles.screen, column]}> <ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerStyle={styles.tray}> {storyUsers.map((u, i) => ( <Pressable key={u.id} style={styles.trayItem} onPress={() => setOpen(i)} accessibilityRole="button" accessibilityLabel={`Open ${u.name}'s story`} > <View style={[styles.ring, { borderColor: seen[u.id] ? '#c7c7cc' : u.color }]}> <Image source={{ uri: avatar(u.avatar, 160) }} style={styles.trayAvatar} /> </View> <Text style={styles.trayName} numberOfLines={1}> {u.name} </Text> </Pressable> ))} </ScrollView> <Text style={styles.hint}> Tap a story. Inside, tap left or right, hold to pause, swipe to the next person: the people turn like a cube, the photos melt into each other. </Text> {storyUsers.map((u) => ( <View key={u.id} style={styles.post}> <View style={styles.postHeader}> <Image source={{ uri: avatar(u.avatar, 80) }} style={styles.postAvatar} /> <Text style={styles.postName}>{u.name}</Text> </View> <Image source={{ uri: photo(u.frames[0].id, 900, 900) }} style={styles.postImage} /> <Text style={styles.postCaption}>{u.frames[0].caption}</Text> </View> ))} <Modal visible={open !== null} animationType="fade" statusBarTranslucent onRequestClose={() => setOpen(null)} > {/* A modal is a new root: gestures and safe areas need their own providers. */} <SafeAreaProvider> <GestureHandlerRootView style={styles.viewerRoot}> {open !== null && ( <Viewer initial={open} onSeen={(id) => setSeen((s) => (s[id] ? s : { ...s, [id]: true }))} onClose={() => setOpen(null)} /> )} </GestureHandlerRootView> </SafeAreaProvider> </Modal> </ScrollView> );}
function Viewer({ initial, onSeen, onClose,}: { initial: number; onSeen: (id: string) => void; onClose: () => void;}) { const users = useRef<MorphCarouselRef>(null); const [current, setCurrent] = useState(initial); useEffect(() => onSeen(storyUsers[current].id), [current, onSeen]);
const next = useCallback(() => { if (current < storyUsers.length - 1) users.current?.next(); else onClose(); }, [current, onClose]); const prev = useCallback(() => { if (current > 0) users.current?.prev(); }, [current]);
return ( <MorphCarousel ref={users} data={storyUsers} keyExtractor={(u) => u.id} initialIndex={initial} pageEffect="cube" pagination={false} windowSize={1} onIndexChange={setCurrent} accessibilityLabel="Stories" getAccessibilityLabel={(u) => `${u.name}'s story`} style={styles.viewer} renderItem={({ item, index }) => ( <UserStory user={item} active={index === current} onEnd={next} onStart={prev} onClose={onClose} /> )} /> );}
function UserStory({ user, active, onEnd, onStart, onClose,}: { user: StoryUser; active: boolean; onEnd: () => void; onStart: () => void; onClose: () => void;}) { const insets = useSafeAreaInsets(); const { width } = useWindowDimensions(); const frames = useRef<MorphCarouselRef>(null); const [frame, setFrame] = useState(0); const [paused, setPaused] = useState(false); const progress = useSharedValue(0); const last = user.frames.length - 1;
const forward = useCallback(() => { if (frames.current && frames.current.getIndex() < last) frames.current.next(); else onEnd(); }, [last, onEnd]); const back = useCallback(() => { if (frames.current && frames.current.getIndex() > 0) frames.current.prev(); else onStart(); }, [onStart]);
const run = useCallback( (from: number) => { progress.value = from; progress.value = withTiming(1, { duration: FRAME_MS * (1 - from), easing: Easing.linear }, (done) => { if (done) runOnJS(forward)(); }); }, [forward, progress], );
// Restart the bar for every frame, and only for the user in view. useEffect(() => { cancelAnimation(progress); progress.value = 0; if (active) run(0); }, [active, frame, progress, run]);
const pause = () => { cancelAnimation(progress); setPaused(true); }; const resume = () => { if (!paused) return; setPaused(false); if (active) run(progress.value); };
const shown = user.frames[frame]; return ( <View style={styles.story}> <MorphCarousel ref={frames} data={user.frames} keyExtractor={(f) => f.id} getImage={(f, _i, target) => ({ uri: photoFor(f.id, target) })} mediaPosition="background" mediaFit="cover" mediaPadding={0} // Photos keep their rectangle; only their content melts. liquid={{ preserveShape: true, blur: 34 }} animation={{ type: 'timing', duration: 520 }} gestureEnabled={false} pagination={false} onIndexChange={setFrame} accessibilityLabel={`${user.name}'s photos`} getAccessibilityLabel={(f) => f.caption} style={StyleSheet.absoluteFill} />
{/* Taps: left third back, the rest forward; hold to pause. */} <Pressable style={StyleSheet.absoluteFill} onPress={(e) => (e.nativeEvent.locationX < width * 0.3 ? back() : forward())} onLongPress={pause} delayLongPress={180} onPressOut={resume} accessibilityRole="button" accessibilityLabel="Next photo" accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]} onAccessibilityAction={(e) => (e.nativeEvent.actionName === 'decrement' ? back() : forward())} />
<View style={[styles.top, sides(insets, 10), { paddingTop: insets.top + 8, opacity: paused ? 0 : 1 }]} pointerEvents="box-none" > <View style={styles.bars}> {user.frames.map((f, i) => ( <Bar key={f.id} index={i} frame={frame} progress={progress} /> ))} </View> <View style={styles.header}> <Image source={{ uri: avatar(user.avatar, 96) }} style={styles.headerAvatar} /> <Text style={styles.headerName}>{user.name}</Text> <Text style={styles.headerTime}>{shown.time}</Text> <View style={styles.flex} /> <Pressable onPress={onClose} hitSlop={12} accessibilityRole="button" accessibilityLabel="Close"> <Ionicons name="close" size={28} color="#fff" /> </Pressable> </View> </View>
<View style={[ styles.bottom, sides(insets, 12), { paddingBottom: insets.bottom + 12, opacity: paused ? 0 : 1 }, ]} > <Text style={styles.caption}>{shown.caption}</Text> <View style={styles.replyRow}> <View style={styles.reply}> <Text style={styles.replyText}>Send message</Text> </View> <Ionicons name="heart-outline" size={28} color="#fff" /> <Ionicons name="paper-plane-outline" size={26} color="#fff" /> </View> </View> </View> );}
function Bar({ index, frame, progress }: { index: number; frame: number; progress: SharedValue<number> }) { const fill = useAnimatedStyle(() => ({ width: `${(index < frame ? 1 : index === frame ? progress.value : 0) * 100}%`, })); return ( <View style={styles.bar}> <Animated.View style={[styles.barFill, fill]} /> </View> );}
const shadow = { textShadowColor: '#0008', textShadowRadius: 6, textShadowOffset: { width: 0, height: 1 } };
const styles = StyleSheet.create({ screen: { paddingVertical: 12, gap: 14 }, tray: { paddingHorizontal: 12, gap: 14 }, trayItem: { alignItems: 'center', width: 76, gap: 6 }, ring: { padding: 3, borderRadius: 40, borderWidth: 3 }, trayAvatar: { width: 62, height: 62, borderRadius: 31, backgroundColor: '#ddd' }, trayName: { fontSize: 12, color: '#1c1c1e' }, hint: { fontSize: 13, lineHeight: 18, color: '#6e6e73', paddingHorizontal: 16 }, post: { backgroundColor: '#fff', paddingBottom: 12, gap: 10 }, postHeader: { flexDirection: 'row', alignItems: 'center', gap: 10, paddingHorizontal: 12, paddingTop: 12 }, postAvatar: { width: 32, height: 32, borderRadius: 16, backgroundColor: '#ddd' }, postName: { fontSize: 14, fontWeight: '700', color: '#1c1c1e' }, postImage: { width: '100%', aspectRatio: 1, backgroundColor: '#e5e5ea' }, postCaption: { fontSize: 14, color: '#1c1c1e', paddingHorizontal: 12 }, viewerRoot: { flex: 1, backgroundColor: '#000' }, viewer: { flex: 1, backgroundColor: '#000' }, story: { flex: 1, backgroundColor: '#000', overflow: 'hidden' }, top: { position: 'absolute', left: 0, right: 0, top: 0, gap: 10 }, bars: { flexDirection: 'row', gap: 4 }, bar: { flex: 1, height: 3, borderRadius: 2, backgroundColor: '#ffffff59', overflow: 'hidden' }, barFill: { height: 3, backgroundColor: '#fff' }, header: { flexDirection: 'row', alignItems: 'center', gap: 8 }, headerAvatar: { width: 34, height: 34, borderRadius: 17, borderWidth: 1, borderColor: '#fff', backgroundColor: '#555', }, headerName: { color: '#fff', fontWeight: '700', fontSize: 14, ...shadow }, headerTime: { color: '#ffffffb3', fontSize: 13, ...shadow }, flex: { flex: 1 }, bottom: { position: 'absolute', left: 0, right: 0, bottom: 0, gap: 12 }, caption: { color: '#fff', fontSize: 20, fontWeight: '700', ...shadow }, replyRow: { flexDirection: 'row', alignItems: 'center', gap: 16 }, reply: { flex: 1, borderWidth: 1, borderColor: '#ffffffb3', borderRadius: 24, paddingHorizontal: 16, paddingVertical: 11, }, replyText: { color: '#fff', fontSize: 14 },});/** * Data for the real-world demos (stories, vertical feed, flash sale, shop, * memories, albums). Photos from Unsplash, free to use under the Unsplash * License (https://unsplash.com/license), loaded at the size the carousel * asks for through imgix (`w`, `h`, `fit=crop`). */
import { WIDTH_STEPS, type ImageTarget } from 'expo-morph-carousel';
/** An Unsplash photo URL cropped to the given size, in physical pixels. */export function photo(id: string, width: number, height: number, extra = '') { return `https://images.unsplash.com/photo-${id}?w=${Math.round(width)}&h=${Math.round(height)}&fit=crop&q=80&fm=jpg${extra}`;}
/** * The photo for a carousel's media area. imgix crops to exactly the box we * ask for, so ask for the area itself (in physical pixels, the width rounded * up to a step so neighbors share cache entries). `target.width` is sized * from the longer side for `cover`, which suits images the server does not * crop but is far too big for a tall full-screen area. */export function photoFor(id: string, target: ImageTarget) { const px = target.layoutWidth * target.pixelRatio; const width = WIDTH_STEPS.find((s) => s >= px) ?? WIDTH_STEPS[WIDTH_STEPS.length - 1]; return photo(id, width, (width * target.layoutHeight) / Math.max(1, target.layoutWidth));}
/** A round avatar crop centered on the face. */export const avatar = (id: string, size = 96) => photo(id, size, size, '&crop=faces');
/* --- stories ------------------------------------------------------------- */
export type StoryUser = { id: string; name: string; avatar: string; color: string; frames: { id: string; caption: string; time: string }[];};
export const storyUsers: StoryUser[] = [ { id: 'maya', name: 'maya.brews', avatar: '1544005313-94ddf0286df2', color: '#c77d4a', frames: [ { id: '1564327368633-151ef1d45021', caption: 'Sunday brunch spot ☕', time: '2h' }, { id: '1516213335993-159eca2981af', caption: 'Latte art level: finally', time: '2h' }, { id: '1528699633788-424224dc89b5', caption: 'Fresh out of the oven', time: '1h' }, ], }, { id: 'leo', name: 'leo.outdoors', avatar: '1627686011747-74adda3d2343', color: '#2f8a7a', frames: [ { id: '1528543606781-2f6e6857f318', caption: 'Bridge run at dawn', time: '5h' }, { id: '1502003148287-a82ef80a6abc', caption: 'Lake day', time: '4h' }, { id: '1520329612326-d6038d1395a1', caption: 'Found the waterfall', time: '3h' }, ], }, { id: 'ana', name: 'ana.nights', avatar: '1558507652-2d9626c4e67a', color: '#3b5bdb', frames: [ { id: '1514439827219-9137a0b99245', caption: 'City lights', time: '8h' }, { id: '1529619768328-e37af76c6fe5', caption: 'Long exposure test', time: '7h' }, { id: '1611416370495-50fac9e1b382', caption: 'Skyline from the roof', time: '6h' }, ], }, { id: 'kai', name: 'kai.moves', avatar: '1498551172505-8ee7ad69f235', color: '#d6336c', frames: [ { id: '1512264815082-9999c2d4894d', caption: 'Freeze', time: '12h' }, { id: '1543893424-907f9ebc6a5d', caption: 'Rehearsal', time: '11h' }, { id: '1511750931603-12cb2c8d28cc', caption: 'Street session', time: '10h' }, ], },];
/* --- vertical feed ------------------------------------------------------- */
export type FeedPost = { id: string; user: string; avatar: string; caption: string; sound: string; likes: number; comments: number; color: string;};
export const feedPosts: FeedPost[] = [ { id: '1592252054006-0570a10f0838', user: 'kai.moves', avatar: '1498551172505-8ee7ad69f235', caption: 'One take, no cuts. Swipe up for the slow-mo 👀 #dance', sound: 'Original sound · kai.moves', likes: 48200, comments: 1312, color: '#3b4a6b', }, { id: '1580895456895-cfdf02e4c23f', user: 'ana.nights', avatar: '1558507652-2d9626c4e67a', caption: 'Rainy walk home through the neon district', sound: 'Midnight drive · lofi mix', likes: 12900, comments: 402, color: '#5a2a4a', }, { id: '1530174883092-c2a7aa3f1cfe', user: 'maya.brews', avatar: '1544005313-94ddf0286df2', caption: 'The perfect espresso in 25 seconds ⏱️ #coffee', sound: 'Morning jazz · cafe set', likes: 30400, comments: 877, color: '#5b3a24', }, { id: '1589739401762-670be815e704', user: 'leo.outdoors', avatar: '1627686011747-74adda3d2343', caption: 'Packing list for a 3-day hike, in the comments', sound: 'Trail mix · acoustic', likes: 8700, comments: 219, color: '#2f3b33', }, { id: '1517328894681-0f5dfabd463c', user: 'ana.nights', avatar: '1558507652-2d9626c4e67a', caption: 'Street lamps and old stories', sound: 'Night city · ambient', likes: 21500, comments: 655, color: '#3a3326', }, { id: '1771345207885-d1d91f4e663f', user: 'leo.outdoors', avatar: '1627686011747-74adda3d2343', caption: 'This bridge is covered in flowers every spring 🌸', sound: 'Spring · piano', likes: 64100, comments: 2048, color: '#6b5a4a', },];
/* --- flash sale ---------------------------------------------------------- */
export type Banner = { id: string; title: string; subtitle: string; color: string };
export const banners: Banner[] = [ { id: '1483985988355-763728e1935b', title: 'Mid-season sale', subtitle: 'Up to 60% off fashion', color: '#b0476c', }, { id: '1520006403909-838d6b92c22e', title: 'New arrivals', subtitle: 'Fresh picks every morning', color: '#7a5c3e', }, { id: '1555529669-e69e7aa0ba9a', title: 'Free shipping', subtitle: 'On orders over $35', color: '#3e6b7a' }, { id: '1758520387283-303b0b332e89', title: 'Weekend deals', subtitle: 'Ends Sunday at midnight', color: '#8a6a2a', },];
export type Deal = { id: string; name: string; price: number; was: number; sold: number; stock: number; color: string;};
export const deals: Deal[] = [ { id: '1542291026-7eec264c27ff', name: 'Runner sneakers', price: 59, was: 119, sold: 412, stock: 480, color: '#c92a2a', }, { id: '1505740420928-5e560c06d30e', name: 'Wireless headphones', price: 79, was: 149, sold: 238, stock: 400, color: '#e8b923', }, { id: '1546868871-7041f2a55e12', name: 'Smart watch', price: 199, was: 279, sold: 96, stock: 200, color: '#495057', }, { id: '1613040809024-b4ef7ba99bc3', name: 'Studio headphones', price: 89, was: 129, sold: 177, stock: 220, color: '#f783ac', }, { id: '1540925819815-17a59df70d64', name: 'Vintage camera', price: 129, was: 189, sold: 51, stock: 120, color: '#7a4a2a', }, { id: '1763987300634-7b0822cbf390', name: 'Rose perfume', price: 39, was: 65, sold: 301, stock: 340, color: '#d6336c', }, { id: '1629198688000-71f23e745b6e', name: 'Skincare set', price: 29, was: 52, sold: 389, stock: 420, color: '#f0c36d', }, { id: '1600185365926-3a2ce3cdb9eb', name: 'Court sneakers', price: 69, was: 99, sold: 140, stock: 300, color: '#e8590c', },];
/* --- shop ---------------------------------------------------------------- */
export type ShopColor = { id: string; name: string; swatch: string; stock: number };
export const shopColors: ShopColor[] = [ { id: '1542291026-7eec264c27ff', name: 'Crimson', swatch: '#c92a2a', stock: 12 }, { id: '1606107557195-0e29a4b5b4aa', name: 'Volt', swatch: '#a9e34b', stock: 4 }, { id: '1491553895911-0055eca6402d', name: 'Graphite', swatch: '#495057', stock: 23 }, { id: '1600185365483-26d7a4cc7519', name: 'Coral', swatch: '#ff8787', stock: 9 }, { id: '1525966222134-fcfa99b8ae77', name: 'Burgundy', swatch: '#862e3c', stock: 2 },];
/* --- memories ------------------------------------------------------------ */
export type Memory = { id: string; place: string; color: string };
export const memories: Memory[] = [ { id: '1506905925346-21bda4d32df4', place: 'Above the clouds', color: '#5b6478' }, { id: '1501785888041-af3ef285b470', place: 'Alpine lake', color: '#2fa7a0' }, { id: '1470071459604-3b5ec3a7fe05', place: 'Misty valley', color: '#6e7a5a' }, { id: '1441974231531-c6227db76b6e', place: 'Forest path', color: '#3e4a2c' }, { id: '1433086966358-54859d0ed716', place: 'The waterfall', color: '#3f6b3a' }, { id: '1507525428034-b723cf961d3e', place: 'Last sunset', color: '#7fc4c8' },];
/* --- albums -------------------------------------------------------------- */
export type Album = { id: string; title: string; artist: string; color: string; length: number };
export const albums: Album[] = [ { id: '1618005198919-d3d4b5a92ead', title: 'Soft Spheres', artist: 'Nova Lane', color: '#7aa2e3', length: 214, }, { id: '1604076913837-52ab5629fba9', title: 'Afterglow', artist: 'The Paper Suns', color: '#e8705a', length: 187, }, { id: '1620641788421-7a1c342ea42e', title: 'Ultraviolet', artist: 'Mira K', color: '#8e44ad', length: 243 }, { id: '1635776062127-d379bfcba9f8', title: 'Night Bloom', artist: 'Halcyon', color: '#5b2c83', length: 199, }, { id: '1669295384050-a1d4357bd1d7', title: 'Tidal', artist: 'Blue Hours', color: '#1c7c7d', length: 226 }, { id: '1604079628040-94301bb21b91', title: 'Ember', artist: 'Juniper & Ash', color: '#e67e22', length: 205, }, { id: '1573655349936-de6bed86f839', title: 'Prism', artist: 'Lumen Club', color: '#2c3e50', length: 238 },];
/** 48200 → "48.2K". */export function compact(n: number) { if (n >= 1e6) return `${(n / 1e6).toFixed(1).replace(/\.0$/, '')}M`; if (n >= 1e3) return `${(n / 1e3).toFixed(1).replace(/\.0$/, '')}K`; return `${n}`;}Xem trên GitHub: app/stories.tsxXem trên GitHub: data/demos.ts