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

119 lines (118 loc) • 3.31 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 { moveBetween as i } from "../../data/lists.js"; import { useDroppable as a } from "../../hooks/useDroppable.js"; import o from "./Board.module.js"; import { Badge as s, Heading as c, cx as l } from "@mitcsutt/kiln-ui"; import { forwardRef as u, useCallback as d, useId as f, useState as p } from "react"; import { jsx as m, jsxs as h } from "react/jsx-runtime"; //#region src/components/Board/Board.tsx var g = (e) => { let t = e?.id; if (typeof t == "string" || typeof t == "number") return String(t); throw Error("Board: give each item an `id`, or pass `getId`."); }, _ = []; function v({ columns: e, items: t, defaultItems: a, onChange: u, children: v, label: x, getLabel: S, getId: C = g, isDisabled: w, handle: T = !1, headingLevel: E = 3, announcements: D, className: O, ...k }, A) { let [j, M] = p(() => a ?? {}), N = t ?? j, P = f(), F = d(({ item: e, target: n, input: r }) => { if (n?.kind !== "list" || e.list === void 0 || e.index === void 0) return; let a = { column: e.list, index: e.index }, o = { column: n.list, index: n.index }, s = N[a.column]?.[a.index]; if (s === void 0 || a.column === o.column && a.index === o.index) return; let c = i(N, a, o); t === void 0 && M(c), u?.(c, { item: s, from: a, to: o, input: r }); }, [ N, t, u ]); return /* @__PURE__ */ m(n, { announcements: D, onDragEnd: F, children: /* @__PURE__ */ m("div", { ref: A, role: "group", "aria-label": x, ...k, className: l(o.board, O), children: e.map((e) => { let t = N[e.id] ?? _, n = `${P}-${e.id}`; return /* @__PURE__ */ h("section", { className: o.column, "aria-labelledby": n, children: [/* @__PURE__ */ h("div", { className: o.header, children: [/* @__PURE__ */ m(c, { id: n, level: E, size: "sm", children: e.label }), /* @__PURE__ */ m(s, { children: t.length })] }), /* @__PURE__ */ m(y, { column: e, headingId: n, scope: P, children: t.map((t, n) => { let i = C(t); return /* @__PURE__ */ m(r, { id: i, label: S(t), index: n, list: e.id, orientation: "vertical", handle: T, disabled: w?.(t) ?? !1, className: o.card, children: /* @__PURE__ */ m(b, { render: v, item: t, id: i, column: e.id, index: n }) }, i); }) })] }, e.id); }) }) }); } function y({ column: e, headingId: t, scope: n, children: r }) { let { ref: i, isOver: s } = a({ id: `${n}-column-${e.id}`, label: e.label, list: e.id }); return /* @__PURE__ */ m("ul", { ref: i, role: "list", "aria-labelledby": t, className: o.cards, "data-over": s ? "" : void 0, children: r }); } function b({ render: n, item: r, id: i, column: a, index: o }) { let s = e("Board"); return n(r, { column: a, index: o, isDragging: t(s.store, d((e) => e.operation?.id === i, [i])) }); } var x = u(v); //#endregion export { x as Board }; //# sourceMappingURL=Board.js.map