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

460 lines (459 loc) • 12.8 kB
import { useDndController as e, useIsomorphicLayoutEffect as t } from "../../core/context.js"; import { Dnd as n } from "../Dnd/Dnd.js"; import { HandleContext as r } from "../DragHandle/context.js"; import { useDragSource as i } from "../../core/useDragSource.js"; import { bottom as a, cloneLayout as o, correctBounds as s } from "../../grid/engine/layout.js"; import { findOrGenerateResponsiveLayout as c, getBreakpointFromWidth as l } from "../../grid/engine/responsive.js"; import { appendItem as u, compactType as d, fitLayout as f, previewLayout as p, stepInLayout as m } from "../../data/layout.js"; import h from "./GridLayout.module.js"; import { cx as g } from "@mitcsutt/kiln-ui"; import { createContext as _, forwardRef as v, useCallback as y, useContext as b, useEffect as x, useMemo as S, useRef as C, useState as w } from "react"; import { jsx as T, jsxs as E } from "react/jsx-runtime"; //#region src/components/GridLayout/GridLayout.tsx var D = { base: 0, sm: 40, md: 48, lg: 64, xl: 80 }, O = _(null), k = (e, t) => ({ item: t, x: e.x, y: e.y, w: e.w, h: e.h }); function A(e) { if (typeof e == "number" || e === void 0) return [["base", e ?? 12]]; let t = Object.keys(D).filter((t) => e[t] !== void 0).map((t) => [t, e[t] ?? 1]); return t.length ? t : [["base", 12]]; } function j({ layout: e, defaultLayout: t, onChange: r, columns: i, rowHeight: a = 9, gap: o = 4, compaction: s = "vertical", label: c, announcements: l, className: u, style: d, children: f, ...p }, m) { return /* @__PURE__ */ T(n, { announcements: l, children: /* @__PURE__ */ T(M, { ref: m, ...p, layout: e, defaultLayout: t, onChange: r, columns: i, rowHeight: a, gap: o, compaction: s, label: c, className: u, style: d, children: f }) }); } var M = v(function({ layout: n, defaultLayout: r, onChange: i, columns: _, rowHeight: v, gap: b, compaction: j, label: M, className: N, style: te, children: P, ...ne }, F) { let I = e("GridLayout"), [re, ie] = w(() => r ?? []), ae = n ?? re, L = S(() => A(_), [_]), R = S(() => L.at(-1) ?? ["base", 12], [L]), [z, oe] = w(null), [B, se] = w(null); x(() => { if (!B || typeof ResizeObserver > "u") return; let e = new ResizeObserver(() => { oe(B.getBoundingClientRect().width); }); return e.observe(B), () => { e.disconnect(); }; }, [B]); let V = S(() => { if (z === null || L.length === 1 || !B) return R; let e = parseFloat(getComputedStyle(B.ownerDocument.documentElement).fontSize) || 16, t = Object.fromEntries(L.map(([t]) => [t, D[t] * e - .5])), n = l(t, z); return L.find(([e]) => e === n) ?? R; }, [ z, L, R, B ]), H = V[1], U = V[0] === R[0], [ce, le] = w([]), ue = y((e) => (le((t) => t.includes(e) ? t : [...t, e]), () => { le((t) => t.filter((t) => t !== e)); }), []), W = S(() => { let e = ae; for (let t of ce) e.some((e) => e.i === t) || (e = u(e, { i: t, w: R[1], h: 2 }, { columns: R[1], compaction: j })); return e; }, [ ae, ce, R, j ]), G = S(() => U ? s(o(W), { cols: H }) : c({ [R[0]]: W }, Object.fromEntries(L.map(([e]) => [e, D[e]])), V[0], R[0], H, d(j)), [ U, W, H, R, L, V, j ]), [K, de] = w({}), q = S(() => f(G, K, { columns: H, compaction: j }), [ G, K, H, j ]), [J, fe] = w(null), [Y, pe] = w(null), [X, me] = w(null); x(() => { if (X === null) return; let e = requestAnimationFrame(() => { me(null); }); return () => { cancelAnimationFrame(e); }; }, [X]); let Z = C({ display: G, fitted: q, fits: K, cols: H, compaction: j, onChange: i, layout: n }); t(() => { Z.current = { display: G, fitted: q, fits: K, cols: H, compaction: j, onChange: i, layout: n }; }); let Q = C(null); x(() => { if (!B || typeof ResizeObserver > "u") return; let e = () => Z.current.display.flatMap((e) => { let t = I.draggables.get(e.i)?.element; return t && !t.hidden ? [[e.i, t]] : []; }), t = () => { let t = Q.current?.getBoundingClientRect(); if (!t?.height) return; let n = e(); if (n.some(([, e]) => e.getAnimations?.().some((e) => e instanceof CSSTransition && e.transitionProperty === "block-size"))) return; for (let [, e] of n) e.setAttribute("data-measuring", ""); let r = Object.fromEntries(n.map(([e, n]) => { let r = n.getBoundingClientRect().height; return [e, Math.ceil((r + t.width) / (t.height + t.width) - .01)]; })); for (let [, e] of n) e.removeAttribute("data-measuring"); de((e) => { let t = Object.keys(r); return t.length === Object.keys(e).length && t.every((t) => e[t] === r[t]) ? e : r; }); }, n = new ResizeObserver(t), r = /* @__PURE__ */ new WeakSet(), i = (e) => { r.has(e) || (r.add(e), n.observe(e)); }, a = () => { i(B); for (let [, t] of e()) for (let e of t.children) i(e); }, o = 0, s = () => { o ||= requestAnimationFrame(() => { o = 0, t(); }); }, c = (e) => { e.propertyName === "block-size" && s(); }; a(); let l = new MutationObserver(() => { a(), s(); }); return l.observe(B, { childList: !0, subtree: !0 }), B.addEventListener("transitionend", c), B.addEventListener("transitioncancel", c), () => { cancelAnimationFrame(o), B.removeEventListener("transitionend", c), B.removeEventListener("transitioncancel", c), l.disconnect(), n.disconnect(); }; }, [B, I]); let he = y((e, t, n) => { let r = null, i = null, a = null, o = null, s = "", c = { key: "", y: 0 }, l = { x: 0, y: 0 }, u = () => ({ columns: Z.current.cols, compaction: Z.current.compaction }), d = () => I.messages, h = (t) => t?.find((t) => t.i === e), g = (e) => { o = e, fe(e); }, _ = () => I.draggables.get(e)?.element ?? null, v = (e, t) => { let n = _(); n?.style.setProperty("--_dx", `${String(e)}px`), n?.style.setProperty("--_dy", `${String(t)}px`); }, y = (e) => k(h(o) ?? e, n()), b = () => { l = { x: 0, y: 0 }, v(0, 0), fe(null), pe(null), o = null, a = null; }; return { blocked: () => U ? null : d().gridLocked(n()), lift(o) { return r = h(Z.current.display) ?? null, i = h(Z.current.fitted) ?? null, !r || !i ? "" : (a = { x: r.x, y: r.y, w: r.w, h: r.h }, c = { key: "", y: 0 }, s = `${String(r.x)},${String(r.y)},${String(r.w)},${String(r.h)}`, pe({ i: e, mode: t, input: o, origin: i }), g(Z.current.display), d().gridLifted(k(r, n()))); }, key(t, n) { if (!r || !a) return null; let i = _(), o = i && getComputedStyle(i).direction === "rtl" && (t === "left" || t === "right") ? t === "left" ? "right" : "left" : t, s = Z.current.display; if (n) { let t = { ...a }; o === "right" && (t.w += 1), o === "left" && --t.w, o === "down" && (t.h += 1), o === "up" && --t.h; let n = p(s, e, t, u()), i = h(n); return i && (a = { ...t, w: i.w, h: i.h }, g(n)), d().gridResized(y(r)); } let c = m(s, e, a, o, u()); return c && (a = c.request, g(c.layout)), d().gridMoved(y(r)); }, pointer(o) { if (!r || !a || !i) return null; let m = _(), y = m?.parentElement, b = Q.current?.getBoundingClientRect(); if (!y || !b) return null; let x = getComputedStyle(y).direction === "rtl", { cols: S } = Z.current, C = b.width, w = (y.getBoundingClientRect().width - (S - 1) * C) / S + C, T = b.height + C; t === "move" && (l = { x: Math.min(Math.max(o.delta.x, -m.offsetLeft), y.clientWidth - m.offsetLeft - m.offsetWidth), y: Math.max(o.delta.y, -m.offsetTop) }, v(l.x, l.y)); let E = t === "move" ? l : o.delta, D = (x ? -E.x : E.x) / w, O = E.y / T; if (t === "move") { let t = i.x + Math.round(D), n = Math.max(0, i.y + Math.round(O)), { display: r, fits: o, cols: s, compaction: l } = Z.current, d = `${String(t)},${String(n)}`, m = c.key === d ? c.y : n, g = c.key === d ? 0 : Infinity; for (let i = 0; i <= n && g > 0; i += 1) { let c = h(f(p(r, e, { ...a, x: t, y: i }, u()), o, { columns: s, compaction: l })), d = c ? Math.abs(c.y - n) : Infinity; d < g && (g = d, m = i); } c = { key: d, y: m }, a = { ...a, x: t, y: m }; } else { let e = i.h + Math.round(O); a = { ...a, w: Math.max(1, i.w + Math.round(D)), h: Math.max(1, e > i.h ? e : Math.min(r.h, e)) }; } let A = `${String(a.x)},${String(a.y)},${String(a.w)},${String(a.h)}`; if (A === s) return null; s = A; let j = p(Z.current.display, e, a, u()); g(j); let M = h(j); return M ? t === "move" ? d().gridMoved(k(M, n())) : d().gridResized(k(M, n())) : null; }, drop() { let t = r, i = o, a = I.operation?.input ?? "keyboard", s = h(i) ?? t, c = i !== null && JSON.stringify(i.map(({ i: e, x: t, y: n, w: r, h: i }) => [ e, t, n, r, i ])) !== JSON.stringify(Z.current.display.map(({ i: e, x: t, y: n, w: r, h: i }) => [ e, t, n, r, i ])); if (a === "pointer" && me(e), b(), c) { let t = [...i]; Z.current.layout === void 0 && ie(t), Z.current.onChange?.(t, { id: e, input: a }); } return s ? d().gridDropped(k(s, n())) : ""; }, cancel() { let e = r; return b(), e ? d().gridCanceled(k(e, n())) : ""; } }; }, [I, U]), $ = S(() => J ? f(J, K, { columns: H, compaction: j }) : q, [ J, K, H, j, q ]), ge = a($), _e = S(() => ({ layout: $, active: Y, landed: X, editable: U, register: ue, behavior: he, messages: () => I.messages }), [ $, Y, X, U, ue, he, I ]), ve = y((e) => { se(e), typeof F == "function" ? F(e) : F && (F.current = e); }, [F]), ye = Y?.input === "pointer" && Y.mode === "move" ? $.find((e) => e.i === Y.i) : void 0; return /* @__PURE__ */ T(O.Provider, { value: _e, children: /* @__PURE__ */ E("div", { ref: ve, role: "group", "aria-label": M, ...ne, className: g(h.grid, N), style: { "--_cols": H, "--_rows": ge, "--_gap": `var(--space-${String(b)})`, "--_row": `var(--space-${String(v)})`, ...te }, "data-dragging": Y ? Y.mode : void 0, children: [ /* @__PURE__ */ T("div", { ref: Q, className: h.probe, "aria-hidden": "true" }), ye ? /* @__PURE__ */ T("div", { className: h.placeholder, style: ee(ye), "aria-hidden": "true" }) : null, P ] }) }); }), ee = (e) => ({ "--_x": e.x, "--_y": e.y, "--_w": e.w, "--_h": e.h }), N = v(function({ id: n, label: a, resizable: o = !0, className: s, style: c, children: l, ...u }, d) { let f = b(O); if (!f) throw Error("GridLayout.Item must be rendered inside a GridLayout."); let p = e("GridLayout.Item"), { register: m, behavior: _, layout: v, active: x, landed: C, editable: w } = f; t(() => m(n), [m, n]); let D = v.find((e) => e.i === n), k = x?.i === n && x.input === "pointer" && x.mode === "move" ? x.origin : D, A = y(() => p.draggables.get(n)?.label ?? a, [ p, n, a ]), j = S(() => _(n, "move", A), [ _, n, A ]), M = S(() => _(n, "resize", A), [ _, n, A ]), N = !w || D?.static === !0, P = i(p, { id: n, label: a, data: void 0, disabled: N, kind: "custom", custom: j, feedback: "none", instructions: "grid", hasHandle: !0 }), ne = y((e) => { P.ref(e), typeof d == "function" ? d(e) : d && (d.current = e); }, [P, d]), F = x?.i === n ? x.mode : void 0; return /* @__PURE__ */ T(r.Provider, { value: { handleRef: P.handleRef, handleProps: P.handleProps, label: a, disabled: N }, children: /* @__PURE__ */ E("div", { ref: ne, ...u, className: g(h.item, s), style: { ...k ? ee(k) : {}, ...c }, hidden: !D || u.hidden, "data-dragging": x?.i === n ? x.input : void 0, "data-mode": F, "data-landed": C === n ? "" : void 0, children: [l, o && !N ? /* @__PURE__ */ T(te, { id: n, label: a, controller: p, behavior: M }) : null] }) }); }); function te({ id: e, label: t, controller: n, behavior: r }) { let { ref: a } = i(n, { id: `${e}\u0000resize`, label: t, data: void 0, disabled: !1, kind: "custom", custom: r, feedback: "none", instructions: "grid", hasHandle: !1, keyboard: !1 }); return /* @__PURE__ */ T("div", { ref: a, className: h.grip, "aria-hidden": "true", "data-kiln-dnd-resize": "", children: /* @__PURE__ */ T("svg", { viewBox: "0 0 12 12", focusable: "false", children: /* @__PURE__ */ T("path", { d: "M11 4 4 11M11 8 8 11" }) }) }); } var P = Object.assign(v(j), { Item: N }); //#endregion export { P as GridLayout }; //# sourceMappingURL=GridLayout.js.map