App mạng xã hội
Một app mạng xã hội nhỏ có tab, menu soạn bài và sheet soạn bài với các màn hình lồng nhau.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”- Đăng nhập bằng tài khoản demo đã điền sẵn (API là server giả chạy trong app, không cần mạng).
- Nút soạn bài nổi trên thanh tab:
<MorphMenu bottomOffset>giữ nó nằm trên tab bar. - Bài mới, Ảnh, Bình chọn, Phát trực tiếp và Tin nhắn mới là các route của nhóm
compose; màn Ảnh mở thêm màn Chỉnh ảnh bên trong sheet, có nút back native. - Menu dùng slot và config riêng (
COMPOSE_COMPONENTS,COMPOSE_CONFIG) nằm trong thư mục tính năng compose.
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.
/** * Root layout of the social example. * * The `app/` folder holds routes only. Each route file re-exports a screen * from `src/features/<name>`; the layouts here configure navigation and the * providers. See README.md for the folder structure. * * app/ * _layout.tsx root Stack, MorphProvider, ComposeMenu, Redux store (this file) * (tabs)/ bottom tabs: Feed, Search, Notifications, Messages * post/[id].tsx post detail, pushed on the root Stack * profile/[handle].tsx profile, pushed on the root Stack * chat/[id].tsx conversation, pushed on the root Stack (covers the tabs) * compose/ the morph sheet: every route in it expands from the menu * login.tsx sign-in, shown while there is no session * dev.tsx network and session debug screen * * `Stack.Protected` switches between the login screen and the app when the * session starts or ends, including when the refresh token is rejected. */
import { Stack } from 'expo-router';import { MorphProvider, MorphTrace, morphSheetScreenOptions } from 'expo-morph-menu';import { StatusBar } from 'expo-status-bar';import { StyleSheet } from 'react-native';import { GestureHandlerRootView } from 'react-native-gesture-handler';import { SafeAreaProvider } from 'react-native-safe-area-context';import { Provider } from 'react-redux';
import { selectIsSignedIn } from '../src/features/auth';import { COMPOSE_COMPONENTS, COMPOSE_CONFIG, COMPOSE_ITEMS, ComposeMenu, composeFabIcon,} from '../src/features/compose';import { theme } from '../src/shared/theme';import { store } from '../src/store';import { useAppSelector } from '../src/store/hooks';
/** * A deep link such as murmur://compose/poll opens the sheet as the first * route. Without an anchor, the sheet would be alone on the root Stack and * closing it would have nowhere to go back to. With this setting, * expo-router places the tabs below the deep-linked route. */export const unstable_settings = { anchor: '(tabs)' };
const STACK_OPTIONS = { headerTintColor: theme.text, headerShadowVisible: false, contentStyle: { backgroundColor: theme.background },};
/** Reads one boolean from the store, so it renders only when the user signs in or out. */function RootStack() { const signedIn = useAppSelector(selectIsSignedIn); return ( <Stack screenOptions={STACK_OPTIONS}> {/* Only reachable with a session. When the session ends, expo-router removes these screens and redirects to /login. */} <Stack.Protected guard={signedIn} redirectTo="/login"> <Stack.Screen name="(tabs)" options={{ headerShown: false }} /> <Stack.Screen name="post/[id]" options={{ title: 'Post' }} /> <Stack.Screen name="profile/[handle]" options={{ title: '' }} /> <Stack.Screen name="chat/[id]" options={{ title: 'Chat' }} /> <Stack.Screen name="compose" options={morphSheetScreenOptions} /> </Stack.Protected> <Stack.Protected guard={!signedIn} redirectTo="/"> <Stack.Screen name="login" options={{ headerShown: false }} /> </Stack.Protected> <Stack.Screen name="dev" options={{ title: 'Network', presentation: 'modal' }} /> </Stack> );}
export default function RootLayout() { return ( <SafeAreaProvider> <GestureHandlerRootView style={styles.root}> <Provider store={store}> <MorphProvider items={COMPOSE_ITEMS} fabIcon={composeFabIcon} config={COMPOSE_CONFIG} components={COMPOSE_COMPONENTS} > <StatusBar style="dark" /> <RootStack /> <ComposeMenu /> {/* Per-frame trace for debugging: EXPO_PUBLIC_MORPH_TRACE=1 npx expo start */} <MorphTrace enabled={process.env.EXPO_PUBLIC_MORPH_TRACE === '1'} /> </MorphProvider> </Provider> </GestureHandlerRootView> </SafeAreaProvider> );}
const styles = StyleSheet.create({ root: { flex: 1 },});/** * The morph sheet. Every route in this group expands from the menu card and * collapses back into it. * * Inside the sheet is a regular native Stack: * - root screens (post, photo, poll, live, message) draw their own header * with a close button; * - pushed screens (crop, thread) use the native header with a back button, * and on iOS the native back swipe. The left-edge dismiss gesture of the * sheet is only active on the root screen. */
import { Stack } from 'expo-router';import { MorphSheetLayout } from 'expo-morph-menu';
import { theme } from '../../src/shared/theme';
export default function ComposeLayout() { return ( <MorphSheetLayout screenOptions={{ headerShadowVisible: false, headerTintColor: theme.text, contentStyle: { backgroundColor: theme.background }, }} > <Stack.Screen name="post" options={{ headerShown: false }} /> <Stack.Screen name="photo" options={{ headerShown: false }} /> <Stack.Screen name="crop" options={{ title: 'Edit photo' }} /> <Stack.Screen name="poll" options={{ headerShown: false }} /> <Stack.Screen name="live" options={{ headerShown: false }} /> <Stack.Screen name="message" options={{ headerShown: false }} /> <Stack.Screen name="thread/[id]" options={{ title: 'Chat' }} /> </MorphSheetLayout> );}/** * The floating "create" menu: its rows, its look, and when it is shown. * The root layout passes these to MorphProvider and renders <ComposeMenu />. */
import { Ionicons } from '@expo/vector-icons';import { usePathname } from 'expo-router';import { MorphMenu, type MorphBackgroundProps, type MorphComponents, type MorphConfigInput, type MorphIcon, type MorphItem,} from 'expo-morph-menu';import { StyleSheet } from 'react-native';import Animated, { interpolateColor, useAnimatedStyle } from 'react-native-reanimated';
import { TAB_BAR_HEIGHT, theme } from '../../../shared/theme';
/** Module-level constants: MorphProvider receives the same objects on every render. */export const COMPOSE_ITEMS: MorphItem[] = [ { key: 'post', label: 'Post', tint: theme.accent, icon: ({ size, color }) => <Ionicons name="create" size={size} color={color} />, href: '/compose/post', }, { key: 'photo', label: 'Photo', tint: '#0ea5e9', icon: ({ size, color }) => <Ionicons name="image" size={size} color={color} />, href: '/compose/photo', }, { key: 'poll', label: 'Poll', tint: '#10b981', icon: ({ size, color }) => <Ionicons name="stats-chart" size={size} color={color} />, href: '/compose/poll', }, { key: 'live', label: 'Go live', tint: theme.live, icon: ({ size, color }) => <Ionicons name="radio" size={size} color={color} />, href: '/compose/live', }, { key: 'message', label: 'New message', tint: '#f59e0b', icon: ({ size, color }) => <Ionicons name="mail" size={size} color={color} />, href: '/compose/message', },];
export const composeFabIcon: MorphIcon = ({ size, color }) => ( <Ionicons name="add" size={size} color={color} />);
export const COMPOSE_CONFIG: MorphConfigInput = { colorScheme: 'light', layout: { margin: 16, cardWidth: 220 }, colors: { light: { sheetBackground: theme.background, fabIcon: '#ffffff', iconForeground: '#ffffff' }, },};
/** * The button and the card share one surface. This Background slot paints it * violet while it is a button and white once it is a card, driven by the * `progress` shared value (on the UI thread, no React render). On the sheet * (variant 'sheet') it is always white, matching the open card, so the * hand-off swaps identical pixels. */function CardBackground({ variant, progress }: MorphBackgroundProps) { const style = useAnimatedStyle(() => ({ backgroundColor: variant === 'sheet' ? '#ffffff' : interpolateColor(progress.value, [0, 0.35], [theme.accent, '#ffffff']), })); return <Animated.View style={[StyleSheet.absoluteFill, style]} />;}
export const COMPOSE_COMPONENTS: MorphComponents = { Background: CardBackground };
/** Screens where the floating button is shown: the tab roots, and the sheet itself. */const TAB_PATHS = new Set(['/', '/search', '/notifications', '/messages']);
/** * The menu overlay. Visible on the four tab screens; on pushed screens * (post, profile, chat) it is hidden. This is the only component that reads * the pathname, so navigation renders it and not the root layout. */export function ComposeMenu() { const pathname = usePathname(); const onTab = TAB_PATHS.has(pathname); // While a compose route is open, the card is where the sheet collapses back // to, so the menu must stay mounted and not hidden. const inSheet = pathname.startsWith('/compose'); return ( <MorphMenu hidden={!onTab && !inSheet} bottomOffset={TAB_BAR_HEIGHT} fabAccessibilityLabel="Create" /> );}Xem trên GitHub: example-social/app/_layout.tsxXem trên GitHub: example-social/app/compose/_layout.tsxXem trên GitHub: example-social/src/features/compose/ui/ComposeMenu.tsx