@reause/integrations 0.1.2

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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.d.ts +13 -0
  3. package/dist/index.iife.js +1621 -0
  4. package/dist/index.iife.min.js +1 -0
  5. package/dist/index.js +13 -0
  6. package/dist/useAsyncValidator.d.ts +83 -0
  7. package/dist/useAsyncValidator.iife.js +192 -0
  8. package/dist/useAsyncValidator.iife.min.js +1 -0
  9. package/dist/useAsyncValidator.js +168 -0
  10. package/dist/useAxios.d.ts +125 -0
  11. package/dist/useAxios.iife.js +288 -0
  12. package/dist/useAxios.iife.min.js +1 -0
  13. package/dist/useAxios.js +265 -0
  14. package/dist/useChangeCase.d.ts +57 -0
  15. package/dist/useChangeCase.iife.js +91 -0
  16. package/dist/useChangeCase.iife.min.js +1 -0
  17. package/dist/useChangeCase.js +68 -0
  18. package/dist/useCookies.d.ts +117 -0
  19. package/dist/useCookies.iife.js +142 -0
  20. package/dist/useCookies.iife.min.js +1 -0
  21. package/dist/useCookies.js +117 -0
  22. package/dist/useDrauu.d.ts +152 -0
  23. package/dist/useDrauu.iife.js +221 -0
  24. package/dist/useDrauu.iife.min.js +1 -0
  25. package/dist/useDrauu.js +221 -0
  26. package/dist/useFocusTrap.d.ts +116 -0
  27. package/dist/useFocusTrap.iife.js +125 -0
  28. package/dist/useFocusTrap.iife.min.js +1 -0
  29. package/dist/useFocusTrap.js +125 -0
  30. package/dist/useFuse.d.ts +86 -0
  31. package/dist/useFuse.iife.js +95 -0
  32. package/dist/useFuse.iife.min.js +1 -0
  33. package/dist/useFuse.js +72 -0
  34. package/dist/useIDBKeyval.d.ts +139 -0
  35. package/dist/useIDBKeyval.iife.js +175 -0
  36. package/dist/useIDBKeyval.iife.min.js +1 -0
  37. package/dist/useIDBKeyval.js +174 -0
  38. package/dist/useJwt.d.ts +52 -0
  39. package/dist/useJwt.iife.js +54 -0
  40. package/dist/useJwt.iife.min.js +1 -0
  41. package/dist/useJwt.js +53 -0
  42. package/dist/useNProgress.d.ts +115 -0
  43. package/dist/useNProgress.iife.js +148 -0
  44. package/dist/useNProgress.iife.min.js +1 -0
  45. package/dist/useNProgress.js +125 -0
  46. package/dist/useQRCode.d.ts +38 -0
  47. package/dist/useQRCode.iife.js +78 -0
  48. package/dist/useQRCode.iife.min.js +1 -0
  49. package/dist/useQRCode.js +55 -0
  50. package/dist/useSortable.d.ts +143 -0
  51. package/dist/useSortable.iife.js +199 -0
  52. package/dist/useSortable.iife.min.js +1 -0
  53. package/dist/useSortable.js +173 -0
  54. package/package.json +92 -0
@@ -0,0 +1,143 @@
1
+ import SortableJs from "sortablejs";
2
+ //#region useSortable/index.d.ts
3
+ /** Accepted DOM target kinds — mirrors upstream's `MaybeElement`. */
4
+ type MaybeElement = HTMLElement | SVGElement | null | undefined;
5
+ /** A plain element or a React ref-like object (`{ current }`) — upstream `MaybeElementRef`. */
6
+ type MaybeElementRef = MaybeElement | {
7
+ readonly current: MaybeElement;
8
+ };
9
+ /** Target of the sortable container (upstream `MaybeRefOrGetter<MaybeElement>`). */
10
+ type SortableTarget = MaybeElementRef;
11
+ /**
12
+ * React return type of `useSortable` — the method bag of upstream
13
+ * `UseSortableReturn` (issue §2B: an object, not a tuple).
14
+ */
15
+ export interface UseSortableReturn {
16
+ /**
17
+ * Create the sortablejs instance for the current target.
18
+ *
19
+ * No-op when an instance already exists (upstream `initSortable`).
20
+ */
21
+ start: () => void;
22
+ /**
23
+ * Destroy the sortablejs instance.
24
+ */
25
+ stop: () => void;
26
+ /**
27
+ * Options getter/setter, delegating to the instance's `option()`.
28
+ *
29
+ * @param name a `Sortable.Options` property.
30
+ * @param value a value; omit it to read the current value.
31
+ */
32
+ option: (<K extends keyof SortableJs.Options>(name: K, value: SortableJs.Options[K]) => void) & (<K extends keyof SortableJs.Options>(name: K) => SortableJs.Options[K]);
33
+ }
34
+ /**
35
+ * Options of `useSortable` — upstream `UseSortableOptions`, with `onUpdate`
36
+ * re-typed for React.
37
+ *
38
+ * Upstream's `onUpdate` is the internal handler that mutates the caller's
39
+ * array; here the hook never mutates `list`, so `onUpdate` receives the
40
+ * reordered array instead (see `moveArrayElement`).
41
+ */
42
+ export interface UseSortableOptions<T = unknown> extends Omit<SortableJs.Options, 'onUpdate'> {
43
+ /**
44
+ * Watch the resolved element and automatically reinitialize Sortable when it
45
+ * changes.
46
+ *
47
+ * When `false` (default), Sortable is only initialized once on mount. You
48
+ * must manually call `start()` if the element changes.
49
+ *
50
+ * When `true`, the instance is destroyed and re-created whenever the
51
+ * resolved element identity changes (e.g. conditional rendering).
52
+ *
53
+ * @default false
54
+ */
55
+ watchElement?: boolean;
56
+ /**
57
+ * Document used to resolve a selector-string target.
58
+ *
59
+ * @default document
60
+ */
61
+ document?: Document;
62
+ /**
63
+ * Called after a drag reorder with the NEW array and the sortablejs event.
64
+ *
65
+ * React state is immutable, so the hook hands the reordered array to the
66
+ * caller instead of mutating `list` in place. When no `onUpdate` is passed,
67
+ * sortablejs still moves the DOM nodes while the array stays as-is — the
68
+ * component then re-renders from stale data and desyncs from the DOM. Always
69
+ * store the new array, e.g.
70
+ * `onUpdate: newList => setList(newList)`.
71
+ */
72
+ onUpdate?: (newList: T[], event: SortableJs.SortableEvent | null) => void;
73
+ }
74
+ /**
75
+ * React port of VueUse's `useSortable` — wrapper for
76
+ * [`sortablejs`](https://github.com/SortableJS/Sortable).
77
+ *
78
+ * Map from @vueuse/integrations `useSortable`
79
+ * (`source/vueuse/packages/integrations/useSortable/`), a reactive wrapper
80
+ * around the `sortablejs` package.
81
+ *
82
+ * Adjustment for React: the list is **immutable**. Upstream's
83
+ * `moveArrayElement` mutates the caller's array in place (deferring the splice
84
+ * through `nextTick` when the list is a ref), which cannot work in React —
85
+ * an in-place mutation does not re-render. The reause
86
+ * `moveArrayElement(list, from, to, e)` is pure: it returns a NEW reordered
87
+ * array (and still performs upstream's DOM fixup when an event is given), and
88
+ * the hook forwards that array to `options.onUpdate`.
89
+ *
90
+ * Adjustment for React: sortablejs manipulates DOM nodes directly, so the
91
+ * component must be a controlled list that re-renders from `onUpdate` — stable
92
+ * `key`s recommended — otherwise React and the DOM desync. Upstream's
93
+ * `onMounted` maps to a mount effect, `onScopeDispose` to the effect cleanup,
94
+ * and `watchElement` to an effect keyed on the resolved element identity.
95
+ *
96
+ * @param el target element, React ref object (`{ current }`), or CSS selector
97
+ * @param list current list items, in render order (never mutated)
98
+ * @param options sortablejs options plus `watchElement` / `document` / `onUpdate`
99
+ *
100
+ * @__NO_SIDE_EFFECTS__
101
+ * @see https://github.com/SortableJS/Sortable#options
102
+ */
103
+ export declare function useSortable<T>(el: SortableTarget | string, list: T[], options?: UseSortableOptions<T>): UseSortableReturn;
104
+ /**
105
+ * Inserts an element into the DOM at a given index.
106
+ * @param parentElement
107
+ * @param element
108
+ * @param {number} index
109
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2
110
+ */
111
+ export declare function insertNodeAt(parentElement: Element, element: Element, index: number): void;
112
+ /**
113
+ * Removes a node from the DOM.
114
+ * @param {Node} node
115
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2
116
+ */
117
+ export declare function removeNode(node: Node): void;
118
+ /**
119
+ * Move an element of `list` from `from` to `to`, returning a NEW array.
120
+ *
121
+ * Map from @vueuse/integrations `useSortable`'s `moveArrayElement`, with one
122
+ * deliberate deviation: upstream mutates the caller's array in place (and
123
+ * defers the splice with `nextTick` when the list is a ref), which cannot work
124
+ * in React — an in-place mutation does not re-render. This implementation is
125
+ * pure: the input array is never mutated and the moved copy is returned. The
126
+ * move only happens when `to` is in range (`to >= 0 && to < list.length`,
127
+ * exactly upstream's bounds check); otherwise a copy of `list` is returned
128
+ * unchanged.
129
+ *
130
+ * The DOM fixup of upstream is kept for compatibility: when `e` is given (a
131
+ * sortablejs event), `e.item` is removed from the DOM and re-inserted at
132
+ * `from` inside `e.from`. That fixup runs regardless of the bounds check, as
133
+ * upstream does — note the upstream quirk of inserting at `from` rather than
134
+ * `to`. Pass `null` (or omit `e`) to only compute the array.
135
+ *
136
+ * @param list the current list — never mutated
137
+ * @param from source index
138
+ * @param to destination index
139
+ * @param e the sortablejs event to apply the DOM fixup for, if any
140
+ * @returns a new array with the element moved
141
+ */
142
+ export declare function moveArrayElement<T>(list: T[], from: number, to: number, e?: SortableJs.SortableEvent | null): T[];
143
+ //#endregion
@@ -0,0 +1,199 @@
1
+ (function(exports, _reause_shared, react, sortablejs) {
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ //#region \0rolldown/runtime.js
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
21
+ value: mod,
22
+ enumerable: true
23
+ }) : target, mod));
24
+ //#endregion
25
+ sortablejs = __toESM(sortablejs, 1);
26
+ //#region useSortable/index.tsx
27
+ /**
28
+ * Resolve a target to a DOM element, or `null` when it cannot be resolved.
29
+ * Upstream resolves elements with `unrefElement` (`@vueuse/core`); the React
30
+ * port composes the same unwrapping from `toValue` / `isRefLike`
31
+ * (`@reause/shared`) — one pass unwraps a ref-like object, a second one
32
+ * covers a ref-like object holding another ref-like
33
+ * (`{ current: { current: element } }`).
34
+ */
35
+ function resolveElement(value) {
36
+ let el = (0, _reause_shared.toValue)(value);
37
+ if ((0, _reause_shared.isRefLike)(el)) el = (0, _reause_shared.toValue)(el);
38
+ if (typeof el === "object" && el !== null && (el instanceof HTMLElement || el instanceof SVGElement)) return el;
39
+ return null;
40
+ }
41
+ /**
42
+ * React port of VueUse's `useSortable` — wrapper for
43
+ * [`sortablejs`](https://github.com/SortableJS/Sortable).
44
+ *
45
+ * Map from @vueuse/integrations `useSortable`
46
+ * (`source/vueuse/packages/integrations/useSortable/`), a reactive wrapper
47
+ * around the `sortablejs` package.
48
+ *
49
+ * Adjustment for React: the list is **immutable**. Upstream's
50
+ * `moveArrayElement` mutates the caller's array in place (deferring the splice
51
+ * through `nextTick` when the list is a ref), which cannot work in React —
52
+ * an in-place mutation does not re-render. The reause
53
+ * `moveArrayElement(list, from, to, e)` is pure: it returns a NEW reordered
54
+ * array (and still performs upstream's DOM fixup when an event is given), and
55
+ * the hook forwards that array to `options.onUpdate`.
56
+ *
57
+ * Adjustment for React: sortablejs manipulates DOM nodes directly, so the
58
+ * component must be a controlled list that re-renders from `onUpdate` — stable
59
+ * `key`s recommended — otherwise React and the DOM desync. Upstream's
60
+ * `onMounted` maps to a mount effect, `onScopeDispose` to the effect cleanup,
61
+ * and `watchElement` to an effect keyed on the resolved element identity.
62
+ *
63
+ * @param el target element, React ref object (`{ current }`), or CSS selector
64
+ * @param list current list items, in render order (never mutated)
65
+ * @param options sortablejs options plus `watchElement` / `document` / `onUpdate`
66
+ *
67
+ * @__NO_SIDE_EFFECTS__
68
+ * @see https://github.com/SortableJS/Sortable#options
69
+ */
70
+ function useSortable(el, list, options = {}) {
71
+ const { document = globalThis.document, watchElement = false, onUpdate, ...resetOptions } = options;
72
+ const instanceRef = (0, react.useRef)(void 0);
73
+ const listRef = (0, react.useRef)(list);
74
+ const onUpdateRef = (0, react.useRef)(onUpdate);
75
+ const optionsRef = (0, react.useRef)(resetOptions);
76
+ listRef.current = list;
77
+ onUpdateRef.current = onUpdate;
78
+ optionsRef.current = resetOptions;
79
+ const cleanup = (0, react.useCallback)(() => {
80
+ var _instanceRef$current;
81
+ (_instanceRef$current = instanceRef.current) === null || _instanceRef$current === void 0 || _instanceRef$current.destroy();
82
+ instanceRef.current = void 0;
83
+ }, []);
84
+ const initSortable = (0, react.useCallback)((target) => {
85
+ if (!target || instanceRef.current !== void 0) return;
86
+ instanceRef.current = new sortablejs.default(target, {
87
+ onUpdate: (e) => {
88
+ var _onUpdateRef$current;
89
+ const newList = moveArrayElement(listRef.current, e.oldIndex, e.newIndex, e);
90
+ (_onUpdateRef$current = onUpdateRef.current) === null || _onUpdateRef$current === void 0 || _onUpdateRef$current.call(onUpdateRef, newList, e);
91
+ },
92
+ ...optionsRef.current
93
+ });
94
+ }, []);
95
+ const start = (0, react.useCallback)(() => {
96
+ const target = typeof el === "string" ? document === null || document === void 0 ? void 0 : document.querySelector(el) : resolveElement(el);
97
+ if (target) initSortable(target);
98
+ }, [
99
+ document,
100
+ el,
101
+ initSortable
102
+ ]);
103
+ const stop = (0, react.useCallback)(() => {
104
+ cleanup();
105
+ }, [cleanup]);
106
+ const option = (0, react.useCallback)((name, value) => {
107
+ var _instanceRef$current2, _instanceRef$current3;
108
+ if (value !== void 0) (_instanceRef$current2 = instanceRef.current) === null || _instanceRef$current2 === void 0 || _instanceRef$current2.option(name, value);
109
+ else return (_instanceRef$current3 = instanceRef.current) === null || _instanceRef$current3 === void 0 ? void 0 : _instanceRef$current3.option(name);
110
+ }, []);
111
+ const resolved = typeof el === "string" ? el : resolveElement(el);
112
+ (0, react.useEffect)(() => {
113
+ if (!watchElement || typeof resolved === "string") return;
114
+ cleanup();
115
+ if (resolved) initSortable(resolved);
116
+ return cleanup;
117
+ }, [
118
+ cleanup,
119
+ initSortable,
120
+ resolved,
121
+ watchElement
122
+ ]);
123
+ (0, react.useEffect)(() => {
124
+ if (watchElement && typeof el !== "string") return;
125
+ start();
126
+ return cleanup;
127
+ }, [
128
+ cleanup,
129
+ start,
130
+ watchElement,
131
+ el
132
+ ]);
133
+ return {
134
+ start,
135
+ stop,
136
+ option
137
+ };
138
+ }
139
+ /**
140
+ * Inserts an element into the DOM at a given index.
141
+ * @param parentElement
142
+ * @param element
143
+ * @param {number} index
144
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2
145
+ */
146
+ function insertNodeAt(parentElement, element, index) {
147
+ const refElement = parentElement.children[index];
148
+ parentElement.insertBefore(element, refElement);
149
+ }
150
+ /**
151
+ * Removes a node from the DOM.
152
+ * @param {Node} node
153
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2
154
+ */
155
+ function removeNode(node) {
156
+ if (node.parentNode) node.parentNode.removeChild(node);
157
+ }
158
+ /**
159
+ * Move an element of `list` from `from` to `to`, returning a NEW array.
160
+ *
161
+ * Map from @vueuse/integrations `useSortable`'s `moveArrayElement`, with one
162
+ * deliberate deviation: upstream mutates the caller's array in place (and
163
+ * defers the splice with `nextTick` when the list is a ref), which cannot work
164
+ * in React — an in-place mutation does not re-render. This implementation is
165
+ * pure: the input array is never mutated and the moved copy is returned. The
166
+ * move only happens when `to` is in range (`to >= 0 && to < list.length`,
167
+ * exactly upstream's bounds check); otherwise a copy of `list` is returned
168
+ * unchanged.
169
+ *
170
+ * The DOM fixup of upstream is kept for compatibility: when `e` is given (a
171
+ * sortablejs event), `e.item` is removed from the DOM and re-inserted at
172
+ * `from` inside `e.from`. That fixup runs regardless of the bounds check, as
173
+ * upstream does — note the upstream quirk of inserting at `from` rather than
174
+ * `to`. Pass `null` (or omit `e`) to only compute the array.
175
+ *
176
+ * @param list the current list — never mutated
177
+ * @param from source index
178
+ * @param to destination index
179
+ * @param e the sortablejs event to apply the DOM fixup for, if any
180
+ * @returns a new array with the element moved
181
+ */
182
+ function moveArrayElement(list, from, to, e) {
183
+ if (e != null) {
184
+ removeNode(e.item);
185
+ insertNodeAt(e.from, e.item, from);
186
+ }
187
+ const array = [...list];
188
+ if (to >= 0 && to < array.length) {
189
+ const element = array.splice(from, 1)[0];
190
+ array.splice(to, 0, element);
191
+ }
192
+ return array;
193
+ }
194
+ //#endregion
195
+ exports.insertNodeAt = insertNodeAt;
196
+ exports.moveArrayElement = moveArrayElement;
197
+ exports.removeNode = removeNode;
198
+ exports.useSortable = useSortable;
199
+ })(this.reause = this.reause || {}, reause, React, Sortable);
@@ -0,0 +1 @@
1
+ (function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=Object.create,a=Object.defineProperty,o=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,c=Object.getPrototypeOf,l=Object.prototype.hasOwnProperty,u=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var i=s(t),c=0,u=i.length,d;c<u;c++)d=i[c],!l.call(e,d)&&d!==n&&a(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=o(t,d))||r.enumerable});return e};r=((e,t,n)=>(n=e==null?{}:i(c(e)),u(t||!e||!e.__esModule||!l.call(e,`default`)?a(n,`default`,{value:e,enumerable:!0}):n,e)))(r,1);function d(e){let n=(0,t.toValue)(e);return(0,t.isRefLike)(n)&&(n=(0,t.toValue)(n)),typeof n==`object`&&n&&(n instanceof HTMLElement||n instanceof SVGElement)?n:null}function f(e,t,i={}){let{document:a=globalThis.document,watchElement:o=!1,onUpdate:s,...c}=i,l=(0,n.useRef)(void 0),u=(0,n.useRef)(t),f=(0,n.useRef)(s),p=(0,n.useRef)(c);u.current=t,f.current=s,p.current=c;let m=(0,n.useCallback)(()=>{var e;(e=l.current)==null||e.destroy(),l.current=void 0},[]),g=(0,n.useCallback)(e=>{e&&l.current===void 0&&(l.current=new r.default(e,{onUpdate:e=>{var t;let n=h(u.current,e.oldIndex,e.newIndex,e);(t=f.current)==null||t.call(f,n,e)},...p.current}))},[]),_=(0,n.useCallback)(()=>{let t=typeof e==`string`?a==null?void 0:a.querySelector(e):d(e);t&&g(t)},[a,e,g]),v=(0,n.useCallback)(()=>{m()},[m]),y=(0,n.useCallback)((e,t)=>{var n,r;if(t!==void 0)(n=l.current)==null||n.option(e,t);else return(r=l.current)==null?void 0:r.option(e)},[]),b=typeof e==`string`?e:d(e);return(0,n.useEffect)(()=>{if(o&&typeof b!=`string`)return m(),b&&g(b),m},[m,g,b,o]),(0,n.useEffect)(()=>{if(!(o&&typeof e!=`string`))return _(),m},[m,_,o,e]),{start:_,stop:v,option:y}}function p(e,t,n){let r=e.children[n];e.insertBefore(t,r)}function m(e){e.parentNode&&e.parentNode.removeChild(e)}function h(e,t,n,r){r!=null&&(m(r.item),p(r.from,r.item,t));let i=[...e];if(n>=0&&n<i.length){let e=i.splice(t,1)[0];i.splice(n,0,e)}return i}e.insertNodeAt=p,e.moveArrayElement=h,e.removeNode=m,e.useSortable=f})(this.reause=this.reause||{},reause,React,Sortable);
@@ -0,0 +1,173 @@
1
+ import { useCallback, useEffect, useRef } from "react";
2
+ import { isRefLike, toValue } from "@reause/shared";
3
+ import SortableJs from "sortablejs";
4
+ //#region useSortable/index.tsx
5
+ /**
6
+ * Resolve a target to a DOM element, or `null` when it cannot be resolved.
7
+ * Upstream resolves elements with `unrefElement` (`@vueuse/core`); the React
8
+ * port composes the same unwrapping from `toValue` / `isRefLike`
9
+ * (`@reause/shared`) — one pass unwraps a ref-like object, a second one
10
+ * covers a ref-like object holding another ref-like
11
+ * (`{ current: { current: element } }`).
12
+ */
13
+ function resolveElement(value) {
14
+ let el = toValue(value);
15
+ if (isRefLike(el)) el = toValue(el);
16
+ if (typeof el === "object" && el !== null && (el instanceof HTMLElement || el instanceof SVGElement)) return el;
17
+ return null;
18
+ }
19
+ /**
20
+ * React port of VueUse's `useSortable` — wrapper for
21
+ * [`sortablejs`](https://github.com/SortableJS/Sortable).
22
+ *
23
+ * Map from @vueuse/integrations `useSortable`
24
+ * (`source/vueuse/packages/integrations/useSortable/`), a reactive wrapper
25
+ * around the `sortablejs` package.
26
+ *
27
+ * Adjustment for React: the list is **immutable**. Upstream's
28
+ * `moveArrayElement` mutates the caller's array in place (deferring the splice
29
+ * through `nextTick` when the list is a ref), which cannot work in React —
30
+ * an in-place mutation does not re-render. The reause
31
+ * `moveArrayElement(list, from, to, e)` is pure: it returns a NEW reordered
32
+ * array (and still performs upstream's DOM fixup when an event is given), and
33
+ * the hook forwards that array to `options.onUpdate`.
34
+ *
35
+ * Adjustment for React: sortablejs manipulates DOM nodes directly, so the
36
+ * component must be a controlled list that re-renders from `onUpdate` — stable
37
+ * `key`s recommended — otherwise React and the DOM desync. Upstream's
38
+ * `onMounted` maps to a mount effect, `onScopeDispose` to the effect cleanup,
39
+ * and `watchElement` to an effect keyed on the resolved element identity.
40
+ *
41
+ * @param el target element, React ref object (`{ current }`), or CSS selector
42
+ * @param list current list items, in render order (never mutated)
43
+ * @param options sortablejs options plus `watchElement` / `document` / `onUpdate`
44
+ *
45
+ * @__NO_SIDE_EFFECTS__
46
+ * @see https://github.com/SortableJS/Sortable#options
47
+ */
48
+ function useSortable(el, list, options = {}) {
49
+ const { document = globalThis.document, watchElement = false, onUpdate, ...resetOptions } = options;
50
+ const instanceRef = useRef(void 0);
51
+ const listRef = useRef(list);
52
+ const onUpdateRef = useRef(onUpdate);
53
+ const optionsRef = useRef(resetOptions);
54
+ listRef.current = list;
55
+ onUpdateRef.current = onUpdate;
56
+ optionsRef.current = resetOptions;
57
+ const cleanup = useCallback(() => {
58
+ var _instanceRef$current;
59
+ (_instanceRef$current = instanceRef.current) === null || _instanceRef$current === void 0 || _instanceRef$current.destroy();
60
+ instanceRef.current = void 0;
61
+ }, []);
62
+ const initSortable = useCallback((target) => {
63
+ if (!target || instanceRef.current !== void 0) return;
64
+ instanceRef.current = new SortableJs(target, {
65
+ onUpdate: (e) => {
66
+ var _onUpdateRef$current;
67
+ const newList = moveArrayElement(listRef.current, e.oldIndex, e.newIndex, e);
68
+ (_onUpdateRef$current = onUpdateRef.current) === null || _onUpdateRef$current === void 0 || _onUpdateRef$current.call(onUpdateRef, newList, e);
69
+ },
70
+ ...optionsRef.current
71
+ });
72
+ }, []);
73
+ const start = useCallback(() => {
74
+ const target = typeof el === "string" ? document === null || document === void 0 ? void 0 : document.querySelector(el) : resolveElement(el);
75
+ if (target) initSortable(target);
76
+ }, [
77
+ document,
78
+ el,
79
+ initSortable
80
+ ]);
81
+ const stop = useCallback(() => {
82
+ cleanup();
83
+ }, [cleanup]);
84
+ const option = useCallback((name, value) => {
85
+ var _instanceRef$current2, _instanceRef$current3;
86
+ if (value !== void 0) (_instanceRef$current2 = instanceRef.current) === null || _instanceRef$current2 === void 0 || _instanceRef$current2.option(name, value);
87
+ else return (_instanceRef$current3 = instanceRef.current) === null || _instanceRef$current3 === void 0 ? void 0 : _instanceRef$current3.option(name);
88
+ }, []);
89
+ const resolved = typeof el === "string" ? el : resolveElement(el);
90
+ useEffect(() => {
91
+ if (!watchElement || typeof resolved === "string") return;
92
+ cleanup();
93
+ if (resolved) initSortable(resolved);
94
+ return cleanup;
95
+ }, [
96
+ cleanup,
97
+ initSortable,
98
+ resolved,
99
+ watchElement
100
+ ]);
101
+ useEffect(() => {
102
+ if (watchElement && typeof el !== "string") return;
103
+ start();
104
+ return cleanup;
105
+ }, [
106
+ cleanup,
107
+ start,
108
+ watchElement,
109
+ el
110
+ ]);
111
+ return {
112
+ start,
113
+ stop,
114
+ option
115
+ };
116
+ }
117
+ /**
118
+ * Inserts an element into the DOM at a given index.
119
+ * @param parentElement
120
+ * @param element
121
+ * @param {number} index
122
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2
123
+ */
124
+ function insertNodeAt(parentElement, element, index) {
125
+ const refElement = parentElement.children[index];
126
+ parentElement.insertBefore(element, refElement);
127
+ }
128
+ /**
129
+ * Removes a node from the DOM.
130
+ * @param {Node} node
131
+ * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2
132
+ */
133
+ function removeNode(node) {
134
+ if (node.parentNode) node.parentNode.removeChild(node);
135
+ }
136
+ /**
137
+ * Move an element of `list` from `from` to `to`, returning a NEW array.
138
+ *
139
+ * Map from @vueuse/integrations `useSortable`'s `moveArrayElement`, with one
140
+ * deliberate deviation: upstream mutates the caller's array in place (and
141
+ * defers the splice with `nextTick` when the list is a ref), which cannot work
142
+ * in React — an in-place mutation does not re-render. This implementation is
143
+ * pure: the input array is never mutated and the moved copy is returned. The
144
+ * move only happens when `to` is in range (`to >= 0 && to < list.length`,
145
+ * exactly upstream's bounds check); otherwise a copy of `list` is returned
146
+ * unchanged.
147
+ *
148
+ * The DOM fixup of upstream is kept for compatibility: when `e` is given (a
149
+ * sortablejs event), `e.item` is removed from the DOM and re-inserted at
150
+ * `from` inside `e.from`. That fixup runs regardless of the bounds check, as
151
+ * upstream does — note the upstream quirk of inserting at `from` rather than
152
+ * `to`. Pass `null` (or omit `e`) to only compute the array.
153
+ *
154
+ * @param list the current list — never mutated
155
+ * @param from source index
156
+ * @param to destination index
157
+ * @param e the sortablejs event to apply the DOM fixup for, if any
158
+ * @returns a new array with the element moved
159
+ */
160
+ function moveArrayElement(list, from, to, e) {
161
+ if (e != null) {
162
+ removeNode(e.item);
163
+ insertNodeAt(e.from, e.item, from);
164
+ }
165
+ const array = [...list];
166
+ if (to >= 0 && to < array.length) {
167
+ const element = array.splice(from, 1)[0];
168
+ array.splice(to, 0, element);
169
+ }
170
+ return array;
171
+ }
172
+ //#endregion
173
+ export { insertNodeAt, moveArrayElement, removeNode, useSortable };
package/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "name": "@reause/integrations",
3
+ "type": "module",
4
+ "version": "0.1.2",
5
+ "description": "Integrations with third-party services — React port of @vueuse/integrations (experimental, AI-mapped)",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": "./dist/index.js",
10
+ "./*": "./dist/*",
11
+ "./useAsyncValidator": "./dist/useAsyncValidator.js",
12
+ "./useAxios": "./dist/useAxios.js",
13
+ "./useChangeCase": "./dist/useChangeCase.js",
14
+ "./useCookies": "./dist/useCookies.js",
15
+ "./useDrauu": "./dist/useDrauu.js",
16
+ "./useFocusTrap": "./dist/useFocusTrap.js",
17
+ "./useFuse": "./dist/useFuse.js",
18
+ "./useIDBKeyval": "./dist/useIDBKeyval.js",
19
+ "./useJwt": "./dist/useJwt.js",
20
+ "./useNProgress": "./dist/useNProgress.js",
21
+ "./useQRCode": "./dist/useQRCode.js",
22
+ "./useSortable": "./dist/useSortable.js",
23
+ "./package.json": "./package.json"
24
+ },
25
+ "main": "./dist/index.js",
26
+ "module": "./dist/index.js",
27
+ "unpkg": "./dist/index.iife.min.js",
28
+ "jsdelivr": "./dist/index.iife.min.js",
29
+ "types": "./dist/index.d.ts",
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "peerDependencies": {
34
+ "async-validator": "^4",
35
+ "axios": "^1",
36
+ "change-case": "^5",
37
+ "drauu": "^1",
38
+ "focus-trap": "^7 || ^8",
39
+ "fuse.js": "^7",
40
+ "idb-keyval": "^6",
41
+ "jwt-decode": "^4",
42
+ "nprogress": "^0.2",
43
+ "qrcode": "^1.5",
44
+ "react": ">=18",
45
+ "sortablejs": "^1",
46
+ "universal-cookie": "^7 || ^8"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "async-validator": {
50
+ "optional": true
51
+ },
52
+ "axios": {
53
+ "optional": true
54
+ },
55
+ "change-case": {
56
+ "optional": true
57
+ },
58
+ "drauu": {
59
+ "optional": true
60
+ },
61
+ "focus-trap": {
62
+ "optional": true
63
+ },
64
+ "fuse.js": {
65
+ "optional": true
66
+ },
67
+ "idb-keyval": {
68
+ "optional": true
69
+ },
70
+ "jwt-decode": {
71
+ "optional": true
72
+ },
73
+ "nprogress": {
74
+ "optional": true
75
+ },
76
+ "qrcode": {
77
+ "optional": true
78
+ },
79
+ "sortablejs": {
80
+ "optional": true
81
+ },
82
+ "universal-cookie": {
83
+ "optional": true
84
+ }
85
+ },
86
+ "dependencies": {
87
+ "@reause/shared": "0.1.2"
88
+ },
89
+ "scripts": {
90
+ "build": "tsdown"
91
+ }
92
+ }