@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
357 lines (356 loc) • 9.87 kB
JavaScript
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, previewLayout as f, stepInLayout as p } from "../../data/layout.js";
import m from "./GridLayout.module.js";
import { cx as h } from "@mitcsutt/kiln-ui";
import { createContext as g, forwardRef as _, useCallback as v, useContext as y, useEffect as b, useMemo as x, useRef as S, useState as C } from "react";
import { jsx as w, jsxs as T } from "react/jsx-runtime";
//#region src/components/GridLayout/GridLayout.tsx
var E = {
base: 0,
sm: 40,
md: 48,
lg: 64,
xl: 80
}, ee = g(null), D = (e, t) => ({
item: t,
x: e.x,
y: e.y,
w: e.w,
h: e.h
});
function O(e) {
if (typeof e == "number" || e === void 0) return [["base", e ?? 12]];
let t = Object.keys(E).filter((t) => e[t] !== void 0).map((t) => [t, e[t] ?? 1]);
return t.length ? t : [["base", 12]];
}
function k({ 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__ */ w(n, {
announcements: l,
children: /* @__PURE__ */ w(A, {
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 A = _(function({ layout: n, defaultLayout: r, onChange: i, columns: g, rowHeight: _, gap: y, compaction: k, label: A, className: M, style: te, children: N, ...ne }, P) {
let F = e("GridLayout"), [re, ie] = C(() => r ?? []), I = n ?? re, L = x(() => O(g), [g]), R = x(() => L.at(-1) ?? ["base", 12], [L]), [z, ae] = C(null), [B, oe] = C(null);
b(() => {
if (!B || typeof ResizeObserver > "u") return;
let e = new ResizeObserver(() => {
ae(B.getBoundingClientRect().width);
});
return e.observe(B), () => {
e.disconnect();
};
}, [B]);
let V = x(() => {
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, E[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], [W, G] = C([]), K = v((e) => (G((t) => t.includes(e) ? t : [...t, e]), () => {
G((t) => t.filter((t) => t !== e));
}), []), q = x(() => {
let e = I;
for (let t of W) e.some((e) => e.i === t) || (e = u(e, {
i: t,
w: R[1],
h: 2
}, {
columns: R[1],
compaction: k
}));
return e;
}, [
I,
W,
R,
k
]), J = x(() => U ? s(o(q), { cols: H }) : c({ [R[0]]: q }, Object.fromEntries(L.map(([e]) => [e, E[e]])), V[0], R[0], H, d(k)), [
U,
q,
H,
R,
L,
V,
k
]), [se, ce] = C(null), [Y, le] = C(null), [X, ue] = C(null);
b(() => {
if (X === null) return;
let e = requestAnimationFrame(() => {
ue(null);
});
return () => {
cancelAnimationFrame(e);
};
}, [X]);
let Z = S({
display: J,
cols: H,
compaction: k,
onChange: i,
layout: n
});
t(() => {
Z.current = {
display: J,
cols: H,
compaction: k,
onChange: i,
layout: n
};
});
let Q = S(null), de = v((e, t, n) => {
let r = null, i = null, a = null, o = "", s = () => ({
columns: Z.current.cols,
compaction: Z.current.compaction
}), c = () => F.messages, l = (t) => t?.find((t) => t.i === e), u = (e) => {
a = e, ce(e);
}, d = () => F.draggables.get(e)?.element ?? null, m = (e, t) => {
let n = d();
n?.style.setProperty("--_dx", `${String(e)}px`), n?.style.setProperty("--_dy", `${String(t)}px`);
}, h = (e) => D(l(a) ?? e, n()), g = () => {
m(0, 0), ce(null), le(null), a = null, i = null;
};
return {
blocked: () => U ? null : c().gridLocked(n()),
lift(a) {
return r = l(Z.current.display) ?? null, r ? (i = {
x: r.x,
y: r.y,
w: r.w,
h: r.h
}, o = `${String(r.x)},${String(r.y)},${String(r.w)},${String(r.h)}`, le({
i: e,
mode: t,
input: a
}), u(Z.current.display), c().gridLifted(D(r, n()))) : "";
},
key(t, n) {
if (!r || !i) return null;
let a = d(), o = a && getComputedStyle(a).direction === "rtl" && (t === "left" || t === "right") ? t === "left" ? "right" : "left" : t, m = Z.current.display;
if (n) {
let t = { ...i };
o === "right" && (t.w += 1), o === "left" && --t.w, o === "down" && (t.h += 1), o === "up" && --t.h;
let n = f(m, e, t, s()), a = l(n);
return a && (i = {
...t,
w: a.w,
h: a.h
}, u(n)), c().gridResized(h(r));
}
let g = p(m, e, i, o, s());
return g && (i = g.request, u(g.layout)), c().gridMoved(h(r));
},
pointer(a) {
if (!r || !i) return null;
let p = d()?.parentElement, h = Q.current?.getBoundingClientRect();
if (!p || !h) return null;
let g = getComputedStyle(p).direction === "rtl", { cols: _ } = Z.current, v = h.width, y = (p.getBoundingClientRect().width - (_ - 1) * v) / _ + v, b = h.height + v, x = (g ? -a.delta.x : a.delta.x) / y, S = a.delta.y / b;
t === "move" ? (m(a.delta.x, a.delta.y), i = {
...i,
x: r.x + Math.round(x),
y: Math.max(0, r.y + Math.round(S))
}) : i = {
...i,
w: Math.max(1, r.w + Math.round(x)),
h: Math.max(1, r.h + Math.round(S))
};
let C = `${String(i.x)},${String(i.y)},${String(i.w)},${String(i.h)}`;
if (C === o) return null;
o = C;
let w = f(Z.current.display, e, i, s());
u(w);
let T = l(w);
return T ? t === "move" ? c().gridMoved(D(T, n())) : c().gridResized(D(T, n())) : null;
},
drop() {
let t = r, i = a, o = F.operation?.input ?? "keyboard", s = l(i) ?? t, u = 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 (o === "pointer" && ue(e), g(), u) {
let t = [...i];
Z.current.layout === void 0 && ie(t), Z.current.onChange?.(t, {
id: e,
input: o
});
}
return s ? c().gridDropped(D(s, n())) : "";
},
cancel() {
let e = r;
return g(), e ? c().gridCanceled(D(e, n())) : "";
}
};
}, [F, U]), $ = se ?? J, fe = a($), pe = x(() => ({
layout: $,
active: Y,
landed: X,
editable: U,
register: K,
behavior: de,
messages: () => F.messages
}), [
$,
Y,
X,
U,
K,
de,
F
]), me = v((e) => {
oe(e), typeof P == "function" ? P(e) : P && (P.current = e);
}, [P]), he = Y?.input === "pointer" && Y.mode === "move" ? $.find((e) => e.i === Y.i) : void 0;
return /* @__PURE__ */ w(ee.Provider, {
value: pe,
children: /* @__PURE__ */ T("div", {
ref: me,
role: "group",
"aria-label": A,
...ne,
className: h(m.grid, M),
style: {
"--_cols": H,
"--_rows": fe,
"--_gap": `var(--space-${String(y)})`,
"--_row": `var(--space-${String(_)})`,
...te
},
"data-dragging": Y ? Y.mode : void 0,
children: [
/* @__PURE__ */ w("div", {
ref: Q,
className: m.probe,
"aria-hidden": "true"
}),
he ? /* @__PURE__ */ w("div", {
className: m.placeholder,
style: j(he),
"aria-hidden": "true"
}) : null,
N
]
})
});
}), j = (e) => ({
"--_x": e.x,
"--_y": e.y,
"--_w": e.w,
"--_h": e.h
}), M = _(function({ id: n, label: a, resizable: o = !0, className: s, style: c, children: l, ...u }, d) {
let f = y(ee);
if (!f) throw Error("GridLayout.Item must be rendered inside a GridLayout.");
let p = e("GridLayout.Item"), { register: g, behavior: _, layout: b, active: S, landed: C, editable: E } = f;
t(() => g(n), [g, n]);
let D = b.find((e) => e.i === n), O = v(() => p.draggables.get(n)?.label ?? a, [
p,
n,
a
]), k = x(() => _(n, "move", O), [
_,
n,
O
]), A = x(() => _(n, "resize", O), [
_,
n,
O
]), M = !E || D?.static === !0, N = i(p, {
id: n,
label: a,
data: void 0,
disabled: M,
kind: "custom",
custom: k,
feedback: "none",
instructions: "grid",
hasHandle: !0
}), ne = v((e) => {
N.ref(e), typeof d == "function" ? d(e) : d && (d.current = e);
}, [N, d]), P = S?.i === n ? S.mode : void 0;
return /* @__PURE__ */ w(r.Provider, {
value: {
handleRef: N.handleRef,
handleProps: N.handleProps,
label: a,
disabled: M
},
children: /* @__PURE__ */ T("div", {
ref: ne,
...u,
className: h(m.item, s),
style: {
...D ? j(D) : {},
...c
},
hidden: !D || u.hidden,
"data-dragging": S?.i === n ? S.input : void 0,
"data-mode": P,
"data-landed": C === n ? "" : void 0,
children: [l, o && !M ? /* @__PURE__ */ w(te, {
id: n,
label: a,
controller: p,
behavior: A
}) : 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__ */ w("div", {
ref: a,
className: m.grip,
"aria-hidden": "true",
"data-kiln-dnd-resize": "",
children: /* @__PURE__ */ w("svg", {
viewBox: "0 0 12 12",
focusable: "false",
children: /* @__PURE__ */ w("path", { d: "M11 4 4 11M11 8 8 11" })
})
});
}
var N = Object.assign(_(k), { Item: M });
//#endregion
export { N as GridLayout };
//# sourceMappingURL=GridLayout.js.map