Sometimes, we want to make a dropdown menu open below the Appbar using React Material-UI.
In this article, we’ll look at how to make a dropdown menu open below the Appbar using React Material-UI.
How to make a dropdown menu open below the Appbar using React Material-UI?
To make a dropdown menu open below the Appbar using React Material-UI, we set the Menu
‘s getContentAnchorEl
to null
.
Then we set anchorOrigin
and transformOrigin
to objects to position the menu.
For instance, we write:
import React from "react";
import { alpha, makeStyles } from "@material-ui/core/styles";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import IconButton from "@material-ui/core/IconButton";
import Typography from "@material-ui/core/Typography";
import Badge from "@material-ui/core/Badge";
import MenuItem from "@material-ui/core/MenuItem";
import Menu from "@material-ui/core/Menu";
import MenuIcon from "@material-ui/icons/Menu";
import AccountCircle from "@material-ui/icons/AccountCircle";
import MailIcon from "@material-ui/icons/Mail";
import NotificationsIcon from "@material-ui/icons/Notifications";
import MoreIcon from "@material-ui/icons/MoreVert";
const useStyles = makeStyles((theme) => ({
grow: {
flexGrow: 1
},
menuButton: {
marginRight: theme.spacing(2)
},
title: {
display: "none",
[theme.breakpoints.up("sm")]: {
display: "block"
}
},
search: {
position: "relative",
borderRadius: theme.shape.borderRadius,
backgroundColor: alpha(theme.palette.common.white, 0.15),
"&:hover": {
backgroundColor: alpha(theme.palette.common.white, 0.25)
},
marginRight: theme.spacing(2),
marginLeft: 0,
width: "100%",
[theme.breakpoints.up("sm")]: {
marginLeft: theme.spacing(3),
width: "auto"
}
},
searchIcon: {
padding: theme.spacing(0, 2),
height: "100%",
position: "absolute",
pointerEvents: "none",
display: "flex",
alignItems: "center",
justifyContent: "center"
},
inputRoot: {
color: "inherit"
},
inputInput: {
padding: theme.spacing(1, 1, 1, 0),
// vertical padding + font size from searchIcon
paddingLeft: `calc(1em + ${theme.spacing(4)}px)`,
transition: theme.transitions.create("width"),
width: "100%",
[theme.breakpoints.up("md")]: {
width: "20ch"
}
},
sectionDesktop: {
display: "none",
[theme.breakpoints.up("md")]: {
display: "flex"
}
},
sectionMobile: {
display: "flex",
[theme.breakpoints.up("md")]: {
display: "none"
}
}
}));
export default function App() {
const classes = useStyles();
const [, setAnchorEl] = React.useState(null);
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null);
const isMobileMenuOpen = Boolean(mobileMoreAnchorEl);
const handleProfileMenuOpen = (event) => {
setAnchorEl(event.currentTarget);
};
const handleMobileMenuClose = () => {
setMobileMoreAnchorEl(null);
};
const handleMobileMenuOpen = (event) => {
setMobileMoreAnchorEl(event.currentTarget);
};
const mobileMenuId = "primary-search-account-menu-mobile";
const renderMobileMenu = (
<Menu
anchorEl={mobileMoreAnchorEl}
id={mobileMenuId}
keepMounted
open={isMobileMenuOpen}
onClose={handleMobileMenuClose}
getContentAnchorEl={null}
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
transformOrigin={{ vertical: "top", horizontal: "center" }}
>
<MenuItem>
<IconButton color="inherit">
<Badge badgeContent={4} color="secondary">
<MailIcon />
</Badge>
</IconButton>
<p>Messages</p>
</MenuItem>
<MenuItem>
<IconButton color="inherit">
<Badge badgeContent={11} color="secondary">
<NotificationsIcon />
</Badge>
</IconButton>
<p>Notifications</p>
</MenuItem>
<MenuItem onClick={handleProfileMenuOpen}>
<IconButton color="inherit">
<AccountCircle />
</IconButton>
<p>Profile</p>
</MenuItem>
</Menu>
);
return (
<div className={classes.grow}>
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="open drawer"
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.grow} />
<div className={classes.sectionMobile}>
<IconButton onClick={handleMobileMenuOpen} color="inherit">
<MoreIcon />
</IconButton>
</div>
</Toolbar>
</AppBar>
{renderMobileMenu}
</div>
);
}
We call makeStyles
to add some styles for the menu.
Then we call the useStyles
hook which is returned by makeStyles
to return the classes
object.
Next, we define states and functions for handling the opening and closing of the menu.
To make the top right menu open below the app bar, we set getContentAnchorEl
to null
.
Then we set anchorOrigin
to an object with the vertical
property set to 'bottom'
and horizontal
set to 'center'
.
And we set transformOrigin
to an object that has the vertical
property set to 'top'
and horizontal
set to 'center'
We assign the Menu
component to renderMobileMenu
and put it below AppBar
.
Conclusion
To make a dropdown menu open below the Appbar using React Material-UI, we set the Menu
‘s getContentAnchorEl
to null
.
Then we set anchorOrigin
and transformOrigin
to objects to position the menu.