-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Adding movementX and movementY to synthenticMouseEvent fixes #6723 #9018
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
fixtures/dom/src/components/fixtures/mouse-events/index.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import FixtureSet from '../../FixtureSet'; | ||
import MouseMovement from './mouse-movement'; | ||
|
||
const React = window.React; | ||
|
||
class MouseEvents extends React.Component { | ||
render() { | ||
return ( | ||
<FixtureSet title="Mouse Events" description=""> | ||
<MouseMovement /> | ||
</FixtureSet> | ||
); | ||
} | ||
} | ||
|
||
export default MouseEvents; |
48 changes: 48 additions & 0 deletions
48
fixtures/dom/src/components/fixtures/mouse-events/mouse-movement.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import TestCase from '../../TestCase'; | ||
|
||
const React = window.React; | ||
|
||
class MouseMovement extends React.Component { | ||
state = { | ||
movement: {x: 0, y: 0}, | ||
}; | ||
|
||
onMove = event => { | ||
this.setState({x: event.movementX, y: event.movementY}); | ||
}; | ||
|
||
render() { | ||
const {x, y} = this.state; | ||
|
||
const boxStyle = { | ||
padding: '10px 20px', | ||
border: '1px solid #d9d9d9', | ||
margin: '10px 0 20px', | ||
}; | ||
|
||
return ( | ||
<TestCase | ||
title="Mouse Movement" | ||
description="We polyfill the movementX and movementY fields." | ||
affectedBrowsers="IE, Safari"> | ||
<TestCase.Steps> | ||
<li>Mouse over the box below</li> | ||
</TestCase.Steps> | ||
|
||
<TestCase.ExpectedResult> | ||
The reported values should equal the pixel (integer) difference | ||
between mouse movements positions. | ||
</TestCase.ExpectedResult> | ||
|
||
<div style={boxStyle} onMouseMove={this.onMove}> | ||
<p>Trace your mouse over this box.</p> | ||
<p> | ||
Last movement: {x},{y} | ||
</p> | ||
</div> | ||
</TestCase> | ||
); | ||
} | ||
} | ||
|
||
export default MouseMovement; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
import SyntheticUIEvent from './SyntheticUIEvent'; | ||
import getEventModifierState from './getEventModifierState'; | ||
|
||
let previousScreenX = null; | ||
let previousScreenY = null; | ||
|
||
/** | ||
* @interface MouseEvent | ||
* @see http://www.w3.org/TR/DOM-Level-3-Events/ | ||
|
@@ -34,6 +37,24 @@ const SyntheticMouseEvent = SyntheticUIEvent.extend({ | |
: event.fromElement) | ||
); | ||
}, | ||
movementX: function(event) { | ||
if ('movementX' in event) { | ||
return event.movementX; | ||
} | ||
|
||
const screenX = previousScreenX; | ||
previousScreenX = event.screenX; | ||
return screenX ? event.screenX - screenX : 0; | ||
}, | ||
movementY: function(event) { | ||
if ('movementY' in event) { | ||
return event.movementY; | ||
} | ||
|
||
const screenY = previousScreenY; | ||
previousScreenY = event.screenY; | ||
return screenY ? event.screenY - screenY : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
}, | ||
}); | ||
|
||
export default SyntheticMouseEvent; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the check here intends to check against
null
but will accidentally also be falsy ifscreenX
is actually equal to0
. Please avoid truthy checks like this, they tend to hide bugs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's equal to
0
then it will still return0
since that's the second expression in the ternary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if saved
screenX
is0
, but then you moved to200
, you want to return200 - 0
rather than0
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe Dan is right, and we need to correct this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/tc39/proposal-nullish-coalescing would have helped a lot with this, yes we should check if equal to null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing is that using the same variable for both
null
and numeric values can mess with VM optimizations. I think it would be better to use a separate boolean flag here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, are we happy to assume screenX on event? If undefined we could end up with
undefined - null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, screenX/Y are very well supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so if screenX will always be an Number, could we not just initiate previousScreenX as 0 Instead of null, and then instead of a ternary or null checking, always perform the minus operation. Because we can guarantee both operands will be Numbers by this point .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't make the first
movementX
equal to the current screen coordinate, instead of0
? I think 0 would be expected (because the cursor didn't "jump" from top left point).