Sidebar menus are a form of navigation that appear on the side of a webpage. These menus provide access to different parts of the website while not interfering with the content on the page.
A button is frequently used to toggle sidebar menus open and closed. These buttons tend to have an icon of three horizontal lines to represent a menu. Because of the shape of this icon, it is commonly referred to as a “hamburger” menu icon. Two outer lines form the “bun,” and the middle line forms the “patty.”
react-burger-menu
is a library that allows you to create a sidebar for your React applications. It also comes with a multitude of effects and styles to customize the look and feel of your menu.
In this tutorial, you will use react-burger-menu
and build a sidebar for a notional restaurant website that serves salads, pizzas, and desserts.
To complete this tutorial, you’ll need:
This tutorial was verified with Node v14.7.0, npm
v6.14.7, react
v16.13.1, and react-burger-menu
v2.7.1.
Start with using create-react-app
to generate a React App and then install dependencies:
Change into the new project directory:
Next, open index.css
:
Add the following styles:
This code sets the box-sizing
property to border-box
to address issues with CSS box model calculations. This code also establishes some vertical padding to prevent collapsed margins.
Save the changes to index.css
and then open App.js
:
Remove the logo.svg
and replace the content of the App
component with a message from the example restaurant:
outer-container
and page-wrap
are important id
s that will be referenced later by react-burger-menu
.
After saving your changes, you can run the React application:
Fix any errors or issues with your project and then open up localhost:3000
in a web browser:
Once you have a Cool Restaurant message properly displaying, you can start building the sidebar.
Your sidebar will require react-burger-menu
and a React component.
First, install react-burger-menu
:
Now, create a new Sidebar.js
file for a new Sidebar
component:
Add the following code:
This code will generate a Sidebar
with menu links to Home
, Salads
, Pizzas
, and Desserts
.
This tutorial will not go into creating separate routes for these items. It will instead be focused on the functionality of the Sidebar
itself.
react-burger-menu
will require you to provide some styles, so create a Sidebar.css
file:
Add the default styles provided by the react-burger-menu
documentation:
For your tutorial, you will need to apply a link color and a hover color. Modify Sidebar.css
to add these custom styles:
Now, add ./Sidebar.css
to your Sidebar.js
file:
Finally, to use the Sidebar
in your application, you will need to revisit App.js
:
Import the Sidebar
component and add it to your App
component:
At this point, you can run your application again:
Fix any errors or issues with your project. And visit localhost:3000
in a web browser:
Now, if you click the “hamburger” menu icon, your sidebar menu will appear with links to Home
, Salads
, Pizzas
, and Desserts
:
Next, you can explore some of the advanced customization available to react-burger-menu
.
Right now, your sidebar uses a slide
animation. react-burger-menu
comes with ten animation possibilities.
To try out another animation, open Sidebar.js
:
And replace slide
with a different animation:
This code will cause the menu to appear with a bubble effect.
This code will cause the menu to appear with an elastic effect.
The full list of animations can be found in the react-burger-menu
docs.
The documentation also features advanced customizations for getting the sidebar menu to suit your application.
In this tutorial, you used react-burger-menu
to create a sidebar menu. A sidebar menu is one common solution for navigating a website.
Alternative libraries for sidebar menus exist, and you may find one that is better suited for your application.
If you’d like to learn more about React, take a look at our How To Code in React.js series, or check out our React topic page for exercises and programming projects.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
This comment has been deleted
How to add an image in the sidebar
Hi, do this menu have the posibility to make items a sub-menu that hold other items inside?
I am not able to install it whit
node init vite
, is there a way to use it? because my lapton cannot really work whitcreate-react-app