This site runs best with JavaScript enabled.

Create a fancy burger menu using reactjs-popup

Youssouf El Azizi

March 11, 2018

This article is a step by step tutorial to create a simple burger menu for your website by using reactjs-popup

Reactjs-popup is a new and simple react popup component built using react fragments which is one of the new features that comes with react 16. And it can handle multiple use cases.By using this tiny react popup component you can create a Tooltips, Modals and Menus.

By the end of this article you will be able to create your custom burger menu with reactjs-popup.

Ready!! Let’s get started.

Step 1: Create the burger Icon component.

We will start by building a burger icon component,

1import React from "react";
3export default ({ open, ...props }) => (
4 <div className={open ? "burger-menu open" : "burger-menu"} {...props}>
5 <div className="bar1" key="b1" />
6 <div className="bar2" key="b2" />
7 <div className="bar3" key="b3" />
8 </div>

As you see we pass the ‘open’ prop to the component so we can permute the icon class name as the preview example explains

1.burger-menu, {
3 display: inline-block;
4 cursor: pointer;
5 position: fixed;
6 right: 20px;
7 bottom: 40px;
8 z-index: 9999;
9 background: #fff;
10 padding: 10px;
11 border-radius: 25px;
12 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
15.burger-menu .bar1,
17.bar3 {
18 width: 25px;
19 height: 3px;
20 background-color: #333;
21 margin: 4px 0;
22 transition: 0.4s;
24 .bar1 {
26 -webkit-transform: rotate(-45deg) translate(-4px, 4px);
27 transform: rotate(-45deg) translate(-4px, 4px);
29 .bar2 {
31 opacity: 0;
33 .bar3 {
35 -webkit-transform: rotate(45deg) translate(-6px, -6px);
36 transform: rotate(45deg) translate(-6px, -6px);

burger Icon state

You can find some good examples for burger icon with animation here

Step 2: Customize CSS Menu

Our menu will be a simple list, so let’s take the reactjs-popup home page menu and use it as an example.

1import React from "react";
3export default ({ close }) => (
4 <div className="menu">
5 <ul>
6 <li onClick={close}>Home</li>
7 <li onClick={close}>Getting Started</li>
8 <li onClick={close}>Component API</li>
9 <li onClick={close}>Use Case - Tooltip</li>
10 <li onClick={close}>Use Case - Modal</li>
11 <li onClick={close}>Use Case - Menu</li>
12 <li onClick={close}>Contributing</li>
13 </ul>
14 </div>

As you see this menu is a simple ul element, nothing special.

Step 3:integrate all stuff with reactjs-popup

All we need to do in this part is to import the reactjs-popup component and set the burger menu as a trigger prop for the popup component and the menu as the popup children. simple, is it ? magic !!

Thanks to the ‘function as a children pattern’ the trigger can access to the popup state easily. we need also to pass props to the burger component like the following.

Adding some custom css and this is the final result.

If you read this article from your smartphone , you can see the burger button to launch the menu in reactjs-popup home page.

Demo Code source

yjose/reactjs-popup-burger-menu_reactjs-popup-burger-menu - reactjs-popup burger menu

Thanks for reading! If you think other people should read this post and use this component,tweet and share the post.

Remember to follow me on Medium so you can get notified about my future posts.

If you liked this story, feel free to 👏👏👏 a few times (Up to 50 times. Seriously).

Share article 👉

Get updates via email

JavaScript, React, React Native and web thoughts