Introduction

In this article, we will discuss React Material UI Menus. Menu is used to show a list of options. Material UI is one of the most popular UI frameworks developed by Google. The Material UI library is designed for faster, easier and developer-friendly user interface development. Now Material-UI is supported by all major browsers and platforms.
You can check my previous article in which we discussed ReactJS and its basic components from the below-mentioned links.
Prerequisites
  • Basic Knowledge of React
  • Visual Studio Code
  • Node and NPM installed
  • Material UI Installed

Create ReactJS project

Let's first create a React application using the following command.
  1. npx create-react-app Matform

Install Material-UI

Now Install Material-UI by using the following command.
  1. npm install @material-ui/core --save
Now, right-click on the "src" folder and add a new component named 'MenuDemo.js'. Add the following code in MenuDemo component.
  1. import React from 'react';
  2. import Button from '@material-ui/core/Button';
  3. import Menu from '@material-ui/core/Menu';
  4. import MenuItem from '@material-ui/core/MenuItem';
  5. import AppBar from '@material-ui/core/AppBar';
  6. import Toolbar from '@material-ui/core/Toolbar';
  7. export default function MenuDemo() {
  8. const [anchorEl, open] = React.useState(null);
  9. const handleClick = event => {
  10. open(event.currentTarget);
  11. };
  12. const handleClose = () => {
  13. open(null);
  14. };
  15. return (
  16. <>
  17. <AppBar position="static">
  18. <Toolbar style={{ 'paddingLeft': "600px" }}>
  19. Material UI Menu
  20. </Toolbar>
  21. </AppBar>
  22. <div>
  23. <Button aria-controls="simple-menu" aria-haspopup="true" onClick={handleClick}>
  24. Open Menu
  25. </Button>
  26. <Menu
  27. id="Menu"
  28. anchorEl={anchorEl}
  29. keepMounted
  30. open={Boolean(anchorEl)}
  31. onClose={handleClose}
  32. >
  33. <MenuItem onClick={handleClose}>About</MenuItem>
  34. <MenuItem onClick={handleClose}>Contact</MenuItem>
  35. </Menu>
  36. </div>
  37. </>
  38. );
  39. }
Open app.js file and add the following code,
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import MenuDemo from './MenuDemo';
  5. function App() {
  6. return (
  7. <div className="App">
  8. <MenuDemo/>
  9. </div>
  10. );
  11. }
  12. export default App;
Run the project by using 'npm start' and check the result,
How To Use Material UI Menus In ReactJS Application
Now, right-click on the "src" folder and add a new component named 'MenuDemo1.js' and add the following code into this component.
  1. import React from 'react';
  2. import IconButton from '@material-ui/core/IconButton';
  3. import Menu from '@material-ui/core/Menu';
  4. import MenuItem from '@material-ui/core/MenuItem';
  5. import MoreVertIcon from '@material-ui/icons/MoreVert';
  6. import AppBar from '@material-ui/core/AppBar';
  7. import Toolbar from '@material-ui/core/Toolbar';
  8. const options = [
  9. 'About',
  10. 'Home',
  11. 'Contact',
  12. 'Demo',
  13. 'Test',
  14. 'React',
  15. ];
  16. const heigt = 25;
  17. export default function MenuDemo1() {
  18. const [anchorEl, setAnchorEl] = React.useState(null);
  19. const open = Boolean(anchorEl);
  20. const handleClick = event => {
  21. setAnchorEl(event.currentTarget);
  22. };
  23. const handleClose = () => {
  24. setAnchorEl(null);
  25. };
  26. return (
  27. <>
  28. <AppBar className="mrg" position="static">
  29. <Toolbar>
  30. <div style={{ 'paddingLeft': "600px" }}> Material UI Social media Icons</div>
  31. </Toolbar>
  32. </AppBar>
  33. <div>
  34. <IconButton
  35. aria-label="more"
  36. aria-controls="long-menu"
  37. aria-haspopup="true"
  38. onClick={handleClick}
  39. >
  40. <MoreVertIcon />
  41. </IconButton>
  42. <Menu
  43. id="long-menu"
  44. anchorEl={anchorEl}
  45. keepMounted
  46. open={open}
  47. onClose={handleClose}
  48. PaperProps={{
  49. style: {
  50. maxHeight: heigt * 4.5,
  51. width: 200,
  52. },
  53. }}
  54. >
  55. {options.map(option => (
  56. <MenuItem key={option} selected={option === 'Pyxis'} onClick={handleClose}>
  57. {option}
  58. </MenuItem>
  59. ))}
  60. </Menu>
  61. </div>
  62. </>
  63. );
  64. }
Run the project by using 'npm start' and check the result,
How To Use Material UI Menus In ReactJS Application
Now, right-click on the "src" folder and add a new component named 'DrawerDemo.js' and add the following code into this component,
  1. import React from 'react';
  2. import Drawer from '@material-ui/core/Drawer';
  3. import Button from '@material-ui/core/Button';
  4. import List from '@material-ui/core/List';
  5. import Divider from '@material-ui/core/Divider';
  6. import ListItem from '@material-ui/core/ListItem';
  7. import ListItemText from '@material-ui/core/ListItemText';
  8. import AppBar from '@material-ui/core/AppBar';
  9. import Toolbar from '@material-ui/core/Toolbar';
  10. export default function DrawerDemo() {
  11. const [state, setState] = React.useState({
  12. top: false,
  13. left: false,
  14. });
  15. const toggleDrawer = (side, open) => event => {
  16. if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
  17. return;
  18. }
  19. setState({ ...state, [side]: open });
  20. };
  21. const sideList = side => (
  22. <div
  23. role="presentation"
  24. onClick={toggleDrawer(side, false)}
  25. onKeyDown={toggleDrawer(side, false)}
  26. >
  27. <List>
  28. {['Home', 'About', 'Contact'].map((text, index) => (
  29. <ListItem button key={text}>
  30. <ListItemText primary={text} />
  31. </ListItem>
  32. ))}
  33. </List>
  34. <Divider />
  35. </div>
  36. );
  37. const fullList = side => (
  38. <div
  39. role="presentation"
  40. onClick={toggleDrawer(side, false)}
  41. onKeyDown={toggleDrawer(side, false)}
  42. >
  43. <List>
  44. {['Home', 'About', 'Contact'].map((text, index) => (
  45. <ListItem button key={text}>
  46. <ListItemText primary={text} />
  47. </ListItem>
  48. ))}
  49. </List>
  50. <Divider />
  51. </div>
  52. );
  53. return (
  54. <>
  55. <AppBar className="mrg" position="static">
  56. <Toolbar>
  57. <div style={{ 'paddingLeft': "600px" }}> Material UI Social media Icons</div>
  58. </Toolbar>
  59. </AppBar>
  60. <div>
  61. <Button color="primary" onClick={toggleDrawer('left', true)}>Open Left</Button>
  62. <Button color="primary" onClick={toggleDrawer('top', true)}>Open Top</Button>
  63. <Drawer open={state.left} onClose={toggleDrawer('left', false)}>
  64. {sideList('left')}
  65. </Drawer>
  66. <Drawer anchor="top" open={state.top} onClose={toggleDrawer('top', false)}>
  67. {fullList('top')}
  68. </Drawer>
  69. </div>
  70. </>
  71. );
  72. }
Open app.js file and add the following code,
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import DrawerDemo from './DrawerDemo';
  5. function App() {
  6. return (
  7. <div className="App">
  8. <DrawerDemo/>
  9. </div>
  10. );
  11. }
  12. export default App;
Run the project by using 'npm start' and check the result,
How To Use Material UI Menus In ReactJS Application
Click on Open Top button and check:
How To Use Material UI Menus In ReactJS Application

Summary

In this article, we learned how we can add Menu components in React applications using React material UI.