Skip to content

Commit d6db4ff

Browse files
committed
fix(DragDropSort): enable mobile drag-and-drop support
Set touch-action: none on the drag activator element (DragButton when useDragButton is true, wrapper div otherwise) to prevent the browser from intercepting touch events for native scrolling. Without this, mobile browsers fire pointercancel before the PointerSensor can activate the drag. Also adds activationConstraint: { distance: 8 } to PointerSensor so the drag only activates after deliberate pointer movement, preventing accidental drags on both desktop and mobile. Adds restrictToWindowEdges modifier to DndContext so dragged items cannot be moved outside the browser viewport. Consumers can override this via the modifiers prop. Fixes #12415
1 parent 6b93dbd commit d6db4ff

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

packages/react-drag-drop/src/components/DragDrop/DragDropContainer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DragCancelEvent
2222
} from '@dnd-kit/core';
2323
import { arrayMove, sortableKeyboardCoordinates } from '@dnd-kit/sortable';
24+
import { restrictToWindowEdges } from '@dnd-kit/modifiers';
2425
import { Draggable } from './Draggable';
2526
import { DraggableDataListItem } from './DraggableDataListItem';
2627
import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
@@ -103,7 +104,9 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
103104
);
104105

105106
const sensors = useSensors(
106-
useSensor(PointerSensor),
107+
useSensor(PointerSensor, {
108+
activationConstraint: { distance: 8 }
109+
}),
107110
useSensor(KeyboardSensor, {
108111
coordinateGetter: sortableKeyboardCoordinates
109112
})
@@ -298,6 +301,7 @@ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps>
298301
<DndContext
299302
sensors={sensors}
300303
collisionDetection={collisionDetectionStrategy}
304+
modifiers={[restrictToWindowEdges]}
301305
onDragEnd={handleDragEnd}
302306
onDragOver={handleDragOver}
303307
onDragStart={handleDragStart}

packages/react-drag-drop/src/components/DragDrop/Draggable.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export const Draggable: React.FunctionComponent<DraggableProps> = ({
2828

2929
const style = {
3030
transform: CSS.Transform.toString(transform),
31-
transition
31+
transition,
32+
...(!useDragButton && { touchAction: 'none' as const })
3233
};
3334

3435
return useDragButton ? (
@@ -38,7 +39,7 @@ export const Draggable: React.FunctionComponent<DraggableProps> = ({
3839
style={style}
3940
{...props}
4041
>
41-
<DragButton {...attributes} {...listeners} />
42+
<DragButton {...attributes} {...listeners} style={{ touchAction: 'none' }} />
4243
{children}
4344
</div>
4445
) : (

0 commit comments

Comments
 (0)