Skip to content

Commit 71cbc34

Browse files
Support horizontal sorting in sortable transform algorithm.
The new approach directly uses target droppable layout positions in order to calculate sorted transforms (which is more flexible and simpler). Note: Requires sorted items to be the same size to avoid odd visual behaviour as items are not currently scaled at all (though that may be implemented in future). This was also true of the previous algorithm.
1 parent 9b1447c commit 71cbc34

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
- Support horizontal sorting in sortable transform algorithm. The new approach
8+
directly uses target droppable layout positions in order to calculate sorted
9+
transforms (which is more flexible and simpler).
10+
11+
Note: Requires sorted items to be the same size to avoid odd visual behaviour
12+
as items are not currently scaled at all (though that may be implemented in
13+
future). This was also true of the previous algorithm.
14+
315
## [0.5.0] - 2022-04-30
416

517
A significant update with multiple improvements and some breaking changes. Most

src/create-sortable.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const createSortable = (
2121
id: string | number,
2222
data: Record<string, any> = {}
2323
): Sortable => {
24-
const [dndState, { anyDraggableActive, displace }] = useDragDropContext()!;
24+
const [dndState, { displace }] = useDragDropContext()!;
2525
const [sortableState] = useSortableContext()!;
2626
const draggable = createDraggable(id, data);
2727
const droppable = createDroppable(id, data);
@@ -37,36 +37,15 @@ const createSortable = (
3737
const resolvedInitialIndex = initialIndex();
3838
const resolvedCurrentIndex = currentIndex();
3939

40-
if (
41-
!anyDraggableActive() ||
42-
resolvedCurrentIndex === resolvedInitialIndex
43-
) {
44-
return delta;
45-
}
46-
47-
const draggableId = dndState.active.draggable!;
48-
const draggableInitialIndex = sortableState.initialIds.indexOf(draggableId);
49-
const draggableLayout = layoutById(draggableId)!;
40+
if (resolvedCurrentIndex !== resolvedInitialIndex) {
41+
const currentLayout = layoutById(id);
42+
const targetLayout = layoutById(
43+
sortableState.initialIds[resolvedCurrentIndex]
44+
);
5045

51-
if (draggable.isActiveDraggable) {
52-
const droppableId = dndState.active.droppable!;
53-
const droppableLayout = layoutById(droppableId)!;
54-
if (resolvedCurrentIndex > resolvedInitialIndex) {
55-
delta.y = droppableLayout.bottom - draggableLayout.bottom;
56-
} else {
57-
delta.y = droppableLayout.top - draggableLayout.top;
58-
}
59-
} else {
60-
if (resolvedCurrentIndex > resolvedInitialIndex) {
61-
const leadingId = sortableState.initialIds[draggableInitialIndex - 1];
62-
const leadingLayout = layoutById(leadingId)!;
63-
const leadingGap = draggableLayout.top - leadingLayout.bottom;
64-
delta.y += draggableLayout.height + leadingGap;
65-
} else {
66-
const trailingId = sortableState.initialIds[draggableInitialIndex + 1];
67-
const trailingLayout = layoutById(trailingId)!;
68-
const trailingGap = trailingLayout.top - draggableLayout.bottom;
69-
delta.y -= draggableLayout.height + trailingGap;
46+
if (currentLayout && targetLayout) {
47+
delta.x = targetLayout.x - currentLayout.x;
48+
delta.y = targetLayout.y - currentLayout.y;
7049
}
7150
}
7251

@@ -125,6 +104,6 @@ const createSortable = (
125104
) as unknown as Sortable;
126105

127106
return sortable;
128-
};
107+
};;;;;;;
129108

130109
export { createSortable };

0 commit comments

Comments
 (0)