Ghi chú
App ghi chú trên nền giấy ấm, nút ở góc dưới bên trái và phím tắt trên các dòng.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”anchor: 'bottom-left'dời nút sang góc trái; thẻ mở ra từ đó.- Slot
Itemhiện chip phím tắt, slotSeparatorvẽ vạch mảnh giữa các dòng (layout.separatorHeight). colors.lightđặt tông giấy cho thẻ, chữ và các màn hình.- Ghi âm có sóng âm animate bằng Reanimated; checklist đánh dấu từng việc.
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 { useEffect, useState } from 'react';import { Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native';import Animated, { 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 notes app on warm paper, with the button in the bottom-left corner. The * rows show a keyboard shortcut and a Separator slot draws lines between * them; the colors come from `config.colors`. */
const PAPER = '#fbf6ec';const INK = '#3b3125';const ACCENT = '#e8a23a';
function Item({ item, colors }: MorphItemProps) { return ( <View style={styles.item}> {renderIcon(item.icon, { size: 18, color: ACCENT })} <Text style={[styles.itemLabel, { color: colors.text }]}>{item.label}</Text> <View style={styles.shortcut}> <Text style={styles.shortcutText}>{item.data?.shortcut as string}</Text> </View> </View> );}
function Separator() { return <View style={styles.separator} />;}
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('note', 'Note', { icon: ion('document-text-outline'), data: { shortcut: '⌘ N' } }), sheetItem('checklist', 'Checklist', { icon: ion('checkbox-outline'), data: { shortcut: '⌘ L' } }), sheetItem('voice', 'Voice memo', { icon: ion('mic-outline'), data: { shortcut: '⌘ R' } }), sheetItem('scan', 'Scan document', { icon: ion('scan-outline'), data: { shortcut: '⌘ D' } }),];
const NOTES = [ { title: 'Trip to Kyoto', body: 'Fushimi Inari at sunrise, tea in Uji, the bamboo path before 8.', color: '#fde9c4', }, { title: 'Groceries', body: '☐ Oat milk\n☐ Lemons\n☑ Basil\n☐ Sourdough', color: '#e3f1d9' }, { title: 'Book ideas', body: 'A lighthouse keeper who collects other people’s letters.', color: '#e7e3f6' }, { title: 'Standup', body: 'Ship the onboarding, review the icons, plan the beta.', color: '#fbe0df' }, { title: 'Workout', body: '5 × 5 squats, 3 × 8 rows, 20 min easy run.', color: '#dff0f4' }, { title: 'Gift list', body: 'Mom: ceramic mug. Leo: climbing chalk.', color: '#fdebd2' },];
function Home() { const insets = useSafeAreaInsets(); return ( <ScrollView style={styles.page} contentContainerStyle={[ column, styles.home, { paddingTop: insets.top + 10, paddingBottom: insets.bottom + 120 }, ]} > <BackToDemos /> <Text style={styles.h1}>Notes</Text> <View style={styles.grid}> {NOTES.map((n) => ( <View key={n.title} style={[styles.card, { backgroundColor: n.color }]}> <Text style={styles.cardTitle}>{n.title}</Text> <Text style={styles.cardBody} numberOfLines={5}> {n.body} </Text> </View> ))} </View> <Text style={styles.hint}> The button sits in the bottom-left corner: `anchor: 'bottom-left'`. </Text> </ScrollView> );}
function Note() { return ( <View style={styles.page}> <SheetHeader title="" color={INK} right={<Text style={styles.done}>Done</Text>} /> <View style={styles.editor}> <TextInput style={styles.editorTitle} defaultValue="Morning pages" placeholder="Title" /> <TextInput style={styles.editorBody} multiline defaultValue={ 'Slept well. The light in the kitchen at 7 is the best part of the day.\n\nToday: finish the draft, call the bakery about Saturday, walk without the phone.' } /> </View> </View> );}
function Checklist() { const [tasks, setTasks] = useState([ { t: 'Book the train', done: true }, { t: 'Pack the rain jacket', done: false }, { t: 'Print the tickets', done: false }, { t: 'Water the plants', done: true }, { t: 'Charge the camera', done: false }, ]); const left = tasks.filter((x) => !x.done).length; return ( <View style={styles.page}> <SheetHeader title="Weekend trip" color={INK} /> <View style={styles.editor}> <Text style={styles.muted}>{left} left</Text> {tasks.map((task, i) => ( <Pressable key={task.t} style={styles.task} onPress={() => setTasks((all) => all.map((x, j) => (j === i ? { ...x, done: !x.done } : x)))} accessibilityRole="checkbox" accessibilityState={{ checked: task.done }} > <Ionicons name={task.done ? 'checkbox' : 'square-outline'} size={24} color={task.done ? ACCENT : '#b9ad9a'} /> <Text style={[styles.taskText, task.done && styles.taskDone]}>{task.t}</Text> </Pressable> ))} </View> </View> );}
const BARS = 28;
function Voice() { const t = useSharedValue(0); const [recording, setRecording] = useState(true); useEffect(() => { t.value = 0; if (recording) t.value = withRepeat(withTiming(1, { duration: 1200 }), -1, true); }, [recording, t]); return ( <View style={styles.page}> <SheetHeader title="Voice memo" color={INK} /> <View style={styles.voice}> <Text style={styles.clock}>{recording ? '0:12' : 'Paused'}</Text> <View style={styles.wave}> {Array.from({ length: BARS }, (_, i) => ( <Bar key={i} i={i} t={t} /> ))} </View> <Pressable style={[styles.record, !recording && { backgroundColor: ACCENT }]} onPress={() => setRecording((r) => !r)} accessibilityLabel={recording ? 'Pause' : 'Record'} > <Ionicons name={recording ? 'pause' : 'mic'} size={30} color="#fff" /> </Pressable> </View> </View> );}
function Bar({ i, t }: { i: number; t: SharedValue<number> }) { const style = useAnimatedStyle(() => { const phase = Math.sin(i * 0.7 + t.value * Math.PI * 2); return { height: 14 + Math.abs(phase) * (24 + (i % 5) * 10) }; }); return <Animated.View style={[styles.bar, style]} />;}
function Scan() { return ( <View style={[styles.page, { backgroundColor: '#2b2620' }]}> <SheetHeader title="Scan document" color="#fff" /> <View style={styles.voice}> <View style={styles.paperSheet}> {[0.9, 0.75, 0.85, 0.6, 0.8, 0.5].map((w, i) => ( <View key={i} style={[styles.paperLine, { width: `${w * 100}%` }]} /> ))} </View> <Text style={[styles.muted, { color: '#d9cfbf' }]}>Hold steady: edges found, capturing…</Text> </View> </View> );}
function Screen({ itemKey }: { itemKey: string }) { if (itemKey === 'checklist') return <Checklist />; if (itemKey === 'voice') return <Voice />; if (itemKey === 'scan') return <Scan />; return <Note />;}
export const notes: Scene = { id: 'notes', title: 'Notes', subtitle: 'Warm paper, bottom-left corner, rows with shortcuts and separators', accent: ACCENT, items, config: { anchor: 'bottom-left', surface: 'solid', radius: { card: 18, fab: 18 }, layout: { cardWidth: 230, rowHeight: 48, separatorHeight: StyleSheet.hairlineWidth }, colors: { light: { surface: '#fffdf8', sheetBackground: PAPER, text: INK, highlight: '#f3e7cf', fabIcon: ACCENT, closeIcon: INK, }, }, }, components: { Item, Separator }, Home, Screen,};
const styles = StyleSheet.create({ page: { flex: 1, backgroundColor: PAPER }, home: { paddingHorizontal: 16, gap: 12 }, h1: { fontSize: 32, fontWeight: '800', color: INK }, grid: { flexDirection: 'row', flexWrap: 'wrap', gap: 10 }, card: { width: '48%', flexGrow: 1, borderRadius: 18, padding: 14, gap: 6, minHeight: 130 }, cardTitle: { fontSize: 16, fontWeight: '700', color: INK }, cardBody: { fontSize: 13, lineHeight: 18, color: '#5e5242' }, hint: { fontSize: 13, color: '#9a8c77', textAlign: 'center', marginTop: 6 }, item: { flex: 1, flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 16 }, itemLabel: { flex: 1, fontSize: 15, fontWeight: '600' }, shortcut: { borderRadius: 6, borderWidth: 1, borderColor: '#e6d9c2', paddingHorizontal: 6, paddingVertical: 2, }, shortcutText: { fontSize: 11, color: '#9a8c77', fontWeight: '600' }, separator: { flex: 1, marginHorizontal: 16, backgroundColor: '#eadfca' }, done: { color: ACCENT, fontSize: 16, fontWeight: '700' }, editor: { paddingHorizontal: 22, gap: 10 }, editorTitle: { fontSize: 28, fontWeight: '800', color: INK }, editorBody: { fontSize: 17, lineHeight: 26, color: '#4a3f31', minHeight: 300, textAlignVertical: 'top' }, muted: { fontSize: 14, color: '#9a8c77' }, task: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingVertical: 8 }, taskText: { fontSize: 17, color: INK }, taskDone: { color: '#b9ad9a', textDecorationLine: 'line-through' }, voice: { flex: 1, alignItems: 'center', justifyContent: 'center', gap: 30 }, clock: { fontSize: 44, fontWeight: '300', color: INK, fontVariant: ['tabular-nums'] }, wave: { flexDirection: 'row', alignItems: 'center', gap: 4, height: 80 }, bar: { width: 5, borderRadius: 3, backgroundColor: ACCENT }, record: { width: 76, height: 76, borderRadius: 38, backgroundColor: '#e5484d', alignItems: 'center', justifyContent: 'center', }, paperSheet: { width: 230, height: 300, backgroundColor: '#fff', borderRadius: 6, padding: 22, gap: 14, borderWidth: 3, borderColor: ACCENT, transform: [{ rotate: '-3deg' }], }, paperLine: { height: 8, borderRadius: 4, backgroundColor: '#e5ded2' },});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/notes.tsxXem trên GitHub: example/scenes/types.tsXem trên GitHub: example/scenes/ui.tsx