-
Notifications
You must be signed in to change notification settings - Fork 375
Remove e.preventDefault from handleTouchEnd #102
Conversation
As far as I'm aware, this isn't needed. Fixes vkbansal#98.
Are you sure, this is not needed? This should have been added for a reason. |
It works for my use case. And all tests still pass. |
@danbovey I agree that it works for your use case, but it should not break for hundreds of others people who depend on this package. I hope you understand my concern. |
I would also like it removed to be honest, we haven't seen any adverse effect to removing it, but maybe @bjarkig82 can tell us more as he added it in e0df3b3 |
I was working on adding a feature that would trigger the context menu if the element is pressed for sertain amount of time. Have you tested this using a touch screen on a windows device? They fire the left click event if the screen is touched for half a second or so. If I remember correctly, I added this to prevent the default browser context menu to appear over the custom context menu in these cases. Sorry about the horrible commit message habit :-) |
Maybe a better solution then would be to keep track of the state of the touch action and make sure that we prevent default only if we showed a contextmenu, but if not then we let the default action occur in touchend: Something like:
|
I agree, that would be a more elegant solution :-) |
Thanks @bjarkig82, at least it does have a purpose being there right now. I've updated this PR with @Seldaek's solution 🎉 |
src/ContextMenuTrigger.js
Outdated
@@ -28,6 +28,7 @@ export default class ContextMenuTrigger extends Component { | |||
renderTag: 'div' | |||
}; | |||
|
|||
static touchHandled = false; |
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.
This shouldn't be static but a regular instance property if you address it with this
below. i.e. just remove the static
keyword and all is fine.
src/ContextMenuTrigger.js
Outdated
@@ -50,6 +51,8 @@ export default class ContextMenuTrigger extends Component { | |||
if (this.props.holdToDisplay >= 0) { | |||
event.persist(); | |||
|
|||
this.touchHandled = true; |
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.
This should be inside the lambda function at line 57, otherwise it'll always be true not just when the touch actually triggered the "hold-to-display" functionality, and we're back at the point where preventDefault fires every time.
Thanks for updating it :) Hopefully now it's safe enough to be merged. |
src/ContextMenuTrigger.js
Outdated
this.touchstartTimeoutId = setTimeout( | ||
() => this.handleContextClick(event), | ||
this.props.holdToDisplay | ||
this.props.holdToDisplay, | ||
this.touchHandled = true |
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.
Woops, that's still not good I'm afraid.. To be fair the short syntax is easy to get confused by, but it should be expanded to this:
this.touchstartTimeoutId = setTimeout(
() => {
this.handleContextClick(event);
this.touchHandled = true;
},
this.props.holdToDisplay
);
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 wanted to, I hate comma separated stuff
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.
But setTimeout takes two args, a callback and then a timeout (this.props.holdToDisplay). The way you fixed it now you left the timeout in the middle of the callback, so that's not gonna work :)
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.
Woops, yeah, this is why you should add curly brackets and semicolons as soon as you start doing more than one thing.
Right now you have: this.touchstartTimeoutId = setTimeout(
() => {
this.handleContextClick(event),
this.props.holdToDisplay,
this.touchHandled = true
}
); But this essentially does this.touchstartTimeoutId = setTimeout(
() => {
this.handleContextClick(event);
this.touchHandled = true;
},
this.props.holdToDisplay
); |
After all of this, I'm not sure it fixing the problem. Running it in Chrome Dev tools which converts clicks to touch events and it's still cancelling regular click events. |
Pushed these changes to my app and running it on Android Chrome = same results |
There is still one issue with the PR I think, in the beginning of handleTouchstart it should set Curious if you can try if that fixes it or not.. |
@Seldaek that last change fixes it! The file changes make sense to me now 😃 Tested on Chrome Dev Tools and Android and it appears to work. |
Nice good to hear :) |
As far as I'm aware, this isn't needed. Fixes #98.