@pyreon/dnd 0.24.4 → 0.24.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -6
- package/src/index.ts +0 -20
- package/src/tests/dnd.test.ts +0 -760
- package/src/tests/integration.test.ts +0 -1662
- package/src/tests/use-sortable-leak.test.ts +0 -110
- package/src/types.ts +0 -92
- package/src/use-drag-monitor.ts +0 -69
- package/src/use-draggable.ts +0 -68
- package/src/use-droppable.ts +0 -69
- package/src/use-file-drop.browser.test.tsx +0 -148
- package/src/use-file-drop.ts +0 -133
- package/src/use-sortable.ts +0 -265
package/src/use-sortable.ts
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine'
|
|
2
|
-
import { draggable, dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'
|
|
3
|
-
import { autoScrollForElements } from '@atlaskit/pragmatic-drag-and-drop-auto-scroll/element'
|
|
4
|
-
import {
|
|
5
|
-
attachClosestEdge,
|
|
6
|
-
type Edge,
|
|
7
|
-
extractClosestEdge,
|
|
8
|
-
} from '@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge'
|
|
9
|
-
import { onCleanup, signal } from '@pyreon/reactivity'
|
|
10
|
-
import type { DropEdge, UseSortableOptions, UseSortableResult } from './types'
|
|
11
|
-
|
|
12
|
-
const SORT_KEY = '__pyreon_sortable_key'
|
|
13
|
-
const SORT_ID = '__pyreon_sortable_id'
|
|
14
|
-
|
|
15
|
-
let _sortableCounter = 0
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Sortable list with signal-driven state, auto-scroll, and edge detection.
|
|
19
|
-
*
|
|
20
|
-
* Features:
|
|
21
|
-
* - Keyed drag items matching `<For by={...}>` pattern
|
|
22
|
-
* - Auto-scroll when dragging near container edges
|
|
23
|
-
* - Closest-edge detection (drop above/below or left/right)
|
|
24
|
-
* - Axis constraint (vertical/horizontal)
|
|
25
|
-
* - Keyboard reordering (Alt+Arrow keys)
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```tsx
|
|
29
|
-
* const items = signal([
|
|
30
|
-
* { id: "1", name: "Alice" },
|
|
31
|
-
* { id: "2", name: "Bob" },
|
|
32
|
-
* { id: "3", name: "Charlie" },
|
|
33
|
-
* ])
|
|
34
|
-
*
|
|
35
|
-
* const { containerRef, itemRef, activeId, overId, overEdge } = useSortable({
|
|
36
|
-
* items,
|
|
37
|
-
* by: (item) => item.id,
|
|
38
|
-
* onReorder: (newItems) => items.set(newItems),
|
|
39
|
-
* })
|
|
40
|
-
*
|
|
41
|
-
* <ul ref={containerRef}>
|
|
42
|
-
* <For each={items()} by={item => item.id}>
|
|
43
|
-
* {(item) => (
|
|
44
|
-
* <li
|
|
45
|
-
* ref={itemRef(item.id)}
|
|
46
|
-
* class={activeId() === item.id ? "dragging" : ""}
|
|
47
|
-
* style={overId() === item.id ? `border-${overEdge()}: 2px solid blue` : ""}
|
|
48
|
-
* >
|
|
49
|
-
* {item.name}
|
|
50
|
-
* </li>
|
|
51
|
-
* )}
|
|
52
|
-
* </For>
|
|
53
|
-
* </ul>
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export function useSortable<T>(options: UseSortableOptions<T>): UseSortableResult {
|
|
57
|
-
if (typeof document === 'undefined') {
|
|
58
|
-
const noop = (_el: HTMLElement | null) => {}
|
|
59
|
-
return {
|
|
60
|
-
containerRef: noop,
|
|
61
|
-
itemRef: () => noop,
|
|
62
|
-
activeId: () => null,
|
|
63
|
-
overId: () => null,
|
|
64
|
-
overEdge: () => null,
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const sortableId = `sortable-${++_sortableCounter}`
|
|
69
|
-
const activeId = signal<string | number | null>(null)
|
|
70
|
-
const overId = signal<string | number | null>(null)
|
|
71
|
-
const overEdge = signal<DropEdge | null>(null)
|
|
72
|
-
const axis = options.axis ?? 'vertical'
|
|
73
|
-
|
|
74
|
-
// Shared (container-level) pdnd cleanups — drained once on unmount.
|
|
75
|
-
const cleanups: (() => void)[] = []
|
|
76
|
-
// Per-item pdnd cleanups, keyed by sort key. Disposed individually on
|
|
77
|
-
// item unmount / re-register so a churning list doesn't accumulate
|
|
78
|
-
// dead registrations for the sortable's whole lifetime.
|
|
79
|
-
const itemCleanups = new Map<string | number, () => void>()
|
|
80
|
-
|
|
81
|
-
/** Perform the reorder based on current active/over/edge state. */
|
|
82
|
-
function performReorder() {
|
|
83
|
-
const dragId = activeId.peek()
|
|
84
|
-
const dropId = overId.peek()
|
|
85
|
-
const edge = overEdge.peek()
|
|
86
|
-
if (dragId == null || dropId == null || dragId === dropId) return
|
|
87
|
-
|
|
88
|
-
const currentItems = options.items()
|
|
89
|
-
const dragIndex = currentItems.findIndex((item) => options.by(item) === dragId)
|
|
90
|
-
const dropIndex = currentItems.findIndex((item) => options.by(item) === dropId)
|
|
91
|
-
if (dragIndex === -1 || dropIndex === -1) return
|
|
92
|
-
|
|
93
|
-
const reordered = [...currentItems]
|
|
94
|
-
const [moved] = reordered.splice(dragIndex, 1)
|
|
95
|
-
if (!moved) return
|
|
96
|
-
|
|
97
|
-
// Determine insert position based on closest edge
|
|
98
|
-
const rawInsert =
|
|
99
|
-
edge === 'bottom' || edge === 'right'
|
|
100
|
-
? dropIndex >= dragIndex
|
|
101
|
-
? dropIndex
|
|
102
|
-
: dropIndex + 1
|
|
103
|
-
: dropIndex <= dragIndex
|
|
104
|
-
? dropIndex
|
|
105
|
-
: dropIndex - 1
|
|
106
|
-
const insertAt = Math.max(0, Math.min(rawInsert, reordered.length))
|
|
107
|
-
|
|
108
|
-
reordered.splice(insertAt, 0, moved)
|
|
109
|
-
options.onReorder(reordered)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function containerRef(el: HTMLElement | null) {
|
|
113
|
-
// Pyreon's runtime calls refs with `null` on unmount; the per-element
|
|
114
|
-
// pdnd cleanups are already registered via `onCleanup` below, so we
|
|
115
|
-
// can no-op the unmount-time call.
|
|
116
|
-
if (!el) return
|
|
117
|
-
// Auto-scroll when dragging near container edges
|
|
118
|
-
cleanups.push(
|
|
119
|
-
autoScrollForElements({
|
|
120
|
-
element: el,
|
|
121
|
-
canScroll: ({ source }) => source.data[SORT_ID] === sortableId,
|
|
122
|
-
}),
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
// Container is a drop target for reorder finalization
|
|
126
|
-
cleanups.push(
|
|
127
|
-
dropTargetForElements({
|
|
128
|
-
element: el,
|
|
129
|
-
getData: () => ({ [SORT_ID]: sortableId }),
|
|
130
|
-
canDrop: ({ source }) => source.data[SORT_ID] === sortableId,
|
|
131
|
-
onDrop: () => {
|
|
132
|
-
performReorder()
|
|
133
|
-
activeId.set(null)
|
|
134
|
-
overId.set(null)
|
|
135
|
-
overEdge.set(null)
|
|
136
|
-
},
|
|
137
|
-
}),
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
// Keyboard reordering: Alt+Arrow keys
|
|
141
|
-
const keyHandler = (e: KeyboardEvent) => {
|
|
142
|
-
if (!e.altKey) return
|
|
143
|
-
|
|
144
|
-
const isUp = axis === 'vertical' ? e.key === 'ArrowUp' : e.key === 'ArrowLeft'
|
|
145
|
-
const isDown = axis === 'vertical' ? e.key === 'ArrowDown' : e.key === 'ArrowRight'
|
|
146
|
-
if (!isUp && !isDown) return
|
|
147
|
-
|
|
148
|
-
const focused = document.activeElement as HTMLElement | null
|
|
149
|
-
if (!focused || !el.contains(focused)) return
|
|
150
|
-
|
|
151
|
-
const focusedKey = focused.dataset.pyreonSortKey
|
|
152
|
-
if (!focusedKey) return
|
|
153
|
-
|
|
154
|
-
e.preventDefault()
|
|
155
|
-
|
|
156
|
-
const currentItems = options.items()
|
|
157
|
-
const currentIndex = currentItems.findIndex((item) => String(options.by(item)) === focusedKey)
|
|
158
|
-
if (currentIndex === -1) return
|
|
159
|
-
|
|
160
|
-
const targetIndex = isUp ? currentIndex - 1 : currentIndex + 1
|
|
161
|
-
if (targetIndex < 0 || targetIndex >= currentItems.length) return
|
|
162
|
-
|
|
163
|
-
const reordered = [...currentItems]
|
|
164
|
-
const temp = reordered[currentIndex]
|
|
165
|
-
reordered[currentIndex] = reordered[targetIndex] as T
|
|
166
|
-
reordered[targetIndex] = temp as T
|
|
167
|
-
options.onReorder(reordered)
|
|
168
|
-
|
|
169
|
-
// Restore focus after DOM update
|
|
170
|
-
requestAnimationFrame(() => {
|
|
171
|
-
const items = el.querySelectorAll('[data-pyreon-sort-key]')
|
|
172
|
-
for (const item of items) {
|
|
173
|
-
if ((item as HTMLElement).dataset.pyreonSortKey === focusedKey) {
|
|
174
|
-
;(item as HTMLElement).focus()
|
|
175
|
-
break
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
el.addEventListener('keydown', keyHandler)
|
|
182
|
-
cleanups.push(() => el.removeEventListener('keydown', keyHandler))
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function itemRef(key: string | number): (el: HTMLElement | null) => void {
|
|
186
|
-
return (el: HTMLElement | null) => {
|
|
187
|
-
// Per-key disposal. The ref fires with the element on mount and
|
|
188
|
-
// with `null` on unmount. The old code pushed every registration
|
|
189
|
-
// onto the shared `cleanups[]` and made the null branch a pure
|
|
190
|
-
// no-op — so for a `<For>`-rendered sortable (the documented usage:
|
|
191
|
-
// todo list / kanban) every removed item's pdnd `draggable` /
|
|
192
|
-
// `dropTargetForElements` registration (and its pointer/drag
|
|
193
|
-
// listeners on the now-detached node) leaked until the WHOLE
|
|
194
|
-
// sortable unmounted. Dispose the prior registration for this key
|
|
195
|
-
// on BOTH unmount (el === null) and re-registration (same key,
|
|
196
|
-
// new element) so the live set tracks live items, not all-ever.
|
|
197
|
-
const prev = itemCleanups.get(key)
|
|
198
|
-
if (prev) {
|
|
199
|
-
prev()
|
|
200
|
-
itemCleanups.delete(key)
|
|
201
|
-
}
|
|
202
|
-
if (!el) return
|
|
203
|
-
el.dataset.pyreonSortKey = String(key)
|
|
204
|
-
if (!el.hasAttribute('tabindex')) el.setAttribute('tabindex', '0')
|
|
205
|
-
el.setAttribute('role', 'listitem')
|
|
206
|
-
el.setAttribute('aria-roledescription', 'sortable item')
|
|
207
|
-
|
|
208
|
-
const allowedEdges: Edge[] = axis === 'vertical' ? ['top', 'bottom'] : ['left', 'right']
|
|
209
|
-
|
|
210
|
-
const cleanup = combine(
|
|
211
|
-
draggable({
|
|
212
|
-
element: el,
|
|
213
|
-
getInitialData: () => ({
|
|
214
|
-
[SORT_KEY]: key,
|
|
215
|
-
[SORT_ID]: sortableId,
|
|
216
|
-
}),
|
|
217
|
-
onDragStart: () => activeId.set(key),
|
|
218
|
-
onDrop: () => {
|
|
219
|
-
queueMicrotask(() => {
|
|
220
|
-
activeId.set(null)
|
|
221
|
-
overId.set(null)
|
|
222
|
-
overEdge.set(null)
|
|
223
|
-
})
|
|
224
|
-
},
|
|
225
|
-
}),
|
|
226
|
-
dropTargetForElements({
|
|
227
|
-
element: el,
|
|
228
|
-
getData: ({ input, element }) =>
|
|
229
|
-
attachClosestEdge(
|
|
230
|
-
{ [SORT_KEY]: key, [SORT_ID]: sortableId },
|
|
231
|
-
{ input, element, allowedEdges },
|
|
232
|
-
),
|
|
233
|
-
canDrop: ({ source }) => source.data[SORT_ID] === sortableId,
|
|
234
|
-
onDragEnter: ({ self }) => {
|
|
235
|
-
overId.set(key)
|
|
236
|
-
overEdge.set(extractClosestEdge(self.data) as DropEdge | null)
|
|
237
|
-
},
|
|
238
|
-
onDrag: ({ self }) => {
|
|
239
|
-
overEdge.set(extractClosestEdge(self.data) as DropEdge | null)
|
|
240
|
-
},
|
|
241
|
-
onDragLeave: () => {
|
|
242
|
-
if (overId.peek() === key) {
|
|
243
|
-
overId.set(null)
|
|
244
|
-
overEdge.set(null)
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
}),
|
|
248
|
-
)
|
|
249
|
-
|
|
250
|
-
itemCleanups.set(key, cleanup)
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
onCleanup(() => {
|
|
255
|
-
for (const cleanup of cleanups) cleanup()
|
|
256
|
-
cleanups.length = 0
|
|
257
|
-
for (const cleanup of itemCleanups.values()) cleanup()
|
|
258
|
-
itemCleanups.clear()
|
|
259
|
-
activeId.set(null)
|
|
260
|
-
overId.set(null)
|
|
261
|
-
overEdge.set(null)
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
return { containerRef, itemRef, activeId, overId, overEdge }
|
|
265
|
-
}
|