Create Sidebars using react-burger-menu in React.js

To create a sidebar in React, use the react-burger-menu or create a sidebar component using the Material UI library. This library will help us to build a sidebar menu component in React.js. The Sidebar is an integral component in React-powered web applications. We are not using Redux so we will build with plain React.js.

Here are the steps to create sidebars using the react-burger-menu in React.js:

Step 1: Install React.js

Type the following command to install React.js.

npx create-react-app sidebar

Go inside the folder and open it in the VSCode editor.

cd sidebar
code .

Install the react-burger-menu library using the following command.

npm install react-burger-menu --save

# or

yarn add react-burger-menu

Step 2: Create a Sidebar Menu

Inside the src folder, create one file called sidebar.js. In this sidebar component, we can add a list of items displayed inside the Sidebar. Here, we will make use of the react-burger-menu as well.

// sidebar.js

import React from 'react';
import { slide as Menu } from 'react-burger-menu';

export default props => {
  return (
    <Menu>
      <a className="menu-item" href="/">
        Home
      </a>

      <a className="menu-item" href="/laravel">
        Laravel
      </a>

      <a className="menu-item" href="/angular">
        Angular
      </a>

      <a className="menu-item" href="/react">
        React
      </a>

      <a className="menu-item" href="/vue">
        Vue
      </a>

      <a className="menu-item" href="/node">
        Node
      </a>
    </Menu>
  );
};

Here we have imported the slide component from the react-burger menu

Step 3: Import the sidebar.js file inside an App.js file.

// App.js

import React from 'react';
import SideBar from './sidebar';

import './App.css';

export default function App() {
  return (
    <div id="App">
      <SideBar />
      <div id="page-wrap">
        <h1>AppDividend</h1>
        <h2>Check out our tutorials the side menubar</h2>
      </div>
    </div>
  );
}

Now, finally, add the CSS code inside the App.css file.

html,
body {
  margin: 0;
}

#App {
  font-family: sans-serif;
  height: 100vh;
}

#page-wrap {
  text-align: center;
  overflow: auto;
}

.bm-item {
  display: inline-block;
  text-decoration: none;
  margin-bottom: 10px;
  color: #d1d1d1;
  transition: color 0.2s;
}

.bm-item:hover {
  color: white;
}

.bm-burger-button {
  position: fixed;
  width: 36px;
  height: 30px;
  left: 36px;
  top: 36px;
}

.bm-burger-bars {
  background: #373a47;
}

.bm-cross-button {
  height: 24px;
  width: 24px;
}

.bm-cross {
  background: #bdc3c7;
}

.bm-menu {
  background: #373a47;
  padding: 2.5em 1.5em 0;
  font-size: 1.15em;
}

.bm-morph-shape {
  fill: #373a47;
}

.bm-item-list {
  color: #b8b7ad;
}

.bm-overlay {
  background: rgba(0, 0, 0, 0.3);
}

After saving the file, go to the terminal and start the React development server.

npm start

How To Create Sidebar Component In React

So at this URL: http://localhost:3000/, you can see that our React.js project is up and running.

To try out our different animations, change the imported slide at the top of our sidebar.js file to any other animations, such as bubbles. To use a different animation, you can substitute slides with any following.

1. slide
2. stack
3. elastic
4. bubble
5. push
6. pushRotate
7. scaleDown
8. scaleRotate
9. fallDown
10. reveal

Properties

Some animations require certain other elements to be on your page.

Page Wrapper

An element wrapping the rest of the content on your page.

<Menu pageWrapId={ "page-wrap" } />
<main id="page-wrap">
  .
  .
  .
</main>

Outer container

An element containing everything, including the menu component.

<div id="outer-container">
  <Menu pageWrapId={ "page-wrap" } outerContainerId={ "outer-container" } />
  <main id="page-wrap">
    .
    .
    .
  </main>
</div>

Create React Sidebar Component using Material UI

Let’s create a simple sidebar component in React using Material UI.

First, install the following material package using Yarn or NPM.

yarn  add @material-ui/core @material-ui/icons

# or

npm install @material-ui/core @material-ui/icons

Once that is installed, we must consider it a base structure in the user interface that our Sidebar will be built upon.

A solution is to use an unordered list (<ul>) element that renders the list items (<li>).

We will import List and ListItem from the @material-ui/core library since the List component is essentially the ul element, and the ListItem component is essentially the li.

Let’s start hardcoding the items in the Sidebar to visualize how this might look to boost our confidence. Sometimes a little extra confidence can help improve our productivity:

Now, create a folder inside the src folder called components.

The next step is to write the following code inside the src >> components >> Sidebar.js file.

Write the following code inside that file.

// Sidebar.js

import React from 'react'

function Sidebar() {
  return null
}

export default Sidebar

Import the component inside the src >> App.js file.

// App.js

import React from 'react';
import './App.css';

import Sidebar from './components/Sidebar';

 function App() {
    return (
      <div>
        <Sidebar items={items}/>
      </div>
    );
  }

export default App;

Now, we add the items which we will display in the Sidebar. Let’s take five elements.

Write the following code inside the App.js file.

// App.js

import React from 'react';
import './App.css';

import Sidebar from './components/Sidebar';

const items = [
  { name: 'home', label: 'Home' },
  { name: 'sales', label: 'Sales' },
  { name: 'orders', label: 'Orders' },
  { name: 'billing', label: 'Billing' },
  { name: 'settings', label: 'Settings' }]

function App() {
  return (
    <div class="sidebar">
      <Sidebar items={items} />
    </div>
  );
}

export default App;

We will also expect an items prop with an array of objects representing each item in the sidebar menu. But, again, we want to keep the functionality as simple as possible, or we could quickly overcomplicate the component.

You might have noticed that our Sidebar is just too dang big! *Side*bars usually take up one side of the screen. So we are going to shrink its width to a suitable size. We will go ahead and put a max-width of 200px on it. So we will create a div element that wraps our List component.

We create another div element instead of directly applying the styles on the List component because we don’t want to make List responsible for the width size.

This way, in the future, we can abstract the List into a reusable sidebar component that can adapt to any size depending on the size of the parent element.

So, add one css class inside the App.css file.

.sidebar {
  max-width: 240px;
  border: 1px solid rgba(0, 0, 0, 0.1);
}

Material-UI uses its CSS styling mechanism using the CSS-in-JS approach. But we will stick to regular CSS in this article to keep things unnecessarily complicated.

Write the Sidebar.js component.

// Sidebar.js

import React from 'react'
import List from '@material-ui/core/List'
import ListItem from '@material-ui/core/ListItem'
import ListItemText from '@material-ui/core/ListItemText'

function Sidebar({ items }) {
  return (
    <div className="sidebar">
      <List disablePadding dense>
        {items.map(({ label, name, ...rest }) => (
          <ListItem key={name} button {...rest}>
            <ListItemText>{label}</ListItemText>
          </ListItem>
        ))}
      </List>
    </div>
  )
}

export default Sidebar

Save the file and see the output. 

react sidebar component

That’s it for the sidebar component in React.

Thanks for taking it.

6 thoughts on “Create Sidebars using react-burger-menu in React.js”

    • how i reduce the size of the menu , i saw the sidebar has a width of 300px but i want reduce to 225 px the property name is bm-menu-wrap. i know this property is for react-menu-burger but can you guide me about what file i will has to modify ?

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.