UNPKG

@uva-glass/component-library

Version:

React components UvA

153 lines (152 loc) 4.17 kB
import { jsx as f } from "react/jsx-runtime"; import { useRef as x, useEffect as w, cloneElement as R, createContext as O, useReducer as A, Children as _, useContext as L } from "react"; import { sortableReducer as j, initialState as q } from "./components/Sortable/sortableReducer.js"; import { c as N } from "./clsx-OuTLNxxd.js"; import { s as u } from "./Sortable.module-DDhRFD1T.js"; import { setDraggingTarget as B, setDraggingSource as H, setClone as X, reset as Y, moveToPosition as k, drop as z, dragStart as F, dragEnd as G, clearDraggingTarget as J, clearDraggingSource as K } from "./components/Sortable/actions.js"; function M({ scrollable: i, onSort: a }) { const { clone: n, draggingTarget: p, drop: D, sortableItems: v, setDraggingTarget: o, reset: S, indices: s } = U(), d = x(null), c = x(null), l = x(s); function m(r) { const e = d.current; if (!(e != null && e.hasChildNodes())) return; const t = r.currentTarget; return Array.from(e.childNodes).findIndex((b) => b.isEqualNode(t)); } function T(r) { const e = m(r); e !== void 0 && (n && c.current && c.current.removeChild(n), D(e)); } function $(r) { if (r.preventDefault(), r.dataTransfer.dropEffect = "move", n) { const { clientX: t, clientY: g } = r, { x: h, y: b, width: y, height: I } = r.currentTarget.getBoundingClientRect(), P = I / 2 - (g - b); n.setAttribute( "style", ` left: ${t - (t - h)}px; top: ${b - P}px; height: ${I}px; width: ${y}px; visibility: visible; ` ); } const e = m(r); e === void 0 || e === p || o(e); } function C(r) { var g; const { relatedTarget: e } = r; e !== null && ((g = d.current) == null ? void 0 : g.contains(e)) || (S(), n && n.setAttribute("style", "visibility: hidden;")); } return w(() => { if (!(!n || !c.current)) return c.current.appendChild(n), function() { n.remove(); }; }, [n]), w(() => { a && s.valueOf() !== l.current.valueOf() && (a(s), l.current = s); }, [s, l, a]), /* @__PURE__ */ f( "div", { className: N(u.sortable__wrapper, { [u["sortable__wrapper--scrollable"]]: i }), ref: c, children: /* @__PURE__ */ f( "ul", { className: N(u.sortable__container, { [u["sortable__container--drag-over"]]: n !== void 0 }), onDragLeave: C, ref: d, role: "presentation", children: v.map((r, e) => /* @__PURE__ */ f( "li", { className: u["sortable__drop-target"], draggable: !1, onDragOver: $, onDrop: T, role: "presentation", children: R(r, { index: e }) }, e )) } ) } ); } const E = O(void 0), Q = (i) => ({ ...q, indices: _.map(i, ({ props: a }) => a.index), maxIndex: _.count(i) - 1, sortableItems: i }); function ne({ children: i, items: a, scrollable: n, onSort: p }) { const D = x(a), [v, o] = A(j, i, Q); function S(t) { o(H(t)); } function s(t) { o(B(t)); } function d(t) { o(k(t)); } function c() { o(J()); } function l() { o(K()); } function m(t) { o(z(t)); } function T(t) { o(F(t)); } function $() { o(G()); } function C() { o(Y()); } function r(t) { o(X(t)); } function e(t) { const g = t.map((h) => D.current[h]); p && p(g); } return /* @__PURE__ */ f( E.Provider, { value: { ...v, clearDraggingSource: l, clearDraggingTarget: c, dragEnd: $, dragStart: T, drop: m, moveToPosition: d, reset: C, setClone: r, setDraggingSource: S, setDraggingTarget: s }, children: /* @__PURE__ */ f(M, { scrollable: n, onSort: e }) } ); } function U() { const i = L(E); if (i === void 0) throw new Error("useSortable must be used within a SortableProvider"); return i; } export { ne as S, M as a, U as u }; //# sourceMappingURL=SortableProvider-Ck_XnBVq.js.map