mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-15 00:01:10 +02:00
Applied formatting to 1487+ files using pnpm format:write - TypeScript/JavaScript files - Svelte components - Astro pages - JSON configs - Markdown docs 13 files still need manual review (Astro JSX comments)
5.1 KiB
5.1 KiB
react-native-context-menu-view Usage Examples
Basic Context Menu
import ContextMenu from 'react-native-context-menu-view';
import { View, Text, Image } from 'react-native';
function BasicExample() {
return (
<ContextMenu
actions={[
{ title: 'Save', systemIcon: 'square.and.arrow.down' },
{ title: 'Share', systemIcon: 'square.and.arrow.up' },
{ title: 'Delete', destructive: true, systemIcon: 'trash' },
]}
onPress={(e) => {
console.log('Action pressed:', e.nativeEvent.name);
// Handle action based on e.nativeEvent.index or e.nativeEvent.name
}}
>
<View className="p-4 bg-gray-100 rounded-lg">
<Text>Long press me!</Text>
</View>
</ContextMenu>
);
}
Context Menu with Submenus (iOS 14+)
import ContextMenu from 'react-native-context-menu-view';
function SubmenuExample() {
return (
<ContextMenu
actions={[
{ title: 'Save', systemIcon: 'square.and.arrow.down' },
{
title: 'Export',
systemIcon: 'square.and.arrow.up.on.square',
// Nested submenu actions
actions: [
{ title: 'Export as PDF', systemIcon: 'doc.fill' },
{ title: 'Export as Image', systemIcon: 'photo' },
{ title: 'Export as Video', systemIcon: 'video' },
],
},
{ title: 'Delete', destructive: true, systemIcon: 'trash' },
]}
onPress={(e) => {
console.log('Action:', e.nativeEvent.name);
}}
>
<View className="p-4 bg-blue-100 rounded-lg">
<Text>Long press for submenu!</Text>
</View>
</ContextMenu>
);
}
Context Menu with Preview
import ContextMenu from 'react-native-context-menu-view';
function PreviewExample() {
return (
<ContextMenu
actions={[
{ title: 'Edit', systemIcon: 'pencil' },
{ title: 'Duplicate', systemIcon: 'plus.square.on.square' },
{ title: 'Delete', destructive: true, systemIcon: 'trash' },
]}
onPress={(e) => {
console.log('Action:', e.nativeEvent.name);
}}
previewBackgroundColor="white"
>
<Image source={{ uri: 'https://example.com/image.jpg' }} className="w-full h-48 rounded-lg" />
</ContextMenu>
);
}
Context Menu on Generated Images
import ContextMenu from 'react-native-context-menu-view';
import * as MediaLibrary from 'expo-media-library';
import * as FileSystem from 'expo-file-system';
function GeneratedImageMenu({ imageUrl }: { imageUrl: string }) {
const handleSave = async () => {
// Request permissions
const { status } = await MediaLibrary.requestPermissionsAsync();
if (status !== 'granted') {
alert('Permission required to save images');
return;
}
// Download and save image
const fileUri = FileSystem.documentDirectory + 'generated-image.jpg';
await FileSystem.downloadAsync(imageUrl, fileUri);
await MediaLibrary.createAssetAsync(fileUri);
alert('Image saved!');
};
return (
<ContextMenu
actions={[
{ title: 'Save to Photos', systemIcon: 'square.and.arrow.down' },
{ title: 'Share', systemIcon: 'square.and.arrow.up' },
{
title: 'More Options',
actions: [
{ title: 'Set as Wallpaper', systemIcon: 'photo.on.rectangle' },
{ title: 'Copy', systemIcon: 'doc.on.doc' },
{ title: 'View Details', systemIcon: 'info.circle' },
],
},
{ title: 'Delete', destructive: true, systemIcon: 'trash' },
]}
onPress={(e) => {
switch (e.nativeEvent.index) {
case 0: // Save to Photos
handleSave();
break;
case 1: // Share
// Implement share
break;
case 2: // Delete
// Implement delete
break;
}
}}
>
<Image source={{ uri: imageUrl }} className="w-full h-full rounded-lg" resizeMode="cover" />
</ContextMenu>
);
}
Available Action Properties
interface Action {
title: string; // Action title
systemIcon?: string; // SF Symbol name (iOS only)
icon?: string; // Custom icon (Android)
destructive?: boolean; // Red text for destructive actions
disabled?: boolean; // Disable the action
inlineChildren?: boolean; // Show submenu items inline
actions?: Action[]; // Nested submenu actions
}
Common SF Symbols (iOS)
square.and.arrow.down- Save/Downloadsquare.and.arrow.up- Share/Exporttrash- Deletepencil- Editdoc.on.doc- Copyheart/heart.fill- Favoritestar/star.fill- Star/Ratephoto- Imagevideo- Videoinfo.circle- Infoeye/eye.slash- Show/Hidecheckmark- Confirm
Android Support
The library also works on Android with Material Design menus:
<ContextMenu
actions={[
{ title: 'Save', icon: 'save' }, // Use Android icon names
{ title: 'Share', icon: 'share' },
{ title: 'Delete', destructive: true, icon: 'delete' },
]}
onPress={(e) => {
console.log('Action:', e.nativeEvent.index);
}}
>
<YourComponent />
</ContextMenu>
Tips
- Long Press: Context menu appears on long press by default
- Submenus: Nest
actionsarrays for submenus (iOS 14+) - SF Symbols: Use Apple's SF Symbols for iOS icons
- Destructive Actions: Set
destructive: truefor delete/remove actions - Event Handling: Use
e.nativeEvent.indexore.nativeEvent.nameto identify actions