Onboarding
Màn giới thiệu toàn màn hình: media làm nền, Skip và Next điều khiển bằng ref API.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”mediaPosition="background"đặt media phía sau các trang;mediaStylegiữ nó ở nửa trên vàpageStyleđẩy chữ xuống dưới.positionValueđiều khiển màu nền bằnginterpolateColortheo ngón tay.- Next gọi
ref.next(); ở trang cuối thì đóng màn hình.
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 { 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' },});import type { ImageSourcePropType } from 'react-native';
export type Product = { id: string; title: string; description: string; image: ImageSourcePropType; /** Shown as a blob while the image loads, and used for accents. */ color: string;};
export const products: Product[] = [ { id: 'orb', title: 'Meet Orb', description: 'A glossy sphere with a soft highlight. Swipe to watch it melt into the next shape.', image: require('../assets/shapes/orb.png'), color: '#6a4fd8', }, { id: 'tile', title: 'Explore Tile', description: 'A rounded square with a warm gradient, the kind of shape app icons are made of.', image: require('../assets/shapes/tile.png'), color: '#f0a020', }, { id: 'ring', title: 'Discover Ring', description: 'A torus with a hole in the middle. The liquid effect fills it in, then opens it again.', image: require('../assets/shapes/ring.png'), color: '#20a890', }, { id: 'pill', title: 'Experience Pill', description: 'A tall capsule. Tall and wide shapes stretch the blob in between.', image: require('../assets/shapes/pill.png'), color: '#3478f6', }, { id: 'heart', title: 'Love Heart', description: 'Two circles and a point. Watch the point grow out of the blob.', image: require('../assets/shapes/heart.png'), color: '#eb405a', },];
/** * Devices for the product showcase: original drawings (example/scripts/ * make-devices.mjs), cropped to their outline. Dark bodies and glowing * screens of different colors make the liquid morph easy to follow. */export const devices: Product[] = [ { id: 'watch', title: 'Meet Orbit Watch', description: 'A rounded case on a woven band. Swipe and it melts into the next device.', image: require('../assets/devices/watch.png'), color: '#6a4fd8', }, { id: 'phone', title: 'Nova Phone', description: 'Edge-to-edge glass with an ocean wallpaper, tall and narrow.', image: require('../assets/devices/phone.png'), color: '#0b7fc4', }, { id: 'laptop', title: 'Explore Slate Laptop', description: 'A wide screen on a thin base: the blob stretches from tall to wide.', image: require('../assets/devices/laptop.png'), color: '#c2368f', }, { id: 'mini', title: 'Cube Mini', description: 'A small silver box with no screen at all, seen from above.', image: require('../assets/devices/mini.png'), color: '#8f959d', }, { id: 'desktop', title: 'Vista Desktop', description: 'A large display with a sunset on a round foot.', image: require('../assets/devices/desktop.png'), color: '#e0566b', },];
/** * Product photos for the showcase, cut out of their studio background and * cropped to the product. Tall and wide products alternate, so the liquid * blob visibly stretches from one outline to the next. */export const showcase: Product[] = [ { id: 'watch-plum', title: 'Watch Series 12', description: 'A plum sport band and an always-on display. Swipe and it melts into the next product.', image: require('../assets/showcase/watch-plum.png'), color: '#5c1f33', }, { id: 'macbook', title: 'MacBook Pro 14', description: 'From a tall watch to a wide laptop: the blob stretches sideways on the way.', image: require('../assets/showcase/macbook.png'), color: '#2c2c30', }, { id: 'iphone-red', title: 'iPhone 18 Pro', description: 'Tall and narrow again, in a deep red finish with a three-camera plateau.', image: require('../assets/showcase/iphone-red.png'), color: '#6e2334', }, { id: 'mini-front', title: 'Mac mini', description: 'The widest of all: a low silver box, seen from the front.', image: require('../assets/showcase/mini-front.png'), color: '#6b7076', }, { id: 'iphone-white', title: 'iPhone 18 Pro, white', description: 'The back and the side: two outlines the liquid has to join.', image: require('../assets/showcase/iphone-white.png'), color: '#7d7a76', }, { id: 'iphone-duo', title: 'iPhone Duo', description: 'A phone that unfolds into a small laptop, half open.', image: require('../assets/showcase/iphone-duo.png'), color: '#1f2b45', }, { id: 'watch-starlight', title: 'Watch Series 12, starlight', description: 'A light band and a vivid purple face, back to a tall outline.', image: require('../assets/showcase/watch-starlight.png'), color: '#6a3fc8', }, { id: 'mini-top', title: 'Mac mini, from above', description: 'A rounded square to finish: the blob settles into an even shape.', image: require('../assets/showcase/mini-top.png'), color: '#5e6369', },];
/** Media height of the remote screen's carousel; the home screen prefetches for it. */export const REMOTE_MEDIA_HEIGHT = 240;/** Horizontal padding of the remote screen around the carousel. */export const REMOTE_SIDE_MARGIN = 16;
/** Photos are requested at 4:3; the carousel covers its area and crops the rest. */export const heightFor = (width: number) => Math.round((width * 3) / 4);
/** * An Unsplash image URL at a given size. Unsplash serves through imgix, which * resizes on the fly: `w` and `h` in pixels, `fit=crop` fills the box. */export function unsplashUrl(id: string, width: number, height: number) { return `https://images.unsplash.com/photo-${id}?w=${width}&h=${height}&fit=crop&q=80&fm=jpg`;}
/** * Remote photos for the image-loading demo, from Unsplash (free to use under * the Unsplash License, https://unsplash.com/license). `uri` is a fixed 800 px * variant; the remote screen builds the URL per size with `unsplashUrl`. * * Unsplash answers with `Cache-Control: max-age=31536000` (one year), so with * `respectHttpHeaders` on, the cache keeps these for a year. */export const photos = [ { id: '1506905925346-21bda4d32df4', title: 'Sea of clouds', color: '#5b6478' }, { id: '1501785888041-af3ef285b470', title: 'Alpine lake', color: '#2fa7a0' }, { id: '1470071459604-3b5ec3a7fe05', title: 'Misty valley', color: '#6e7a5a' }, { id: '1441974231531-c6227db76b6e', title: 'Forest path', color: '#3e4a2c' }, { id: '1507525428034-b723cf961d3e', title: 'Beach at sunset', color: '#7fc4c8' }, { id: '1500530855697-b586d89ba3ee', title: 'Desert road', color: '#a0553c' }, { id: '1433086966358-54859d0ed716', title: 'Waterfall', color: '#3f6b3a' },].map((p) => ({ ...p, uri: unsplashUrl(p.id, 800, 600),}));Xem trên GitHub: app/onboarding.tsxXem trên GitHub: data/products.ts