The issue of the back button not functioning in the React Multi-level push menu SCSS has

I have been developing a mobile-friendly Multi-level push navigation menu for my website using dynamically generated links based on projects from my GitHub account. I found a push menu on CodePen here and am in the process of integrating it into React instead of just using plain HTML and CSS. Everything seems to be working well except for the Back Button.

https://i.stack.imgur.com/AHyKP.png

https://i.stack.imgur.com/ZqaD9.png

Interestingly, when I scroll to the bottom of the page and click on any empty space under "Shell," it acts as a back button and takes me to the previous menu. This push menu does not utilize JavaScript, but I am willing to incorporate it if it can resolve this issue. I have attempted troubleshooting numerous times, but some parts of the SCSS are unfamiliar to me. My website is built on Next.js using React. Below are snippets of my render function and my sass code. Feel free to reach out if you have any questions or require more information.

This is the render() in the index.jsx

render() {
    return (
        <div className={`${this.renderCSS()}`}>
            <div className="nav">
                <nav>
                    <a className="mobile-menu-trigger">Open mobile menu</a>
                    {/* <button className={'mobile-menu-trigger'} >Mobile Menu</button> */}
                    <ul className="menu menu-bar">
                        <li>
                            <a className="menu-link menu-bar-link" aria-haspopup="true">Projects</a>
                            <ul className="mega-menu mega-menu--multiLevel">
                                <li className="mobile-menu-back-item">
                                    <a className="menu-link mobile-menu-back-link">Back</a>
                                </li>
                                {Object.keys(this.props.languages).map(language => {
                                    const allProjects = this.props.languages[language];
                                    return (
                                        <li key={language}>
                                            <a className="menu-link mega-menu-link" aria-haspopup="true">{language}</a>
                                            <ul className="menu menu-list">
                                                {allProjects.map((item, index) => {
                                                    const project = this.props.projects[item];
                                                    const openGithubRepo = () => {
                                                        window.open(project.svn_url, "_blank");
                                                    }
                                                    const openGithubPages = () => {
                                                        window.open(`https://ngwessels.github.io/${project.name}/`, "_blank");
                                                    }
                                                    if (project.has_pages) {
                                                        return (
                                                            <li key={project.id}>
                                                                <a className="menu-link menu-list-link"
                                                                    aria-haspopup="true">{project.name}</a>
                                                                <ul className="menu menu-list">
                                                                    <li>
                                                                        <a className="menu-link menu-list-link" onClick={openGithubRepo}>Open Github</a>
                                                                    </li>
                                                                    <li>
                                                                        <a className="menu-link menu-list-link" onClick={openGithubPages}>Visit Live Example</a>
                                                                    </li>
                                                                </ul>
                                                            </li>
                                                        );
                                                    } else {
                                                        return (
                                                            <li key={project.id}>
                                                                <a className="menu-link menu-list-link" onClick={openGithubRepo}>{project.name}</a>
                                                            </li>
                                                        )
                                                    }
                                                })}

                                            </ul>
                                        </li>
                                    )
                                })}

                            </ul>
                        </li>

                        <li>
                            <a className="menu-link menu-bar-link">Static link</a>
                        </li>

                    </ul>
                </nav>
            </div>
        </div>
    )
}

And here is the styles.scss

      $color-accent:                      black;
  $color-light:                       #ffffff;
  $color-dark:                        black;
  $menu-link-padding:                 20px 25px;
  $breakpoint:                        950px;
  $mega-menu-multiLevel-colWidth:     100/3 + 0%;
  $mobile-menu-back-height:           "calc(1.4em + 40px)";
  $mobile-menu-back-offset:           "calc(0px - (1.4em + 40px))";
  $menu-mobile-width:                 350px;




  // ------------------ SHARED STYLES

  nav {
    ul, li {
      list-style: none;
      padding: 0;
      margin: 0;
    } 
    a {
      display: block;
      text-decoration: none;
      &:hover, &:visited {
        text-decoration: none;
      }
    }
  }
  .nav {
    a {
      font-family: 'Arial Black';
    }
  }

  .menu-bar {
    background: $color-light;
    display: flex;
  }

  .menu-link {
    padding: $menu-link-padding;
    background: $color-light;
    color: $color-accent;
    transition: background .2s, color .2s;
    position: relative;
    z-index: 1;
    &[aria-haspopup="true"] {
      padding-right: 40px;
      &:after {
        content: "";
        background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/1397521/arrowRight.svg#accent');
        background-size: 14px;
        width: 14px;
        height: 14px;
        font-size:12px;
        position: absolute;
        right: 10px;
        top:50%;
        transform: translateY(-50%);
      }
    }
  }

  .mega-menu-header {
    font-size: 1.2em;
    text-transform: uppercase;
    font-weight: bold;
    color: darken($color-accent, 5%);
  }

  .mega-menu {
    background: $color-light;
    z-index: 10;
  }

  .mega-menu--multiLevel {
    flex-direction: column;
  }


  // ------------------ MEDIA QUERIES
...

Answer №1

After much consideration, I stumbled upon a clever solution that required the use of JavaScript to apply styles to an element. If someone happens to come across a solution that solely relies on CSS, kindly share it with me. Below is the snippet of the code:

render() {
    return (
        <div className={`${this.renderCSS()}`}>
            <div className="nav">
                <nav>
                    <a className="mobile-menu-trigger">Open mobile menu</a>
                    <ul className="menu menu-bar" id={'menu-bar'}>
                        <li>
                            <a className="menu-link menu-bar-link" aria-haspopup="true" onClick={() => {
                                const el = document.getElementById('popup');
                                el.style.left = '0';
                            }}>Projects</a>
                            <ul className="mega-menu mega-menu--multiLevel" id={'popup'}>
                                <li className="mobile-menu-back-item">
                                    <a className="menu-link mobile-menu-back-link" onClick={() => {
                                        const el = document.getElementById('popup');
                                        el.style.right = '100%';
                                    }}>Back</a>
                                </li>
                                {Object.keys(this.props.languages).map(language => {
                                    const allProjects = this.props.languages[language];
                                    return (
                                        <li key={language}>
                                            <a className="menu-link mega-menu-link" aria-haspopup="true">{language}</a>
                                            <ul className="menu menu-list">
                                                {allProjects.map((item, index) => {
                                                    const project = this.props.projects[item];
                                                    const openGithubRepo = () => {
                                                        window.open(project.svn_url, "_blank");
                                                    }
                                                    const openGithubPages = () => {
                                                        window.open(`https://ngwessels.github.io/${project.name}/`, "_blank");
                                                    }
                                                    if (project.has_pages) {
                                                        return (
                                                            <li key={project.id}>
                                                                <a className="menu-link menu-list-link"
                                                                    aria-haspopup="true">{project.name}</a>
                                                                <ul className="menu menu-list">
                                                                    <li>
                                                                        <a className="menu-link menu-list-link" onClick={openGithubRepo}>Open Github</a>
                                                                    </li>
                                                                    <li>
                                                                        <a className="menu-link menu-list-link" onClick={openGithubPages}>Visit Live Example</a>
                                                                    </li>
                                                                </ul>
                                                            </li>
                                                        );
                                                    } else {
                                                        return (
                                                            <li key={project.id}>
                                                                <a className="menu-link menu-list-link" onClick={openGithubRepo}>{project.name}</a>
                                                            </li>
                                                        )
                                                    }
                                                })}

                                            </ul>
                                        </li>
                                    )
                                })}

                            </ul>
                        </li>

                        <li>
                            <a className="menu-link menu-bar-link">Static link</a>
                        </li>

                        {/* <li className="mobile-menu-header">
                            <a className="">
                                <span>Home</span>
                            </a>
                        </li> */}
                    </ul>
                </nav>
            </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

CSS: How to target a specific element using its ID with nth-child selector?

Here is the HTML code that I am working with: <dl id="id"> <dt>test</dt> <dd>whatever</dd> <dt>test1</dt> <dd>whatever2</dd> .... </dl> I am trying to select the third dd ele ...

Utilizing carouFredsel for thumbnails and compatibility with IE7

I've successfully integrated carouFredSel (using the basic example with next/prev and pagination) on Chrome, Firefox, IE9, and IE8. However, when I tested it on IE7, the plugin didn't seem to work correctly. The list just displayed vertically wit ...

Enhancing the accessibility of Material UI Autocomplete through a Custom ListboxComponent

I have developed a custom ListboxComponent for the MUI Autocomplete component in MUI v4. How can I ensure that it meets the necessary accessibility requirements, such as navigating through options using the arrow keys? <Autocomplete ListboxComponent ...

Inconsistency in div height caused by positioning disparities

My demonstration shows two lines that should appear equal in height, but one ends up looking thicker due to its top position. Even just adjusting the top position by 1px can make a significant difference in how they look. Is there any way to prevent this u ...

Error in NextJs and ReactJs: Model schema has not been registered

During my project work, I came across a MissingSchemaError when attempting to query a `one to many` relationship and use `.populate()` on another model's objects. Issue Addressed in this question: MissingSchemaError: Schema hasn't been registered ...

Change the class of each item in a list individually by hovering over them with the mouse using JavaScript

How to toggle classes in a list item one by one using the mouseover event in JavaScript? const items = document.querySelectorAll("ul li"); let currentItem; for (const item of items) { item.addEventListener("mouseover", e => { currentItem &am ...

Text arranged in a vertical orientation with a group of text aligned along the baseline

I am trying to create the following layout: https://i.sstatic.net/HYNDw.png Here is what I need: The word total and the number 120 should align vertically in the center The words per month should align at the bottom with respect to the number 120 The ...

What is the best way to transfer the information from my MYSQL database into a JSON array?

After setting up a MySQL database that I can query using ExpressJS and NodeJS, I found the following data structure: id: 1 username: 'someUsername' email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9fedfef1f ...

What is the best way to fix the Syntax error that reads "Unexpected token (1:13)"?

I can't seem to figure out why my code keeps showing errors in the browser. I'm still new to coding and learning slowly, with help from knowledgeable individuals on stackoverflow :) Card 1.jsx Syntax error:() Unexpected token (1:13) > 1 | i ...

What is the best way to prioritize the display of custom login buttons based on the last button used for login?

I have implemented 4 different login methods for my app, each with its own associated component. I am looking to rearrange the buttons based on the last used login method. I already have a function to determine the last login method. let lastSignedInMetho ...

Installation of React has encountered an error and has been aborted. Please try running yarnpkg add --exact react react-dom react-scripts cra-template again

I am facing difficulties while trying to create npx create-react-app . ➤ YN0009: │ core-js@npm:2.6.11 could not be built successfully (exit code 1, check logs at: /C:/Users/acer/AppData/Local/Temp/logfile-10088VPjTiMAhqTi1.log) ➤ YN0009: │ fseven ...

Radiant Curvature Background Gradient

On my website, I'm trying to replicate the unique background gradient found here. Unlike a basic linear gradient that can be easily repeated as an image to fill any length, this one poses a challenge. Can anyone share a technique or method to help me ...

Add a background image to the parent element while preserving the existing background color

Can you help me figure out how to apply a background image to the menu while still keeping the background color? HTML: <div class="top"> <div class="menu"> Nop. </div> Nam hendrerit erat eget tellus mollis facilisis. ...

Bring a div box to life using AngularJS

Struggling to animate a div-Box in AngularJS? Despite trying multiple examples, the animation just won't cooperate. I'm aiming to implement a feature where clicking on a button triggers a transition animation to display the search form. I under ...

Positioning divs around a circle can be quite challenging

Among my collection of divs with various classes and multiple child divs representing guests at a table, each main div symbolizes a specific restaurant table type. I have created a jsfiddle for demonstration. http://jsfiddle.net/rkqBD/ In the provided ex ...

Failure to include jQuery and CSS in the AJAX response

Having trouble with my slider. The script that is added at runtime is not showing up in the ajax response. Does anyone know why this might be happening? $.ajax({ url:"ajax_get_response_eng_to_change.php", type:"POST", async:true, data:{c ...

Troubleshooting a 400 error with the Stripe Webhook due to problems with the raw

After diligently following Stripes documentation on setting up webhooks and testing locally using their Stripe CLI, I have encountered a persistent issue. While I can successfully create events, I consistently receive a response of 400 POST. 2021-12-14 23: ...

Implementing an array of error messages for a single validation rule in React Hook Form

Make sure to use react-hook-form version 7.11.1 for this task. I have a basic control that should display multiple error messages for a single validation rule when it is invalid. When registering this control, I include a custom validation function in the ...

Tips for increasing the size of a parent div when a child div is displayed with a set width?

Is there a way to make the parent div automatically expand when the child div with a fixed width is displayed onclick? Goal: I want the child div to show up when I click the link, and at the same time, I need the parent div to expand or scale to fit the ...

Setting the canonical tag in Next.js 13 is a straightforward process that involves defining the

In Nextjs 13, what is the best way to set a canonical tag? Previously, I would utilize the <head> tag for this purpose, but it appears to be deprecated now. Additionally, the generateMetadata function does not provide any guidance on how to include ...