@momoi-labs/kiso-react 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/diagram.d.ts +54 -4
- package/dist/diagram.js +87 -15
- package/package.json +2 -2
package/dist/diagram.d.ts
CHANGED
|
@@ -25,22 +25,72 @@ export type DiagramProps = Omit<React.ComponentProps<"div">, "children"> & Diagr
|
|
|
25
25
|
};
|
|
26
26
|
declare function Diagram({ label, background, strength, borderStyle, cornerStyle, cornerSize, cornerMarks, className, children, ...props }: DiagramProps): React.JSX.Element;
|
|
27
27
|
declare function DiagramColumn({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
28
|
+
export type DiagramNodeGroupProps = React.ComponentProps<"div"> & DiagramFrameProps;
|
|
29
|
+
declare function DiagramNodeGroup({ borderStyle, cornerStyle, cornerSize, cornerMarks, className, ...props }: DiagramNodeGroupProps): React.JSX.Element;
|
|
28
30
|
export type DiagramNodeProps = Omit<React.ComponentProps<"div">, "title" | "children"> & DiagramFrameProps & {
|
|
29
31
|
/** Edges refer to the node by this id. Unique within the Diagram. */
|
|
30
32
|
id: string;
|
|
31
|
-
/** The name in weight. It never wraps.
|
|
32
|
-
|
|
33
|
+
/** The name in weight. It never wraps. A node that only holds parts, such
|
|
34
|
+
as a DiagramFold, leaves it out. */
|
|
35
|
+
title?: React.ReactNode;
|
|
33
36
|
/** Default icon, label and tone. `icon` and `label` override it. */
|
|
34
37
|
kind?: DiagramKind;
|
|
35
38
|
icon?: LucideIcon;
|
|
39
|
+
/** The caption above the title. Without one, the icon leads the title. */
|
|
36
40
|
label?: string;
|
|
37
41
|
/** Marks the node the screen is about. */
|
|
38
42
|
accent?: boolean;
|
|
39
43
|
status?: DiagramStatus;
|
|
40
|
-
/**
|
|
44
|
+
/** Text renders under the title in mono, with its line breaks kept.
|
|
45
|
+
Elements, such as facts or a fold, render as they are. */
|
|
41
46
|
children?: React.ReactNode;
|
|
42
47
|
};
|
|
43
48
|
declare function DiagramNode({ id, title, kind, icon, label, accent, status, borderStyle, cornerStyle, cornerSize, cornerMarks, className, children, ...props }: DiagramNodeProps): React.JSX.Element;
|
|
49
|
+
export type DiagramFactProps = Omit<React.ComponentProps<"div">, "children"> & {
|
|
50
|
+
icon?: LucideIcon;
|
|
51
|
+
label: string;
|
|
52
|
+
/** The value, right-aligned in mono. */
|
|
53
|
+
children?: React.ReactNode;
|
|
54
|
+
/** Said, muted, when there is no value. */
|
|
55
|
+
empty?: string;
|
|
56
|
+
};
|
|
57
|
+
declare function DiagramFact({ icon: Icon, label, empty, className, children, ...props }: DiagramFactProps): React.JSX.Element;
|
|
58
|
+
export type DiagramFoldProps = Omit<React.ComponentProps<"div">, "title"> & {
|
|
59
|
+
title: React.ReactNode;
|
|
60
|
+
/** The number of things the fold holds, shown next to the title. */
|
|
61
|
+
count?: number;
|
|
62
|
+
/** Open by default. */
|
|
63
|
+
defaultOpen?: boolean;
|
|
64
|
+
};
|
|
65
|
+
declare function DiagramFold({ title, count, defaultOpen, className, children, ...props }: DiagramFoldProps): React.JSX.Element;
|
|
66
|
+
export type DiagramFilterOption = {
|
|
67
|
+
value: string;
|
|
68
|
+
label: string;
|
|
69
|
+
count: number;
|
|
70
|
+
};
|
|
71
|
+
export type DiagramFilterProps = Omit<React.ComponentProps<"div">, "onChange"> & {
|
|
72
|
+
options: DiagramFilterOption[];
|
|
73
|
+
/** The option shown alone, or null for all of them. */
|
|
74
|
+
value: string | null;
|
|
75
|
+
onValueChange: (value: string | null) => void;
|
|
76
|
+
};
|
|
77
|
+
declare function DiagramFilter({ options, value, onValueChange, className, ...props }: DiagramFilterProps): React.JSX.Element;
|
|
78
|
+
export type DiagramSectionProps = Omit<React.ComponentProps<"div">, "title"> & {
|
|
79
|
+
/** Without a title the lines sit flat, with no tree. */
|
|
80
|
+
title?: React.ReactNode;
|
|
81
|
+
icon?: LucideIcon;
|
|
82
|
+
count?: number;
|
|
83
|
+
/** A control at the end of the heading, such as an add button. */
|
|
84
|
+
action?: React.ReactNode;
|
|
85
|
+
};
|
|
86
|
+
declare function DiagramSection({ title, icon: Icon, count, action, className, children, ...props }: DiagramSectionProps): React.JSX.Element;
|
|
87
|
+
export type DiagramLineProps = Omit<React.ComponentProps<"div">, "onClick"> & {
|
|
88
|
+
/** Right-aligned detail, such as a target or a state. */
|
|
89
|
+
end?: React.ReactNode;
|
|
90
|
+
/** Makes the line a button. */
|
|
91
|
+
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
|
92
|
+
};
|
|
93
|
+
declare function DiagramLine({ end, onClick, className, children, ...props }: DiagramLineProps): React.JSX.Element;
|
|
44
94
|
export type DiagramEdgeProps = {
|
|
45
95
|
/** One source id, or several that merge into the target. */
|
|
46
96
|
from: string | string[];
|
|
@@ -51,4 +101,4 @@ export type DiagramEdgeProps = {
|
|
|
51
101
|
tone?: DiagramEdgeTone;
|
|
52
102
|
};
|
|
53
103
|
declare function DiagramEdge({ from, to, label, line, tone }: DiagramEdgeProps): null;
|
|
54
|
-
export { Diagram, DiagramColumn, DiagramNode, DiagramEdge };
|
|
104
|
+
export { Diagram, DiagramColumn, DiagramNodeGroup, DiagramNode, DiagramFact, DiagramFold, DiagramFilter, DiagramSection, DiagramLine, DiagramEdge };
|
package/dist/diagram.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import { clsx as cn } from "clsx";
|
|
4
|
-
import { Container, Database, GitBranch, Route, Server } from "lucide-react";
|
|
4
|
+
import { ChevronDown, ChevronUp, Container, Database, GitBranch, Route, Server } from "lucide-react";
|
|
5
|
+
import { Badge } from "./badge.js";
|
|
5
6
|
import { Card } from "./card.js";
|
|
6
7
|
import { StatusBadge } from "./status-badge.js";
|
|
7
8
|
import { useIsomorphicLayoutEffect } from "./use-isomorphic-layout-effect.js";
|
|
@@ -51,6 +52,7 @@ function elbow(sx, sy, tx, ty, mid) {
|
|
|
51
52
|
}
|
|
52
53
|
function Diagram({ label, background, strength, borderStyle, cornerStyle, cornerSize, cornerMarks, className, children, ...props }) {
|
|
53
54
|
const frame = React.useRef(null);
|
|
55
|
+
const scroll = React.useRef(null);
|
|
54
56
|
const nodes = React.useRef(new Map()).current;
|
|
55
57
|
const [edges, setEdges] = React.useState([]);
|
|
56
58
|
const [version, setVersion] = React.useState(0);
|
|
@@ -70,9 +72,18 @@ function Diagram({ label, background, strength, borderStyle, cornerStyle, corner
|
|
|
70
72
|
const context = React.useMemo(() => ({ nodes, register, addEdge }), [nodes, register, addEdge]);
|
|
71
73
|
useIsomorphicLayoutEffect(() => {
|
|
72
74
|
const root = frame.current;
|
|
73
|
-
|
|
75
|
+
const region = scroll.current;
|
|
76
|
+
if (!root || !region)
|
|
74
77
|
return;
|
|
78
|
+
// Lay the drawing out wrapped to the panel first. If it still overflows,
|
|
79
|
+
// wrapping only cramps it, so it takes its natural width and scrolls.
|
|
80
|
+
const fit = () => {
|
|
81
|
+
root.removeAttribute("data-overflow");
|
|
82
|
+
if (root.offsetWidth > region.clientWidth)
|
|
83
|
+
root.setAttribute("data-overflow", "");
|
|
84
|
+
};
|
|
75
85
|
const measure = () => {
|
|
86
|
+
fit();
|
|
76
87
|
const origin = root.getBoundingClientRect();
|
|
77
88
|
const box = (id) => {
|
|
78
89
|
const rect = nodes.get(id)?.getBoundingClientRect();
|
|
@@ -81,48 +92,109 @@ function Diagram({ label, background, strength, borderStyle, cornerStyle, corner
|
|
|
81
92
|
return {
|
|
82
93
|
left: rect.left - origin.left,
|
|
83
94
|
right: rect.right - origin.left,
|
|
95
|
+
top: rect.top - origin.top,
|
|
96
|
+
height: rect.height,
|
|
84
97
|
y: rect.top - origin.top + rect.height / 2,
|
|
85
98
|
};
|
|
86
99
|
};
|
|
87
|
-
|
|
88
|
-
|
|
100
|
+
// Each edge into a target gets its own entry point, spread down the
|
|
101
|
+
// target's side in the order of their sources, so their arrows and
|
|
102
|
+
// labels never land on the same spot. One edge enters at the middle.
|
|
103
|
+
const measured = edges.flatMap((edge) => {
|
|
89
104
|
const target = box(edge.to);
|
|
90
|
-
if (!target)
|
|
91
|
-
continue;
|
|
92
105
|
const sources = edge.from.map(box).filter((b) => b !== null);
|
|
93
|
-
|
|
94
|
-
|
|
106
|
+
return target && sources.length ? [{ edge, target, sources }] : [];
|
|
107
|
+
});
|
|
108
|
+
const entries = new Map();
|
|
109
|
+
const top = (item) => Math.min(...item.sources.map((s) => s.y));
|
|
110
|
+
for (const id of new Set(measured.map((item) => item.edge.to))) {
|
|
111
|
+
const group = measured.filter((item) => item.edge.to === id).sort((a, b) => top(a) - top(b));
|
|
112
|
+
group.forEach((item, index) => {
|
|
113
|
+
entries.set(item.edge, item.target.top + (item.target.height * (index + 1)) / (group.length + 1));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const next = { paths: [], labels: [] };
|
|
117
|
+
for (const { edge, target, sources } of measured) {
|
|
118
|
+
const ty = entries.get(edge) ?? target.y;
|
|
95
119
|
const start = Math.max(...sources.map((s) => s.right));
|
|
96
120
|
const mid = (start + target.left) / 2;
|
|
97
121
|
sources.forEach((source, index) => {
|
|
98
|
-
const { trunk, branch } = elbow(source.right, source.y, target.left,
|
|
122
|
+
const { trunk, branch } = elbow(source.right, source.y, target.left, ty, mid);
|
|
99
123
|
next.paths.push({ key: `${edge.key}:${index}:trunk`, d: trunk, arrow: false });
|
|
100
124
|
next.paths.push({ key: `${edge.key}:${index}`, d: branch, line: edge.line, tone: edge.tone, arrow: true });
|
|
101
125
|
});
|
|
102
|
-
// The label sits on the
|
|
126
|
+
// The label sits on the edge's own run into the target, above the line.
|
|
103
127
|
if (edge.label)
|
|
104
|
-
next.labels.push({ key: edge.key, x: mid, y:
|
|
128
|
+
next.labels.push({ key: edge.key, x: mid, y: ty - RADIUS, text: edge.label });
|
|
105
129
|
}
|
|
106
130
|
setDrawn(next);
|
|
107
131
|
};
|
|
108
132
|
measure();
|
|
109
133
|
const observer = new ResizeObserver(measure);
|
|
110
134
|
observer.observe(root);
|
|
135
|
+
observer.observe(region);
|
|
111
136
|
nodes.forEach((element) => observer.observe(element));
|
|
112
137
|
return () => observer.disconnect();
|
|
113
138
|
}, [edges, nodes, version]);
|
|
114
|
-
return (_jsx(DiagramContext.Provider, { value: context, children: _jsx("div", { ...props, "data-slot": "diagram", className: cn("card diagram", className), "data-background-style": background, "data-background-strength": strength, children: _jsx("div", { "data-slot": "diagram-scroll", className: "diagram-scroll", role: "
|
|
139
|
+
return (_jsx(DiagramContext.Provider, { value: context, children: _jsx("div", { ...props, "data-slot": "diagram", className: cn("card diagram", className), "data-background-style": background, "data-background-strength": strength, children: _jsx("div", { ref: scroll, "data-slot": "diagram-scroll", className: "diagram-scroll", role: "group", "aria-label": label, tabIndex: 0, children: _jsxs("div", { ref: frame, "data-slot": "diagram-canvas", className: "diagram-canvas", children: [_jsx("div", { "data-slot": "diagram-lanes", className: "diagram-lanes", "data-border-style": borderStyle, "data-corner-style": cornerStyle, "data-corner-size": cornerSize, "data-corner-marks": cornerMarks, "data-edge-labels": edges.some((edge) => edge.label) || undefined, children: children }), _jsxs("svg", { className: "diagram-edges", "aria-hidden": "true", children: [_jsx("defs", { children: ["neutral", "success", "danger"].map((tone) => (_jsx("marker", { id: `${marker}-${tone}`, className: "diagram-arrow", "data-tone": tone, viewBox: "0 0 10 10", refX: "9", refY: "5", markerWidth: "7", markerHeight: "7", orient: "auto-start-reverse", children: _jsx("path", { d: "M 0 0 L 10 5 L 0 10 z" }) }, tone))) }), drawn.paths.map((path) => (_jsx("path", { d: path.d, "data-line": path.line, "data-tone": path.tone, markerEnd: path.arrow ? `url(#${marker}-${path.tone ?? "neutral"})` : undefined }, path.key))), drawn.labels.map((item) => (_jsx("text", { x: item.x, y: item.y, className: "diagram-edge-label", children: item.text }, item.key)))] })] }) }) }) }));
|
|
115
140
|
}
|
|
116
141
|
function DiagramColumn({ className, ...props }) {
|
|
117
142
|
return _jsx("div", { ...props, "data-slot": "diagram-column", className: cn("diagram-column", className) });
|
|
118
143
|
}
|
|
144
|
+
/* A group draws several nodes as one card, split by a rule, such as the
|
|
145
|
+
container that receives requests with the other containers under it.
|
|
146
|
+
Edges still reach each node on its own. The group carries the frame, so
|
|
147
|
+
its frame props replace the nodes' own. */
|
|
148
|
+
function DiagramNodeGroup({ borderStyle, cornerStyle, cornerSize, cornerMarks, className, ...props }) {
|
|
149
|
+
return (_jsx(Card, { ...props, "data-slot": "diagram-node-group", className: cn("diagram-node-group", className), "data-border-style": borderStyle, "data-corner-style": cornerStyle, "data-corner-size": cornerSize, "data-corner-marks": cornerMarks }));
|
|
150
|
+
}
|
|
119
151
|
function DiagramNode({ id, title, kind, icon, label, accent, status, borderStyle, cornerStyle, cornerSize, cornerMarks, className, children, ...props }) {
|
|
120
152
|
const { register } = useRegistry("DiagramNode");
|
|
121
153
|
const ref = React.useCallback((element) => register(id, element), [id, register]);
|
|
122
154
|
const spec = kind ? kinds[kind] : undefined;
|
|
123
155
|
const Icon = icon ?? spec?.icon;
|
|
124
156
|
const caption = label ?? spec?.label;
|
|
125
|
-
|
|
157
|
+
const glyph = Icon && _jsx(Icon, { className: "icon icon-sm", strokeWidth: 1.75, "aria-hidden": "true", focusable: "false" });
|
|
158
|
+
const items = React.Children.toArray(children);
|
|
159
|
+
const text = items.filter((item) => typeof item === "string" || typeof item === "number");
|
|
160
|
+
const parts = items.filter((item) => typeof item !== "string" && typeof item !== "number");
|
|
161
|
+
return (_jsxs(Card, { ...props, ref: ref, "data-slot": "diagram-node", className: cn("diagram-node", className), "data-kind": kind, "data-tone": spec?.tone ?? "neutral", "data-accent": accent || undefined, "data-status": status, "data-border-style": borderStyle, "data-corner-style": cornerStyle, "data-corner-size": cornerSize, "data-corner-marks": cornerMarks, children: [caption && (_jsxs("span", { className: "diagram-node-kind", children: [glyph, _jsx("span", { className: "t-caps", children: caption })] })), title != null && (_jsxs("span", { className: "diagram-node-title", "data-lead": (!caption && !!glyph) || undefined, children: [!caption && glyph, title] })), text.length > 0 && _jsx("span", { className: "diagram-node-text mono", children: text }), status && (_jsx(StatusBadge, { tone: statuses[status].tone, className: "diagram-node-status", children: statuses[status].label })), parts] }));
|
|
162
|
+
}
|
|
163
|
+
/* One fact about a node as a line: its icon and name, the value on the
|
|
164
|
+
right. Long values wrap under the name before they widen the node. */
|
|
165
|
+
function DiagramFact({ icon: Icon, label, empty, className, children, ...props }) {
|
|
166
|
+
const value = children ?? null;
|
|
167
|
+
return (_jsxs("div", { ...props, "data-slot": "diagram-fact", className: cn("diagram-fact", className), children: [_jsxs("span", { className: "diagram-fact-label", children: [Icon && _jsx(Icon, { className: "icon icon-sm", strokeWidth: 1.75, "aria-hidden": "true", focusable: "false" }), label] }), value != null && value !== false
|
|
168
|
+
? _jsx("span", { className: "diagram-fact-value mono", children: value })
|
|
169
|
+
: _jsx("span", { className: "diagram-fact-value muted", children: empty })] }));
|
|
170
|
+
}
|
|
171
|
+
/* A part of a node that folds under a header. Folded, its body stays in the
|
|
172
|
+
layout with no height, so the node keeps its width and the edges do not
|
|
173
|
+
move sideways. */
|
|
174
|
+
function DiagramFold({ title, count, defaultOpen = true, className, children, ...props }) {
|
|
175
|
+
const [open, setOpen] = React.useState(defaultOpen);
|
|
176
|
+
const body = React.useId();
|
|
177
|
+
const Chevron = open ? ChevronUp : ChevronDown;
|
|
178
|
+
return (_jsxs("div", { ...props, "data-slot": "diagram-fold", className: cn("diagram-fold", className), "data-open": open, children: [_jsxs("button", { type: "button", className: "diagram-fold-head", "aria-expanded": open, "aria-controls": body, onClick: () => setOpen(!open), children: [_jsxs("span", { className: "diagram-fold-title", children: [title, count != null && _jsx(Badge, { className: "badge-count", children: count })] }), _jsx(Chevron, { className: "icon icon-sm", strokeWidth: 1.75, "aria-hidden": "true", focusable: "false" })] }), _jsx("div", { id: body, className: "diagram-fold-body", children: children })] }));
|
|
179
|
+
}
|
|
180
|
+
/* Chips that show one kind of line at a time. The first chip shows all of
|
|
181
|
+
them with their total. Render it only when there is more than one kind. */
|
|
182
|
+
function DiagramFilter({ options, value, onValueChange, className, ...props }) {
|
|
183
|
+
const total = options.reduce((sum, option) => sum + option.count, 0);
|
|
184
|
+
return (_jsxs("div", { role: "group", "aria-label": "Show", ...props, "data-slot": "diagram-filter", className: cn("diagram-filter", className), children: [_jsxs("button", { type: "button", "aria-pressed": value === null, onClick: () => onValueChange(null), children: ["All ", total] }), options.map((option) => (_jsxs("button", { type: "button", "aria-pressed": value === option.value, onClick: () => onValueChange(option.value), children: [option.label, " ", option.count] }, option.value)))] }));
|
|
185
|
+
}
|
|
186
|
+
/* One kind of thing a node lists: a heading with its count and action, and
|
|
187
|
+
its lines hanging from the heading as a tree. */
|
|
188
|
+
function DiagramSection({ title, icon: Icon, count, action, className, children, ...props }) {
|
|
189
|
+
return (_jsxs("div", { ...props, "data-slot": "diagram-section", className: cn("diagram-section", className), "data-flat": title == null || undefined, children: [title != null && (_jsxs("div", { className: "diagram-section-head", children: [_jsxs("span", { className: "diagram-section-title", children: [Icon && _jsx(Icon, { className: "icon icon-sm", strokeWidth: 1.75, "aria-hidden": "true", focusable: "false" }), title, count != null && _jsx(Badge, { className: "badge-count", children: count })] }), action] })), children] }));
|
|
190
|
+
}
|
|
191
|
+
/* One line of a section: a name in mono and, on the right, where it goes. */
|
|
192
|
+
function DiagramLine({ end, onClick, className, children, ...props }) {
|
|
193
|
+
const content = _jsxs(_Fragment, { children: [_jsx("span", { className: "diagram-line-name mono", children: children }), end != null && _jsx("span", { className: "diagram-line-end", children: end })] });
|
|
194
|
+
if (onClick) {
|
|
195
|
+
return (_jsx("button", { ...props, type: "button", "data-slot": "diagram-line", className: cn("diagram-line", className), onClick: onClick, children: content }));
|
|
196
|
+
}
|
|
197
|
+
return _jsx("div", { ...props, "data-slot": "diagram-line", className: cn("diagram-line", className), children: content });
|
|
126
198
|
}
|
|
127
199
|
/* An edge renders nothing itself: it registers with the Diagram, which draws
|
|
128
200
|
it once the nodes are measured. */
|
|
@@ -133,4 +205,4 @@ function DiagramEdge({ from, to, label, line, tone }) {
|
|
|
133
205
|
React.useEffect(() => addEdge({ key: `${sources}->${to}`, from: sources.split("\n"), to, label, line, tone }), [addEdge, sources, to, label, line, tone]);
|
|
134
206
|
return null;
|
|
135
207
|
}
|
|
136
|
-
export { Diagram, DiagramColumn, DiagramNode, DiagramEdge };
|
|
208
|
+
export { Diagram, DiagramColumn, DiagramNodeGroup, DiagramNode, DiagramFact, DiagramFold, DiagramFilter, DiagramSection, DiagramLine, DiagramEdge };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@momoi-labs/kiso-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Kiso React components built with shadcn/ui and Radix",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"prepack": "npm run build"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@momoi-labs/kiso": "^0.
|
|
35
|
+
"@momoi-labs/kiso": "^0.19.0",
|
|
36
36
|
"class-variance-authority": "0.7.1",
|
|
37
37
|
"clsx": "2.1.1",
|
|
38
38
|
"radix-ui": "1.6.7",
|