Skip to content

Commit

Permalink
Add transition when keyboard sorting without DragOverlay (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic authored Jan 10, 2022
1 parent 6e52501 commit 1ade2f3
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/transition-keyboard-sorting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dnd-kit/sortable': minor
---

Use `transition` for the active draggable node when keyboard sorting without a `<DragOverlay />`.
6 changes: 4 additions & 2 deletions packages/core/src/components/DndContext/DndContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,10 @@ export const DndContext = memo(function DndContext({

const internalContext = useMemo(() => {
const context: InternalContextDescriptor = {
activatorEvent,
activators,
active,
activeNodeRect,
activators,
ariaDescribedById: {
draggable: draggableDescribedById,
},
Expand All @@ -654,9 +655,10 @@ export const DndContext = memo(function DndContext({

return context;
}, [
activatorEvent,
activators,
active,
activeNodeRect,
activators,
dispatch,
draggableDescribedById,
draggableNodes,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/hooks/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function useDraggable({
const key = useUniqueId(ID_PREFIX);
const {
activators,
activatorEvent,
active,
activeNodeRect,
ariaDescribedById,
Expand Down Expand Up @@ -84,6 +85,7 @@ export function useDraggable({

return {
active,
activatorEvent,
activeNodeRect,
attributes: memoizedAttributes,
isDragging,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/store/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export const defaultPublicContext: PublicContextDescriptor = {
};

export const defaultInternalContext: InternalContextDescriptor = {
activatorEvent: null,
activators: [],
active: null,
activeNodeRect: null,
activators: [],
ariaDescribedById: {
draggable: '',
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface PublicContextDescriptor {
}

export interface InternalContextDescriptor {
activatorEvent: Event | null;
activators: SyntheticListeners;
active: Active | null;
activeNodeRect: ClientRect | null;
Expand Down
15 changes: 12 additions & 3 deletions packages/sortable/src/hooks/useSortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UseDraggableArguments,
UseDroppableArguments,
} from '@dnd-kit/core';
import {CSS, useCombinedRefs} from '@dnd-kit/utilities';
import {CSS, isKeyboardEvent, useCombinedRefs} from '@dnd-kit/utilities';

import {Context} from '../components';
import type {SortingStrategy} from '../types';
Expand Down Expand Up @@ -74,6 +74,7 @@ export function useSortable({
});
const {
active,
activatorEvent,
activeNodeRect,
attributes,
setNodeRef: setDraggableNodeRef,
Expand Down Expand Up @@ -116,7 +117,12 @@ export function useSortable({
? getNewIndex({id, items, activeIndex, overIndex})
: index;
const activeId = active?.id;
const previous = useRef({activeId, items, newIndex, containerId});
const previous = useRef({
activeId,
items,
newIndex,
containerId,
});
const itemsHaveChanged = items !== previous.current.items;
const shouldAnimateLayoutChanges = animateLayoutChanges({
active,
Expand Down Expand Up @@ -189,7 +195,10 @@ export function useSortable({
return disabledTransition;
}

if (shouldDisplaceDragSource || !transition) {
if (
(shouldDisplaceDragSource && !isKeyboardEvent(activatorEvent)) ||
!transition
) {
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion stories/2 - Presets/Sortable/Sortable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export function SortableItem({
})}
onRemove={onRemove ? () => onRemove(id) : undefined}
transform={transform}
transition={!useDragOverlay && isDragging ? 'none' : transition}
transition={transition}
wrapperStyle={wrapperStyle({index, isDragging, id})}
listeners={listeners}
data-index={index}
Expand Down

0 comments on commit 1ade2f3

Please sign in to comment.