Skip to content

Commit 62867e8

Browse files
committed
feat: reduce drag line flicker with delay
1 parent 4cd03ce commit 62867e8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

.changeset/pink-ravens-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@headless-tree/core": patch
3+
---
4+
5+
Introduced a short delay before hiding the drag line when leaving a drag target, which helps to reduce flickering of the dragline when moving between items

packages/core/src/features/drag-and-drop/feature.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export const dragAndDropFeature: FeatureImplementation = {
193193
return;
194194
}
195195
dataRef.current.lastDragCode = nextDragCode;
196+
dataRef.current.lastDragEnter = Date.now();
196197

197198
const target = getDragTarget(e, item, tree);
198199

@@ -222,13 +223,16 @@ export const dragAndDropFeature: FeatureImplementation = {
222223
},
223224

224225
onDragLeave: () => {
225-
const dataRef = tree.getDataRef<DndDataRef>();
226-
dataRef.current.lastDragCode = "no-drag";
227-
tree.applySubStateUpdate("dnd", (state) => ({
228-
...state,
229-
draggingOverItem: undefined,
230-
dragTarget: undefined,
231-
}));
226+
setTimeout(() => {
227+
const dataRef = tree.getDataRef<DndDataRef>();
228+
if ((dataRef.current.lastDragEnter ?? 0) + 100 >= Date.now()) return;
229+
dataRef.current.lastDragCode = "no-drag";
230+
tree.applySubStateUpdate("dnd", (state) => ({
231+
...state,
232+
draggingOverItem: undefined,
233+
dragTarget: undefined,
234+
}));
235+
}, 100);
232236
},
233237

234238
onDragEnd: (e: DragEvent) => {

packages/core/src/features/drag-and-drop/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ItemInstance, SetStateFn } from "../../types/core";
33
export interface DndDataRef {
44
lastDragCode?: string;
55
lastAllowDrop?: boolean;
6+
lastDragEnter?: number;
67
windowDragEndListener?: () => void;
78
}
89

0 commit comments

Comments
 (0)