Incorporating both an X button and hamburger menu is essential in my Bootstrap design, especially since I am working with React.js

Currently, I am working with the most recent version of Bootstrap and aiming to switch between the recognizable X icon and a hamburger menu. It seems like these two icons are overlapping each other, and I'm unsure if I can modify the default 3 lines of the menu.

There are two main questions that arise from this situation:

  1. How can I effectively toggle between these two icons?

  2. When the menu is activated, I expect it to cover the entire screen in white (at the moment, it doesn't achieve full white coverage)

Unfortunately, I don't have a sandbox example available for reference right now.

Your insights and suggestions on this matter would be greatly appreciated!

/*JSX code*/
import React from 'react';
import { NavigationBarStyled } from './style';
import { Nav, Navbar } from 'react-bootstrap';
//IMAGES
import logo from '../../images/adyslogo.png';
import Image from 'react-bootstrap/Image';
import { GrCart } from 'react-icons/gr';
import CloseButton from '../../images/closebutton.svg';

// Rest of the JSX code remains same as provided in the original text

export default NavigationBar;


/*CSS*/
import styled from 'styled-components';


export const NavigationBarStyled = styled.nav`
    .navbar {
        background-color: #FFF;
        height: 80px;
    }

    // CSS styling continues...

`;

Answer №1

If you're looking to toggle state easily, remember that you may not get the smooth height calculation that Bootstrap provides.

import React, { Component } from "react";
//import { Link } from "gatsby";
//import Image from "./image";
import "../../scss/molecules/_topnav.scss";

//const helpers = require("../../helpers");

class Nav extends Component {
    state = { collapse: true };

    

    constructor(props) {
        super(props);

        this.brandLogo = this.props.brandLogo;
        this.listItems = this.props.pages
            //.sort((a, b) => (a.node.navigationOrder || 100) - (b.node.navigationOrder || 100))
            .map((page) => page.node.slug && page.node.pageName && !page.node.hideInNavigation && (
            <li className="nav-item" key={page.node.id}>
                <Link to={`${page.node.slug}`} className="nav-link" title={page.node.pageName}>{page.node.pageName}</Link>
            </li>)
       );
  }

  render() {
      return (
          <>
              <nav className={[
                  "navbar", 
                  "navbar-expand-lg", 
                  "navbar-light", 
                  "fixed-top", 
                  this.state.collapse ? "" : "active"].join(" ")}
              >
                  <a className="navbar-brand" href="/"><Image fluid={this.brandLogo} /></a>
                  <button 
                      className="navbar-toggler"
                      type="button"
                      data-toggle="collapse"
                      data-target="#topnav"
                      aria-controls="topnav"
                      aria-expanded={!this.state.collapse}
                      aria-label="Toggle navigation" onClick={() => {
                          this.setState({ collapse: !this.state.collapse });
                      }
                  }>
                      <span className="navbar-toggler-icon"></span>
                  </button>
                  <div
                      className={[
                          "collapse",
                          "navbar-collapse",
                          "fade",
                          !this.state.collapse ? "show" : "",
                      ].join(" ")} id="topnav">
                      <ul className="navbar-nav ml-lg-auto">{this.listItems}</ul>
                  </div>
                </nav>
            </>
        );
    }
}

export default Nav;

If you want an SCSS solution for covering state and mobile vs desktop navigation, consider a simple toggle between display "none" and "block". You can use transitions to adjust for height calculation discrepancies.

You can find the full code at devlab.career. Hopefully, this example helps you implement something similar too.

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 CSS wrappers around div elements of varying sizes

I am looking for a way to "wrap" different divs of varying sizes, similar to how Microsoft Word wraps text around an image. I have a simplified structure outlined below: <div id = "div1"></div> <div id = "div2"></div> <div id = ...

An error is anticipated when () is added, but surprisingly, I still encounter an error as well. This issue arises in React-Native and Typescript

I am still relatively new to React-Native, but I have been using React-Native, TypeScript, and integrating it with Firebase. An unusual error has occurred, which is visible in the screenshot below. However, when checking in VSC, the error seems to be non-e ...

Employing state management in React to toggle the sidebar

A working example of a sidebar that can be toggled to open/close using CSS, HTML and JavaScript is available. Link to the Example The goal is to convert this example to React by utilizing states instead of adding/removing CSS classes. To ensure the side ...

Is there a workaround for utilizing a custom hook within the useEffect function?

I have a custom hook named Api that handles fetching data from my API and managing auth tokens. In my Main app, there are various ways the state variable "postId" can be updated. Whenever it changes, I want the Api to fetch new content for that specific p ...

Discrepancy in image dimensions between Android and iOS

Currently, I am in the process of building a website and have encountered an issue with the display of images on Android and iOS phones. The image appears differently on an Android Galaxy J-5 compared to an iOS iPhone 6s screen. My goal is to make it look ...

What is the best way to pass a function variable as a prop in a React JS component?

import React, { useState } from 'react' import Display from './components/Display'; const App = () => { const [input,setInput] = useState(""); const fetchData = async () => { const weatherAPI = await fet ...

Tips for smoothly transitioning from a simple transform to a rotate effect

I need help with an HTML element that needs to rotate when hovered over. Here is the code I have: The issue I'm facing is that I don't want a transition for translateX, only for the rotation. What steps should I take to achieve this? .cog { ...

What method does the CSS standard dictate for measuring the width of an element?

In terms of measuring an element's width, Chrome appears to calculate it from the inner margin inclusive of padding. On the other hand, Firefox and IE determine the width of the box from the border, excluding padding but including the margin. Personal ...

What is the method to have the text cursor within a text field start a few pixels in?

I need a text field with the cursor starting a few pixels (let's say 4) from the left-hand side. I am aware that this can be achieved by adjusting the size of the text field using padding, but I am curious if there is a way to resize the text box with ...

What is the best way to adjust the position of an image background within a container without affecting the container's own placement?

While working on a registration form, I encountered an issue with moving the background image within a container. Despite adding position: relative and using top: with a negative value to shift the image, the entire container moved instead of just the back ...

Animation event in CSS3 fails to trigger on Firefox browser

Exploring the potential of CSS3 animation to detect when an input transitions from the enable to disable state, I encountered a challenge specifically in Firefox. Here is the code snippet that showcases the issue: View Demo on jsFiddle HTML: <input t ...

Dealing with side effects in react/redux: Best practices and tips

Trying to find the best way to integrate an async side-effects handler into my react/redux setup has been quite a challenge. In my react-router-driven application, all the main containers at root level are smoothly dispatching actions and receiving update ...

Issue with CSS3 animations

.button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; ...

Customizing the Style of Page Numbers in Material UI Pagination – A Tailored Approach

As a newbie to using material-ui, I am currently working on creating this component. https://i.stack.imgur.com/2BkYM.png I have managed to style the next and previous buttons in a similar way as shown in the image. The default style displays the page num ...

What is the most effective method for incorporating a floating section next to a Susy gallery?

Check out my latest progress on the codepen here The number of items displayed in the gallery is dynamic based on search results. However, I am looking to add a fixed area to the right side that remains visible as the user scrolls through the gallery. Ess ...

Stop iPhone body scrolling when fullscreen overlay is opened

My goal is to prevent the body from scrolling when my fullscreen overlay navigation is open. I have added a class show-nav to the body with the CSS attribute overflow: hidden, which works perfectly on desktop but not on iPhones. After researching similar ...

Capturing the action phase in Liferay to change the cursor to 'waiting' mode

I'm currently working on a large Liferay project and have encountered a specific issue: Whenever something in the system is loading or processing, I need to change the cursor to a waiting GIF. While this is simple when using Ajax, there are many inst ...

Achieving a targeted field from the response to input into the getOptionLabel function for autocomplete customization

When dealing with the response from 'ItemData', I need to extract a specific field, such as 'itemNo', and assign it to 'ItemData'. This will allow me to utilize it in the getOptionLabel method of autocomplete. My requirement i ...

Ensure that the navigation bar remains consistent when switching between pages

My issue lies within the React components I am working with. Essentially, my goal is to create a Single Page Application (SPA) using ReactJS. While setting up authentication using auth0-js, I also established some routes for navigation. The layout is stru ...

Having trouble with Material UI V5's <ListItemButton> onClick function not responding?

I've encountered an issue with my search suggestions list created using the Link API. The onClick handler doesn't trigger as expected. How should I go about fixing this? Below is the code for my list: <Paper elevation={5} sx={{ ma ...