Tips for keeping the DropDown Menu displayed even after moving the cursor away from the targeted element in Material-UI

import React from 'react';
import { makeStyles, Typography, Grid } from '@material-ui/core';

const styles = makeStyles((theme) => ({
    root: {},
    textStyle: {
        fontFamily: 'brandon-grotesque-black',
        textTransform: 'uppercase',
        cursor: 'pointer',

        '& + $dropDown': {
            visibility: 'hidden',
            transition: 'all 0s ease 1s',
        },

        '&:hover': {
            '& + $dropDown': {
                transitionDelay: '0s',
                visibility: 'visible',
            },
        },
    },

    dropDown: {
        width: 'max-content',
        position: 'absolute',
        top: '100px',
        pointerEvents: 'none',

        '& ul': {
            listStyleType: 'none',
            padding: '0px 0px',

            '& li': {
                backgroundColor: 'purple !important',
            },
            '&:hover': {},
            // visibility: 'hidden',
        },
    },
}));

const Shop: React.FC = () => {
    const classes = styles();

    const trendingArr = [
        'New Arrivals',
        'Best Sellers',
        'The Summer Edit',
        'FRAME & Mejuri',
        'New Arrivals',
        'Best Sellers',
        'The Summer Edit',
        'FRAME & Mejuri',
    ];

    return (
        <>
            <Typography variant='body1' className={classes.textStyle}>
                Shop
            </Typography>

            <Grid className={classes.dropDown} container direction='row'>
                <ul>
                    <li>
                        <a>
                            <Typography variant='body2'>
                                <b>Trending</b>
                            </Typography>
                        </a>
                    </li>

                    {trendingArr.map((item, i) => (
                        <li>
                            <a>
                                <Typography variant='body2' key={i}>
                                    {item}
                                </Typography>
                            </a>
                        </li>
                    ))}
                </ul>
            </Grid>
        </>
    );
};

export default Shop;


While hovering over the Shop text, the drop down menu appears. However, when moving towards the drop down menu, it becomes invisible. How can I ensure that the drop down remains visible even after unhovering from the Shop text, allowing interaction with the drop down elements? I aim to keep the drop down menu visible as I move away from the Shop text and towards the menu itself.

Answer №1

 import React from 'react';
import { makeStyles, Typography, Grid } from '@material-ui/core';

const styles = makeStyles((theme) => ({
    root: {
        '& $dropDown': {
            visibility: 'hidden',
            transition: 'all 0s ease 0.2s',
            display: 'inline-flex',
        },

        '&:hover $dropDown': {
            transitionDelay: '0s',
            visibility: 'visible',
        },
    },
    textStyle: {
        fontFamily: 'brandon-grotesque-light',
        textTransform: 'uppercase',
        cursor: 'pointer',
        '&:hover': {
            color: theme.palette.primary.light,
        },
    },

    dropDown: {
        top: '80px',
        position: 'absolute',
        width: '100%',
        height: 'auto',
        left: '0',
        backgroundColor: theme.palette.primary.main,
        '& ul': {
            listStyleType: 'none',
            padding: '0px 0px',
            '& li': {
                padding: '5px 0px 5px 30px',
                '& a': {
                    display: 'block',
                },
            },
        },
    },

    rowsStyle: {
        paddingLeft: '80px !important',
        position: 'relative',
    },

    columnStyle: {
        fontFamily: 'brandon-grotesque-black',
    },
    linkStyle: {
        fontFamily: 'brandon-grotesque-light',
        cursor: 'pointer',
        '&:hover': {
            color: theme.palette.primary.light,
        },
    },
}));

const Shop: React.FC = () => {
    const classes = styles();

    const trendingArr = [
        'New Arrivals',
        'Best Sellers',
        'The Summer Edit',
        'FRAME & Mejuri',
        'New Arrivals',
        'Best Sellers',
        'The Summer Edit',
        'FRAME & Mejuri',
    ];

    return (
        <Grid className={classes.root}>
            <Typography variant='body1' className={classes.textStyle}>
                Shop
            </Typography>

            <Grid className={classes.dropDown} container direction='row'>
                <ul>
                    <li>
                        <a>
                            <Typography
                                variant='body2'
                                className={classes.columnStyle}
                            >
                                Trending
                            </Typography>
                        </a>
                    </li>

                    {trendingArr.map((item, i) => (
                        <li>
                            <a>
                                <Typography
                                    className={classes.linkStyle}
                                    variant='body2'
                                    key={i}
                                >
                                    {item}
                                </Typography>
                            </a>
                        </li>
                    ))}
                </ul>
             
            </Grid>
        </Grid>
    );
};

export default Shop;

Instead of focusing on the text itself, directing attention to the container resolved the issue with the dropdown.

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

Mobile version experiencing issue with Navbar not collapsing

The dropdown navigation bar on the mobile version of my website is not functioning properly. Despite several attempts, I have been unable to figure out a way to make it work effectively. <!DOCTYPE html> <html lang="en"> <head> & ...

Are there any plugins available that can create a Ken Burns effect using JQuery?

All I need is a straightforward plugin, not one for creating slideshows. In this case, I only have 1 div and 1 image. The goal is to have the image animate within the div, shifting left/right or up/down without any white space visible. The size of the ima ...

Creating a stylish horizontal divider with circular accents at either end

Can someone help me create a border similar to the one shown in this image? I've attempted using the :after and :before CSS attributes, but without success. The HTML element is an h1 that requires a border at the bottom. Is it achievable? ...

Code Snippet: Adjust Table Column Alignment using CSS

Apologies for the basic question, as I am new to CSS. Below is the UI that I am working with: https://i.stack.imgur.com/crAYE.png Here is the code that corresponds to the UI: <div class="row centered-form center-block"> <div cla ...

Question about React.js: What is the best way to add multiple classes to a component by using a ternary operator?

I am looking to apply multiple classes to a component by utilizing a ternary operator. In our shared ts theme file, we have the general button styles defined, but for this specific component, I want to adjust the sizes based on screen width. To achieve thi ...

The stacking order of elements is not affected by the z-index property when using absolute positioning

I have developed a unique custom toggle switch component with the following structure: <template> <div> <label class="switch"> <input type="checkbox" :checked="value" @c ...

Exploring the application of the PUT method specific to a card ID in vue.js

A dashboard on my interface showcases various cards containing data retrieved from the backend API and stored in an array called notes[]. When I click on a specific card, a pop-up named updatecard should appear based on its id. However, I am facing issues ...

The window fails to retain the scroll position when overflow is enabled

Whenever I click on a specific div, I use jQuery to dynamically apply overflow: hidden to the html and body. $(".mydiv").on("click", function() { $("html, body").css("overflow", "hidden"); }); However, this action causes the window to scroll to the to ...

Resolving the issue of nested modals within Bootstrap

I am experiencing some issues with my customer visits list page. When I try to add a visit, it opens a bootstrap popup ("#Pop1") to record the visit details. Within this modal, there is an option to add a new customer on the spot, which then triggers anoth ...

SVG image element in Firefox does not respond to hover effect

I am currently working on developing an engaging SVG map. My goal is to have the image elements increase in size from 20px to 50px when hovered over. Below is an example of what I'm aiming to achieve: <svg xmlns="http://www.w3.org/2000/svg&quo ...

Can someone help me figure out the best way to locate a material-ui slider within a react

I am seeking to incorporate multiple material-ui sliders into a single react component that share a common event handler. However, I have encountered difficulties in identifying which slider triggered the event. Despite referring to the API documentation, ...

Insert red vertical lines to separate the sections

https://i.stack.imgur.com/smAGj.png Is there a way to insert a "vertical line" between the three sections, i.e., between each of them? I am looking to add a divider between the first and second sections and another separator between the second and third s ...

Switch the background color of the body when hovering over a link

Is it possible to change the page background when hovering over a a using only CSS? I am searching for a solution that does not involve any JavaScript. I understand that we can access child elements with CSS, but I am unsure if it is possible to target th ...

Connect the CSS file in React index.html to the Flask backend

I am attempting to connect a CSS template that is serving static backend content through Flask with my React frontend. Here is the structure I am working with: client/ src/ components/ menu.jsx public/ ...

The click event is activated following the :active selector being triggered

My Angular application features a button with a slight animation - it moves down by a few pixels when clicked on: &:active { margin: 15px 0 0 0; } The button also has a (click)="myFunction()" event listener attached to it. A common issue arises w ...

React: Combining State and Props in a Single Parameter

I'm a software developer in the early stages of my career. I recently encountered an issue while working with the Textfield component from Material UI. Although I am able to retrieve the value using a property, I am unable to edit the text field. As ...

In IE8, the :last-child pseudo-class appears to be ineffective

Here is the HTML structure I am working with: <div class="footerMenu"> <ul> <li>Home</li> <li>About</li> <li>Feedback</li> <li>Contact us</li> </ul> ...

Aligning text and lists using Bootstrap on both desktop and mobile devices

I want to format a text with a list similar to the images provided https://i.stack.imgur.com/6giDH.png for desktop and tablet view, but for mobile display I would like it to look like this https://i.stack.imgur.com/7zSXi.png This is my current approach ...

Utilizing iPad, Safari, and CSS to effortlessly fill a page with flexbox properties

Trying to create a flex layout that expands the main area to fill the entire height of the display, with a footer always at the bottom when the content is small. However, if the main content exceeds the display size, it should behave like a normal page and ...

Determine the height using jQuery before adding the class

I've been grappling with jQuery to accurately calculate the height of an element and then apply a specific CSS class to adjust its height. The issue I'm facing is that jQuery seems to be executing these calculations out of order, which is causing ...