@frontify/fondue
Version:
Design system of Frontify
261 lines (260 loc) • 11.4 kB
JavaScript
import { jsx as S, jsxs as as } from "react/jsx-runtime";
import { memo as rs, forwardRef as os, useRef as ns, useEffect as ds, useCallback as M, Children as gt, useMemo as z, createElement as ls } from "react";
import { useDndContext as ps, useDndMonitor as us } from "@dnd-kit/core";
import { useSortable as cs } from "@dnd-kit/sortable";
import { CSS as is } from "@dnd-kit/utilities";
import { noop as ms } from "lodash-es";
import { TreeItemColorsClassMap as ws, TreeItemBorderRadiusClassMap as E, TreeItemSpacingClassMap as fs, TreeItemBorderClassMap as Tt, TreeItemBorderStyleClassMap as bs, TreeItemShadowClassMap as hs } from "../types.es.js";
import { useDebounce as Dt } from "../../../hooks/useDebounce.es.js";
import { PARENT_FOCUS_VISIBLE_STYLE as ys, FOCUS_VISIBLE_STYLE as Is } from "../../../utilities/focusStyle.es.js";
import { merge as h } from "../../../utilities/merge.es.js";
import { DragHandle as gs } from "./DragHandle.es.js";
import { ExpandButton as Ts } from "./ExpandButton.es.js";
import { useTreeItem as Ds } from "./useTreeItem.es.js";
import { removeFragmentsAndEnrichChildren as Ns } from "../utils/removeFragmentsAndEnrichChildren.es.js";
import { useDeepCompareEffect as Ss } from "../utils/useDeepCompareEffect.es.js";
import { INDENTATION_WIDTH as Nt, EXPAND_ONHOVER_DELAY as xs } from "../helpers/constants.es.js";
const As = ({ isSorting: a, wasDragging: W }) => !(a || W), Bs = rs(
os(
({
id: a,
type: W,
label: y,
onDrop: _,
accepts: St,
showCaret: U,
children: I,
parentId: H,
level: u = 0,
contentComponent: g,
onClick: R,
onSelect: F,
onExpand: c,
onShrink: x,
registerOverlay: A,
registerNodeChildren: Y,
unregisterNodeChildren: d,
draggable: K = !0,
expandable: O = !0,
showDragHandlerOnHoverOnly: xt = !0,
dragHandlerPosition: i = "left",
showContentWhileDragging: B = !0,
itemStyle: X,
ignoreItemDoubleClick: q = !1,
expandOnSelect: At = !1,
"data-test-id": Bt = "fondue-tree-item"
}, w) => {
var rt, ot, nt, dt, lt, pt, ut, ct, it, mt, wt, ft, bt, ht, yt;
const { active: o, over: C } = ps(), { isSelected: p, isExpanded: m, projection: s } = Ds(a), G = ns(), e = (o == null ? void 0 : o.id) === a, t = e && s !== null && s !== void 0 ? s : null, Ct = typeof ((ot = (rt = C == null ? void 0 : C.data) == null ? void 0 : rt.current) == null ? void 0 : ot.accepts) == "string" ? (nt = C.data.current.accepts) == null ? void 0 : nt.split(", ") : [], vt = typeof (t == null ? void 0 : t.accepts) == "string" ? (dt = t.accepts) == null ? void 0 : dt.split(", ") : [], T = ((lt = o == null ? void 0 : o.data.current) == null ? void 0 : lt.type) || "", D = ((pt = s == null ? void 0 : s.previousNode) == null ? void 0 : pt.depth) !== void 0 && (s == null ? void 0 : s.depth) > ((ut = s == null ? void 0 : s.previousNode) == null ? void 0 : ut.depth), Lt = D && s.depth - 1 === ((ct = s == null ? void 0 : s.previousNode) == null ? void 0 : ct.depth), $ = D && ((it = s == null ? void 0 : s.previousNode) == null ? void 0 : it.accepts) !== void 0 && (((mt = s == null ? void 0 : s.previousNode) == null ? void 0 : mt.accepts.includes(`${T}-deeper`)) || ((wt = s == null ? void 0 : s.previousNode) == null ? void 0 : wt.accepts.includes(`${T}-within`))), Mt = e && Lt && ((ft = t == null ? void 0 : t.previousNode) == null ? void 0 : ft.accepts) !== void 0 && ((bt = t == null ? void 0 : t.previousNode) == null ? void 0 : bt.accepts.includes(`${T}-within`)) || (t == null ? void 0 : t.isWithinParent) && vt.includes(`${T}-within`), N = e && (o == null ? void 0 : o.data.current) && (Ct.includes(T) && !D || Mt), J = Dt((r) => {
G.current === r && (c == null || c(r));
}, xs);
ds(() => {
var r, l, b;
e && (G.current = D ? (r = t == null ? void 0 : t.previousNode) == null ? void 0 : r.id : null), e && $ && (t != null && t.parentId) && t.previousNode && t.parentId === t.previousNode.id && t.parentId !== ((b = (l = o == null ? void 0 : o.data) == null ? void 0 : l.current) == null ? void 0 : b.parentId) && J(t == null ? void 0 : t.parentId);
}, [
o == null ? void 0 : o.data,
t == null ? void 0 : t.parentId,
t == null ? void 0 : t.previousNode,
J,
e,
$,
D
]);
const Et = M(
(r) => {
var It;
const { over: l, active: b } = r;
!e || !t || b.id === (l == null ? void 0 : l.id) && (t == null ? void 0 : t.depth) === ((It = b.data.current) == null ? void 0 : It.level) || e && l && N && _ && _({
id: b.id,
parentId: t.parentId,
sort: t.position,
contentComponent: g,
parentType: t.type
});
},
[e, t, N, _, g]
), Wt = M(
(r) => {
r.active.id === a && (A == null || A({
contentComponent: g,
children: I,
id: a,
label: y,
level: u,
dragHandlerPosition: i,
showContentWhileDragging: B
}));
},
[
I,
g,
i,
a,
y,
u,
A,
B
]
), _t = M(
(r) => {
r.active.id === a && document.body.style.setProperty("cursor", N ? "grabbing" : "no-drop");
},
[N, a]
);
us({
onDragEnd: Et,
onDragStart: Wt,
onDragMove: _t
});
const Q = M(
(r) => {
r == null || r.stopPropagation(), m ? x == null || x(a) : c == null || c(a);
},
[a, m, c, x]
), Ht = Dt(
(r) => {
r.stopPropagation(), !(q && r.detail >= 2) && (At && Q(), F == null || F(a), R == null || R(a));
},
q ? 300 : 0
), Z = H && (o == null ? void 0 : o.id) === H, P = gt.count(I) > 0, { enrichedChildren: k, childrenIds: Rt } = z(() => {
const r = Ns(I, { parentId: a, level: u + 1 });
return {
enrichedChildren: r,
childrenIds: r.map((l) => l.props.id)
};
}, [I, a, u]), {
attributes: Ft,
listeners: Yt,
transform: v,
transition: $t,
setDraggableNodeRef: kt,
setDroppableNodeRef: Vt,
setActivatorNodeRef: zt
} = cs({
id: a,
disabled: !K,
data: { type: W, accepts: St, parentId: H, level: u },
animateLayoutChanges: As,
transition: null
});
Ss(() => {
if (gt.count(k) === 0) {
d == null || d(a);
return;
}
if (e || Z) {
d == null || d(a);
return;
}
m ? Y == null || Y({ id: a, children: k }) : d == null || d(a);
}, [e, m, Z, k, a]);
const n = z(() => ({
spacingY: "none",
contentHight: "single-line",
shadow: "none",
borderRadius: "small",
borderWidth: "none",
borderStyle: "none",
activeColorStyle: "neutral",
...X
}), [X]), f = ws[n.activeColorStyle ?? "neutral"], { liClassName: Ut, backgroundClassName: Kt } = z(() => ({
liClassName: h([
Is,
"tw-box-content tw-relative tw-cursor-default tw-transition-colors tw-outline-none tw-ring-inset tw-group tw-no-underline tw-leading-5",
!e && p ? f.selectedTextColor : f.textColor,
fs[n.spacingY ?? "none"]
]),
backgroundClassName: h([
"tw-block tw-absolute tw-inset-0 tw-transition-colors -tw-z-10",
n.borderWidth !== "none" ? E[n.borderRadius ?? "small"] : "",
!e && (!p || n.activeColorStyle !== "neutral") && f.pressedBackgroundColor,
!e && p ? f.selectedBackgroundColor : f.backgroundColor,
p && ys
])
}), [e, p, n, f]), Ot = B ? !0 : !e, Xt = h([
"tw-max-w-full tw-grow tw-overflow-hidden",
e && B ? "tw-opacity-75 tw-blur-sm tw-grayscale" : ""
]), qt = m && !e, j = K && !e, Gt = y !== void 0 && !e, L = !e && O && (U === void 0 ? P : U);
let V = "";
!e && !m && L && $ && ((ht = s == null ? void 0 : s.previousNode) == null ? void 0 : ht.id) === a && (s == null ? void 0 : s.depth) > ((yt = s == null ? void 0 : s.previousNode) == null ? void 0 : yt.depth) && (V = h([
"tw-border-solid tw-border-box-selected-strong",
E[n.borderRadius ?? "small"],
Tt.small
]));
const Jt = n.borderWidth !== "none" && V === "" ? h([
Tt[n.borderWidth ?? "none"],
E[n.borderRadius ?? "small"],
bs[n.borderStyle ?? "none"]
]) : "", Qt = n.contentHight === "single-line" ? "tw-h-10" : "tw-h-fit", Zt = n.contentHight === "single-line" ? "tw-h-12" : "tw-h-fit", Pt = h([
"tw-relative tw-z-0 tw-transition-colors tw-flex tw-items-center tw-leading-5 tw-width-fit tw-justify-between tw-group",
hs[n.shadow ?? "none"],
e ? "tw-border-dashed tw-border-2 tw-pr-0" : Jt,
e && E[n.borderRadius ?? "small"],
e ? Zt : Qt,
e && (N ? "tw-border-box-selected-strong tw-bg-box-selected-hover" : "tw-bg-box-negative-hover tw-border-box-negative-strong-hover"),
V
]), tt = t != null && t.depth ? t.depth * Nt : void 0, st = e ? 0 : u * Nt, jt = { paddingLeft: tt ?? st }, ts = n.borderWidth !== "none" ? {} : { marginLeft: -1 * (tt ?? st) }, ss = {
transform: is.Transform.toString(v),
transition: $t
}, et = { ...Yt, ...Ft }, at = i !== "none" ? /* @__PURE__ */ S(
gs,
{
...i !== "none" ? et : {},
active: p,
ref: zt,
disabled: !j,
"aria-hidden": !j,
showDragHandlerOnHoverOnly: xt,
activeColorStyle: n.activeColorStyle ?? "neutral"
}
) : null, es = i === "none" ? { ...et } : {};
return /* @__PURE__ */ ls(
"li",
{
...es,
id: a,
key: a,
tabIndex: 0,
role: "treeitem",
style: jt,
onKeyDown: ms,
"aria-label": y,
"aria-level": u + 1,
onClick: Ht,
className: Ut,
ref: (r) => {
Vt(r), typeof w == "function" ? w(r) : w != null && w.current && (w.current = r);
},
"data-test-id": Bt,
"aria-selected": p,
"aria-expanded": m,
"data-has-children": P,
"aria-owns": Rt.join(" ")
},
/* @__PURE__ */ as("div", { ref: kt, className: Pt, style: ss, children: [
/* @__PURE__ */ S("span", { className: Kt, style: ts, "aria-hidden": !0 }),
i === "left" && at,
O && /* @__PURE__ */ S(
Ts,
{
active: v != null && v.y ? !1 : p,
onClick: Q,
expanded: qt,
disabled: !L,
"aria-hidden": !L,
className: L ? "tw-visible" : "tw-invisible tw-pointer-events-none"
}
),
Gt && /* @__PURE__ */ S("span", { className: "first:tw-ml-3.5 tw-w-full tw-h-full tw-flex tw-items-center", children: y }),
Ot && /* @__PURE__ */ S("div", { className: Xt, children: g }),
i === "right" && at
] })
);
}
)
);
Bs.displayName = "FondueTreeItem";
export {
Bs as TreeItem
};
//# sourceMappingURL=TreeItem.es.js.map