Sidebar Nested Dropdown Menu Reactjs
NickName:Mugi Ask DateTime:2021-07-15T04:36:05

Sidebar Nested Dropdown Menu Reactjs

I tried to build a nested dropdown menu from scratch on a sidebar.

The issue is when I open a nested menu it opens all nested menu that is in the sidebar at the same time, how should I manage to only open the target one ?

Btw if there's is a better and neat pattern please let me know. Thank you in advance for the insights.

import React, { useState } from "react";
import dropIcon from "./../../assets/dropArrow.svg";
import "./styles.scss";

const PhoneNavigation = ({ openSideBar }) => {
  const [openSub, setOpenSub] = useState(false);

  const handleOpenSubMenu = (e) => {
    if (!openSub) {
      setOpenSub(true);
      e.target.previousElementSibling.firstElementChild.classList.add(
        "show_sub_menu"
      );
    } else {
      setOpenSub(false);
      e.target.previousElementSibling.firstElementChild.classList.remove(
        "show_sub_menu"
      );
    }
  };

  return (
    <nav className={openSideBar ? "side_bar show_side_bar" : "side_bar"}>
      <ul>
        <div className="side_bar_item">
          <li className="side_bar_link">
            Courses
            <div className="sub_menu_box">
              <ul>
                <li className="sub_menu_link">Online</li>
                <li className="sub_menu_link">Schedule</li>
                <li className="sub_menu_link">Private</li>
                <li className="sub_menu_link">Classes</li>
              </ul>
            </div>
          </li>
          <img
            onClick={handleOpenSubMenu}
            src={dropIcon}
            alt="dropdown-menu-icon"
          />
        </div>
        <div className="side_bar_item">
          <li className="side_bar_link">
            About us
            <div
              className={
                openSub ? "sub_menu_box show_sub_menu" : "sub_menu_box"
              }
            >
              <ul>
                <li className="sub_menu_link">FAQ</li>
                <li className="sub_menu_link">Qualifications</li>
                <li className="sub_menu_link">Contact us</li>
              </ul>
            </div>
          </li>
          <img
            onClick={handleOpenSubMenu}
            src={dropIcon}
            alt="dropdown-menu-icon"
          />
        </div>
        <div className="side_bar_item">
          <li className="side_bar_link">
            About us
            <div
              className={
                openSub ? "sub_menu_box show_sub_menu" : "sub_menu_box"
              }
            >
              <ul>
                <li className="sub_menu_link">FAQ</li>
                <li className="sub_menu_link">Qualifications</li>
                <li className="sub_menu_link">Contact us</li>
              </ul>
            </div>
          </li>
          <img
            onClick={handleOpenSubMenu}
            src={dropIcon}
            alt="dropdown-menu-icon"
          />
        </div>
      </ul>
    </nav>
  );
};

export default PhoneNavigation;

// Style Sheet

$base-color: #081a45;
$base-color2: #00aae7;

.side_bar {
  position: fixed;
  background-color: white;
  top: 100px;
  width: 100%;
  height: 100%;
  z-index: 99;
  transform: translate(-100%);
  transition: all 5s ease-in;

  .side_bar_item {
    display: flex;
    width: 90%;
    align-items: center;
    margin: 20px auto;
    border-bottom: 1px solid $base-color;
    padding-bottom: 30px;
    position: relative;

    li {
      list-style: none;
    }
    .side_bar_link {
      color: $base-color;
      font-weight: bold;

      .sub_menu_box {
        position: relative;
        left: 20px;
        width: 300px;
        display: none;

        .sub_menu_link {
          margin: 8px 0;
          font-size: 0.9rem;
          color: $base-color2;
          font-weight: normal;
        }
      }
      .show_sub_menu {
        display: block;
      }
    }
    img {
      position: absolute;
      cursor: pointer;
      width: 15px;
      top: 5px;
      left: 95%;
    }
  }
}

.show_side_bar {
  transform: translate(0);
  transition: all 5s ease-in;
}

Copyright Notice:Content Author:「Mugi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68384777/sidebar-nested-dropdown-menu-reactjs

More about “Sidebar Nested Dropdown Menu Reactjs” related questions

Sidebar Nested Dropdown Menu Reactjs

I tried to build a nested dropdown menu from scratch on a sidebar. The issue is when I open a nested menu it opens all nested menu that is in the sidebar at the same time, how should I manage to only

Show Detail

Nested sidebar menu with material ui and Reactjs

I am trying to develop a sidebar menu using material ui. I am able to make it for simple list. In my project I have a requirement of nested sidebar menu which I am not able to achieve. If I am tryi...

Show Detail

Semantic UI Sidebar & Dropdown menu

I am trying to create a simple web ui with semantic ui. I want a sidebar with a menu in it in some items will have subitems... Shouldn't be that hard hu? http://jsfiddle.net/ycm8ctfx/ &lt;div cl...

Show Detail

Bootstrap sidebar dropdown menu

Is there a bootstrap-based sidebar navigation panel with a drop down? I've looked extensively and cannot locate one. Here is my current code (It creates a sub menu but the drop down is unfunctional...

Show Detail

Hiding sidebar and dropdown menu

I want to hide drop down menu and sidebar when you click outside each "divs". I added @media rule and also have a button for each div the will show and hide each divs. I used javascript to hide and...

Show Detail

Sidebar menu dropdown coming down from the right

I have completed the sidebar menu already but I want it to dropdown coming from the right but mine is coming downwards. I try to search for those whose dropdown list comes out from the right but I ...

Show Detail

ReactJS multi level dropdown menu

I'm trying to build a multi level ReactJs dropdown menu. I'm using the following post/code as base:the following post and this jsfiddle code as starting point of my ReactJs component: class Dropdo...

Show Detail

How to Create a Dropdown menu in Thesis Sidebar?

I have an issue with Thesis Wordpress theme. I want to create a dropdown menu for my categories and archives in the sidebar, not the navigation menu. If anyone knows how or can point me to a website

Show Detail

Problems creating sidebar toggle dropdown menu

I'm working on http://www.variied.com/market/men/. I'm trying to create a toggle dropdown menu on the sidebar that is triggered when someone hits the "Tops" link on the sidebar, which will then tog...

Show Detail

Bootstrap 4 Sidebar Menu Dropdown

I need to create a Bootstrap 4 Sidebar Menu Dropdown, as the below (image). I'm think about use Dropright buttons, I looking for good exemples of code, but unsuccessfully... Could any one give me a

Show Detail