Open In App

React MUI Menu API

MUI or Material-UI is a UI library providing predefined robust and customisable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI Menu API. The menu displays a list of choices of action that we can perform on the site. The API provides a lot of functionality and we will learn to implement them.



Import Menu API

import Menu from '@mui/material/Menu';
// or
import { Menu } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.



CSS Rules:

Syntax: Create a Menu as follows:

<Menu
      anchorEl={anchorEl}
      open={open}
      onClose={handleClose}
      anchorOrigin={{
        vertical: "top",
        horizontal: "left",
      }}
      transformOrigin={{
        vertical: "top",
        horizontal: "left",
      }}
    >
      <MenuItem>
        HTML
      </MenuItem>
      <MenuItem>
        CSS
      </MenuItem>
      <MenuItem>
        Javascript
      </MenuItem>
      <Divider />
</Menu>

Installing and Creating React app and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab @mui/icons-material

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a Menu.

App.js
 

Output:

 

Example 2: In the following example, we have changed the transition duration.

App.js
 

Output:

 

Reference: https://mui.com/material-ui/api/menu/

Article Tags :