The mouseleave event should only be activated when exiting the boundaries of the parent container div, not when crossing the borders of child div

When leaving the styledLink child component, the "mouseleave" event is triggered. However, I want it to trigger when leaving the entire container. I've attempted various solutions but Gatsby doesn't seem to cooperate well with these event listeners. Any suggestions or help would be greatly appreciated! Thank you :)

const MenuDropDown: React.FC<{Props}> = 
({ menuLinks = [
    { title: 'Events', url: '/events' },
    { title: 'Performers', url: '/performers' },
    { title: 'Venues', url: '/venues' },
  ], setFocus, focus, explore }) => {
    const ref = useRef<HTMLDivElement>(null);

    useEffect(() => {
        /**
         * Alert if hovered outside of element
         */
        function handleUserEvent(event: any) {
            event.stopPropagation();
            console.log("fired");
            console.log(ref.current);
            if (ref && ref.current) {
                setFocus(false);
            }
        }
        // Bind the event listener

        ref.current?.addEventListener('mouseleave', handleUserEvent);
        return () => {
            ref.current?.removeEventListener('mouseleave', handleUserEvent);
        };
    }, [ref]);

    return (
        <S.Wrapper explore={explore}>
            <S.Container ref={ref}>
                {menuLinks.length > 0 &&
                    menuLinks.map((data, key) => {
                        const url = data.url;
                        const title = data.title;
                        return (
                            <S.StyledLink key={key} activeStyle={{ background: 'rgb(245, 245, 244)' }} to={url}>
                                {title}
                            </S.StyledLink>
                        );
                    })}
            </S.Container>
        </S.Wrapper>
    );
};

export default MenuDropDown;
export const Wrapper = styled.div<{ explore?: boolean; isAuthenticated?: boolean }>`
  box-shadow: rgb(36 63 97 / 12%) 0px 3px 20px, rgb(36 63 97 / 8%) 0px 1.5px 4px;
  border-radius: 10px;
  //opacity: 0;
  background-color: white;
  position: absolute;
  top: 100%;
  display: block;
  z-index: 1000;
  height: 100%;
  pointer-events: none;
  /* transition: opacity 200ms ease-in-out 0s; */
  width: 140px;
  margin-top: 28px;
  margin-right: ${(props) => (props.explore ? '65px' : null)};
  margin-left: ${(props) => (props.explore ? null : '250px')};
`;

export const Container = styled.div`
  pointer-events: none;
  z-index: 900;
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
  border-radius: 10px;
  //background: rgba(245, 245, 244, 0.2);
  background: white;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
  /* height: 200px;
    margin-top: 200px; */
`;

export const StyledLink = styled(Link)`
  pointer-events: auto;
  text-align: left;
  -webkit-font-smoothing: antialiased;
  font-family: proxima-nova, 'Proxima Nova', 'Helvetica Neue', Arial, Helvetica, sans-serif;
  font-size: 1.5rem;
  line-height: 1.575rem;
  margin: 5px 0px;
  font-weight: 500;
  /* box-sizing: border-box; */
  width: 100%;
  padding: 0.95rem 1.15rem;
  border-radius: 0.5rem;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  color: rgb(24, 24, 24);
`;

Answer №1

Ensuring that the CSS property "pointer-events: auto;" was correctly applied to all child components solved the issue.

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

The grid is experiencing improper sizing in the row orientation

Challenges with Grid Layout In my current project, I am facing a challenge in creating a responsive layout with a dynamic grid within a page structure that includes a header and footer. The main issue arises when the grid items, designed to resemble books ...

What is the HTML code needed to stack two divs on top of each other?

Many individuals seem to be inquiring about this issue, yet I am still unable to resolve it completely. The image I have linked closely resembles what I am aiming for. I would like the image in each section to flow from left to right, as it currently does, ...

What is the best way to retrieve the border-color inline style using jQuery?

I have a HTML tag like this. <span id="createOrderFormId:accountNo" style="border-color: red;"><</span> To retrieve the style value for the border-color property, I tried using the following jQuery code: $( document ).ready(function() { ...

What is the best way to line up a number and text using CSS?

Creating a basic gauge meter with min value 0 and max value 2. The current UI progress is as follows: .sc-gauge { width:200px; height:200px; margin:200px auto; } .sc-background { position:relative; height:100px; margin-bottom:10px; background-color:g ...

A guide to creating a personalized horizontal dashed separator in Material UI

I'm looking to customize a divider template by turning it into a horizontal dashed divider, but I'm struggling to override it. Even when attempting an sx edit with a borderRadius, the divider doesn't change as expected. source code: import ...

The placeholder feature seems to be malfunctioning when it comes to entering phone numbers in a react

I am working on a MUI phone number form field. I want the placeholder to show up initially, but disappear when the user starts typing. How can I achieve this functionality in my code? import React from "react"; import MuiPhoneNumber from " ...

Having trouble getting the NextAuth Custom Sign In Page to function properly when using multiple tabs simultaneously

I've been incorporating the credential feature of nextauth into my website. However, I've encountered an issue: When I create a custom sign-in page and submit the data using the signIn feature, everything works smoothly. But the problem arises wh ...

How to Emphasize Html Select Element on Mobile Devices?

Is there a way to make the HTML select element on Android appear more distinguishable as a dropdown list, rather than just looking like plain text? Any suggestions for highlighting it so users can easily identify and select an option from this element? Up ...

Different Ways to Incorporate Several Entry Points to CRA

My current CRA is still intact, and I am looking to implement a feature where accessing a specific URL will lead to loading a different public/index.html page. Following that, ReactDom should render a "different" Main component to kickstart the application ...

The browser is only showing particular changes made to the CSS file

I'm currently working on a web project and have the following CSS and HTML code: style.css: body { background-color: red; font-size: 14px; font-family: Roboto,arial,sans-serif color: gray; } img { height: 92px; width: 272px; ...

Tips on sending a ZOD schema to a client component in order to programmatically apply validation rules to an input field

Recently, I developed a custom input field client component for a proof of concept that required built-in validation. Initially, I hardcoded the validation logic just to demonstrate how the UI changes based on certain criteria being met by the input. Howev ...

When I engage with the input field, it ceases to be in focus

Here is the code I've been working on: https://github.com/Michael-Liendo/url-shortener/blob/main/src/pages/index.js If you want to see the issue for yourself, check it out at: ...

Struggling with resolving the error message "EEXIST: file already exists, mkdir 'C:UsersOKRDesktopMeetUp' after the apk generation failed in React Native? Learn how to troubleshoot this

Currently, I am in the process of testing my React Native apk app file. These are the steps I followed before generating the apk: react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.a ...

Paths to External Stylesheets

Imagine having a stylesheet body { background-image: url('/images/background.jpg'); } situated in the header section of a nearby document <html> <head> <link rel="StyleSheet" href="http://externalserver/stylesheet.cs ...

iPhone - touchstart event not functioning in JavaScript

Looking for a solution to dynamically resize a div in JavaScript when clicking on a link element (<a>). However, the code doesn't seem to function properly on my iPhone. Let's start with a basic link: <a href="#" onfocus="menu()">ME ...

Should components be stored in state for optimal performance?

Should entire React Components be stored in the component state or redux state? While it is optional (as you can store a string and render the component conditionally), in some cases, it's simpler to just store the whole component. For instance, cons ...

A blank page is appearing mysteriously, free of any errors

I have experience with ReactJs, but I am new to Redux. Currently, I am working on implementing an async action where I call an API and display the data received from it. Initially, when all the Redux components (actions, reducers, containers) were on a sin ...

Most effective method for adjusting image size to match div container (as background)

I'm attempting to create a background with 85% height and 100% width on the index page using Bootstrap 4. However, when I set an image as the background in a div, it doesn't resize correctly and only shows a portion of the image (as the image is ...

Is my component method not being accurately simulated by Jest and Enzyme?

Incorporating the "onKeyPress" event in my input, I aim to update a specific state when the "enter" key is pressed. This functionality involves appending the new input value to the existing languages array in the state. Although the functionality operates ...

Encountering Challenges When Trying to Upload File onto Azure Blob Storage

My current project involves integrating file uploads to Azure Blob Storage. The backend setup using Node.js with Express and @azure/storage-blob is already done, but I'm facing challenges with the frontend implementation. I need guidance to ensure pro ...