Slot tuỳ biến
Mọi phần hiển thị được thay qua slot: thẻ màu navy, dòng có phụ đề, trigger, separator và scrim riêng.
Hoạt động thế nào
Phần tiêu đề “Hoạt động thế nào”componentsthayBackground,Item,Trigger,SeparatorvàScrim; package vẫn giữ các container có animation, cử chỉ và phần chuyển giao.Itemđọchovered(một shared value) để tự vẽ trạng thái hover, nênHighlightlànull.- Dòng có phụ đề cao hơn:
itemHeight(item)cho biết trước mọi chiều cao, vì các dòng không bao giờ được đo. - Các slot là thuần (pure) và nền là đục: thẻ và màn hình toàn phần hiện cùng những dòng đó và hoán đổi trong một khung hình.
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.
/** * Slot example: every visible part is replaced with the app's own UI. * * Every slot here is pure (same props, same output) and does not animate its * own size, as the slot contract of expo-morph-menu requires. The hover state * is read from the `hovered` shared value and animated with Reanimated. */
import { Ionicons } from '@expo/vector-icons';import { BlurView } from 'expo-blur';import { renderIcon, type MorphBackgroundProps, type MorphComponents, type MorphConfigInput, type MorphItem, type MorphItemProps, type MorphLayerProps, type MorphTriggerProps,} from 'expo-morph-menu';import { StyleSheet, Text, View } from 'react-native';import Animated, { Extrapolation, interpolate, useAnimatedStyle } from 'react-native-reanimated';
const NAVY = '#1f2a44';
function Background(_: MorphBackgroundProps) { return <View style={[StyleSheet.absoluteFill, styles.bg]} />;}
function Item({ item, index, hovered, colors }: MorphItemProps) { const subtitle = item.data?.subtitle as string | undefined; const hover = useAnimatedStyle(() => ({ backgroundColor: hovered.value === index ? 'rgba(255,255,255,0.12)' : 'transparent', })); return ( <Animated.View style={[styles.item, hover]}> <View style={[styles.badge, { backgroundColor: item.tint ?? '#334' }]}> {renderIcon(item.icon, { size: 16, color: colors.iconForeground })} </View> <View style={styles.texts}> <Text style={styles.title} numberOfLines={1}> {item.label} </Text> {subtitle ? ( <Text style={styles.subtitle} numberOfLines={1}> {subtitle} </Text> ) : null} </View> <Ionicons name="chevron-forward" size={14} color="rgba(255,255,255,0.4)" /> </Animated.View> );}
function Separator() { return <View style={styles.sep} />;}
function Trigger({ progress }: MorphTriggerProps) { const style = useAnimatedStyle(() => ({ opacity: interpolate(progress.value, [0, 0.35], [1, 0], Extrapolation.CLAMP), transform: [{ rotate: `${interpolate(progress.value, [0, 0.35], [0, 90], Extrapolation.CLAMP)}deg` }], })); return ( <Animated.View style={style}> <Ionicons name="apps" size={22} color="#fff" /> </Animated.View> );}
function Scrim(_: MorphLayerProps) { return <BlurView intensity={20} tint="dark" style={StyleSheet.absoluteFill} />;}
export const CUSTOM_COMPONENTS: MorphComponents = { Background, Item, Separator, Trigger, Scrim, // The Item slot draws its own hover state, so the default highlight is turned off. Highlight: null,};
export const CUSTOM_CONFIG: MorphConfigInput = { radius: { card: 18, fab: 18 }, layout: { cardWidth: 260, separatorHeight: StyleSheet.hairlineWidth }, colors: { light: { iconForeground: '#fff' }, dark: { iconForeground: '#fff' } },};
/** Rows with a subtitle are taller. A pure function of the item data. */export const customItemHeight = (item: MorphItem) => (item.data?.subtitle ? 58 : 46);
const styles = StyleSheet.create({ bg: { backgroundColor: NAVY, borderWidth: StyleSheet.hairlineWidth, borderColor: 'rgba(255,255,255,0.15)' }, item: { flex: 1, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, marginHorizontal: 6, borderRadius: 12, }, badge: { width: 30, height: 30, borderRadius: 9, alignItems: 'center', justifyContent: 'center', marginRight: 12, }, texts: { flex: 1 }, title: { color: '#fff', fontSize: 15, fontWeight: '600' }, subtitle: { color: 'rgba(255,255,255,0.6)', fontSize: 12, marginTop: 2 }, sep: { flex: 1, marginHorizontal: 18, backgroundColor: 'rgba(255,255,255,0.15)' },});import { Ionicons } from '@expo/vector-icons';import { Stack, useGlobalSearchParams } from 'expo-router';import { MorphMenu, MorphProvider, MorphTrace, morphSheetScreenOptions } from 'expo-morph-menu';import { StatusBar } from 'expo-status-bar';import { useMemo, useState } from 'react';import { GestureHandlerRootView } from 'react-native-gesture-handler';import { SafeAreaProvider } from 'react-native-safe-area-context';
import { CUSTOM_COMPONENTS, CUSTOM_CONFIG, customItemHeight } from '../components/CustomSlots';import { DemoContext, type UiVariant } from '../context/demo';import { ITEMS } from '../data/items';import { findScene } from '../scenes';import { prefetchSheet } from '../data/sheetData';
/** `npm run example:trace` enables the per-frame trace used by the end-to-end tests. */const TRACE = process.env.EXPO_PUBLIC_MORPH_TRACE === '1';
/** A deep link into the sheet keeps the home screen below it, so closing has a screen to return to. */export const unstable_settings = { anchor: 'index' };
export default function RootLayout() { // The documentation site opens the demo with a scene, a variant or a corner // in the URL, for example /?scene=wallet, /?variant=custom or /?anchor=left. // Read once, at start; on a phone the home screen switches them. const params = useGlobalSearchParams<{ scene?: string; variant?: string; anchor?: string }>(); const [sceneId, setScene] = useState(() => findScene(params.scene)?.id ?? 'basic'); const scene = findScene(sceneId); const [variant, setVariant] = useState<UiVariant>(() => params.variant === 'custom' ? 'custom' : 'default', ); const [anchor] = useState<'bottom-left' | 'bottom-right'>(() => params.anchor === 'left' ? 'bottom-left' : 'bottom-right', ); const demo = useMemo(() => ({ variant, setVariant, scene: sceneId, setScene }), [variant, sceneId]); const custom = variant === 'custom';
return ( <SafeAreaProvider> <GestureHandlerRootView style={{ flex: 1 }}> <DemoContext.Provider value={demo}> <MorphProvider // Another demo app is another menu: start it fresh. key={sceneId} items={scene?.items ?? ITEMS} // Called while the pointer is on a row, before the row is selected. onHover={(item) => prefetchSheet(item.key)} // Icon for every <MorphCloseButton>: an element, an image source, or a function. closeIcon={({ size, color }) => <Ionicons name="close" size={size} color={color} />} // The custom variant replaces every visible part through slots. components={scene ? scene.components : custom ? CUSTOM_COMPONENTS : undefined} itemHeight={scene ? scene.itemHeight : custom ? customItemHeight : undefined} fabIcon={scene?.fabIcon} // The screens of this app are light only, so the close button uses light colors too. config={ scene ? { colorScheme: 'light', ...scene.config } : custom ? { ...CUSTOM_CONFIG, colorScheme: 'light', anchor } : { colorScheme: 'light', anchor } } > <Stack screenOptions={{ headerShown: false }}> <Stack.Screen name="index" /> <Stack.Screen name="(sheet)" options={morphSheetScreenOptions} /> </Stack>
{/* After the navigator, so the button and the card are drawn above every screen. The menu has no route of its own. */} <MorphMenu /> {TRACE && <MorphTrace />} <StatusBar style={scene?.config?.colorScheme === 'dark' ? 'light' : 'dark'} /> </MorphProvider> </DemoContext.Provider> </GestureHandlerRootView> </SafeAreaProvider> );}Xem trên GitHub: example/components/CustomSlots.tsxXem trên GitHub: example/app/_layout.tsx