Search code examples
reactjsmobile-safaridraggabletouch-eventgsap

Draggable element in iframe on mobile is buggy


I'm creating a third-party app, which is loaded through an iframe.
The iframe creates a draggable element in side it.

On first page load (before scrolling the page), the draggable (slider) works fine.
But after scrolling the page behind the iframe, it's hard to get the slider to slide again.

(Note that this is a mobile issue, safari on iOS especially, but also chrome on iOS)

In the following demo I have used the GSAP Draggable library with react to create the draggable element but have also tried to code it in plain react with no luck.

Here is a demo: https://unfjl.csb.app/

I have tried a lot of different things (css, touchevents etc), but cannot get it to work...

What could cause this behaviour?

Update (no iframe example)

Demo no iframe: https://p5cu9.csb.app/

Same example with slider as above, but in this example the draggable is rendered directly in the DOM and not in an iframe. The problem still occurs. I suspect it has something to do with the fixed css position of the slider...


Solution

  • This is gonna sound really strange, but a bug in iOS Safari causes that and the only way I know of to get around it (which I discovered today via Googling) is to add a "touchstart" listener to the of the top-level page (not the iframe)!

    document.getElementsByTagName("body")[0].addEventListener("touchstart",function(){});
    

    That isn't something I can add to Draggable because it wouldn't be able to access the parent document (outside the iframe) to add that properly (security restrictions in the browser).

    You could also add a touchforcechange listener that prevents the default behavior (I'll be adding that to Draggable itself too):

    yourDraggableElement.addEventListener("touchforcechange", function(event) {
      event.preventDefault();
    });
    

    Browser bugs are fun, aren't they?! ;)

    (Originally answered in the GreenSock forums at https://greensock.com/forums/topic/21450-draggable-in-iframe-on-mobile-is-buggy/?tab=comments#comment-101225)