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