The dropdown menu disappears when I hover over the tab, making it impossible for me to navigate it in the react app

My navigation component includes a Games tab with a dropdown menu that appears when you hover over it. However, I'm facing an issue where the dropdown menu closes before the user can transition from hovering over the games tab to the actual dropdown menu due to a small gap in between.

Check out the JSX code snippet below:

                    <li className='navbar__link' onMouseEnter={() => setIsDropdownOpen(true)} onMouseLeave={() => setIsDropdownOpen(false)}>
                      <Link to="/games" className='link__styles'>Games</Link>
                      <div className={`dropdown-menu ${isDropdownOpen ? 'show' : ''}`} onMouseEnter={() => setIsDropdownOpen(true)} onMouseLeave={() => setIsDropdownOpen(false)}>
                        <span className="dropdown-column">
                          <h3>Games</h3>
                          <p>Word games, logic puzzles and crosswords, including an extensive archive.</p>
                        </span>
                        <span className="dropdown-column">
                          <h5>Play</h5>
                          <ul>
                            <a href="link">
                              <li>
                                <img src={WordleIcon} alt="" className="dropdown-tile" />
                                <span>Wordle</span>
                              </li>
                            </a>
                            <a href="link">
                              <li>
                                <img src={CrosswordIcon} alt="" className='dropdown-tile2'/>
                                <span>Crossword</span>
                              </li>
                            </a>
                            <a href="link">
                              <li>
                                <img src={WordsearchIcon} alt=""className='dropdown-tile2'/>
                                <span>Wordsearch</span>
                              </li>
                            </a>
                          </ul>
                        </span>
                      </div>
                    </li>

Here is the relevant CSS code:

.dropdown-menu {
    position: absolute;
    top: calc(100% - 4px);
    left: 0;
    width: 100%;
    background-color: white;
    display: none;
    z-index: 999;
    padding: 2% 8%;
    box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.1);
}

.navbar__link:hover .dropdown-menu,
.dropdown-menu:hover {
    display: flex;
    gap: 2rem;
    justify-content: center;
}

.dropdown-column {
    max-width: 200px;
}

.dropdown-column > p {
    margin-top: 1rem;
}

.dropdown-tile {
    width: 25px;
    margin-right: 0.2rem;
}

.dropdown-tile2 {
    width: 17.5px;
    margin: 0 0.4rem 0 0.2rem;
}

Answer №1

A suggestion would be to introduce a slight delay before automatically closing the dropdown menu.

Answer №2

It is recommended to eliminate the onMouseLeave event from the li tag and remove the hover effect in the CSS file. Instead, add a show class in the CSS file. The hover effect works by toggling the show class - when the show class appears, the dropdown is displayed, and when it's not present, the dropdown list is hidden. By following these steps, your dropdown list should function correctly.

.dropdown-menu {
  position: absolute;
  top: 22px;
  left: 0;
  width: 100%;
  background-color: white;
  display: none;
  z-index: 999;
  padding: 2% 8%;
  box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.1);
}

.show{
  display: flex;
  gap: 2rem;
  justify-content: center;
}

.dropdown-column {
  max-width: 200px;
}

.dropdown-column > p {
  margin-top: 1rem;
}

.dropdown-tile {
  width: 25px;
  margin-right: 0.2rem;
}

.dropdown-tile2 {
  width: 17.5px;
  margin: 0 0.4rem 0 0.2rem;
}

Here is the updated JSX code

<li className='navbar__link'
         onMouseEnter={() => setIsDropdownOpen(true)} 
        >
          <Link  to="/games" className='link__styles'>Games</Link>
                      <div  className={`dropdown-menu ${isDropdownOpen ? 'show' : ''}`} 
                      onMouseEnter={() => setIsDropdownOpen(true)} 
                      >
                        <span className="dropdown-column">
                          <h3>Games</h3>
                          <p>Word games, logic puzzles and crosswords, including an extensive archive.</p>
                        </span>
                        <span className="dropdown-column">
                          <h5>Play</h5>
                            <ul>
                        <a href="link">
                          <li>
                            <img src={WordleIcon} alt="" className="dropdown-tile" />
                            <span>Wordle</span>
                          </li>
                        </a>
                        <a href="link">
                          <li>
                            <img src={CrosswordIcon} alt="" className='dropdown-tile2'/>
                            <span>Crossword</span>
                          </li>
                        </a>
                        <a href="link">
                          <li>
                            <img src={WordsearchIcon} alt="" className='dropdown-tile2'/>
                            <span>Wordsearch</span>
                          </li>
                        </a>
                      </ul>
                        </span>
                      </div>
                    </li>

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

Using AngularJS to dynamically update the DOM with the response from a service method

Here's the HTML code: <div ng-controller="AutoDeployController as autoDeploy"> <input type="text" ng-model="autoDeploy.message"> <p>Message: {{ autoDeploy.message }}</p> </div> <button ng-click="autoDeploy.chan ...

Both FireFox and Chrome are currently experiencing issues with video chat functionality through WEBRTC

Hello, I'm relatively new to this and have been dedicating about four consecutive days to figuring it out. Despite my efforts, I still can't seem to make it work. Please forgive any oversights as I am quite exhausted at the moment but I'll d ...

Craft a brief description for every individual component discovered

Is there a way to identify and shorten all elements containing a <tspan> tag to just ten characters each? For example: <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> ...

Performing an HTTP request response in JavaScript

I am trying to make an HTTP request that returns the data in JSON format using 'JSON.stringify(data)'. var xhr = new XMLHttpRequest(); xhr.open("GET", "/api/hello", true); xhr.send(); xhr.onreadystatechange = function () { console.log(xhr.r ...

How come my files successfully upload to Cloudinary but do not get reflected in the database?

I am currently facing an issue with uploading a file to Cloudinary and storing it in a MongoDb database. While the upload process works fine (I can see the new image in my Cloudinary account), the database does not get updated. The backend of this project ...

When trying to use preact as an alias for react, the error "Module not found: 'react/jsx-runtime'" is thrown

Avoid using the outdated guide I linked; follow the one provided in the answer instead I am trying to transition from react to preact by following their migration guide. I updated my webpack.config.js to include: alias: { "react": "pr ...

Utilizing React Recharts to create personalized tooltips

In my current project, I am looking to create a custom tooltip to replace the default one in recharts. The default tooltip that I want to change is shown below: https://i.stack.imgur.com/TjTiL.png I would like the new custom tooltip to look like this: ...

Deliver a communication from the dialogue box on the MEAN stack website to the task pane

Currently experimenting with the Dialog API within Office addins. Able to successfully trigger a Dialog box from my task pane using: $scope.openDialog = function () { Office.context.ui.displayDialogAsync('https://localhost:3000/home', ...

Implementing the useRouter hook in a function in Next.js: A step-by-step guide

In my Next.js project, I have a function that gathers events for Firebase analytics. import analytics from '@utility/firebase'; const handleLogEvents = (event) => { const currentDate = new Date(); logEvent(analytics, event.event_name, { ...

Tips for retrieving arguments within a method called from a template in vue.js?

Here is an example where I am attempting to call a method from the template and pass in some arguments. How can I access those arguments from within the method? Snippet from script: methods: { showBinaries(job, id) { let test_url = process.en ...

Launching a React project using the terminal - my step-by-step process

I'm attempting to initiate a new react-app utilizing the command npx create-react-app <app name> as shown below: npx create-react-app new-app However, after downloading, it seems to be stuck. It's not progressing at all. I've even rei ...

What steps can I take to fix the 'module not found error' in webpack?

After setting up my ReactJS application with React-Bootstrap on Webpack 2, I encountered an error when trying to run it: ERROR in ./~/css-loader!./~/sass-loader/lib/loader.js!./app/css/bootstrap.css Module not found: Error: Can't resolve '../fon ...

Possible conflict between CSS and JavaScript on Magento website

Problem Description: Visit this link for more details I am facing an issue with the product upload process when using certain attributes. It appears to be related to a positioning problem. Despite trying various solutions, such as removing the position at ...

Error: Variable 'err' is undefined in Node.js

Why am I getting a ReferenceError: err is not defined even though it is supposed to be defined here? const sampleObject = require('./sampleObject'); const sampleModel = (callback) => { if (true) { sampleObject.sampleRetrieval(err ...

"Function.click operates successfully when no arguments are given, but encounters issues when arguments are supplied

I had a function that was functioning correctly: $('#buttFurniture').click(onFilterCLick); However, when I decided to pass arguments to the function, it stopped working: $('#buttFurniture').click(onFilterCLick(arrOutput, arrFurniture ...

Increased spacing HTML

Trying to create a footer in HTML, but each time I attempt it ends up with an extra margin on the left side. Here is my current footer code: <div id="footer"> Tester </div> #footer { background-image: url(images/backgroundrpatternf ...

Improving the functionality of dynamically updating server-side rendered props

I have posts with comments and I would like to create a post view that displays all the comments for that post. In my implementation, the getServerSideProps function passes the entire post (including comments) to the page. My goal is to dynamically update ...

Error: Unable to execute this.context.router.push due to it not being a function

I recently encountered an issue where the browser console displayed the same message as the post title. The problem occurred when trying to navigate to a profile page after clicking a dropdown button. contextTypes: { router: React.PropTypes.func }, _h ...

How can state values be transferred between components?

I have come across various answers on different platforms but haven't been able to achieve the desired results. That's why I am reaching out with this question. So, my question is related to 3 files named: App.js, SignUp.js, and Welcome.js. My ...

Encountering an issue with MUI Props: "Must provide 4 to 5 type arguments."

I'm attempting to use a custom component and pass in AutocompleteProps as a prop, all while utilizing typescript. Here's my current setup: type Props = { autoCompleteProps?: AutocompleteProps<T> label: string loading?: boolean } cons ...