Skip to content

Commit

Permalink
Added status to draggable instances
Browse files Browse the repository at this point in the history
  • Loading branch information
clauderic committed Jun 22, 2024
1 parent 794cf2f commit 2ccc27c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/draggable-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@dnd-kit/abstract': patch
'@dnd-kit/dom': patch
'@dnd-kit/react': patch
---

Added `status` property to draggable instances to know the current status of a draggable instance. Useful to know if an instance is being dropped.
5 changes: 5 additions & 0 deletions packages/abstract/src/core/entities/draggable/draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface Input<T extends Data = Data> extends EntityInput<T> {
sensors?: Sensors;
}

export type DraggableStatus = 'idle' | 'dragging' | 'dropping';

export class Draggable<T extends Data = Data> extends Entity<T> {
constructor(
{modifiers, type, sensors, ...input}: Input<T>,
Expand Down Expand Up @@ -50,6 +52,9 @@ export class Draggable<T extends Data = Data> extends Entity<T> {
@reactive
public accessor type: Type | undefined;

@reactive
public accessor status: DraggableStatus = 'idle';

/**
* A boolean indicating whether the draggable item is the source of a drag operation.
*/
Expand Down
12 changes: 5 additions & 7 deletions packages/dom/src/core/plugins/feedback/Feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export class Feedback extends Plugin<DragDropManager> {

/* Initialize drag operation shape */
dragOperation.shape = new DOMRectangle(element);
source.status = 'dragging';

let elementMutationObserver: MutationObserver | undefined;
let documentMutationObserver: MutationObserver | undefined;
Expand Down Expand Up @@ -365,6 +366,8 @@ export class Feedback extends Plugin<DragDropManager> {
droppable.placeholder = undefined;
}

source.status = 'idle';

moved = false;
};

Expand All @@ -373,20 +376,15 @@ export class Feedback extends Plugin<DragDropManager> {
const onComplete = cleanup;
cleanup = undefined;

source.status = 'dropping';

const transform = currentTransform;

if (!transform) {
onComplete?.();
return;
}

let projectedTransform: Transform = {
x: 0,
y: 0,
scaleX: 1,
scaleY: 1,
};

manager.renderer.rendering.then(() => {
/* Force the source element to be promoted to the top layer before animating it */
showPopover(element);
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/core/draggable/useDraggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function useDraggable<T extends Data = Data>(
)
);
const isDragSource = useComputed(() => draggable.isDragSource);
const status = useComputed(() => draggable.status);

useOnValueChange(id, () => (draggable.id = id));
useOnValueChange(handle, () => (draggable.handle = handle));
Expand All @@ -55,6 +56,9 @@ export function useDraggable<T extends Data = Data>(
get isDragSource() {
return isDragSource.value;
},
get status() {
return status.value;
},
handleRef: useCallback(
(element: Element | null) => {
draggable.handle = element ?? undefined;
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/sortable/useSortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function useSortable<T extends Data = Data>(input: UseSortableInput<T>) {

const isDropTarget = useComputed(() => sortable.isDropTarget);
const isDragSource = useComputed(() => sortable.isDragSource);
const status = useComputed(() => sortable.status);

useOnValueChange(id, () => (sortable.id = id));
useOnValueChange(index, () => (sortable.index = index), layoutEffect);
Expand Down Expand Up @@ -112,6 +113,9 @@ export function useSortable<T extends Data = Data>(input: UseSortableInput<T>) {
get isDropTarget() {
return isDropTarget.value;
},
get status() {
return status.value;
},
handleRef: useCallback(
(element: Element | null) => {
sortable.handle = element ?? undefined;
Expand Down

0 comments on commit 2ccc27c

Please sign in to comment.