React Native Popup Menu – Over Flow Menu

Popup Menu

React Native Popup Menu – Over Flow Menu is the most common thing that we can see in the Android applications. In the initial stage, the popup menu can be seen in the header only, but after the introduction of the material design, the popup menu can be seen in many places.

You can use the popup menu if you want to provide the number of options to select but don’t want to highlight them. Most developers use them when they provide 2-3 options to select and they don’t want to use navigation drawer (Side Menu).

How to use Menu Component for Popup Menu

<Menu
  ref={this.setMenuRef}
  button={
    <Text onPress={this.showMenu} style={this.props.textStyle}>
      {this.props.menutext}
    </Text>
  }>
  <MenuItem onPress={this.option1Click}>op1:Go to second Page</MenuItem>
  <MenuItem onPress={this.option2Click}>op2:Demo Option</MenuItem>
  <MenuItem onPress={this.option3Click} disabled>
    op3:Disabled option
  </MenuItem>
  <MenuDivider />
  <MenuItem onPress={this.option4Click}>
    op4:Option After Divider
  </MenuItem>
</Menu>

In this example, You will see how to generate the popup menu from the header or from anywhere. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

This example is updated for the React Navigation 4.0+. For more about React Navigation 4 please have a look at Using React Navigation 4.0 in React Native apps.

To switch between the screens and to make a navigation header we need to add react-navigation in our application.

To install the dependencies open the terminal and jump into your project

cd ProjectName

1. To use Menu, { MenuItem, MenuDivider } you need to install react-native-material-menu

npm install react-native-material-menu --save

2. Install react-navigation dependency to import createAppContainer

npm install react-navigation --save

3. Install react-native-gesture-handler dependency to support gesture navigation

npm install react-native-gesture-handler --save

4. Install react-navigation-stack to import createStackNavigator

npm install react-navigation-stack --save

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example, we need to install the pods for react-native-gesture-handler.

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Project Structure

To start with this Example you need to create a directory like below screenshot:

  1. A pages directory which has another directory called component.
  2. Add two files in the Pages directory called FirstPage.js and SecondPage.js.
  3. Add two files in a component called CustomMenu.js and CustomMenuIcon.js.

App.js will have our Stack Navigator which is like an index file of our application. FirstPage.js and SecondPage.js will be the first and second screens of the application. We will use CustomMenu.js and CustomMenuIcon.js as custom popup menu components.

Code for Popup Menu

Now Open App.js in any code editor and replace the code with the following code

App.js

//This is an example code for popup menu// 
import React, { Component } from 'react';
//import react in our code. 

//For react-navigation 3.0+
//import { createAppContainer, createStackNavigator } from 'react-navigation';
//For react-navigation 4.0+
import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';

import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
//import all the screens we are going to switch 
 
const App = createStackNavigator({
    FirstPage: { screen: FirstPage },
    SecondPage: { screen: SecondPage }, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);
export default createAppContainer(App);

Open FirstPage.js in any code editor and replace the code with the following code.

FirstPage.js

//This is an example code for the popup menu//
import React, { Component } from 'react';
//import react in our code.
import { View, Text, Button } from 'react-native';
//import all the components we are going to use.
import CustomMenu from './component/CustomMenu';
//import our Custom menu component
import CustomMenuIcon from './component/CustomMenuIcon';
//import our Custom Icon menu component

class FirstPage extends Component {
  //Navigation option to create menu in header
  static navigationOptions = ({ navigation }) => {
    return {
      //Heading/title of the header
      title: navigation.getParam('Title', 'Popup Menu Example'),
      //Heading style
      headerStyle: {
        backgroundColor: navigation.getParam('BackgroundColor', 'red'),
      },
      //Heading text color
      headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
      //Heading Menu in Right Side
      headerRight: (
        //Custom menu component
        <CustomMenuIcon
          //Menu Text
          menutext="Menu"
          //Menu View Style
          menustyle={{
            marginRight: 16,
            flexDirection: 'row',
            justifyContent: 'flex-end',
          }}
          //Menu Text Style
          textStyle={{
            color: 'white',
          }}
          //Click functions for the menu items
          option1Click={() => {
            navigation.navigate('SecondPage');
          }}
          option2Click={() => {}}
          option3Click={() => {}}
          option4Click={() => {
            alert('Option 4');
          }}
        />
      ),
    };
  };

  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={{ flex: 1, backgroundColor: '#ffdffd' }}>
        <View
          style={{ alignItems: 'center', flex: 1, justifyContent: 'center' }}>
          <Text
            style={{
              fontSize: 20,
              fontWeight: 'bold',
              textAlign: 'center',
            }}>
            This is an example of menu
            {'\n\n'}
            To see the menu option
            {'\n'}
            Click on "Show Menu"
            {'\n'}
            or
            {'\n'}
            header "Menu"
          </Text>
          <CustomMenu
            //Menu Text
            menutext="Show Menu"
            //Menu View Style
            menustyle={{
              margin: 10,
              flexDirection: 'row',
              justifyContent: 'flex-end',
            }}
            //Menu Text Style
            textStyle={{
              fontSize: 25,
              marginTop: 16,
              color: 'red',
            }}
            //Click functions for the menu items
            option1Click={() => {
              this.props.navigation.navigate('SecondPage');
            }}
            option2Click={() => {}}
            option3Click={() => {}}
            option4Click={() => {
              alert('Option 4');
            }}
          />
        </View>
      </View>
    );
  }
}
export default FirstPage;

Open SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

//This is an example code for the popup menu//
import React, { Component } from 'react';
//import react in our code. 
import { StyleSheet, View, Text} from 'react-native';
//import all the components we are going to use.
import CustomMenuIcon from './component/CustomMenuIcon';
//import our Custom menu component
 
export default class SecondPage extends Component {
  //Navigation option to create menu in header
  static navigationOptions = ({ navigation }) => {
    return {
      //Heading/title of the header
      title: navigation.getParam('Title', 'Popup Menu Example'),
      //Heading style
      headerStyle: {
        backgroundColor: navigation.getParam('BackgroundColor', 'red'),
      },
      //Heading text color
      headerTintColor: navigation.getParam('HeaderTintColor', '#fff'),
      //Heading Menu in Right Side
      headerRight: (
        //Custom menu component
        <CustomMenuIcon
          //Menu Text
          menutext="Menu"
          //Menu View Style
          menustyle={{
            marginRight: 16,
            flexDirection: 'row',
            justifyContent: 'flex-end',
          }}
          //Menu Text Style
          textStyle={{
            color: 'white',
          }}
          //Click functions for the menu items
          option1Click={() => {
            navigation.navigate('FirstPage');
          }}
          option2Click={() => {}}
          option3Click={() => {}}
          option4Click={() => {
            alert('Option 4');
          }}
        />
      ),
    };
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <Text>You are on SecondPage</Text>
        <Text>Menu on the top as well</Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    margin:50,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Open CustomMenu.js in any code editor and replace the code with the following code.

CustomMenu.js

//This is an example code for the popup menu//
import React, { Component } from 'react';
//import react in our code.
import { View, Text } from 'react-native';
//import all the components we are going to use.
import Menu, { MenuItem, MenuDivider } from 'react-native-material-menu';
//import menu and menu item

export default class CustomMenu extends Component {
  _menu = null;
  setMenuRef = ref => {
    this._menu = ref;
  };
  showMenu = () => {
    this._menu.show();
  };
  hideMenu = () => {
    this._menu.hide();
  };
  option1Click = () => {
    this._menu.hide();
    this.props.option1Click();
  };
  option2Click = () => {
    this._menu.hide();
    this.props.option2Click();
  };
  option3Click = () => {
    this._menu.hide();
    this.props.option3Click();
  };
  option4Click = () => {
    this._menu.hide();
    this.props.option4Click();
  };
  render() {
    return (
      <View style={this.props.menustyle}>
        <Menu
          ref={this.setMenuRef}
          button={
            <Text onPress={this.showMenu} style={this.props.textStyle}>
              {this.props.menutext}
            </Text>
          }>
          <MenuItem onPress={this.option1Click}>op1:Go to second Page</MenuItem>
          <MenuItem onPress={this.option2Click}>op2:Demo Option</MenuItem>
          <MenuItem onPress={this.option3Click} disabled>
            op3:Disabled option
          </MenuItem>
          <MenuDivider />
          <MenuItem onPress={this.option4Click}>
            op4:Option After Divider
          </MenuItem>
        </Menu>
      </View>
    );
  }
}

Open CustomMenuIcon.js in any code editor and replace the code with the following code.

CustomMenuIcon.js

//This is an example code for the popup menu//
import React, { Component } from 'react';
//import react in our code.
import { View, Text,Image, TouchableOpacity  } from 'react-native';
//import all the components we are going to use.
import Menu, { MenuItem, MenuDivider } from 'react-native-material-menu';
//import menu and menu item

export default class CustomMenuIcon extends Component {
  _menu = null;
  setMenuRef = ref => {
    this._menu = ref;
  };
  showMenu = () => {
    this._menu.show();
  };
  hideMenu = () => {
    this._menu.hide();
  };
  option1Click = () => {
    this._menu.hide();
    this.props.option1Click();
  };
  option2Click = () => {
    this._menu.hide();
    this.props.option2Click();
  };
  option3Click = () => {
    this._menu.hide();
    this.props.option3Click();
  };
  option4Click = () => {
    this._menu.hide();
    this.props.option4Click();
  };
  render() {
    return (
      <View style={this.props.menustyle}>
        <Menu
          ref={this.setMenuRef}
          button={
            <TouchableOpacity onPress={this.showMenu}>
           <Image 
              source={{uri: 'https://raw.githubusercontent.com/AboutReact/sampleresource/master/menu_icon.png'}} 
              style={{width: 30, height: 30}} 
              
            />
            </TouchableOpacity>
          }>
          <MenuItem onPress={this.option1Click}>op1:Go to second Page</MenuItem>
          <MenuItem onPress={this.option2Click}>op2:Demo Option</MenuItem>
          <MenuItem onPress={this.option3Click} disabled>
            op3:Disabled option
          </MenuItem>
          <MenuDivider />
          <MenuItem onPress={this.option4Click}>
            op4:Option After Divider
          </MenuItem>
        </Menu>
      </View>
    );
  }
}

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

                
      

Output in Online Emulator

That was the React Native Popup Menu. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

7 thoughts on “React Native Popup Menu – Over Flow Menu”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.