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

Is there a way to combine fixed and dynamic size elements within a flexbox container?

Currently, I am working on creating a layout that combines the automatic sizing feature of flexbox with fixed-size elements: <View style={{ height: 70, alignItems: "center" }}> <Text style={{ flex: 1, textAlign: "right", paddingRight: 10 }}&g ...

Looking to implement a dual-column layout for posts on WordPress using Bootstrap

Creating an intranet website for a client which functions similar to a Facebook or Twitter feed. The layout includes three columns using Bootstrap's grid system. The first column serves as a navigation sidebar, while the other two columns should disp ...

Creating a scrollable HTML5 div container with fixed divs positioned within it

I am attempting to develop an app container that mimics the functionality of Microsoft Excel. The container should scroll both horizontally and vertically, with fixed headers on the left and top that move along with the content. Here is a rough representat ...

Experiencing difficulty adjusting the width of a Tumblr post with JavaScript or jQuery?

Calling all coding experts! I've been working on customizing a Tumblr theme, and everything is looking good except for one issue. I'm struggling to adjust the width of photos on a permalink (post) page. Check out this link: You'll notice t ...

JavaScript tip: Improve the way you highlight the current navigation page while scrolling by finding alternative methods to using "scrollY > x"

Currently, my webpage layout is divided into 4 sections - HOME, ABOUT, SKILLS, and CONTACT. Below is the JavaScript code I am using to highlight a specific section on the navigation bar based on the scroll position: let home = document.querySelector(" ...

Setting the value in an Autocomplete Component using React-Hook-Forms in MUI

My form data contains: `agreementHeaderData.salesPerson ={{ id: "2", name: "Jhon,Snow", label: "Jhon,Snow", value: "<a href="/cdn-cgi/l/email-prot ...

Showing formatted JSON in the view using ASP.NET MVC

Is there a method to modify JSON for presentation in the interface? I am looking for a way to automatically update my API documentation when adding new properties. It would be great if I could also apply CSS styling to certain elements. This feature is som ...

Allow the div to break out of the overflow: hidden property without disrupting the overall page layout

Currently, I am attempting to separate a child element from its parent that has overflow: hidden applied. The main reason for restricting the dimensions of the parent is for a JavaScript workaround. I have experimented with positioning the child element, ...

Animating the navbar with CSS transitions

Seeking advice on adding a transition effect to my navbar. Specifically, I'd like to have a colored background appear below each link when hovered over. For example, when hovering over "HOME," a yellow color should display, and it should disappear whe ...

The jQuery navbar's active class fails to function properly when an href is added

I'm facing a puzzling situation. I created a navbar using jQuery Mobile following the instructions on their official website: jQuery Mobile Navbar Demos Everything functions smoothly until I introduce href attributes in the list elements within the u ...

Having trouble installing @mui/styles with NPM for React 18?

I'm facing an issue while trying to integrate Material-UI Styles into a React project. When I execute the command in the terminal: npm i @mui/styles I encounter the following error message: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve ...

Verify if any choices are available before displaying the div block

I need to determine if there is a specific option selected in a dropdown menu, and then display a div if the option exists, otherwise hide it. I'm not just checking the currently selected option, but all available options. if (jQuery(".sd select opti ...

Apply CSS styling to the v-html output

I am trying to enhance the style of HTML code using a v-html but have been struggling with finding a working solution so far. Can anyone help me out? :( Below is my code snippet: Template : <div class="para" v-html="value" /> Script : exp ...

Conceal an element along with its space, then signal the application to show alternative content using React

Greetings everyone! I seek assistance with a React Application that I am currently developing. As a newcomer to the Javascript world, I apologize if my inquiry seems trivial. The application comprises of two main elements: a loader, implemented as a React ...

Revamp the Bootstrap Form Upload feature

I am looking to redesign the Bootstrap Form Upload field. The main reason behind this is that in Bootstrap, the file upload field is treated as one single component, making it impossible to separate the file upload button from the file upload text. The fi ...

Collapse or display div elements with jQuery - First close all other elements before opening the selected item

The Problem at Hand Currently, the script is expected to hide all elements with the "gallery-collapse" class and reveal the specific content based on the clicked link. However, sometimes multiple divs might appear simultaneously when switching between ite ...

The persistent issue of the modal box disappearing persists, despite my efforts to remove additional instances of bootstrap.min

I have been struggling to prevent my modal box from disappearing, despite trying various solutions found online. How can I ensure that it stays visible? Here is the code in question: <html> <head> <title> Test Slides </titl ...

Stop the css animation from playing

I created a slideshow using HTML and CSS, and I'm looking to implement a feature where the slideshow pauses when my mouse hovers over it. I attempted to achieve this by using -webkit-animation-play-state: paused;, but unfortunately, it only pauses one ...

Customizing Bootstrap tooltip appearances with CSS

After successfully setting up Bootstrap 4.5.0, I decided to experiment with customizing the styles of tooltips. The code snippet below shows how I attempted this. Initially, I included the necessary stylesheets at the top of the page: <link rel=&q ...

Various formatting options on GitHub Pages

As a beginner in programming, I recently deployed my first project to github pages. However, I am facing two issues: While the desktop version of the site looks perfect, the cards on the home page appear unseparated on mobile and iPad devices. Despite try ...