Nhắn tin
Danh sách chat với menu soạn tin: các dòng có tiêu đề và phụ đề, màn bắt đầu chat, tạo nhóm hoặc kênh.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”- Slot
Itemvẽ ô icon, nhãn vàitem.data.subtitle; Highlight mặc định vẫn đánh dấu dòng dưới ngón tay. itemHeighttrả về 60 cho dòng có phụ đề: chiều cao biết trước, các dòng không bao giờ được đo.fabIconthay dấu + bằng icon soạn tin, tô bằngcolors.fabIcon.- Tin nhắn mới lọc danh bạ khi gõ; Nhóm mới chọn thành viên; Kênh mới có công tắc công khai.
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 { renderIcon, type MorphItem, type MorphItemProps } from 'expo-morph-menu';import { useState } from 'react';import { Pressable, ScrollView, StyleSheet, Switch, Text, TextInput, View } from 'react-native';import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { sheetItem, type Scene } from './types';import { BackToDemos, Initials, SheetHeader } from './ui';import { column } from '../components/layout';
/** * A messaging app. The rows have a title and a subtitle: an `Item` slot draws * them, and `itemHeight` gives the taller rows their height in advance. The * built-in Highlight still marks the row under the finger. */
const BLUE = '#2f6bff';
function Item({ item, colors }: MorphItemProps) { const subtitle = item.data?.subtitle as string | undefined; return ( <View style={styles.item}> <View style={[styles.itemIcon, { backgroundColor: item.tint }]}> {renderIcon(item.icon, { size: 17, color: colors.iconForeground })} </View> <View style={styles.flex}> <Text style={[styles.itemTitle, { color: colors.text }]} numberOfLines={1}> {item.label} </Text> {subtitle ? ( <Text style={styles.itemSubtitle} numberOfLines={1}> {subtitle} </Text> ) : null} </View> </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('message', 'New message', { tint: BLUE, icon: ion('create'), data: { subtitle: 'Write to someone' }, }), sheetItem('group', 'New group', { tint: '#22b07d', icon: ion('people'), data: { subtitle: 'Up to 200 people' }, }), sheetItem('channel', 'New channel', { tint: '#ff8a3d', icon: ion('megaphone'), data: { subtitle: 'Broadcast to many' }, }), sheetItem('invite', 'Invite friends', { tint: '#b05cff', icon: ion('gift'), data: { subtitle: 'Share a link' }, }),];
const CHATS = [ { name: 'Design team', color: '#7c5cff', last: 'Mia: the new icons are in Figma', time: '9:41', unread: 3 }, { name: 'Leo Park', color: '#ff7a59', last: 'See you at 7?', time: '9:12', unread: 1 }, { name: 'Ana Silva', color: '#1fb6a6', last: 'Photo', time: 'Yesterday', unread: 0 }, { name: 'Weekend trip', color: '#e0559b', last: 'Kai: I booked the cabin', time: 'Yesterday', unread: 0 }, { name: 'Mom', color: '#f5a623', last: 'Call me when you can', time: 'Mon', unread: 0 }, { name: 'Run club', color: '#22b07d', last: 'Tomorrow 6 am, river loop', time: 'Sun', unread: 0 },];const CONTACTS = [ 'Ana Silva', 'Ben Cole', 'Chloe Kim', 'Dev Patel', 'Emma Ross', 'Kai Ito', 'Leo Park', 'Mia Chen',];const COLORS = ['#1fb6a6', '#5b8def', '#e0559b', '#f5a623', '#7c5cff', '#22b07d', '#ff7a59', '#b05cff'];
function Home() { const insets = useSafeAreaInsets(); return ( <ScrollView style={styles.page} contentContainerStyle={[column, { paddingTop: insets.top + 10, paddingBottom: insets.bottom + 120 }]} > <View style={styles.pad}> <BackToDemos /> <Text style={styles.h1}>Chats</Text> <View style={styles.search}> <Ionicons name="search" size={16} color="#8e8e93" /> <Text style={styles.searchText}>Search</Text> </View> </View> {CHATS.map((c) => ( <View key={c.name} style={styles.chat}> <Initials name={c.name} color={c.color} size={50} /> <View style={styles.flex}> <View style={styles.chatTop}> <Text style={styles.chatName}>{c.name}</Text> <Text style={styles.chatTime}>{c.time}</Text> </View> <View style={styles.chatTop}> <Text style={styles.chatLast} numberOfLines={1}> {c.last} </Text> {c.unread ? ( <View style={styles.unread}> <Text style={styles.unreadText}>{c.unread}</Text> </View> ) : null} </View> </View> </View> ))} </ScrollView> );}
function Message() { const [q, setQ] = useState(''); const list = CONTACTS.filter((c) => c.toLowerCase().includes(q.toLowerCase())); return ( <View style={styles.page}> <SheetHeader title="New message" /> <View style={styles.pad}> <View style={styles.search}> <Ionicons name="search" size={16} color="#8e8e93" /> <TextInput value={q} onChangeText={setQ} placeholder="To: name or number" style={styles.input} placeholderTextColor="#8e8e93" /> </View> </View> <ScrollView> {list.map((c) => ( <View key={c} style={styles.contact}> <Initials name={c} color={COLORS[CONTACTS.indexOf(c)]} /> <Text style={styles.contactName}>{c}</Text> </View> ))} </ScrollView> </View> );}
function Group() { const [picked, setPicked] = useState<string[]>(['Mia Chen', 'Leo Park']); const toggle = (c: string) => setPicked((p) => (p.includes(c) ? p.filter((x) => x !== c) : [...p, c])); return ( <View style={styles.page}> <SheetHeader title="New group" right={<Text style={styles.action}>Create</Text>} /> <View style={styles.pad}> <TextInput placeholder="Group name" defaultValue="Weekend crew" style={styles.field} /> <Text style={styles.label}>{picked.length} members</Text> </View> <ScrollView> {CONTACTS.map((c, i) => { const on = picked.includes(c); return ( <Pressable key={c} style={styles.contact} onPress={() => toggle(c)} accessibilityState={{ checked: on }} > <Initials name={c} color={COLORS[i]} /> <Text style={styles.contactName}>{c}</Text> <Ionicons name={on ? 'checkmark-circle' : 'ellipse-outline'} size={24} color={on ? '#22b07d' : '#c7c7cc'} /> </Pressable> ); })} </ScrollView> </View> );}
function Channel() { const [pub, setPub] = useState(true); return ( <View style={styles.page}> <SheetHeader title="New channel" right={<Text style={styles.action}>Next</Text>} /> <View style={[styles.pad, { gap: 12 }]}> <View style={styles.channelIcon}> <Ionicons name="megaphone" size={34} color="#fff" /> </View> <TextInput placeholder="Channel name" defaultValue="Product updates" style={styles.field} /> <TextInput placeholder="Description" style={[styles.field, { height: 90 }]} multiline defaultValue="Release notes and sneak peeks, every Friday." /> <View style={styles.switchRow}> <View style={styles.flex}> <Text style={styles.contactName}>Public channel</Text> <Text style={styles.chatLast}>Anyone can find it and join</Text> </View> <Switch value={pub} onValueChange={setPub} /> </View> </View> </View> );}
function Invite() { return ( <View style={styles.page}> <SheetHeader title="Invite friends" /> <View style={[styles.pad, { alignItems: 'center', gap: 14, marginTop: 30 }]}> <View style={[styles.channelIcon, { backgroundColor: '#b05cff' }]}> <Ionicons name="gift" size={34} color="#fff" /> </View> <Text style={styles.h2}>Chat is better together</Text> <Text style={[styles.chatLast, { textAlign: 'center' }]}> Friends who join with your link get a month of extra storage, and so do you. </Text> <View style={styles.link}> <Text style={styles.linkText}>chat.example/invite/alex</Text> <Ionicons name="copy-outline" size={18} color={BLUE} /> </View> </View> </View> );}
function Screen({ itemKey }: { itemKey: string }) { if (itemKey === 'group') return <Group />; if (itemKey === 'channel') return <Channel />; if (itemKey === 'invite') return <Invite />; return <Message />;}
export const chat: Scene = { id: 'chat', title: 'Messaging', subtitle: 'Chat list with a compose menu: rows with subtitles from an Item slot', accent: BLUE, items, config: { radius: { card: 22 }, layout: { cardWidth: 264 }, colors: { light: { highlight: '#eef3ff', fabIcon: BLUE } }, }, components: { Item }, itemHeight: (item) => (item.data?.subtitle ? 60 : 46), fabIcon: ({ size, color }) => <Ionicons name="create-outline" size={size - 2} color={color} />, Home, Screen,};
const styles = StyleSheet.create({ flex: { flex: 1 }, page: { flex: 1, backgroundColor: '#fff' }, pad: { paddingHorizontal: 16, gap: 10 }, h1: { fontSize: 32, fontWeight: '800', color: '#1c1c1e' }, h2: { fontSize: 22, fontWeight: '800', color: '#1c1c1e' }, search: { flexDirection: 'row', alignItems: 'center', gap: 8, backgroundColor: '#f2f2f7', borderRadius: 12, paddingHorizontal: 12, height: 40, marginBottom: 6, }, searchText: { color: '#8e8e93', fontSize: 15 }, input: { flex: 1, fontSize: 15, color: '#1c1c1e' }, chat: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16, paddingVertical: 10 }, chatTop: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', gap: 8 }, chatName: { fontSize: 16, fontWeight: '700', color: '#1c1c1e' }, chatTime: { fontSize: 12, color: '#8e8e93' }, chatLast: { flex: 1, fontSize: 14, color: '#6e6e73' }, unread: { minWidth: 20, height: 20, borderRadius: 10, backgroundColor: BLUE, alignItems: 'center', justifyContent: 'center', paddingHorizontal: 5, }, unreadText: { color: '#fff', fontSize: 12, fontWeight: '700' }, item: { flex: 1, flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 14 }, itemIcon: { width: 34, height: 34, borderRadius: 12, alignItems: 'center', justifyContent: 'center' }, itemTitle: { fontSize: 15, fontWeight: '600' }, itemSubtitle: { fontSize: 12, color: '#8e8e93', marginTop: 1 }, contact: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16, paddingVertical: 8 }, contactName: { flex: 1, fontSize: 16, color: '#1c1c1e', fontWeight: '500' }, action: { color: BLUE, fontSize: 16, fontWeight: '700' }, field: { backgroundColor: '#f2f2f7', borderRadius: 12, paddingHorizontal: 14, paddingVertical: 12, fontSize: 16, color: '#1c1c1e', }, label: { fontSize: 13, color: '#8e8e93', fontWeight: '600', textTransform: 'uppercase' }, channelIcon: { alignSelf: 'center', width: 80, height: 80, borderRadius: 40, backgroundColor: '#ff8a3d', alignItems: 'center', justifyContent: 'center', }, switchRow: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingVertical: 6 }, link: { flexDirection: 'row', alignItems: 'center', gap: 10, backgroundColor: '#eef3ff', borderRadius: 14, paddingHorizontal: 16, paddingVertical: 14, }, linkText: { color: BLUE, fontSize: 15, fontWeight: '600' },});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/chat.tsxXem trên GitHub: example/scenes/types.tsXem trên GitHub: example/scenes/ui.tsx