react-dnd / react-dnd

Drag and Drop for React
http://react-dnd.github.io/react-dnd
MIT License
21.08k stars 1.99k forks source link

Dragging in mobile from vite compiled code incorrectly scrolls the view #3645

Open andylwelch opened 1 month ago

andylwelch commented 1 month ago

Describe the bug Building my repository with vite I always encounter an issue that while dragging an item in a scroll zone that the scroll also moves to match the drag (e.g. stays in the same position!).

Everything works fine running via vite dev.

After many hours of hunting in the code I realised that the default treeshaking removes { passive: false } in the react-dnd-touch-backend/dist/TouchBackendImpl.js file

import { supportsPassive } from './utils/supportsPassive.js';

...

addEventListener(subject, event, handler, capture = false) {
        const options = supportsPassive ? {
            capture,
            passive: false
        } : capture;
        this.listenerTypes.forEach(function(listenerType) {
            const evt = eventNames[listenerType][event];
            if (evt) {
                subject.addEventListener(evt, handler, options);
            }
        });
    }

For example this is the code after treeshaking; image instead of the following with treeshaking disabled; image

Without this { passive: false } the e1.preventDefault() in the this.handleTopMove = (e1)=>{ function is unable to stop the default scroll behaviour;

image

Reproduction

Steps to reproduce the behavior:

  1. Use drag and drop in a scroll container
  2. Build with vite
  3. Run built code on a mobile/touch device
  4. Attempt to drag an item and see the scroll move while dragging.

Expected behavior While dragging the touch scroll to be prevented.

Screenshots image

Desktop (please complete the following information):

Additional context I fixed this problem by copying the entire dist folder to my src code and using the copy instead. Note that I did not have to change any code to fix this issue. I suspect that the vite => esbuild/'rollup' compiler is incorrectly assuming there are no sideEffects and is removing the following

image

Perhaps we could rename the constant supportsPassive to a getSupportsPassive function which can be run inside TouchBackendImpl and shouldnt be removed by treeshaking?

vezaynk commented 1 week ago

I'm also facing this issue. I made a reproduction of it here (though I initially suspected multi-backend to be at fault): https://github.com/vezaynk/dnd-vite-issue-demo

andylwelch commented 1 week ago

Thanks for the reproduction @vezaynk. Glad this issue was useful for someone!