Creating a Hamburger Menu Navigation with React and Material-UI 5
Published July 15, 2021Last updated June 6, 2024
Easily build a React hamburger menu navigation with the Library Material-UI. Understand the logic behind the Material-UI Drawer and customize it to your needs.
Going through the Material-UI drawer documentation, which in general is a great resource, I have decided to write a post to make it even easier for everyone wanting to implement the drawer.
In this post, I will show you in detail how you include a hamburger menu navigation that slides out from the side in your React web app. I also will explain how every line of code works and interacts. This lets you customize your drawer navigation even further.
First, I will go through the code bit by bit with a fully styled code example at the end of this page. As a prerequisite, you need to have a React web app set up and Material-UI installed.
1. The basic structure of the Material-UI Drawer
The example below is the basic structure of the drawer. Here it is enclosed within the navigation bar, shown by the tags <AppBar> and <Toolbar>.
First, you can see the <IconButton> which represents the actual hamburger menu icon. It has the onClick event that triggers the drawer to show up.
The sx property is new in Material-UI 5. It is a form of convenient inline JavaScript styling with access to the Material-UI Theme values. In this case, it is used to have some margin on the right side and to display the hamburger menu only on devices of the size “xs” (<600px). You can read more about the sx property in the Material-UI documentation.
Copy
The next element is the actual drawer, the navigation that slides in from the side when clicking the hamburger icon. The drawer itself covers the full viewport as you can see in the screenshot below.
Drawer Properties
Within the <Drawer> tag you can set the properties.
anchor:right, left, top, bottom
for the side of which the drawer should slide in
variant:permanent, persistent, temporary
if the drawer can be closed and how easily it can be closed, in this post I will focus on the temporary drawer
that can be more easily closed
open:boolean
when open is true, the drawer is shown
onClose:function
callback function that closes the drawer
onOpen:function
callback function that opens the drawer
You can also style the background (Drawer Outside in the screenshot), which is by default a black transparent overlay.
Enclosed in the <Drawer> tags is the actual drawer navigation. Styling the <Box> will style the whole drawer navigation area. You place anything you would like to include in the navigation within this <Box>.
2. Logic behind the opening and closing of the drawer
We now have to wrap the code from above in a React function whereas the code from above should be in the return statement:
Copy
At the beginning of the function (before the return statement) we need to declare a React useState, which we need to preserve the state of the drawer. If it would be a normal variable, the value of that variable would have been lost after the function got executed.
The useState looks like that, “open” as the variable, “setOpen” to change the value, and “false” as the initial value:
Copy
We still need to define the callback Function after the useState. We could change the open variable straight away by calling setOpen from the <Drawer> Element.
However, we then would lose the ability to switch focus between elements with the Tab-key. To make that still possible we have to capture that event through following function. It does not change the state when the user hits Shift or Tab.
Copy
3. Adding elements to the hamburger navigation
Now we should have a working hamburger navigation or drawer (do not forget the imports). The only thing that is missing are the elements within the drawer.
In my example, I have added a close button, some list elements, a search bar, and a register and login button. You can choose whatever fits your requirements. Here you can find the Icons provided by Material-UI.