-
Notifications
You must be signed in to change notification settings - Fork 920
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
Fix touch event issue #594
Changes from 6 commits
212d142
34a3fc8
1354a63
9785fe7
d1afdd5
f5cf913
c5358d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,24 @@ import * as alpha from '../../helpers/alpha' | |
import Checkboard from './Checkboard' | ||
|
||
export class Alpha extends (PureComponent || Component) { | ||
constructor(props) { | ||
super(props) | ||
|
||
this.container = null | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unbindEventListeners() | ||
} | ||
|
||
componentDidMount() { | ||
// e.preventDefault() does not work properly on React synthetic touch event. | ||
// As a workaround, touch events have to be added to directly to node | ||
// https://github.com/facebook/react/issues/9809#issuecomment-414072263 | ||
this.container.addEventListener('touchstart', this.handleChange) | ||
this.container.addEventListener('touchmove', this.handleChange) | ||
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. It seems they should be 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. @fsubal It's done. Thanks for reviewing 😄 Aren't they GCed when unmounted as they are attached to the node in the component? But I guess it is safe to remove them explicitly in |
||
} | ||
|
||
handleChange = (e) => { | ||
const change = alpha.calculateChange(e, this.props.hsl, this.props.direction, this.props.a, this.container) | ||
change && typeof this.props.onChange === 'function' && this.props.onChange(change, e) | ||
|
@@ -96,8 +110,6 @@ export class Alpha extends (PureComponent || Component) { | |
style={ styles.container } | ||
ref={ container => this.container = container } | ||
onMouseDown={ this.handleMouseDown } | ||
onTouchMove={ this.handleChange } | ||
onTouchStart={ this.handleChange } | ||
> | ||
<div style={ styles.pointer }> | ||
{ this.props.pointer ? ( | ||
|
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.
follow up here, the comment you're referencing doesn't actually show the solution.
You need to update your use to one of these:
{ passive: false }
option to theaddEventListener
el.ontouchstart
Note if you use the
{ passive: false }
option route, you should probably do the check to see if it is supported too.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.
@hartzis I have used the second option. I think the diffrence is that
el.addEventListener('touchstart')
is used instead ofel.ontouchstart
. Aren't those same? Please correct me if I'm wrong.Thanks for following up!
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.
They are different. Pretty helpful explanation: https://stackoverflow.com/a/35093997
In order to gain the ability to call
preventDefault
you will need to add also add the{ passive: false }
option for chrome.or do what dan recomended
Let me know if this helps, i just stumbled across this PR since it referenced a facebook issue i was looking at and i'm dealing with a lot of the same issues as the maintainer for https://github.com/dogfessional/react-swipeable
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.
@hartzis Thanks for the information. After looking into this issue more, I found that
ontouchstart
is currently an experimental feature. https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/ontouchstart. Also in Chrome, the flagtouch-event
needs to be enabled to useontouchstart
API.I'm not sure why dan used
ontouchstart
...Regarding
passive
option, I think it does not need to be set tofalse
unless the event handler is attached to document level e.g.window
,document
orbody
. Document level touch event handlers are treated as passiveThere 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.
@cokencode That is an incredibly helpful link, thank you for sharing. I either totally missed or forgot the
window, document or body
part of that change. You rock!