-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[TouchRipple] Abort on scroll #3407
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,14 @@ function push(array, obj) { | |
} | ||
|
||
function shift(array) { | ||
//Remove the first element in the array using React immutability helpers | ||
return update(array, {$splice: [[0, 1]]}); | ||
} | ||
|
||
const TouchRipple = React.createClass({ | ||
|
||
propTypes: { | ||
abortOnScroll: React.PropTypes.bool, | ||
centerRipple: React.PropTypes.bool, | ||
children: React.PropTypes.node, | ||
color: React.PropTypes.string, | ||
|
@@ -40,6 +42,12 @@ const TouchRipple = React.createClass({ | |
PureRenderMixin, | ||
], | ||
|
||
getDefaultProps() { | ||
return { | ||
abortOnScroll: true, | ||
}; | ||
}, | ||
|
||
getInitialState() { | ||
//Touch start produces a mouse down event for compat reasons. To avoid | ||
//showing ripples twice we skip showing a ripple for the first mouse down | ||
|
@@ -92,6 +100,9 @@ const TouchRipple = React.createClass({ | |
this.setState({ | ||
ripples: shift(currentRipples), | ||
}); | ||
if (this.props.abortOnScroll) { | ||
this._stopListeningForScrollAbort(); | ||
} | ||
}, | ||
|
||
_handleMouseDown(event) { | ||
|
@@ -108,13 +119,62 @@ const TouchRipple = React.createClass({ | |
}, | ||
|
||
_handleTouchStart(event) { | ||
//If the user is swiping (not just tapping), save the position so we can | ||
//abort ripples if the user appears to be scrolling | ||
if (this.props.abortOnScroll && event.touches) { | ||
this._startListeningForScrollAbort(event); | ||
this._startTime = Date.now(); | ||
} | ||
this.start(event, true); | ||
}, | ||
|
||
_handleTouchEnd() { | ||
this.end(); | ||
}, | ||
|
||
//Check if the user seems to be scrolling and abort the animation if so | ||
_handleTouchMove(event) { | ||
//Stop trying to abort if we're already 300ms into the animation | ||
const timeSinceStart = Math.abs(Date.now() - this._startTime); | ||
if (timeSinceStart > 300) { | ||
this._stopListeningForScrollAbort(); | ||
return; | ||
} | ||
|
||
//If the user is scrolling... | ||
const deltaY = Math.abs(event.touches[0].clientY - this._firstTouchY); | ||
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. I believe that it would also be useful to make sure the user is not scrolling along |
||
const deltaX = Math.abs(event.touches[0].clientX - this._firstTouchX); | ||
//Call it a scroll after an arbitrary 6px (feels reasonable in testing) | ||
if (deltaY > 6 || deltaX > 6) { | ||
let currentRipples = this.state.ripples; | ||
const ripple = currentRipples[0]; | ||
//This clone will replace the ripple in ReactTransitionGroup with a | ||
//version that will disappear immediately when removed from the DOM | ||
const abortedRipple = React.cloneElement(ripple, {aborted: true}); | ||
//Remove the old ripple and replace it with the new updated one | ||
currentRipples = shift(currentRipples); | ||
currentRipples = push(currentRipples, abortedRipple); | ||
this.setState({ripples: currentRipples}, () => { | ||
//Call end after we've set the ripple to abort otherwise the setState | ||
//in end() merges with this and the ripple abort fails | ||
this.end(); | ||
}); | ||
} | ||
}, | ||
|
||
_startListeningForScrollAbort(event) { | ||
this._firstTouchY = event.touches[0].clientY; | ||
this._firstTouchX = event.touches[0].clientX; | ||
//Note that when scolling Chrome throttles this event to every 200ms | ||
//Also note we don't listen for scroll events directly as there's no general | ||
//way to cover cases like scrolling within containers on the page | ||
document.body.addEventListener('touchmove', this._handleTouchMove); | ||
}, | ||
|
||
_stopListeningForScrollAbort() { | ||
document.body.removeEventListener('touchmove', this._handleTouchMove); | ||
}, | ||
|
||
_getRippleStyle(event) { | ||
const style = {}; | ||
const el = ReactDOM.findDOMNode(this); | ||
|
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.
return
?