UNPKG

@mitcsutt/kiln-dnd

Version:

Kiln drag and drop: sortable lists, boards, drop zones and grid layouts with one keyboard model and one announcer, rendered through kiln-ui

99 lines (98 loc) • 2.65 kB
import { useDndController as e } from "../../core/context.js"; import { useStore as t } from "../../core/store.js"; import { Dnd as n } from "../Dnd/Dnd.js"; import { ListEntry as r } from "../internal/ListEntry.js"; import { arrayMove as i } from "../../data/lists.js"; import { useDroppable as a } from "../../hooks/useDroppable.js"; import o from "./Sortable.module.js"; import { cx as s } from "@mitcsutt/kiln-ui"; import { forwardRef as c, useCallback as l, useId as u, useState as d } from "react"; import { jsx as f } from "react/jsx-runtime"; //#region src/components/Sortable/Sortable.tsx var p = (e) => { let t = e?.id; if (typeof t == "string" || typeof t == "number") return String(t); throw Error("Sortable: give each item an `id`, or pass `getId`."); }; function m({ items: e, defaultItems: t, onChange: a, children: s, label: c, getLabel: m, getId: _ = p, isDisabled: v, orientation: y = "vertical", handle: b = !1, announcements: x, className: S, ...C }, w) { let [T, E] = d(() => t ?? []), D = e ?? T, O = u(), k = l(({ item: t, target: n, input: r }) => { if (n?.kind !== "list" || t.index === void 0) return; let o = t.index, s = D[o]; if (s === void 0 || o === n.index) return; let c = i(D, o, n.index); e === void 0 && E(c), a?.(c, { item: s, from: o, to: n.index, input: r }); }, [ D, e, a ]); return /* @__PURE__ */ f(n, { announcements: x, onDragEnd: k, children: /* @__PURE__ */ f(h, { ref: w, ...C, list: O, label: c, orientation: y, className: S, children: D.map((e, t) => { let n = _(e), i = v?.(e) ?? !1; return /* @__PURE__ */ f(r, { id: n, label: m(e), index: t, list: O, orientation: y, handle: b, disabled: i, className: o.item, children: /* @__PURE__ */ f(g, { render: s, item: e, index: t, id: n }) }, n); }) }) }); } var h = c(function({ list: e, label: t, orientation: n, className: r, children: i, ...c }, u) { let { ref: d } = a({ id: `${e}-container`, label: t, list: e, orientation: n }), p = l((e) => { d(e), typeof u == "function" ? u(e) : u && (u.current = e); }, [d, u]); return /* @__PURE__ */ f("ul", { ref: p, role: "list", "aria-label": t, ...c, className: s(o.list, r), "data-orientation": n, children: i }); }); function g({ render: e, item: t, index: n, id: r }) { return e(t, { index: n, isDragging: _(r) }); } function _(n) { let r = e("Sortable"); return t(r.store, l((e) => e.operation?.id === n, [n])); } var v = c(m); //#endregion export { v as Sortable }; //# sourceMappingURL=Sortable.js.map