Use native context menu functionality from React Native. On iOS 13+ this uses UIMenu functionality, and on Android it uses a ContextMenu.
On iOS 12 and below, nothing happens. You may wish to do a Platform.OS === 'ios' && parseInt(Platform.Version, 10) <= 12 check, and add your own onLongPress handler.
$ npm install react-native-context-menu-view --save
cd ios/
pod installimport ContextMenu from "react-native-context-menu-view";
const Example = () => {
return (
<ContextMenu
actions={[{ title: "Title 1" }, { title: "Title 2" }]}
onPress={(e) => {
console.warn(
`Pressed ${e.nativeEvent.name} at index ${e.nativeEvent.index}`
);
}}
>
<View style={styles.yourOwnStyles} />
</ContextMenu>
);
};See example/ for basic usage.
Optional. The title above the popup menu.
Array of { title: string, subtitle?: string, systemIcon?: string, icon?: string, iconColor?: string, destructive?: boolean, selected?: boolean, disabled?: boolean, disabled?: boolean, inlineChildren?: boolean, actions?: Array<ContextMenuAction> }.
-
titleis the title of the action -
subtitleis the subtitle of the action (iOS 15+ only) -
systemIconrefers to an icon name within SF Symbols (iOS only) -
iconrefers to an SVG asset name that is provided in Assets.xcassets or to a Drawable on Android; when bothsystemIconandiconare provided,iconwill take a higher priority and it will overridesystemIcon -
iconColorwill change the color of the icon provided to theiconprop and has no effect onsystemIcon(default: black) -
destructiveitems are rendered in red (iOS only, default: false) -
selecteditems have a checkmark next to them (iOS only, default: false) -
disabledmarks whether the action is disabled or not (default: false) -
actionswill provide a one level deep nested menu; when child actions are supplied, the child's callback will contain its name but the same index as the topmost parent menu/action index -
inlineChildrenmarks whether its children (if any) should be rendered inline instead of in their own child menu (iOS only, default: false)
Optional. When the popup is opened and the user picks an option. Called with { nativeEvent: { index, indexPath, name } }. When a nested action is selected the top level parent index is used for the callback.
To get the full path to the item, indexPath is an array of indices to reach the item. For a top-level item, it'll be an array with a single index. For an item one deep, it'll be an array with two indexes.
Optional, iOS only. When the context menu preview is tapped.
Optional. When the popup is opened and the user cancels.
Optional. The background color of the preview. This is displayed underneath your view. Set this to transparent (or another color) if the default causes issues.
Optional. When set to true, the context menu is triggered with a single tap instead of a long press, and a preview is not show and no blur occurs. Uses the iOS 14 Menu API on iOS and a simple tap listener on android.
Optional. Disable menu interaction.
