Skip to content

Commit 7901be4

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 9e88adf commit 7901be4

2 files changed

Lines changed: 13 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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export const Draggable: React.FunctionComponent<DraggableProps> = ({
3131

3232
const style = {
3333
transform: CSS.Transform.toString(transform),
34-
transition
34+
transition,
35+
...(!useDragButton && { touchAction: 'none' as const })
3536
};
3637

3738
return useDragButton ? (
@@ -41,7 +42,12 @@ export const Draggable: React.FunctionComponent<DraggableProps> = ({
4142
style={style}
4243
{...props}
4344
>
44-
<DragButton {...attributes} {...listeners} {...(dragButtonAriaLabel && { 'aria-label': dragButtonAriaLabel })} />
45+
<DragButton
46+
{...attributes}
47+
{...listeners}
48+
{...(dragButtonAriaLabel && { 'aria-label': dragButtonAriaLabel })}
49+
style={{ touchAction: 'none' }}
50+
/>
4551
{children}
4652
</div>
4753
) : (

0 commit comments

Comments
 (0)