Repository navigation
fix: set relatedTarget when the pointer moves between elements - #1333
Open
Om-singhaI wants to merge 1 commit into
Open
Om-singhaI wants to merge 1 commit into
Om-singhaI wants to merge 1 commit into
Conversation
`mouseout`, `mouseleave`, `pointerout` and `pointerleave` now carry the element the pointer moved to, and `mouseover`, `mouseenter`, `pointerover` and `pointerenter` carry the element it came from. React reads `relatedTarget` on `mouseout` to decide which components get `onMouseLeave`. With `null` it treated every move between elements as the pointer leaving the page, so moving from a parent into its child called the parent's `onMouseLeave`. If the element the pointer came from has been removed from the document, `relatedTarget` stays `null`. React ignores a `mouseover` whose `relatedTarget` is a node it rendered and waits for the matching `mouseout`, but the `mouseout` of a removed node never reaches React, so `onMouseEnter` would be lost. Fixes testing-library#1187 Fixes testing-library#1182
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
relatedTargetwas alwaysnullonmouseover,mouseout,mouseenter,mouseleaveand the matching pointer events. This sets it to the element the pointer moved to (for out and leave) or came from (for over and enter).Fixes #1187, fixes #1182. This should also fix #1154.
Why
React reads
relatedTargetonmouseoutto work out which components getonMouseLeave. Withnullit assumes the pointer left the page. So if you hover a parent and click a child that only renders while the parent is hovered, the parent'sonMouseLeaveruns, the child unmounts and the click never lands. The workarounds in the thread areskipHover, which skips the pointer move insideuser.click, orfireEvent.click.How
Mouse.moveandPointer.movenow passnextTargetasrelatedTargetfor out and leave, andprevTargetfor over and enter. IfprevTargetwas removed from the document it staysnull. React ignores amouseoverwhoserelatedTargetis a node it rendered, and the removed node'smouseoutnever reaches React, soonMouseEnterwould be lost. The first move still starts fromdocument.body, sorelatedTargetisbodythere. TouchPointer.initandPointer.releasestill usenull.Tests:
tests/react/index.tsxfor the hover then click children lost target聽#1187 case and for hovering after the previous element was removedrelatedTargetchecks for mouse and pointer events intests/pointer/move.tsandtests/convenience/hover.tsnpx jest tests/pointer tests/convenience tests/system tests/react/index.tsxpasses all 100 tests. With the source change reverted, the 4 tests for this fail and 96 pass. Without the removed element check, its 2 tests fail. I also wrote small tests for the #1182 example and the case described in #1154 (not in this PR). Both fail on main and pass with this change. No event log snapshots changed.Checklist