Clicking on the icon will reveal a drop-down menu in ReactJS

Currently, I have a Navbar setup with a conditional statement that displays the user-icon if the userstatus is true (indicating the user is signed in), otherwise it shows the signin. My goal is to make it so that when I click on either the user-icon or signin, a drop-down menu appears. I plan on using this for a logout button later on. I'm having trouble figuring out how to achieve this functionality. While looking at the documentation on , I noticed that I can implement something similar but I want to use my own icon (glyphicon glyphicon-user) instead of the standard rectangle box.

https://i.sstatic.net/BsOtL.png

  • I want to achieve a design like the one shown below, but instead of using a Dropdown button, I want to incorporate the user-icon.

https://i.sstatic.net/ie0sh.png

If you have any suggestions or pointers, please share them with me.

-Code excerpt

<li>
  <a className="active" href="/signin">
    {userstatus ? (
      <a className=" signin-icon glyphicon glyphicon-user"></a>
    ) : (
      <a>SIGNIN</a>
    )}
  </a>
</li>

Answer №1

One option that I do not recommend is to override the Bootstrap CSS, as it can be a lot of work and hassle. This involves resetting all the CSS and using !important for a lot of styles.

Another alternative is to create your own dropdown menu, which I personally find to be much simpler than dealing with the previous solution's complications.

If you are using React hooks, for example:

const [isDropdownOpen, setDropdownOpen] = useState(false);
const [list, setList] = useState([1, 2, 3]);
const toggleDropDown = () => setDropdownOpen(!isDropdownOpen);
const DropdownList = useMemo(() => list.map(el => <div>el</div>));
return (
<>
<button class="signin-icon glyphicon glyphicon-user" onClick={toggleDropDown}>
{isDropdownOpen ? DropdownList : false}
</>
)

Answer №2

Insert the following classes

<Dropdown.Toggle variant="light" className="bg-white border-0 p-0 success" id="dropdown-basic">
    <ADD YOUR ICON HERE>
  </Dropdown.Toggle>

css

.dropdown-toggle:after {
   border: 0;
}

Answer №3

After reviewing the provided link, you have the opportunity to customize the following:

<Dropdown>
  <Dropdown.Toggle variant="success" id="dropdown-basic">
    {* insert your preferred icon code *} (//for example, I used <FaBeer /> )
  </Dropdown.Toggle>

  <Dropdown.Menu>
    <Dropdown.Item href="#/action-1">First Action</Dropdown.Item>
    <Dropdown.Item href="#/action-2">Second Action</Dropdown.Item>
    <Dropdown.Item href="#/action-3">Another Option</Dropdown.Item>
  </Dropdown.Menu>
</Dropdown>

You can modify the actions as desired within the Dropdown.Menu tag.

Answer №4

There are many ways to accomplish this task. By doing a simple search engine query, you'll come across numerous methods. One technique involves using pure CSS to create a dropdown menu by placing menu items and a menu button within a parent div. The menu items will only appear when the parent div is hovered over.

.dropdown {
  width: 100px;
  background: purple;
}
.dropdown .item {
  display: none;
}

.dropdown:hover .item{
  display: block;
}
<div class="dropdown">
<button>
Button
</button>
<div class="item">
<p>
item 1
</p>
</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a clickable pagination box involves utilizing CSS to style the entire <li> element with the necessary properties

Is there a way to make the entire LI box clickable for the pagination box? The HTML and CSS parts are functioning correctly, as I am able to click the link with the page number to navigate to the next page. However, the issue arises when trying to click t ...

Troubleshooting Guide: Resolving the Error Message 'ControlContainerAngular' Provider Not Found in Your Angular App

Incorporating bootstrap-4 into my angular7 application for design purposes. <nav class="navbar navbar-light bg-light"> <form class="form-inline"> <div class="input-group"> <div class="inp ...

Counting items in AngularJS filters: a step-by-step guide

I have a question regarding my jsfiddle example. Here is the link to my jsfiddle How can I ensure that it counts the number and only displays once instead of repeating multiple times? This is my HTML code: <section class="left" style="border-right:1 ...

What is the best way to implement a toggle effect for my specific situation?

I need assistance creating a toggle feature for my angular app. This is what I currently have: <div ng-repeat='item in items'> <div ng-click='toggle-{{item.number}} =!toggle-{{item.number}}> <span> ...

Creating a responsive ListView/DataView in BootstrapWould you like to know how to

I am in the process of creating my very first webshop. I have been trying to populate my webpage with all the products from my SQL database using a listview. However, I encountered an issue where the design is not responsive. My goal is to display the prod ...

Guide to importing a JSON file?

Is there a way to access JSON data from a separate file? I attempted: obdatabase.json { "pobject" :[ { "pname":"Pikachu" , "pid":"1" }, { "pname":"squirtle" , "pid":"2" }, { "pname":"Justinbieber" , "pid":"3" } ]}; test.php <script src= ...

Identifying DNS lookup errors in JavaScript: A beginner's guide

Is there a method to identify DNS lookup errors through JavaScript? Are there any code snippets or techniques that can achieve this? Is it a challenging task or is there a solution available? If anyone can provide some insight on this, I would greatly a ...

Experience working with MySQL, PHP, JSON, and JavaScript

When working with JavaScript that retrieves data from a server’s database and displays it on a webpage, issues can arise. $.getJSON( 'ajax.php',{id:$('bla').val()}, function(json) { $('#myClone').clone().removeAttr(&apo ...

Revamping HTML Label 'for' with JavaScript: Unveiling Effective Techniques

I'm facing an issue with changing the target of a label that is associated with a checkbox using JavaScript. Here's the code I have: <input type="checkbox" id="greatId" name="greatId"> <label id="checkLabel" for="greatId">Check Box&l ...

Arranging website elements to conform to a compact grid system

Recently, I've been brainstorming a unique concept for a website layout that involves a background composed of tiny color-shifting squares, each measuring about 10px. The challenge I'm facing is aligning the elements on the page with this grid, a ...

MUI DataGrid - Accessing the GridColumnsPanel

Within the MUI DataGrid, there is an option to access the column visibility panel by clicking on the options menu located on any header column. However, I have not yet discovered a method to display this panel from outside the Grid. I had envisioned a but ...

Looking to automate the clicking of a JavaScript URL upon loading the page

I am currently utilizing the following JavaScript function for a link on the homepage. The requirement is for the link to be automatically clicked upon page load. Link- document.writeln("<a href=\"javascript: live_chat_link(m_sTicketType ...

Using d3.js to toggle visibility of bars when clicking on the legend

// Handling the browser onresize event window.onresize = function () { scope.$apply(); }; scope.render = function (data) { var ageNames = d3.keys(data[0]).filter(function (key) { ...

The troubleshooting issue with jQuery animate Scrolltop malfunctioning

My goal is to use jQuery to scroll the page to a specific div when a button is clicked. Despite not seeing any errors in the JavaScript console, the page does not actually scroll to the desired location. I have tried placing the jQuery js file before and a ...

When attempting to display the image file path using the JavaScript API Notification in Angular, there is a challenge in

Hello fellow developers, I am currently facing an issue with retrieving a local image from my assets folder and displaying it on a new Notification object generated by the Web Notification API. I have an image in my assets folder with a .jpeg extension. ...

Preserving the selected options in a dynamically populated dropdown list after submitting a form using php

I am attempting to preserve form values even after submitting the form. document.getElementById('start_date').value = "<?php echo $_POST['start_date'];?>"; document.getElementById('end_date').value = "<?php echo $_P ...

alignment of text output and label that responds to various screensizes

Here is some html code with a label and an output next to it. <div class="panel-body"> <div class="col-sm-6"> <label class="head6">Company Name : </label><span class="head9 halfR"> ...

Executing a function stored in an array using JavaScript

While practicing JavaScript, I came across a sample code where I tried to call a function from an array. However, the output returned undefined along with the expected result from the function. I'm confused as to why this is happening, especially sinc ...

Encountering an issue with the for loop in React Native when using FlatList

As a beginner in programming, I am eager to dynamically render a list. The Navbar parent component holds the state with different Food Types categories such as Mexican and Chinese, each with its corresponding menu. My goal is to display each Food Type fol ...

Setting the width to 100% in the CSS file is not functioning properly in Chrome browser

Below is the CSS code I am using: td.tdhover input:hover,td.tdhover select:hover,td.tdhover textarea:hover{ background: #f2f5a9; color:#ff0000; } td.tdhover select, td.tdhover input, td.tdhover textarea{ position:relative; width:100%; } td.tdhover inp ...