

Android PopupMenu and iOS14+ UIMenu components for react-native.
Falls back to ActionSheet for versions below iOS14.
Installation
via npm:
npm install @react-native-menu/menu
via yarn:
yarn add @react-native-menu/menu
Installing on iOS with React Native 0.63 and above
There is an issue(https://github.com/facebook/react-native/issues/29246) causing projects with this module to fail on build on React Native 0.63 and above.
This issue may be fixed in future versions of react native.
As a work around, look for lines in [YourPrject].xcodeproj under LIBRARY_SEARCH_PATHS with "\"$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)\"", and change swift-5.0 to swift-5.3.
Linking
The package is automatically linked when building the app. All you need to do is:
npx pod-install
Usage
import { MenuView, MenuComponentRef } from '@react-native-menu/menu';
const App = () => {
const menuRef = useRef<MenuComponentRef>(null);
return (
<View style={styles.container}>
<Button
title="Show Menu with ref (Android only)"
onPress={() => menuRef.current?.show()}
/>
<MenuView
ref={menuRef}
title="Menu Title"
onPressAction={({ nativeEvent }) => {
console.warn(JSON.stringify(nativeEvent));
}}
actions={[
{
id: 'add',
title: 'Add',
titleColor: '#2367A2',
image: Platform.select({
ios: 'plus',
android: 'ic_menu_add',
}),
imageColor: '#2367A2',
subactions: [
{
id: 'nested1',
title: 'Nested action',
titleColor: 'rgba(250,180,100,0.5)',
subtitle: 'State is mixed',
image: Platform.select({
ios: 'heart.fill',
android: 'ic_menu_today',
}),
imageColor: 'rgba(100,200,250,0.3)',
state: 'mixed',
},
{
id: 'nestedDestructive',
title: 'Destructive Action',
attributes: {
destructive: true,
},
image: Platform.select({
ios: 'trash',
android: 'ic_menu_delete',
}),
},
],
},
{
id: 'share',
title: 'Share Action',
titleColor: '#46F289',
subtitle: 'Share action on SNS',
image: Platform.select({
ios: 'square.and.arrow.up',
android: 'ic_menu_share',
}),
imageColor: '#46F289',
state: 'on',
},
{
id: 'destructive',
title: 'Destructive Action',
attributes: {
destructive: true,
},
image: Platform.select({
ios: 'trash',
android: 'ic_menu_delete',
}),
},
]}
shouldOpenOnLongPress={false}
>
<View style={styles.button}>
<Text style={styles.buttonText}>Test</Text>
</View>
</MenuView>
</View>
);
};
Declarative usage
It's also possible to obtain the action is a more React-ish, declarative fashion. Refer to the react-to-imperative package, and see an example here.
Reference
Props
ref (Android only)
Ref to the menu component.
title (iOS only)
The title of the menu.
isAnchoredToRight (Android only)
Boolean determining if menu should anchored to right or left corner of parent view.
shouldOpenOnLongPress
Boolean determining if menu should open after long press or on normal press
actions
Actions to be displayed in the menu.
themeVariant (iOS only)
String to override theme of the menu. If you want to control theme universally across your app, see this package.
Object representing Menu Action.
export type MenuAction = {
id?: string;
title: string;
titleColor?: number | ColorValue;
subtitle?: string;
attributes?: MenuAttributes;
state?: MenuState;
image?: string;
imageColor?: number | ColorValue;
subactions?: MenuAction[];
};
The attributes indicating the style of the action.
type MenuAttributes = {
destructive?: boolean;
disabled?: boolean;
hidden?: boolean;
};
The state of the action.
type MenuState = 'off' | 'on' | 'mixed';
onPressAction
Callback function that will be called when selecting a menu item.
It will contain id of the given action.
| ({nativeEvent}) => void | No |
Events
Callback function that will be called when the menu is dismissed. This event fires at the start of the dismissal, before any animations complete.
Callback function that will be called when the menu is opened. This event fires right before the menu is displayed.
Example usage:
<MenuView
onOpenMenu={() => {
console.log('Menu was opened');
}}
onCloseMenu={() => {
console.log('Menu was closed');
}}
>
<View>
<Text>Open Menu</Text>
</View>
</MenuView>
Custom icons (Android)
You might want to use custom icons in the MenuAction image attribute. To do so, follow these steps.
-
Search for your icon on e.g. Material Icons, customize the fill, weight, grade etc.
to your liking and then press the Android tab and click Download. This wil download an xml file, e.g.
search_24px.xml. You can create your own icon or get it somewhere else, as long as it is in a format that Android
understands.
-
If using bare react-native, copy the downloaded xml file to your android/app/src/main/res/drawable folder.
If you are using Expo, add a dependency
on @expo/config-plugins, and then you can use
an expo config plugin to copy the file from your assets folder to the drawable
folder. Copy the config plugin to your app and reference it in your app.json or app.config.js in the plugins
section like this:
{
"expo": {
"plugins": [
[
"./plugin/withAndroidDrawables",
{
"drawableFiles": [ "./assets/my_icon.xml" ]
}
]
]
}
}
-
In your MenuAction you can now reference the icon using its file name, without the .xml extension. For example,
image: 'my_icon' will use the my_icon.xml file you copied to the drawable folder.
-
Remember to run a new build to see changes to these icons.
Testing with Jest
In some cases, you might want to mock the package to test your components. You can do this by using the jest.mock function.
import type { MenuComponentProps } from '@react-native-menu/menu';
jest.mock('@react-native-menu/menu', () => ({
MenuView: jest.fn((props: MenuComponentProps) => {
const React = require('react');
class MockMenuView extends React.Component {
render() {
return React.createElement(
'View',
{ testID: props.testID },
props.actions.map(action =>
React.createElement('Button', {
key: action.id,
title: action.title,
onPress: () => {
if (action.id && props?.onPressAction) {
props.onPressAction({ nativeEvent: { event: action.id } });
}
},
testID: action.id
})
),
this.props.children
);
}
}
return React.createElement(MockMenuView, props);
})
}));
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT