Skip to content
Open
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
35 changes: 31 additions & 4 deletions packages/dom/src/sortable/sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
defaultCollisionDetection,
type CollisionDetector,
} from '@dnd-kit/collision';
import type {Alignment} from '@dnd-kit/geometry';
import type {Alignment, Shape} from '@dnd-kit/geometry';
import {Draggable, Droppable} from '@dnd-kit/dom';
import type {
DraggableInput,
Expand Down Expand Up @@ -100,6 +100,8 @@ const store = new WeakStore<
TemporaryState
>();

const shapeStore = new WeakStore<DragDropManager, UniqueIdentifier, Shape>();

export class Sortable<T extends Data = Data> {
public draggable: Draggable<T>;
public droppable: Droppable<T>;
Expand Down Expand Up @@ -161,6 +163,17 @@ export class Sortable<T extends Data = Data> {
initialGroup: this.group,
}))
);

const isSource = untracked(
() => this.id === this.manager?.dragOperation.source?.id
);

if (isSource && !shapeStore.get(this.manager, this.id)) {
const shape = untracked(() => this.droppable.shape);
if (shape) {
shapeStore.set(this.manager, this.id, shape);
}
}
}
},
() => {
Expand All @@ -173,7 +186,7 @@ export class Sortable<T extends Data = Data> {
this.#previousIndex = index;
this.#previousGroup = group;

this.animate();
this.animate(false);
}
},
() => {
Expand All @@ -191,6 +204,15 @@ export class Sortable<T extends Data = Data> {
manager?.registry.register(plugin);
}
},
() => {
const manager = untracked(() => this.manager);
const id = untracked(() => this.id);
const status = manager?.dragOperation.status;

if (status?.dragging && manager && shapeStore.get(manager, id)) {
queueMicrotask(() => this.animate(true));
}
},
...inputEffects(),
],
type,
Expand All @@ -210,15 +232,18 @@ export class Sortable<T extends Data = Data> {
this.transition = transition;
}

protected animate() {
protected animate(idChanged = false) {
untracked(() => {
const {manager, transition} = this;
const {shape} = this.droppable;

if (!manager) return;

const {idle} = manager.dragOperation.status;

const shape = idChanged
? shapeStore.get(manager, this.id)
: (this.droppable.shape ?? shapeStore.get(manager, this.id));

if (!shape || !transition || (idle && !transition.idle)) {
return;
}
Expand Down Expand Up @@ -251,6 +276,8 @@ export class Sortable<T extends Data = Data> {
return;
}

shapeStore.set(manager, this.id, updatedShape);

const delta = {
x: shape.boundingRectangle.left - updatedShape.boundingRectangle.left,
y: shape.boundingRectangle.top - updatedShape.boundingRectangle.top,
Expand Down
Loading