Nghe nhạc
App nghe nhạc nền tối: equalizer chuyển động trong nút, playlist, bài hát, radio và nghe cùng bạn bè.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”- Slot
Triggerlà bốn thanh chuyển động khi nút đóng và co lại khiprogresstăng. - Slot chỉ animate nội dung của chính nó; package lo việc nút nở thành thẻ.
surface: 'solid'vớicolors.dark.surfacecho thẻ một tông màu sâu trên mọi nền tảng.
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 item và màn hình là dùng được.
import { Ionicons } from '@expo/vector-icons';import type { MorphItem, MorphTriggerProps } from 'expo-morph-menu';import { useEffect, useState } from 'react';import { Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native';import Animated, { Extrapolation, interpolate, useAnimatedStyle, useSharedValue, withRepeat, withTiming, type SharedValue,} from 'react-native-reanimated';import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { sheetItem, type Scene } from './types';import { BackToDemos, SheetHeader } from './ui';import { column } from '../components/layout';
/** * A music app in dark mode. The Trigger slot is a small equalizer that * keeps moving while the button is closed and shrinks away as it opens * (it reads `progress`, the package animates the size). */
const PINK = '#ff3d77';const BG = '#0b0b10';
function EqBar({ i, beat, progress,}: { i: number; beat: SharedValue<number>; progress: SharedValue<number>;}) { const style = useAnimatedStyle(() => { const h = 6 + Math.abs(Math.sin(beat.value * Math.PI * 2 + i * 1.3)) * 14; return { height: h * interpolate(progress.value, [0, 0.35], [1, 0], Extrapolation.CLAMP) }; }); return <Animated.View style={[styles.eq, style]} />;}
function Trigger({ progress }: MorphTriggerProps) { const beat = useSharedValue(0); useEffect(() => { beat.value = withRepeat(withTiming(1, { duration: 1400 }), -1); }, [beat]); return ( <View style={styles.eqRow}> {[0, 1, 2, 3].map((i) => ( <EqBar key={i} i={i} beat={beat} progress={progress} /> ))} </View> );}
const ion = (name: keyof typeof Ionicons.glyphMap) => function Icon({ size, color }: { size: number; color: string }) { return <Ionicons name={name} size={size} color={color} />; };
const items: MorphItem[] = [ sheetItem('playlist', 'New playlist', { tint: PINK, icon: ion('albums') }), sheetItem('songs', 'Add songs', { tint: '#7c5cff', icon: ion('musical-notes') }), sheetItem('radio', 'Start a radio', { tint: '#1fb6a6', icon: ion('radio') }), sheetItem('live', 'Listen together', { tint: '#ff9f1a', icon: ion('headset') }),];
const ALBUMS = [ { title: 'Night Drive', artist: 'Halcyon', color: '#5b2c83' }, { title: 'Afterglow', artist: 'Paper Suns', color: '#e8705a' }, { title: 'Tidal', artist: 'Blue Hours', color: '#1c7c7d' }, { title: 'Ember', artist: 'Juniper & Ash', color: '#e67e22' },];const SONGS = [ 'Neon Tides', 'Paper Planes', 'Glass House', 'Slow Motion', 'Northern Lights', 'Golden Hour', 'Static',];
function Home() { const insets = useSafeAreaInsets(); return ( <ScrollView style={{ backgroundColor: BG }} contentContainerStyle={[ column, styles.home, { paddingTop: insets.top + 10, paddingBottom: insets.bottom + 120 }, ]} > <BackToDemos dark /> <Text style={styles.h1}>Listen now</Text> <ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerStyle={{ gap: 14 }}> {ALBUMS.map((a) => ( <View key={a.title} style={{ width: 150, gap: 6 }}> <View style={[styles.cover, { backgroundColor: a.color }]}> <Ionicons name="musical-note" size={40} color="#ffffff66" /> </View> <Text style={styles.albumTitle}>{a.title}</Text> <Text style={styles.albumArtist}>{a.artist}</Text> </View> ))} </ScrollView> <Text style={styles.section}>Recently played</Text> {SONGS.slice(0, 5).map((s, i) => ( <View key={s} style={styles.song}> <View style={[styles.songCover, { backgroundColor: ALBUMS[i % 4].color }]} /> <View style={{ flex: 1 }}> <Text style={styles.songTitle}>{s}</Text> <Text style={styles.albumArtist}>{ALBUMS[i % 4].artist}</Text> </View> <Ionicons name="ellipsis-horizontal" size={18} color="#8a8a96" /> </View> ))} <Text style={styles.hint}>The equalizer in the button is a Trigger slot.</Text> </ScrollView> );}
const COVERS = ['#ff3d77', '#7c5cff', '#1fb6a6', '#ff9f1a', '#2f6bff', '#e0559b'];
function Playlist() { const [color, setColor] = useState(0); return ( <View style={styles.sheet}> <SheetHeader title="New playlist" color="#fff" right={<Text style={styles.action}>Create</Text>} /> <View style={styles.center}> <View style={[styles.bigCover, { backgroundColor: COVERS[color] }]}> <Ionicons name="albums" size={64} color="#ffffffaa" /> </View> <TextInput style={styles.title} defaultValue="Sunday morning" placeholderTextColor="#666" /> <View style={styles.swatches}> {COVERS.map((c, i) => ( <Pressable key={c} onPress={() => setColor(i)} style={[styles.swatch, { backgroundColor: c }, i === color && styles.swatchOn]} accessibilityLabel={`Cover color ${i + 1}`} /> ))} </View> </View> </View> );}
function Songs() { const [added, setAdded] = useState<string[]>(['Glass House']); return ( <View style={styles.sheet}> <SheetHeader title="Add songs" color="#fff" right={<Text style={styles.action}>{added.length}</Text>} /> <ScrollView contentContainerStyle={[column, styles.home]}> {SONGS.map((s, i) => { const on = added.includes(s); return ( <Pressable key={s} style={styles.song} onPress={() => setAdded((a) => (on ? a.filter((x) => x !== s) : [...a, s]))} > <View style={[styles.songCover, { backgroundColor: COVERS[i % COVERS.length] }]} /> <View style={{ flex: 1 }}> <Text style={styles.songTitle}>{s}</Text> <Text style={styles.albumArtist}>{ALBUMS[i % 4].artist}</Text> </View> <Ionicons name={on ? 'checkmark-circle' : 'add-circle-outline'} size={26} color={on ? PINK : '#8a8a96'} /> </Pressable> ); })} </ScrollView> </View> );}
function Radio() { const genres = ['Lo-fi', 'Indie', 'Jazz', 'Electronic', 'Hip hop', 'Classical', 'Ambient', 'Rock']; const [pick, setPick] = useState(0); return ( <View style={styles.sheet}> <SheetHeader title="Start a radio" color="#fff" /> <View style={[styles.home, styles.genres]}> {genres.map((g, i) => ( <Pressable key={g} onPress={() => setPick(i)} style={[ styles.genre, { backgroundColor: COVERS[i % COVERS.length] }, i === pick && styles.genreOn, ]} > <Text style={styles.genreText}>{g}</Text> </Pressable> ))} </View> <Text style={[styles.hint, { marginTop: 18 }]}> {genres[pick]} radio: endless songs picked for you. </Text> </View> );}
function Live() { const [seconds, setSeconds] = useState(0); useEffect(() => { const t = setInterval(() => setSeconds((s) => s + 1), 1000); return () => clearInterval(t); }, []); return ( <View style={styles.sheet}> <SheetHeader title="Listen together" color="#fff" /> <View style={styles.center}> <View style={styles.liveBadge}> <View style={styles.liveDot} /> <Text style={styles.liveText}> LIVE · {Math.floor(seconds / 60)}:{String(seconds % 60).padStart(2, '0')} </Text> </View> <View style={[styles.bigCover, { backgroundColor: '#ff9f1a' }]}> <Ionicons name="headset" size={64} color="#ffffffaa" /> </View> <Text style={styles.songTitle}>3 friends are listening with you</Text> <Text style={styles.albumArtist}>Now: Golden Hour · Paper Suns</Text> </View> </View> );}
function Screen({ itemKey }: { itemKey: string }) { if (itemKey === 'songs') return <Songs />; if (itemKey === 'radio') return <Radio />; if (itemKey === 'live') return <Live />; return <Playlist />;}
export const music: Scene = { id: 'music', title: 'Music', subtitle: 'Dark player: an equalizer in the button, from a Trigger slot', accent: PINK, items, config: { colorScheme: 'dark', surface: 'solid', radius: { card: 24 }, colors: { dark: { surface: '#1a1a24', sheetBackground: BG, highlight: 'rgba(255,61,119,0.16)' } }, }, components: { Trigger }, Home, Screen,};
const styles = StyleSheet.create({ home: { paddingHorizontal: 16, gap: 12 }, h1: { fontSize: 32, fontWeight: '800', color: '#fff' }, cover: { width: 150, height: 150, borderRadius: 14, alignItems: 'center', justifyContent: 'center' }, albumTitle: { color: '#fff', fontSize: 15, fontWeight: '700' }, albumArtist: { color: '#8a8a96', fontSize: 13 }, section: { color: '#fff', fontSize: 20, fontWeight: '800', marginTop: 8 }, song: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingVertical: 4 }, songCover: { width: 48, height: 48, borderRadius: 8 }, songTitle: { color: '#fff', fontSize: 15, fontWeight: '600' }, hint: { color: '#8a8a96', fontSize: 13, textAlign: 'center' }, eqRow: { flexDirection: 'row', alignItems: 'flex-end', gap: 3, height: 22 }, eq: { width: 4, borderRadius: 2, backgroundColor: PINK }, sheet: { flex: 1, backgroundColor: BG }, center: { flex: 1, alignItems: 'center', justifyContent: 'center', gap: 18, paddingHorizontal: 24 }, bigCover: { width: 200, height: 200, borderRadius: 20, alignItems: 'center', justifyContent: 'center' }, title: { color: '#fff', fontSize: 26, fontWeight: '800', textAlign: 'center' }, swatches: { flexDirection: 'row', gap: 12 }, swatch: { width: 34, height: 34, borderRadius: 17 }, swatchOn: { borderWidth: 3, borderColor: '#fff' }, action: { color: PINK, fontSize: 16, fontWeight: '700' }, genres: { flexDirection: 'row', flexWrap: 'wrap', gap: 12, marginTop: 10 }, genre: { width: '47%', flexGrow: 1, height: 84, borderRadius: 14, padding: 12, justifyContent: 'flex-end' }, genreOn: { borderWidth: 3, borderColor: '#fff' }, genreText: { color: '#fff', fontSize: 18, fontWeight: '800' }, liveBadge: { flexDirection: 'row', alignItems: 'center', gap: 8, backgroundColor: '#ff3b30', borderRadius: 14, paddingHorizontal: 12, paddingVertical: 6, }, liveDot: { width: 8, height: 8, borderRadius: 4, backgroundColor: '#fff' }, liveText: { color: '#fff', fontWeight: '800', fontSize: 13, fontVariant: ['tabular-nums'] },});import type { ComponentType } from 'react';import type { MorphComponents, MorphConfigInput, MorphIcon, MorphItem } from 'expo-morph-menu';
/** * A demo app built on the same two routes as the basic example: the home * screen (`app/index.tsx`) and the sheet (`app/(sheet)/[key].tsx`). Each * scene brings its own items, config, slots and screens, so it shows how far * one `MorphProvider` can be taken. */export type Scene = { id: string; title: string; /** One line for the list of demos. */ subtitle: string; /** Accent color of the list entry. */ accent: string; items: MorphItem[]; config?: MorphConfigInput; components?: MorphComponents; itemHeight?: (item: MorphItem, index: number) => number; fabIcon?: MorphIcon; /** The home screen of the demo app. */ Home: ComponentType; /** The full-screen route of a row (`itemKey` is the row's key). */ Screen: ComponentType<{ itemKey: string }>;};
/** A row that opens `/(sheet)/[key]` with its key. */export function sheetItem( key: string, label: string, rest: Omit<MorphItem, 'key' | 'label' | 'href'> = {},): MorphItem { return { key, label, href: { pathname: '/(sheet)/[key]', params: { key } }, ...rest };}import { Ionicons } from '@expo/vector-icons';import { MorphCloseButton } from 'expo-morph-menu';import type { ReactNode } from 'react';import { Pressable, StyleSheet, Text, View } from 'react-native';import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useDemo } from '../context/demo';
/** Header of a sheet screen: close button, title, and an optional action on the right. */export function SheetHeader({ title, color = '#1c1c1e', right,}: { title: string; color?: string; right?: ReactNode;}) { const insets = useSafeAreaInsets(); return ( <View style={[styles.header, { paddingTop: insets.top + 6 }]}> <MorphCloseButton accessibilityLabel="Close" /> <Text style={[styles.title, { color }]} numberOfLines={1}> {title} </Text> <View style={styles.right}>{right}</View> </View> );}
/** Pill at the top of a demo's home screen that goes back to the list of demos. */export function BackToDemos({ dark = false }: { dark?: boolean }) { const { setScene } = useDemo(); return ( <Pressable onPress={() => setScene('basic')} style={[styles.back, { backgroundColor: dark ? 'rgba(255,255,255,0.12)' : 'rgba(0,0,0,0.06)' }]} accessibilityRole="button" accessibilityLabel="All demos" hitSlop={8} > <Ionicons name="chevron-back" size={14} color={dark ? '#fff' : '#1c1c1e'} /> <Text style={[styles.backText, { color: dark ? '#fff' : '#1c1c1e' }]}>Demos</Text> </Pressable> );}
/** A colored circle with initials, for people in the demos. */export function Initials({ name, color, size = 40 }: { name: string; color: string; size?: number }) { const letters = name .split(' ') .map((w) => w[0]) .join('') .slice(0, 2) .toUpperCase(); return ( <View style={{ width: size, height: size, borderRadius: size / 2, backgroundColor: color, alignItems: 'center', justifyContent: 'center', }} > <Text style={{ color: '#fff', fontWeight: '700', fontSize: size * 0.38 }}>{letters}</Text> </View> );}
const styles = StyleSheet.create({ header: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16, paddingBottom: 10 }, title: { flex: 1, fontSize: 17, fontWeight: '700', textAlign: 'center' }, right: { minWidth: 32, alignItems: 'flex-end' }, back: { alignSelf: 'flex-start', flexDirection: 'row', alignItems: 'center', gap: 2, paddingLeft: 8, paddingRight: 12, paddingVertical: 6, borderRadius: 14, }, backText: { fontSize: 13, fontWeight: '600' },});Xem trên GitHub: example/scenes/music.tsxXem trên GitHub: example/scenes/types.tsXem trên GitHub: example/scenes/ui.tsx