Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent default handling of touch events?

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:

  1. I tried capturing onTouchStart, onTouchMove and onTouchEnd. It turns out that on FF Android the first event that fires when doing pinch on component is onTouchStart with a single touch, then onTouchStart with two touches, then onTouchMove. But calling event.preventDefault() or event.stopPropagation() in onTouchMove handler doesn't (always) stop page zoom/scroll. Preventing event escalation in the first call to onTouchStart 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.

  2. Second approach was setting touch-action: none on document.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 then touch-action is ignored. So this is not useful on Chrome.

  3. 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.

like image 621
johndodo Avatar asked Mar 28 '18 17:03

johndodo


People also ask

What does event prevent default do?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

Does Safari support touch events?

Gestures handled by Safari on iOS emulate mouse events. In addition, you can register for iOS-specific multi-touch and gesture events directly. Orientation events are another example of an iOS-specific event.

What is the use of Touch_move?

The touchmove event is used to execute a script when the user moves the finger across the screen. It works only on touch screen devices and triggers once for every movement and continues to trigger until the finger is released. All HTML elements supported by this event.


1 Answers

TL;DR: I was missing { passive: false } when registering event handlers.

The issue I had with preventDefault() with Chrome was due to their scrolling "intervention" (read: breaking the web IE-style). In short, because the handlers that don't call preventDefault() can be handled faster, a new option was added to addEventListener named passive. If set to true then event handler promises not to call preventDefault (if it does, the call will be ignored). Chrome however decided to go a step further and make {passive: true} default (since version 56).

Solution is calling the event listener with passive explicitly set to false:

window.addEventListener('touchmove', ev => {   if (weShouldStopDefaultScrollAndZoom) {     ev.preventDefault();     ev.stopImmediatePropagation();   }; }, { passive: false }); 

Note that this negatively impacts performance.

As a side note, it seems I misunderstood touch-action CSS, however I still can't use it because it needs to be set before touch sequence starts. But if this is not the case, it is probably more performant and seems to be supported on all applicable platforms (Safari on Mac does not support it, but on iOS it does). This post says it best:

For your case you probably want to mark your text area (or whatever) 'touch-action: none' to disable scrolling/zooming without disabling all the other behaviors.

The CSS property should be set on the component and not on document as I did it:

<div style="touch-action: none;">   ... my component ... </div> 

In my case I will still need to use passive event handlers, but it's good to know the options... Hope it helps someone.

like image 105
johndodo Avatar answered Sep 22 '22 20:09

johndodo