Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: mouse events are bubbled up too far with some pointer-events rules #28817

Open
EricKarschner37 opened this issue Apr 10, 2024 · 1 comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@EricKarschner37
Copy link

If an element with the css rule pointer-events: none has a descendant with the rule pointer-events: all, then the descendant's mouse events are bubbled up to all of its ancestors

React version: 18.2

Steps To Reproduce

import React from 'react'
import ReactDOM from 'react-dom'

function Container() {
  return (
    <div
      style={{padding: '20px', border: '1px solid #aaa', pointerEvents: 'none'}}
      onMouseLeave={(e) => console.log('container', e)}
      id="container"
    >
      <div onMouseLeave={() => {console.log('child 1')}}>
        <div
          style={{
            pointerEvents: 'none',
            border: '1px solid black',
            padding: '10px',
          }}
          onMouseLeave={() => {console.log('child with pointerEvents: none')}}
        >
          <div
            style={{
              width: '100px',
              height: '100px',
              backgroundColor: 'red',
              pointerEvents: 'all',
            }}
            onMouseLeave={() => {console.log('child with pointerEvents: all')}}
          />
        </div>
      </div>
    </div>
  )
}

function App() {
  return <Container />
}

ReactDOM.render(<App />, document.getElementById('root'))
  1. Hover the rendered red square
  2. Mouse away from it. Each component's onMouseLeave callback will be fired

Link to code example:

https://codesandbox.io/p/sandbox/tender-smoke-cqk24y?file=%2Fsrc%2Findex.js%3A1%2C1-40%2C1

The current behavior

The onMouseLeave event is propagated beyond the component which was actually left

The expected behavior

The onMouseLeave trigger is called for the element which was moused away from, and not its parents

@EricKarschner37 EricKarschner37 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Apr 10, 2024
@halilxibrahim
Copy link

As far as I understand, there may be a behavioral problem with the pointer-events feature. If the elements are disabled with pointer-events: none, you can solve the mouse events of the child elements with pointer-events: all from being transferred correctly to the parent elements as follows

Solutions:

Use Side-by-Side Level Elements:

Avoid this problem by using elements at the same level rather than nested elements. Thus, the pointer-events property will work without being affected.
Divide Events Further:

Bind events to more specific elements so that each element processes only its own events. This will prevent unwanted domain sharing.
Try an Alternative CSS Approach:

Maybe changing pointer-events dynamically in a particular case or using a different structure could solve the problem.
Preventing React Event Bubbling:

Prevent events from being escalated by catching React events and stopping them. You can catch the onMouseLeave event and stop the event from being propagated upwards using methods such as e.preventDefault() or e.stopPropagation().
Which Approach Should You Use?:

Do trial and error to determine which approach works best for your situation. This will depend on your specific use case and needs.

I hope I understood the problem and helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants