Exploring ways to update the design of my navigation bar when clicked by utilizing hooks to dynamically incorporate a new className

Can someone help me understand how to change the styling of my hamburger icon when it's clicked? I'm trying to add a new className to it using hooks and then target that class in Sass. The className is being added correctly, but the new styles aren't being applied.

I've played around with the nesting of my styles as I'm new to Sass and React, but I believe I'm targeting everything correctly.

import { useContext, useState } from "react";
import { Link } from "react-router-dom";

import classes from "./styles/MainNavigation.module.css";
import FavoritesContext from "../../store/favorites-context";

function MainNaviagtion() {
  const [isOpen, setOpen] = useState(false);
  const favoriteCtx = useContext(FavoritesContext);

  const mobileMenuHandler = () => {
    setOpen(!isOpen);
  };

  return (
    <header className={classes.header}>
      <div className={classes.logo}>Meet Up Tracker</div>
      <nav>
        <ul id={classes.menu}>
          <li>
            <Link className={classes.link} to="/">
              All Meetups
            </Link>
          </li>
          <li>
            <Link className={classes.link} to="/new-meetups">
              New Meetups
            </Link>
          </li>
          <li>
            <Link className={classes.link} to="/favorites">
              My Favorites
              <span className={classes.badge}>
                {favoriteCtx.totalFavorites}
              </span>
            </Link>
          </li>
        </ul>
      </nav>

      <div id={classes.hamburgerIcon} onClick={mobileMenuHandler} className={isOpen ? 'open' : null}>
        <div  className={classes.bar1}></div>
        <div className={classes.bar2}></div>
        <div className={classes.bar3}></div>
        <ul className={classes.mobileMenu}>
          <li>
            <Link className={classes.link} to="/">
              All Meetups
            </Link>
          </li>
          <li>
            <Link className={classes.link} to="/new-meetups">
              New Meetups
            </Link>
          </li>
          <li>
            <Link className={classes.link} to="/favorites">
              My Favorites
              <span className={classes.badge}>
                {favoriteCtx.totalFavorites}
              </span>
            </Link>
          </li>
        </ul>
      </div>
    </header>
  );
}

export default MainNaviagtion;
$mobile: 576px;
$tablet: 768px;
$laptop: 992px;
$desktop: 1200px;

.header {
  width: 100%;
  height: 5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #77002e;
  padding: 0 10%;

  .logo {
    font-size: 30px;
    color: white;
    font-weight: bold;
  }

  .badge {
    background-color: #cc2062;
    color: white;
    border-radius: 12px;
    padding: 0 1rem;
    margin-left: 0.5rem;
  }
}

#hamburgerIcon {
  margin: auto 0;
  display: none;
  cursor: pointer;

  div {
    width: 35px;
    height: 3px;
    background-color: #fff;
    margin: 6px 0;
    transition: 0.4s;
  }
}

.open {
      .bar1 {
        -webkit-transform: rotate(-45deg) translate(-6px, 6px);
        transform: rotate(-45deg) translate(-6px, 6px);
      }

      .bar2 {
        opacity: 0;
      }

      .bar3 {
        -webkit-transform: rotate(45deg) translate(-6px, -8px);
        transform: rotate(45deg) translate(-6px, -8px);
      }

      .mobile-menu {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
      }
    }

.mobileMenu {
  display: none;
  position: absolute;
  top: 50px;
  left: 0;
  height: calc(100vh - 50px);
  width: 100%;
}

.header ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: -webkit-inline-box;
  align-items: baseline;
}

.header li {
  margin-left: 3rem;
}

.header a {
  text-decoration: none;
  font-size: 20px;
  color: #fcb8d2;
}

.header a:hover,
.header a:active,
.header a.active {
  color: white;
}

@media (max-width: $tablet) {
  .header {
    .logo {
      font-size: 25px;
    }
    .link {
      display: flex;
      align-items: center;
      font-size: 15px;
      border: 2px solid black;
    }
  }

  .header ul {
    display: -webkit-inline-box;
  }

  .header li {
    margin-left: 1rem;
  }
}

@media (max-width: $mobile) {
  #menu {
    display: none;
  }

  #hamburgerIcon {
    display: block !important;
  }
}

Answer №1

It appears that you are utilizing the variable classes to access your styles. However, within your code, you are using a simple 'open' class name within the element where you intend to modify the styles.

You may want to consider revising your code for the icon to something like

isOpen ? classes.open : null

This approach should make use of the actual class name defined in the stylesheet.

Furthermore, there seems to be a discrepancy in the class names between your mobile menu. Your React code does not include a hyphen, whereas your CSS does.

Simply using the plain 'open' class may not accurately apply the desired styles. I hope this explanation helps!

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

Failing to utilize callback functions results in forgetting information

I am facing an issue with my code where changes in the parent component trigger a re-render of the child element. The Menu component is supposed to appear on right-click on top of the placeholder tag, but when it does, the entire parent component flicker ...

Should we pause JQUERY AJAX to prioritize usability, or only when absolutely necessary?

I am struggling with my LoadingStatus Function, which has two options: SHOW and HIDE. When a JQUERY POST is made, the Show option triggers to display, while the HIDE option occurs after the RESPONSE comes back. The problem I'm encountering is that s ...

Setting up the perfect environment with Vue.js, Foundation, and Webpack integration

I've been working on integrating the foundation.css file into my Vue webpack application. To achieve this, I first installed foundation using: - npm install foundation-sites. Then, I proceeded to install the extract text plugin with the following co ...

What is the process for obtaining a list of child injectors (services) from a component using Angular 4?

Within my project, I have a structure that consists of a Parent component with two child components named Child Component1 and Child Component2. Both of these child components extend the functionality of the parent component. In Child Component1, Service ...

Guide on sending data to MongoDB using Postman

I am currently facing an issue while trying to send data to the MongoDB database using Postman. Despite everything seeming fine, I keep encountering errors. https://i.stack.imgur.com/Gcf5Q.jpg https://i.stack.imgur.com/ntjwu.jpg https://i.stack.imgur.co ...

What is the best way to merge two nested arrays of objects in JavaScript that share a common parent?

Hello everyone, I am facing an issue when trying to combine two nested parent arrays of objects. I want to merge only the children items that have the same group parent. Here is my scenario: I have two arrays var arr1 = [{group: "a", items: [&quo ...

Keep the user on the current page even after submitting the parameter

I have a situation where I am loading a page into a specific div. This loaded page contains a link that includes a parameter which directs to another page for deletion. However, when I click on the loaded page within the div, it redirects me to the deletio ...

When applying a gradient on top of an image with background-image, the gradient appears larger in size compared to

Struggling with HTML <div class="outer"> <div class="bgimagegradient"></div> <img src="foo.jpg"> <div> .backgroundimage { position: absolute; right: 25px; z-index: -100; ...

Describing data types in TypeScript, when an <Array> contains various structures

I recently started using TypeScript and I'm working on eliminating all instances of any types. Issue: In my React Component, I iterate over an array of objects to extract key/value pairs. The component is passed the following props: tags, tagKeys ...

Eliminate all ul elements inside li elements except for the current li

To achieve a specific task of removing all ul elements within li except for the current li, we can employ various techniques. <ul> <li id="Li0"> <ul> <li><span>Childnode1</span></l ...

What is the best way to test a function that returns a JSX element in a unit test

I created a function that takes in a parameter and returns a JSX.Element //title.tsx export const getTitle = (pageTitle: string) => { return <title> {pageTitle} </title>; } This is how I am currently testing it: describe('Title c ...

What is the best way to navigate users to a different page when they click on a button?

I recently designed a button in Dreamweaver software. Could you please guide me on how to set up the button so that when clicked, it redirects the user to a different page using Dreamweaver? Below is the HTML code for the button: <input type="submit" ...

Generate a GridGeometry of size 16x16 that is responsive to the viewport dimensions using Three.js

The question is quite simple, I am looking for help to create a specific Three.js and WebGL project. Although I have a basic understanding of creating a Cube in Three.js and WebGL, I am struggling with a more complex task. I am currently using this pen a ...

The image in next.js has a transparent background

Currently utilizing next.js and React. Sass is being used for styling purposes. The goal is to achieve an overlapping effect where the div tag and image appear to overlap, with the image in front and a pseudo-element behind it. However, there ...

Error with react/display-name in ESLint when using dynamic imports in Next.js

I encountered an error message from ESLint that says: Component definition is missing display name This error occurred in the following code snippet: const Disqus = dynamic(() => import('@/components/blog/disqus'), { ssr: false, loading: ...

Transferring the AJAX response into a JavaScript variable

New to AJAX and JS here. I am implementing an AJAX code that fetches data from a php service. I am trying to figure out how to store the returned data in a JavaScript variable, which I can then display on the UI. Below is my AJAX code snippet: <script ...

What is the reason for the Web worker creating a new Class Instance every time a module is imported?

We are currently running a time-consuming function on a web worker. In addition, we have a Dispatcher Class that creates a single instance for other classes to utilize, as shown below: class Dispatcher { constructor() { console.log("calling"); ...

Tips for entering a value into a text field using JavaScript with Selenium WebDriver

Currently, I am utilizing WebDriver in conjunction with Java. My aim is to insert text into a text field utilizing JavaScript. What would be the best approach to achieve this task? ...

What is the process of creating and customizing popovers in Bootstrap 5 with jquery?

Is there a way to dynamically create and add content to Bootstrap 5 popovers using JavaScript or jQuery? In Bootstrap 3, I used the following method: $('#element').popover({ placement : 'left', trigger : 'focus', html: true } ...

Using JavaScript to Calculate Distance from Wifi Signal Strength (RSSI)

Has anyone discovered a reliable method for converting an RSSI signal to an accurate distance? We have experimented with various formulas, but each one yields a different outcome. One formula in particular that we've been testing is as follows: pub ...