UNPKG

@uva-glass/component-library

Version:

React components UvA

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