Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ vi.mock('@dnd-kit/core', () => ({
DndContext: mocks.dndContext,
closestCenter: 'closestCenter',
KeyboardSensor: 'KeyboardSensor',
PointerSensor: 'PointerSensor',
MouseSensor: 'MouseSensor',
TouchSensor: 'TouchSensor',
useSensor: mocks.useSensor,
useSensors: mocks.useSensors,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
type DragEndEvent,
type DragStartEvent,
KeyboardSensor,
PointerSensor,
MouseSensor,
TouchSensor,
useSensor,
useSensors,
} from '@dnd-kit/core';
Expand Down Expand Up @@ -339,7 +340,7 @@ const SortableGridListItem = <T,>({
const rowRef = useRef<HTMLDivElement | null>(null);
const {attributes, listeners, setNodeRef, transform, transition, isDragging} = useSortable({
id,
disabled: !isMovable,
disabled: !enabled || !isMovable,
});

const handleNodeRef = (node: HTMLDivElement | null) => {
Expand Down Expand Up @@ -404,15 +405,15 @@ const SortableGridListItem = <T,>({
}
}

if (e.target !== e.currentTarget) return;
if (!enabled || e.target !== e.currentTarget) return;

(listeners?.onKeyDown as JSX.KeyboardEventHandler<HTMLDivElement>)?.(e);
};

// ? useSortable returns a fresh listeners object each render, so memoizing is a no-op
// When fullRowDraggable, dnd-kit listeners must not override the guarded handleKeyDown
let rowListenersSafe: Omit<NonNullable<typeof listeners>, 'onKeyDown'> | undefined;
if (fullRowDraggable && isMovable && listeners != null) {
if (enabled && fullRowDraggable && isMovable && listeners != null) {
const {onKeyDown: _ignored, ...rest} = listeners;
rowListenersSafe = rest;
}
Expand Down Expand Up @@ -471,7 +472,7 @@ const SortableGridListItem = <T,>({
'relative flex items-center rounded outline-none',
'focus-visible:ring-2 focus-visible:ring-ring/25 focus-visible:ring-inset',
isDragging && 'bg-surface-neutral shadow-[0_2px_8px_2px] shadow-main/10 ring-1 ring-main/5',
fullRowDraggable && isMovable && (isDragging ? 'cursor-grabbing' : 'cursor-grab'),
enabled && fullRowDraggable && isMovable && (isDragging ? 'cursor-grabbing' : 'cursor-grab'),
resolvedClassName,
)}
{...rowListenersSafe}
Expand All @@ -490,7 +491,7 @@ const SortableGridListItem = <T,>({
tabIndex={-1}
disabled={!enabled}
aria-label={dragLabel}
{...(fullRowDraggable ? undefined : listeners)}
{...(enabled && !fullRowDraggable ? listeners : undefined)}
>
<GripVertical className='size-5' />
</button>
Expand Down Expand Up @@ -530,7 +531,8 @@ export const SortableGridList = <T,>({
const pendingBlurClearVersionRef = useRef(0);

const sensors = useSensors(
useSensor(PointerSensor, {activationConstraint: {distance: 5}}),
useSensor(MouseSensor, {activationConstraint: {distance: 5}}),
useSensor(TouchSensor, {activationConstraint: {delay: 200, tolerance: 5}}),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
KeyboardSensor,
type MeasuringConfiguration,
MeasuringStrategy,
PointerSensor,
MouseSensor,
TouchSensor,
useSensor,
useSensors,
} from '@dnd-kit/core';
Expand Down Expand Up @@ -221,16 +222,16 @@ const SortableListItem = <T,>({
});

const handleKeyDown: JSX.KeyboardEventHandler<HTMLDivElement> = e => {
if (e.target !== e.currentTarget) return;
if (!enabled || e.target !== e.currentTarget) return;
(listeners?.onKeyDown as JSX.KeyboardEventHandler<HTMLDivElement>)?.(e);
};

// When fullRowDraggable, dnd-kit listeners must not override the guarded handleKeyDown
const rowListenersSafe = useMemo(() => {
if (!fullRowDraggable || !isMovable || !listeners) return undefined;
if (!enabled || !fullRowDraggable || !isMovable || !listeners) return undefined;
const {onKeyDown: _ignored, ...rest} = listeners;
return rest;
}, [fullRowDraggable, isMovable, listeners]);
}, [enabled, fullRowDraggable, isMovable, listeners]);

const handleFocus = () => setIsFocused(true);

Expand Down Expand Up @@ -275,7 +276,7 @@ const SortableListItem = <T,>({
tabIndex={-1}
disabled={!enabled}
aria-label={dragLabel}
{...(fullRowDraggable ? undefined : listeners)}
{...(enabled && !fullRowDraggable ? listeners : undefined)}
>
<GripVertical className='size-5' />
</button>
Expand Down Expand Up @@ -310,8 +311,8 @@ const SortableListItem = <T,>({
isDragging && 'bg-surface-neutral shadow-[0_2px_8px_2px] shadow-main/10 ring-1 ring-main/5',
isDragging && !dropAllowed && 'opacity-40',
// Full-row drag targets must not turn a press-drag into a text selection.
fullRowDraggable && isMovable && 'touch-none select-none',
fullRowDraggable && isMovable && (isDragging ? 'cursor-grabbing' : 'cursor-grab'),
enabled && fullRowDraggable && isMovable && 'select-none',
enabled && fullRowDraggable && isMovable && (isDragging ? 'cursor-grabbing' : 'cursor-grab'),
resolvedClassName,
)}
{...rowListenersSafe}
Expand Down Expand Up @@ -357,7 +358,8 @@ export const SortableList = <T,>({
const [drop, setDrop] = useState<{info: SortableDragInfo; hint: SortableDropHint | null} | null>(null);

const sensors = useSensors(
useSensor(PointerSensor, {activationConstraint: {distance: 5}}),
useSensor(MouseSensor, {activationConstraint: {distance: 5}}),
useSensor(TouchSensor, {activationConstraint: {delay: 200, tolerance: 5}}),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
}),
Expand Down