@cujuju/solid-reorder-list 0.3.0 → 0.4.0
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/README.md +561 -551
- package/dist/index.cjs +126 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +126 -14
- package/dist/index.js.map +1 -1
- package/package.json +60 -60
- package/src/createReorderGrid.ts +824 -778
- package/src/createReorderList.ts +489 -442
- package/src/shared.ts +53 -0
package/dist/index.js
CHANGED
|
@@ -33,6 +33,40 @@ function createCancelListeners(opts) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
+
var DEFAULT_TOUCH_HOLD_MS = 200;
|
|
37
|
+
var DEFAULT_TOUCH_HOLD_TOLERANCE = 10;
|
|
38
|
+
var DEFAULT_ARMED_CLASS = "reorder-armed";
|
|
39
|
+
function startPressHold(e, opts) {
|
|
40
|
+
const touch = e.pointerType === "touch" || e.pointerType === "pen";
|
|
41
|
+
if (opts.ms <= 0 || !touch) {
|
|
42
|
+
return { held: true, move: () => true, stop: () => {
|
|
43
|
+
} };
|
|
44
|
+
}
|
|
45
|
+
const startX = e.clientX;
|
|
46
|
+
const startY = e.clientY;
|
|
47
|
+
let held = false;
|
|
48
|
+
const swallowMenu = (ev) => {
|
|
49
|
+
ev.preventDefault();
|
|
50
|
+
ev.stopPropagation();
|
|
51
|
+
};
|
|
52
|
+
const timer = setTimeout(() => {
|
|
53
|
+
held = true;
|
|
54
|
+
opts.onHeld();
|
|
55
|
+
}, opts.ms);
|
|
56
|
+
document.addEventListener("contextmenu", swallowMenu, { capture: true });
|
|
57
|
+
return {
|
|
58
|
+
get held() {
|
|
59
|
+
return held;
|
|
60
|
+
},
|
|
61
|
+
move(ev) {
|
|
62
|
+
return held || Math.hypot(ev.clientX - startX, ev.clientY - startY) <= opts.tolerance;
|
|
63
|
+
},
|
|
64
|
+
stop() {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
document.removeEventListener("contextmenu", swallowMenu, { capture: true });
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
36
70
|
|
|
37
71
|
// src/createReorderList.ts
|
|
38
72
|
function createReorderList(options) {
|
|
@@ -43,13 +77,17 @@ function createReorderList(options) {
|
|
|
43
77
|
const DRAG_SCALE = options.dragScale ?? 0.97;
|
|
44
78
|
const getStopPropagation = () => options.stopPropagation ?? true;
|
|
45
79
|
const getSkipSelector = () => options.skipSelector ?? DEFAULT_SKIP_SELECTOR;
|
|
80
|
+
const getTouchHoldMs = () => options.touchHoldMs ?? DEFAULT_TOUCH_HOLD_MS;
|
|
81
|
+
const getTouchHoldTolerance = () => options.touchHoldTolerance ?? DEFAULT_TOUCH_HOLD_TOLERANCE;
|
|
82
|
+
const getArmedClass = () => options.armedClass ?? DEFAULT_ARMED_CLASS;
|
|
46
83
|
const nodes = /* @__PURE__ */ new Map();
|
|
47
84
|
let disposeRoot;
|
|
48
|
-
const { activeId, setActiveId, targetIdx, setTargetIdx } = createRoot((dispose) => {
|
|
85
|
+
const { activeId, setActiveId, targetIdx, setTargetIdx, armedId, setArmedId } = createRoot((dispose) => {
|
|
49
86
|
disposeRoot = dispose;
|
|
50
87
|
const [activeId2, setActiveId2] = createSignal(null);
|
|
51
88
|
const [targetIdx2, setTargetIdx2] = createSignal(-1);
|
|
52
|
-
|
|
89
|
+
const [armedId2, setArmedId2] = createSignal(null);
|
|
90
|
+
return { activeId: activeId2, setActiveId: setActiveId2, targetIdx: targetIdx2, setTargetIdx: setTargetIdx2, armedId: armedId2, setArmedId: setArmedId2 };
|
|
53
91
|
});
|
|
54
92
|
let pointerCleanup = null;
|
|
55
93
|
let drag = null;
|
|
@@ -232,14 +270,34 @@ function createReorderList(options) {
|
|
|
232
270
|
const ids = options.ids();
|
|
233
271
|
const sourceIndex = ids.indexOf(id);
|
|
234
272
|
if (sourceIndex === -1) return;
|
|
273
|
+
if (pointerCleanup) {
|
|
274
|
+
pointerCleanup();
|
|
275
|
+
pointerCleanup = null;
|
|
276
|
+
}
|
|
235
277
|
const start = ptr(e);
|
|
236
278
|
const threshold = getActivateDistance();
|
|
237
279
|
let activated = false;
|
|
280
|
+
const hold = startPressHold(e, {
|
|
281
|
+
ms: getTouchHoldMs(),
|
|
282
|
+
tolerance: getTouchHoldTolerance(),
|
|
283
|
+
onHeld: () => setArmedId(id)
|
|
284
|
+
});
|
|
285
|
+
const release = () => {
|
|
286
|
+
if (pointerCleanup) {
|
|
287
|
+
pointerCleanup();
|
|
288
|
+
pointerCleanup = null;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
238
291
|
const onMove = (ev) => {
|
|
239
292
|
if (!activated) {
|
|
293
|
+
if (!hold.held) {
|
|
294
|
+
if (!hold.move(ev)) release();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
240
297
|
const distance = Math.abs(ptr(ev) - start);
|
|
241
298
|
if (distance >= threshold) {
|
|
242
299
|
activated = true;
|
|
300
|
+
setArmedId(null);
|
|
243
301
|
activate(id, sourceIndex, ids, ptr(ev));
|
|
244
302
|
}
|
|
245
303
|
return;
|
|
@@ -248,22 +306,31 @@ function createReorderList(options) {
|
|
|
248
306
|
};
|
|
249
307
|
const onUp = (upEvent) => {
|
|
250
308
|
if (upEvent.button !== 0) return;
|
|
251
|
-
|
|
252
|
-
pointerCleanup();
|
|
253
|
-
pointerCleanup = null;
|
|
254
|
-
}
|
|
309
|
+
release();
|
|
255
310
|
if (activated) commitDrag();
|
|
256
311
|
};
|
|
312
|
+
const onPointerCancel = () => {
|
|
313
|
+
release();
|
|
314
|
+
if (activated) cancelDrag();
|
|
315
|
+
};
|
|
257
316
|
document.addEventListener("pointermove", onMove);
|
|
258
317
|
document.addEventListener("pointerup", onUp);
|
|
318
|
+
document.addEventListener("pointercancel", onPointerCancel);
|
|
259
319
|
pointerCleanup = () => {
|
|
320
|
+
hold.stop();
|
|
321
|
+
setArmedId(null);
|
|
260
322
|
document.removeEventListener("pointermove", onMove);
|
|
261
323
|
document.removeEventListener("pointerup", onUp);
|
|
324
|
+
document.removeEventListener("pointercancel", onPointerCancel);
|
|
262
325
|
};
|
|
263
326
|
}
|
|
264
327
|
if (getOwner()) {
|
|
265
328
|
onCleanup(() => {
|
|
266
329
|
if (drag) cancelDrag();
|
|
330
|
+
if (pointerCleanup) {
|
|
331
|
+
pointerCleanup();
|
|
332
|
+
pointerCleanup = null;
|
|
333
|
+
}
|
|
267
334
|
nodes.clear();
|
|
268
335
|
disposeRoot?.();
|
|
269
336
|
});
|
|
@@ -275,7 +342,7 @@ function createReorderList(options) {
|
|
|
275
342
|
nodes.set(id, el);
|
|
276
343
|
},
|
|
277
344
|
onPointerDown: (e) => onPointerDown(id, e),
|
|
278
|
-
classList: { [getActiveClass()]: activeId() === id },
|
|
345
|
+
classList: { [getActiveClass()]: activeId() === id, [getArmedClass()]: armedId() === id },
|
|
279
346
|
"data-reorder-id": id
|
|
280
347
|
}),
|
|
281
348
|
/** Whether the given item is currently being dragged */
|
|
@@ -286,10 +353,16 @@ function createReorderList(options) {
|
|
|
286
353
|
activeId,
|
|
287
354
|
/** The current drop target index, or -1 if not dragging */
|
|
288
355
|
targetIndex: targetIdx,
|
|
356
|
+
/** The ID of the item whose touch hold has completed, or null */
|
|
357
|
+
armedId,
|
|
289
358
|
/** Manually dispose the isolated reactive root. Call this if you created
|
|
290
359
|
* the primitive outside a SolidJS owner scope and need deterministic cleanup. */
|
|
291
360
|
dispose: () => {
|
|
292
361
|
if (drag) cancelDrag();
|
|
362
|
+
if (pointerCleanup) {
|
|
363
|
+
pointerCleanup();
|
|
364
|
+
pointerCleanup = null;
|
|
365
|
+
}
|
|
293
366
|
nodes.clear();
|
|
294
367
|
disposeRoot?.();
|
|
295
368
|
}
|
|
@@ -312,13 +385,17 @@ function createReorderGrid(options) {
|
|
|
312
385
|
const getStopPropagation = () => options.stopPropagation ?? true;
|
|
313
386
|
const getSkipSelector = () => options.skipSelector ?? DEFAULT_SKIP_SELECTOR;
|
|
314
387
|
const getScrollContainer = () => options.scrollContainer?.() ?? null;
|
|
388
|
+
const getTouchHoldMs = () => options.touchHoldMs ?? DEFAULT_TOUCH_HOLD_MS;
|
|
389
|
+
const getTouchHoldTolerance = () => options.touchHoldTolerance ?? DEFAULT_TOUCH_HOLD_TOLERANCE;
|
|
390
|
+
const getArmedClass = () => options.armedClass ?? DEFAULT_ARMED_CLASS;
|
|
315
391
|
const nodes = /* @__PURE__ */ new Map();
|
|
316
392
|
let disposeRoot;
|
|
317
|
-
const { activeId, setActiveId, targetIdx, setTargetIdx } = createRoot((dispose) => {
|
|
393
|
+
const { activeId, setActiveId, targetIdx, setTargetIdx, armedId, setArmedId } = createRoot((dispose) => {
|
|
318
394
|
disposeRoot = dispose;
|
|
319
395
|
const [activeId2, setActiveId2] = createSignal(null);
|
|
320
396
|
const [targetIdx2, setTargetIdx2] = createSignal(-1);
|
|
321
|
-
|
|
397
|
+
const [armedId2, setArmedId2] = createSignal(null);
|
|
398
|
+
return { activeId: activeId2, setActiveId: setActiveId2, targetIdx: targetIdx2, setTargetIdx: setTargetIdx2, armedId: armedId2, setArmedId: setArmedId2 };
|
|
322
399
|
});
|
|
323
400
|
let pointerCleanup = null;
|
|
324
401
|
let drag = null;
|
|
@@ -603,16 +680,36 @@ function createReorderGrid(options) {
|
|
|
603
680
|
const ids = options.ids();
|
|
604
681
|
const sourceIndex = ids.indexOf(id);
|
|
605
682
|
if (sourceIndex === -1) return;
|
|
683
|
+
if (pointerCleanup) {
|
|
684
|
+
pointerCleanup();
|
|
685
|
+
pointerCleanup = null;
|
|
686
|
+
}
|
|
606
687
|
const startX = e.clientX;
|
|
607
688
|
const startY = e.clientY;
|
|
608
689
|
const threshold = getActivateDistance();
|
|
609
690
|
let activated = false;
|
|
691
|
+
const hold = startPressHold(e, {
|
|
692
|
+
ms: getTouchHoldMs(),
|
|
693
|
+
tolerance: getTouchHoldTolerance(),
|
|
694
|
+
onHeld: () => setArmedId(id)
|
|
695
|
+
});
|
|
696
|
+
const release = () => {
|
|
697
|
+
if (pointerCleanup) {
|
|
698
|
+
pointerCleanup();
|
|
699
|
+
pointerCleanup = null;
|
|
700
|
+
}
|
|
701
|
+
};
|
|
610
702
|
const onMove = (ev) => {
|
|
611
703
|
if (!activated) {
|
|
704
|
+
if (!hold.held) {
|
|
705
|
+
if (!hold.move(ev)) release();
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
612
708
|
const dx = ev.clientX - startX;
|
|
613
709
|
const dy = ev.clientY - startY;
|
|
614
710
|
if (Math.hypot(dx, dy) >= threshold) {
|
|
615
711
|
activated = true;
|
|
712
|
+
setArmedId(null);
|
|
616
713
|
const scroll = getScroll(getScrollContainer());
|
|
617
714
|
activate(
|
|
618
715
|
id,
|
|
@@ -628,22 +725,31 @@ function createReorderGrid(options) {
|
|
|
628
725
|
};
|
|
629
726
|
const onUp = (upEvent) => {
|
|
630
727
|
if (upEvent.button !== 0) return;
|
|
631
|
-
|
|
632
|
-
pointerCleanup();
|
|
633
|
-
pointerCleanup = null;
|
|
634
|
-
}
|
|
728
|
+
release();
|
|
635
729
|
if (activated) commitDrag();
|
|
636
730
|
};
|
|
731
|
+
const onPointerCancel = () => {
|
|
732
|
+
release();
|
|
733
|
+
if (activated) cancelDrag();
|
|
734
|
+
};
|
|
637
735
|
document.addEventListener("pointermove", onMove);
|
|
638
736
|
document.addEventListener("pointerup", onUp);
|
|
737
|
+
document.addEventListener("pointercancel", onPointerCancel);
|
|
639
738
|
pointerCleanup = () => {
|
|
739
|
+
hold.stop();
|
|
740
|
+
setArmedId(null);
|
|
640
741
|
document.removeEventListener("pointermove", onMove);
|
|
641
742
|
document.removeEventListener("pointerup", onUp);
|
|
743
|
+
document.removeEventListener("pointercancel", onPointerCancel);
|
|
642
744
|
};
|
|
643
745
|
}
|
|
644
746
|
if (getOwner()) {
|
|
645
747
|
onCleanup(() => {
|
|
646
748
|
if (drag) cancelDrag();
|
|
749
|
+
if (pointerCleanup) {
|
|
750
|
+
pointerCleanup();
|
|
751
|
+
pointerCleanup = null;
|
|
752
|
+
}
|
|
647
753
|
nodes.clear();
|
|
648
754
|
disposeRoot?.();
|
|
649
755
|
});
|
|
@@ -655,7 +761,7 @@ function createReorderGrid(options) {
|
|
|
655
761
|
nodes.set(id, el);
|
|
656
762
|
},
|
|
657
763
|
onPointerDown: (e) => onPointerDown(id, e),
|
|
658
|
-
classList: { [getActiveClass()]: activeId() === id },
|
|
764
|
+
classList: { [getActiveClass()]: activeId() === id, [getArmedClass()]: armedId() === id },
|
|
659
765
|
"data-reorder-id": id
|
|
660
766
|
}),
|
|
661
767
|
/** Whether the given item is currently being dragged. */
|
|
@@ -666,6 +772,8 @@ function createReorderGrid(options) {
|
|
|
666
772
|
activeId,
|
|
667
773
|
/** The current drop target index, or -1 if not dragging. */
|
|
668
774
|
targetIndex: targetIdx,
|
|
775
|
+
/** The ID of the item whose touch hold has completed, or null. */
|
|
776
|
+
armedId,
|
|
669
777
|
/** Abort an in-progress drag without firing onReorder.
|
|
670
778
|
* Idempotent no-op when no drag is active. (R13a)
|
|
671
779
|
*
|
|
@@ -680,6 +788,10 @@ function createReorderGrid(options) {
|
|
|
680
788
|
* deterministic cleanup. */
|
|
681
789
|
dispose: () => {
|
|
682
790
|
if (drag) cancelDrag();
|
|
791
|
+
if (pointerCleanup) {
|
|
792
|
+
pointerCleanup();
|
|
793
|
+
pointerCleanup = null;
|
|
794
|
+
}
|
|
683
795
|
nodes.clear();
|
|
684
796
|
disposeRoot?.();
|
|
685
797
|
}
|