Bố cục trang
Chọn hiệu ứng khác bên dưới carousel trong demo: mọi bố cục trong bảng bên dưới đều chạy ở đó.
Không có getImage thì carousel là carousel thẻ thuần: renderItem là cả tấm thẻ và pageEffect quyết định thẻ chuyển động thế nào. Có getImage thì page effect áp cho phần nội dung bên dưới vùng media.
Hai loại hiệu ứng độc lập và kết hợp được với nhau: effect (mặc định liquid) morph ảnh trong vùng media, còn pageEffect di chuyển các trang. Liquid vẫn là mặc định; page effect được thêm vào bên cạnh chứ không thay thế nó. Màn Layouts trong app ví dụ cho thấy ảnh liquid phía trên và các thẻ chuyển động theo từng page effect bên dưới.
import { MorphCarousel, pageEffects } from 'expo-morph-carousel';
<MorphCarousel data={cards} renderItem={({ item }) => <Card item={item} />} pageEffect="coverflow" // hoặc pageEffects.coverflow({ angle: 60, spacing: 0.5 }) pageSize={280} // hẹp hơn carousel: lộ các trang kế bên hai bên pageGap={12} style={{ height: 380 }}/>| Hiệu ứng | Trông thế nào | Tuỳ chọn |
|---|---|---|
slide |
Các trang nằm cạnh nhau | |
parallax |
Trang hiện tại đủ cỡ, trang kế bên nhỏ hơn và gần hơn. Dùng với pageSize |
scale 0.84, offset 0, opacity 1 |
stack |
Một chồng bài: thẻ sau nằm chờ phía sau, thẻ hiện tại trượt ra | visible 3, spacing 16, scaleStep 0.06, opacityStep 0.18, rotation 0, stackAxis 'across' |
coverflow |
Trang kế bên xoay 3D và chồng lên nhau | angle 48, scale 0.82, spacing 0.62 |
cube |
Một khối lập phương đặc đang xoay: các mặt kề nhau chung cạnh, cả khối được nhìn qua cùng một phối cảnh. Trang nên phủ kín khung (không margin, không bo góc, không pageGap); cạnh gần mắt nhô về phía mắt và to ra (khoảng 6% với phối cảnh mặc định), nên cần chừa chỗ: trang hẹp hơn carousel, và một khoảng lề theo chiều vuông góc với hướng cuộn bên trong trang. Chính xác trên iOS, Android và web: trên Android transform được dựng sao cho khi tách ra (dịch, xoay, scale, khoảng cách camera) vẫn vẽ đúng phép chiếu đó |
perspective 2,5 × cạnh dài hơn của trang |
flip |
Thẻ lật tại chỗ; trang sau là mặt lưng | |
fade |
Crossfade tại chỗ | |
scale-fade |
Trang cũ co lại và mờ dần, trang mới từ lớn hơn một chút thu về | scale 0.86 |
rotate |
Các trang đung đưa theo cung tròn | angle 14, drop 24 |
tinder |
Một chồng thẻ; thẻ trên cùng bị ném ra kèm nghiêng | visible 2, angle 18 |
Mọi hiệu ứng chạy được cả ngang lẫn dọc và tự lật chiều trong bố cục phải sang trái. Khi bật Reduce Motion, các hiệu ứng theo tên chuyển về slide.
Bố cục phải sang trái theo I18nManager.isRTL: chiều tiến là sang trái, vuốt sang phải để tiến, chấm phân trang và vùng chạm được lật lại. Trong app Expo, bố cục RTL chỉ được bật khi có "extra": { "supportsRTL": true } trong app.json.
Hiệu ứng tự viết là một worklet nhận offset của trang (0 đang hiện, 1 trang sau, -1 trang trước, số lẻ khi đang chuyển), step (khoảng cách giữa hai trang), kích thước trang và viewport, direction, rtl và platform (Platform.OS: Android vẽ transform 3D từ bản đã tách thành phần, nên hiệu ứng có perspective có thể cần thứ tự khác trên Android), rồi trả về opacity, zIndex, transform và transformOrigin. Phải tự tính cả phần dịch chuyển:
const door: PageEffectFn = ({ offset, step }) => { 'worklet'; const o = Math.max(-1, Math.min(1, offset)); return { zIndex: Math.round(100 - Math.abs(offset) * 10), transformOrigin: o > 0 ? 'left center' : 'right center', transform: [{ perspective: 800 }, { translateX: offset * step }, { rotateY: `${o * -60}deg` }], };};renderItem cũng nhận offset dạng shared value, để animate nội dung bên trong thẻ tách biệt với thẻ:
renderItem={({ item, offset }) => <Card item={item} offset={offset} />}
// trong Cardconst title = useAnimatedStyle(() => ({ transform: [{ translateX: interpolate(offset.value, [-1, 0, 1], [-60, 0, 60]) }],}));Mã nguồn của demo
Phần tiêu đề “Mã nguồn của demo”import { MorphCarousel, pageEffects, type AnimationConfig, type PageEffect, type PageEffectFn, type SnapMode,} from 'expo-morph-carousel';import { useLocalSearchParams } from 'expo-router';import { useMemo, useState } from 'react';import { I18nManager, Image, Pressable, ScrollView, StyleSheet, Switch, Text, View } from 'react-native';import Animated, { interpolate, useAnimatedStyle, type SharedValue } from 'react-native-reanimated';
import { products, type Product } from '../data/products';import { column, useContentWidth } from '../data/layout';
/** * Page effects with the liquid images. With "Liquid images" on (the default), * the product shots melt into each other in the media area while the cards * underneath move with the chosen page effect: both effects at once. Off, the * carousel is a plain card carousel and the image is part of each card. * Every built-in page effect, a custom one, snap modes, spring or timing, a * peek at the neighbors, vertical. */
/** A custom page effect: cards swing like a door hinged on their leading edge. */const door: PageEffectFn = ({ offset, step }) => { 'worklet'; const o = Math.max(-1, Math.min(1, offset)); return { opacity: 1 - Math.abs(o) * 0.6, zIndex: Math.round(100 - Math.abs(offset) * 10), transformOrigin: o > 0 ? 'left center' : 'right center', transform: [{ perspective: 800 }, { translateX: offset * step }, { rotateY: `${o * -60}deg` }], };};
const EFFECTS: { label: string; value: PageEffect; peek?: boolean }[] = [ { label: 'slide', value: 'slide' }, { label: 'parallax', value: 'parallax', peek: true }, { label: 'stack', value: pageEffects.stack({ visible: 3, rotation: 8 }) }, { label: 'coverflow', value: 'coverflow', peek: true }, { label: 'cube', value: 'cube' }, { label: 'flip', value: 'flip' }, { label: 'fade', value: 'fade' }, { label: 'scale-fade', value: 'scale-fade' }, { label: 'rotate', value: 'rotate', peek: true }, { label: 'tinder', value: 'tinder' }, { label: 'custom door', value: door },];
const SNAPS: SnapMode[] = ['page', 'momentum', 'free'];const ANIMATIONS: { label: string; value: AnimationConfig }[] = [ { label: 'spring', value: { type: 'spring' } }, { label: 'bouncy', value: { type: 'spring', damping: 12, stiffness: 160 } }, { label: 'timing 300 ms', value: { type: 'timing', duration: 300 } },];
export default function LayoutsScreen() { const width = useContentWidth(); // `/layouts?effect=cube&autoplay=1` opens on an effect and plays it (handy // for recordings and devices that block injected taps). const params = useLocalSearchParams<{ effect?: string; autoplay?: string }>(); const [effect, setEffect] = useState(() => Math.max( EFFECTS.findIndex((e) => e.label === params.effect), 0, ), ); const [snap, setSnap] = useState<SnapMode>('page'); const [anim, setAnim] = useState(0); const [loop, setLoop] = useState(true); const [vertical, setVertical] = useState(false); const [peekOverride, setPeekOverride] = useState<boolean | null>(null); const [page, setPage] = useState(0); const [moving, setMoving] = useState(false); const [rests, setRests] = useState(0); const [liquid, setLiquid] = useState(true); const [tap, setTap] = useState(true); const peek = peekOverride ?? !!EFFECTS[effect].peek; // A cube is drawn bigger than its page while it turns (the near edge comes // toward the eye), so it gets room around it and edge-to-edge faces. const isCube = EFFECTS[effect].value === 'cube';
const viewport = width - 32; const animation = ANIMATIONS[anim].value; const pageSize = peek ? Math.round((vertical ? 340 : viewport) * 0.72) : isCube ? Math.round((vertical ? 300 : viewport) * 0.78) : undefined; const data = useMemo(() => [...products, ...products.map((p) => ({ ...p, id: `${p.id}-2` }))], []);
return ( <ScrollView contentInsetAdjustmentBehavior="automatic" contentContainerStyle={[styles.screen, column]}> <MorphCarousel key={`${vertical ? 'v' : 'h'}-${liquid ? 'liquid' : 'cards'}`} testID="layouts-carousel" data={data} keyExtractor={(p) => p.id} getAccessibilityLabel={(p) => p.title} accessibilityLabel="Cards" pageEffect={EFFECTS[effect].value} pageSize={pageSize} pageGap={peek ? 12 : 0} snapMode={snap} // Tap the left 30% to go back, anywhere else to go forward; a tap on a // peeking neighbor goes to it. tapToNavigate={tap} animation={animation} loop={loop} autoplay={params.autoplay ? { interval: 1800, whenScreenReaderEnabled: true } : undefined} direction={vertical ? 'vertical' : 'horizontal'} // The liquid morph of the images, above the cards. getImage={liquid ? (p) => p.image : undefined} effect="liquid" mediaHeight={220} mediaPadding={24} getPlaceholderColor={(p) => p.color} style={liquid ? styles.carouselWithMedia : styles.carousel} onIndexChange={setPage} onScrollStart={() => setMoving(true)} onScrollEnd={(i) => { setMoving(false); setPage(i); setRests((n) => n + 1); }} renderItem={({ item, offset }) => ( <Card item={item} offset={offset} showImage={!liquid} face={isCube ? (vertical ? 'vertical' : 'horizontal') : undefined} /> )} />
<Text style={styles.status} testID="layouts-status"> Page {page + 1} / {data.length} · {moving ? 'moving' : 'at rest'} · onScrollEnd ×{rests} ·{' '} {I18nManager.isRTL ? 'RTL' : 'LTR'} </Text>
<Label>Page effect</Label> <Chips labels={EFFECTS.map((e) => e.label)} selected={effect} onSelect={(i) => { setEffect(i); setPeekOverride(null); }} /> <Label>Snap mode</Label> <Chips labels={SNAPS} selected={SNAPS.indexOf(snap)} onSelect={(i) => setSnap(SNAPS[i])} /> <Label>Animation</Label> <Chips labels={ANIMATIONS.map((a) => a.label)} selected={anim} onSelect={setAnim} />
<View style={styles.toggles}> <Toggle label="Liquid images (media above the cards)" value={liquid} onChange={setLiquid} /> <Toggle label="Peek at neighbors (pageSize 72%)" value={peek} onChange={setPeekOverride} /> <Toggle label="Tap to navigate (left 30% back, rest forward)" value={tap} onChange={setTap} /> <Toggle label="Loop" value={loop} onChange={setLoop} /> <Toggle label="Vertical" value={vertical} onChange={setVertical} /> </View> </ScrollView> );}
/** * The card. Its title slides at half speed using the page's `offset`. With the * liquid media on, the image lives in the media area instead of the card. */function Card({ item, offset, showImage, face,}: { item: Product; offset: SharedValue<number>; showImage: boolean; /** A face of the cube: fills the page along the axis it turns on. */ face?: 'horizontal' | 'vertical';}) { // A face turning away from the light gets darker. const shade = useAnimatedStyle(() => ({ opacity: Math.min(Math.abs(offset.value), 1) * 0.45 })); const title = useAnimatedStyle(() => ({ transform: [{ translateX: interpolate(offset.value, [-1, 0, 1], [-60, 0, 60]) }], opacity: interpolate(Math.abs(offset.value), [0, 0.8], [1, 0.3]), })); return ( <View style={[ styles.card, { backgroundColor: item.color }, face === 'horizontal' && styles.faceHorizontal, face === 'vertical' && styles.faceVertical, ]} > {showImage && <Image source={item.image} style={styles.cardImage} resizeMode="contain" />} <Animated.Text style={[styles.cardTitle, title]}>{item.title}</Animated.Text> {!showImage && <Text style={styles.cardText}>{item.description}</Text>} {face && ( <Animated.View style={[StyleSheet.absoluteFill, styles.faceShade, shade]} pointerEvents="none" /> )} </View> );}
function Label({ children }: { children: React.ReactNode }) { return <Text style={styles.label}>{children}</Text>;}
function Chips({ labels, selected, onSelect,}: { labels: string[]; selected: number; onSelect: (i: number) => void;}) { return ( <View style={styles.chips}> {labels.map((l, i) => ( <Pressable key={l} onPress={() => onSelect(i)} style={[styles.chip, i === selected && styles.chipOn]} testID={`chip-${l}`} > <Text style={[styles.chipText, i === selected && styles.chipTextOn]}>{l}</Text> </Pressable> ))} </View> );}
function Toggle({ label, value, onChange,}: { label: string; value: boolean; onChange: (v: boolean) => void;}) { return ( <View style={styles.toggle}> <Text style={styles.toggleText}>{label}</Text> <Switch value={value} onValueChange={onChange} accessibilityLabel={label} /> </View> );}
const styles = StyleSheet.create({ screen: { padding: 16, gap: 10 }, carousel: { height: 380 }, carouselWithMedia: { height: 500, borderRadius: 26, backgroundColor: '#fff' }, cardText: { fontSize: 14, lineHeight: 19, color: '#ffffffd9', textAlign: 'center', paddingHorizontal: 18 }, card: { flex: 1, margin: 8, borderRadius: 26, alignItems: 'center', justifyContent: 'center', gap: 16, shadowColor: '#000', shadowOpacity: 0.18, shadowRadius: 12, shadowOffset: { width: 0, height: 6 }, elevation: 6, }, cardImage: { width: '62%', height: '52%' }, // Cube faces meet at their edges: no margin along the scroll axis, no // corners, no shadow (a shadow on a view turned in 3D is drawn flat on // Android). Room across the axis: the near edge comes toward the eye and // grows by up to ~6%, and the carousel clips its pages. faceHorizontal: { marginHorizontal: 0, marginVertical: 20, borderRadius: 0, elevation: 0, shadowOpacity: 0, }, faceVertical: { marginVertical: 0, marginHorizontal: 20, borderRadius: 0, elevation: 0, shadowOpacity: 0 }, faceShade: { backgroundColor: '#000' }, cardTitle: { fontSize: 24, fontWeight: '800', color: '#fff' }, label: { marginTop: 6, fontSize: 13, color: '#6e6e73' }, status: { fontSize: 13, fontWeight: '600', color: '#1c1c1e', textAlign: 'center' }, chips: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 }, chip: { paddingHorizontal: 12, paddingVertical: 8, borderRadius: 999, backgroundColor: '#fff' }, chipOn: { backgroundColor: '#5b4bd6' }, chipText: { fontSize: 14, color: '#1c1c1e' }, chipTextOn: { color: '#fff', fontWeight: '600' }, toggles: { marginTop: 6, borderRadius: 16, backgroundColor: '#fff', paddingHorizontal: 14 }, toggle: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingVertical: 8 }, toggleText: { fontSize: 15, color: '#1c1c1e', flexShrink: 1 },});