I am trying to do something similar to what embedded Google maps do. My component should ignore single touch (allowing user to scroll page) and pinch outside of itself (allowing user to zoom page), but should react to double touch (allowing user to navigate inside the component) and disallow any default action in this case.
How do I prevent default handling of touch events, but only in the case when user is interacting with my component with two fingers?
What I have tried:
I tried capturing
onTouchStart
,onTouchMove
andonTouchEnd
. It turns out that on FF Android the first event that fires when doing pinch on component isonTouchStart
with a single touch, thenonTouchStart
with two touches, thenonTouchMove
. But callingevent.preventDefault()
orevent.stopPropagation()
inonTouchMove
handler doesn't (always) stop page zoom/scroll. Preventing event escalation in the first call toonTouchStart
does help - unfortunately at that time I don't know yet if it's going to be multitouch or not, so I can't use this.Second approach was setting
touch-action: none
ondocument.body
. This works with Chrome Android, but I could only make it work with Firefox Android if I set this on all elements (except for my component). So while this is doable, it seems like it could have unwanted side effects and performance issues. EDIT: Further testing revealed that this works for Chrome only if the CSS is set before the touch has started. In other words, if I inject CSS styles when I detect 2 fingers thentouch-action
is ignored. So this is not useful on Chrome.I have also tried adding a new event listener on component mount:
document.body.addEventListener("touchmove", ev => { ev.preventDefault(); ev.stopImmediatePropagation(); }, true);
(and the same for
touchstart
). Doing so works in Firefox Android, but does nothing on Chrome Android.
I am running out of ideas. Is there a reliable cross-browser way to achieve what Google apparently did, or did they use multiple hacks and lots of testing on every browser to make it work? I would appreciate if someone pointed out an error in my approach(es) or propose a new way.