forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for Pointer Events as discussed in facebook#499. It is heavily based on previous work in facebook#1389 and will add most pointer events to the `SimpleEventPlugin` and enter/leave events to `EnterLeaveEventPlugin`. I added a new DOM fixture to test all pointer events and make sure my implementation does indeed work. I tested on Chrome 65 and Firefox 59 without seeing any issues. If you think the fixtures is not necessary for future changes, I'm happy to remove them as well. The only open question is if we want to add a polyfill. For the sake of simplicity, I opted against a polyfill for this PR. However, this work is compatible with [PEP][] (I've verified this behavior in Safari 11 by loading PEP in `fixtures/dom/public/index.html`). [PEP]: https://github.com/jquery/PEP
- Loading branch information
1 parent
fa8e678
commit 9bdee33
Showing
15 changed files
with
424 additions
and
57 deletions.
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
99 changes: 99 additions & 0 deletions
99
fixtures/dom/src/components/fixtures/pointer-events/draw-box.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,99 @@ | ||
const React = window.React; | ||
|
||
class DrawBox extends React.Component { | ||
state = { | ||
isDrawing: false, | ||
isCapturing: false, | ||
isOver: false, | ||
x: 0, | ||
y: 0, | ||
}; | ||
|
||
el = React.createRef(); | ||
|
||
onDown = event => { | ||
this.setState({ | ||
isDrawing: true, | ||
...this.extractRelativeCoordinates(event), | ||
}); | ||
|
||
this.el.current.setPointerCapture(event.pointerId); | ||
}; | ||
|
||
onMove = event => { | ||
if (!this.state.isDrawing) { | ||
return; | ||
} | ||
|
||
const nextState = this.extractRelativeCoordinates(event); | ||
|
||
const ctx = this.el.current.getContext('2d'); | ||
ctx.beginPath(); | ||
ctx.moveTo(this.state.x, this.state.y); | ||
ctx.lineTo(nextState.x, nextState.y); | ||
ctx.stroke(); | ||
ctx.closePath(); | ||
|
||
this.setState(nextState); | ||
}; | ||
|
||
onUp = event => { | ||
this.setState({ | ||
isDrawing: false, | ||
}); | ||
}; | ||
|
||
onOver = event => { | ||
this.setState({isOver: true}); | ||
}; | ||
|
||
onOut = event => { | ||
this.setState({isOver: false}); | ||
}; | ||
|
||
onGotCapture = event => { | ||
this.setState({isCapturing: true}); | ||
}; | ||
|
||
onLostCapture = event => { | ||
this.setState({isCapturing: false}); | ||
}; | ||
|
||
extractRelativeCoordinates = event => { | ||
const rect = this.el.current.getBoundingClientRect(); | ||
|
||
return { | ||
x: event.clientX - rect.left, | ||
y: event.clientY - rect.top, | ||
}; | ||
}; | ||
|
||
render() { | ||
const {isOver, isCapturing} = this.state; | ||
|
||
const boxStyle = { | ||
border: `1px solid ${isCapturing ? 'blue' : isOver ? 'red' : '#d9d9d9'}`, | ||
margin: '10px 0 20px', | ||
touchAction: 'none', | ||
}; | ||
|
||
return ( | ||
<canvas | ||
ref={this.el} | ||
width={300} | ||
height={300} | ||
style={boxStyle} | ||
onPointerDown={this.onDown} | ||
onPointerMove={this.onMove} | ||
onPointerUp={this.onUp} | ||
onPointerCancel={this.onUp} | ||
onPointerOver={this.onOver} | ||
onPointerOut={this.onOut} | ||
onGotPointerCapture={this.onGotCapture} | ||
onLostPointerCapture={this.onLostCapture} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default DrawBox; |
27 changes: 27 additions & 0 deletions
27
fixtures/dom/src/components/fixtures/pointer-events/drawing.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,27 @@ | ||
import TestCase from '../../TestCase'; | ||
import DrawBox from './draw-box'; | ||
|
||
const React = window.React; | ||
|
||
class Drawing extends React.Component { | ||
render() { | ||
return ( | ||
<TestCase title="Drawing" description=""> | ||
<TestCase.Steps> | ||
<li>Draw on the canvas below with any pointer tool</li> | ||
</TestCase.Steps> | ||
|
||
<TestCase.ExpectedResult> | ||
You should see strokes as you move the pointer tool. While your | ||
pointer tool is over the canvas, it should also have a red border. | ||
While drawing, the canvas must have a blue border to signalize that a | ||
pointer capture was received. | ||
</TestCase.ExpectedResult> | ||
|
||
<DrawBox /> | ||
</TestCase> | ||
); | ||
} | ||
} | ||
|
||
export default Drawing; |
34 changes: 34 additions & 0 deletions
34
fixtures/dom/src/components/fixtures/pointer-events/hover-box.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,34 @@ | ||
const React = window.React; | ||
|
||
class DrawBox extends React.Component { | ||
render() { | ||
const boxStyle = { | ||
border: `1px solid #d9d9d9`, | ||
margin: '10px 0 20px', | ||
padding: '20px 20px', | ||
touchAction: 'none', | ||
}; | ||
|
||
const obstacleStyle = { | ||
border: `1px solid #d9d9d9`, | ||
width: '25%', | ||
height: '200px', | ||
margin: '12.5%', | ||
display: 'inline-block', | ||
}; | ||
|
||
return ( | ||
<div | ||
style={boxStyle} | ||
onPointerOver={this.props.onOver} | ||
onPointerOut={this.props.onOut} | ||
onPointerEnter={this.props.onEnter} | ||
onPointerLeave={this.props.onLeave}> | ||
<div style={obstacleStyle} /> | ||
<div style={obstacleStyle} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default DrawBox; |
62 changes: 62 additions & 0 deletions
62
fixtures/dom/src/components/fixtures/pointer-events/hover.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,62 @@ | ||
import TestCase from '../../TestCase'; | ||
import HoverBox from './hover-box'; | ||
|
||
const React = window.React; | ||
|
||
class Hover extends React.Component { | ||
state = { | ||
overs: 0, | ||
outs: 0, | ||
enters: 0, | ||
leaves: 0, | ||
}; | ||
|
||
onOver = () => { | ||
this.setState({overs: this.state.overs + 1}); | ||
}; | ||
|
||
onOut = () => { | ||
this.setState({outs: this.state.outs + 1}); | ||
}; | ||
|
||
onEnter = () => { | ||
this.setState({enters: this.state.enters + 1}); | ||
}; | ||
|
||
onLeave = () => { | ||
this.setState({leaves: this.state.leaves + 1}); | ||
}; | ||
|
||
render() { | ||
const {overs, outs, enters, leaves} = this.state; | ||
|
||
return ( | ||
<TestCase title="Hover" description=""> | ||
<TestCase.Steps> | ||
<li>Hover over the above box and the obstacles</li> | ||
</TestCase.Steps> | ||
|
||
<TestCase.ExpectedResult> | ||
Overs and outs should increase when moving over the obstacles but | ||
enters and leaves should not. | ||
</TestCase.ExpectedResult> | ||
|
||
<HoverBox | ||
onOver={this.onOver} | ||
onOut={this.onOut} | ||
onEnter={this.onEnter} | ||
onLeave={this.onLeave} | ||
/> | ||
|
||
<p> | ||
Pointer Overs: <b>{overs}</b> <br /> | ||
Pointer Outs: <b>{outs}</b> <br /> | ||
Pointer Enters: <b>{enters}</b> <br /> | ||
Pointer Leaves: <b>{leaves}</b> <br /> | ||
</p> | ||
</TestCase> | ||
); | ||
} | ||
} | ||
|
||
export default Hover; |
18 changes: 18 additions & 0 deletions
18
fixtures/dom/src/components/fixtures/pointer-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,18 @@ | ||
import FixtureSet from '../../FixtureSet'; | ||
import Drawing from './drawing'; | ||
import Hover from './hover'; | ||
|
||
const React = window.React; | ||
|
||
class PointerEvents extends React.Component { | ||
render() { | ||
return ( | ||
<FixtureSet title="Pointer Events" description=""> | ||
<Drawing /> | ||
<Hover /> | ||
</FixtureSet> | ||
); | ||
} | ||
} | ||
|
||
export default PointerEvents; |
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
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
Oops, something went wrong.