@slashsuite/ui-react 0.13.2 → 0.14.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/MIGRATING.md +8 -0
- package/agent-docs/components/state-machine.md +13 -9
- package/dist/FlowDiagram.d.ts +15 -4
- package/dist/FlowDiagram.js +24 -29
- package/dist/StateMachine.d.ts +28 -7
- package/dist/StateMachine.js +47 -24
- package/dist/flowBox.d.ts +15 -0
- package/dist/flowBox.js +33 -0
- package/dist/flowLayout.d.ts +7 -1
- package/dist/flowLayout.js +5 -4
- package/dist/fromXState.d.ts +1 -1
- package/dist/fromXState.js +8 -2
- package/dist/stateMachineDots.d.ts +8 -3
- package/dist/stateMachineDots.js +8 -3
- package/dist/stateMachineRoles.d.ts +11 -0
- package/dist/stateMachineRoles.js +36 -0
- package/dist/useFlowLayout.d.ts +18 -3
- package/dist/useFlowLayout.js +35 -6
- package/package.json +1 -1
package/MIGRATING.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Migrating @slashsuite/ui-react
|
|
2
2
|
|
|
3
|
+
## 0.13 to 0.14: StateMachine roles have colours
|
|
4
|
+
|
|
5
|
+
No code has to change. `StateMachine` now gives each role (a state's `owner`, or a transition's new
|
|
6
|
+
`actor`) its own colour, on the state's role tag, the box's top edge and the lines that role moves, and
|
|
7
|
+
draws a legend of the roles under the diagram. A `human` state no longer takes the warn tone: its tag and
|
|
8
|
+
box are neutral, and the role colour says who acts. To keep a role on one colour across diagrams, pin it
|
|
9
|
+
with `roleTones` (`roleTones={{ owner: "cat2" }}`).
|
|
10
|
+
|
|
3
11
|
## @hackcove is now @slashsuite: the npm scope moves
|
|
4
12
|
|
|
5
13
|
If the app is still on 0.10, run the codemod (below, "Running the codemod") first: the 0.10 to 0.11
|
|
@@ -5,7 +5,7 @@ Group: Data
|
|
|
5
5
|
When to use: The states a thing goes through and the events that move it, with where each thing is now: tickets in a workflow, orders in fulfilment, jobs in a queue.
|
|
6
6
|
|
|
7
7
|
### The one right way
|
|
8
|
-
One state waits on a person, and only it is `human`:
|
|
8
|
+
One state waits on a person, and only it is `human`: its tag says so, and its owner's colour says who.
|
|
9
9
|
```tsx
|
|
10
10
|
import { StateMachine } from "@slashsuite/ui-react";
|
|
11
11
|
|
|
@@ -26,9 +26,10 @@ import { StateMachine } from "@slashsuite/ui-react";
|
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
### Do
|
|
29
|
-
- Give it `states` (`{ id, label, kind, owner }`), `transitions` (`{ from, to, event }`) and `items` (`{ id, label, state, href }`), plain data with no layout: it is drawn like a `FlowDiagram`.
|
|
30
|
-
- `kind` says what a state is: `human` for a state waiting on a person
|
|
31
|
-
- `owner` is the role
|
|
29
|
+
- Give it `states` (`{ id, label, kind, owner }`), `transitions` (`{ from, to, event, actor }`) and `items` (`{ id, label, state, href }`), plain data with no layout: it is drawn like a `FlowDiagram`.
|
|
30
|
+
- `kind` says what a state is: `human` for a state waiting on a person, `failed` for an error (danger), `terminal` for done (muted), `initial` for the start. Each is a tag in words. Leave the rest `normal`.
|
|
31
|
+
- `owner` is the role that acts in a state, and a transition's `actor` the role that makes a move its from-state's owner does not. Each role has its own colour, the same for the same machine: on its tag, its boxes, its arrows and the legend. `roleTones` pins one.
|
|
32
|
+
- Keep a state's `label` short: every box is drawn one size, so the boxes line up, and a long label wraps inside it and makes every box a line taller.
|
|
32
33
|
- An item's `id` is its short ref, shown by its dot; its `label` is the full name, its tooltip and what a screen reader says. Give `href` and the dot is a link.
|
|
33
34
|
- Change an item's `state` and its dot runs along the arrow to its new box; there is nothing to call.
|
|
34
35
|
- `active` rings the one state that is current, for a machine with one thing in it.
|
|
@@ -37,8 +38,9 @@ import { StateMachine } from "@slashsuite/ui-react";
|
|
|
37
38
|
### Don't
|
|
38
39
|
- Do not leave an item out because its state is unknown: it goes in the "Not in any state" box, where someone sees it.
|
|
39
40
|
- Do not mark every state `human` or `failed`: the one that needs a person no longer stands out.
|
|
41
|
+
- Do not give a machine more than eight roles: the colours repeat past the eighth.
|
|
40
42
|
|
|
41
|
-
Every state marked `human`:
|
|
43
|
+
Every state marked `human`: the tags say nothing.
|
|
42
44
|
```tsx
|
|
43
45
|
import { StateMachine } from "@slashsuite/ui-react";
|
|
44
46
|
|
|
@@ -60,13 +62,13 @@ import { StateMachine } from "@slashsuite/ui-react";
|
|
|
60
62
|
|
|
61
63
|
### Accessibility
|
|
62
64
|
- The drawing is an image named by `label`, with the count of states and transitions; the box round it is a group named by `label`, and takes the keyboard focus so a machine that scrolls can be scrolled with the arrow keys.
|
|
63
|
-
- Each dot is named by its item's label and its state (`Fix the login, in Review`), and takes the focus; one with `href` is a link. A box with more than
|
|
64
|
-
- A visually hidden list after it carries every state, its tags and the transitions out of it, as `FlowDiagram`'s does.
|
|
65
|
+
- Each dot is named by its item's label and its state (`Fix the login, in Review`), and takes the focus; one with `href` is a link. A box with more than six shows a `+N` button that shows the rest.
|
|
66
|
+
- A visually hidden list after it carries every state, its tags and the transitions out of it, as `FlowDiagram`'s does. A role is always named in words, on its tag and in the legend, never by colour alone.
|
|
65
67
|
- With reduced motion a dot moves to its new state at once, with no run.
|
|
66
68
|
|
|
67
69
|
### Props
|
|
68
70
|
### StateMachine
|
|
69
|
-
A state machine, drawn, with the things moving through it: each state a box, each transition an arrow named by its event, laid out like a `FlowDiagram`. A state's `kind`
|
|
71
|
+
A state machine, drawn, with the things moving through it: each state a box, each transition an arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` is a tag that says it: `human`, `failed` (which also takes the danger tone), `terminal` (muted) and `initial` (start). Every box is one size, the label wrapping inside it, and the boxes sit close. Each role gets its own colour from the categorical scale (`cat1` to `cat8`, remapped by every theme): a state's `owner` is a tag with its role's colour, and a bar of it across the box's top. A transition's arrow is drawn in its `actor`'s colour, else in the colour of the role that owns the state it leaves. The roles take the colours in order, the states' owners first, so the same machine always gets the same colours; `roleTones` pins a role to one. A legend under the machine names each role and its colour. Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its dot runs along the arrow between the two (straight across when there is none); with reduced motion it moves at once. `active` rings the one state that is current. `fromXState` makes `states` and `transitions` from an xstate machine.
|
|
70
72
|
- active: string. The `id` of the one state to ring as current.
|
|
71
73
|
- id: string. The root element's `id`: an anchor, or the target of a `for` or an `aria-describedby`.
|
|
72
74
|
- items: readonly StateMachineItem[] (default []). The things in the machine, each in the state its `state` names.
|
|
@@ -74,6 +76,8 @@ A state machine, drawn, with the things moving through it: each state a box, eac
|
|
|
74
76
|
- loading: boolean. The data is not here yet: an empty slot draws a bone of its shape, a filled one draws as usual.
|
|
75
77
|
Left out, the part takes the nearest loading layout's or part's; `false` opts it out inside one.
|
|
76
78
|
- loadingLabel: string (default Loading). What a screen reader hears while this part is loading, when its own `loading` is set.
|
|
79
|
+
- roleTones: Readonly<Record<string, CategoryTone>>. Pins a role to a colour (`{ owner: "cat2" }`). A role left out takes the next colour in order
|
|
80
|
+
that no role is pinned to.
|
|
77
81
|
- states: readonly StateMachineState[] (required). The states, each with a unique `id`.
|
|
78
82
|
- transitions: readonly StateMachineTransition[] (required). The moves between them. One whose `from` or `to` is not a state is not drawn.
|
|
79
83
|
|
|
@@ -81,7 +85,7 @@ Left out, the part takes the nearest loading layout's or part's; `false` opts it
|
|
|
81
85
|
- StateMachineDefinition: `{ states: readonly StateMachineState[]; transitions: readonly StateMachineTransition[] }`. The machine: its states and the transitions between them. `fromXState` makes one from an xstate machine.
|
|
82
86
|
- StateMachineState: `{ id: string; label: string; kind?: StateMachineKind; owner?: string }`. One state: its `id` (what a transition's `from` and `to` and an item's `state` name), its `label`, its `kind`, and the role that `owner`s it.
|
|
83
87
|
- StateMachineKind: `"initial" | "normal" | "human" | "terminal" | "failed"`. What a state is: `initial` (where things start), `normal`, `human` (waiting on a person), `terminal` (done, nothing leaves it) or `failed` (stopped on an error).
|
|
84
|
-
- StateMachineTransition: `{ from: string; to: string; event?: string }`. One move between states, with the `event` that makes it, shown on the arrow.
|
|
88
|
+
- StateMachineTransition: `{ from: string; to: string; event?: string; actor?: string }`. One move between states, with the `event` that makes it, shown on the arrow, and the `actor`, the role that makes the move, whose colour the arrow is drawn in.
|
|
85
89
|
- StateMachineItem: `{ id: string; label: string; state: string; href?: string }`. A thing in the machine: its short `id` (a ref, shown by its dot), its full `label` (the dot's name and tooltip), the `state` it is in, and an optional `href` its dot links to.
|
|
86
90
|
- XStateMachineLike: `{ id?: string | undefined; initial?: string | { target: string; } | undefined; states?: Record<string, XStateNodeConfig> | undefined; } | { config: { id?: string | undefined; initial?: string | { target: string; } | undefined; states?: Record<string, XStateNodeConfig> | undefined; }; }`. A machine's config, or what `setup(…).createMachine(config)` returns (it keeps it as `config`).
|
|
87
91
|
- XStateNodeConfig: `{ id?: string; type?: string; meta?: Record<string, unknown>; on?: Record<string, XStateTransitionConfig | readonly XStateTransitionConfig[]>; always?: XStateTransitionConfig | readonly XStateTransitionConfig[]; after?: Record<string, XStateTransitionConfig | readonly XStateTransitionConfig[]>; invoke?: { onDone?: XStateTransitionConfig | readonly XStateTransitionConfig[]; onError?: XStateTransitionConfig | readonly XStateTransitionConfig[] } | readonly { onDone?: XStateTransitionConfig | readonly XStateTransitionConfig[]; onError?: XStateTransitionConfig | readonly XStateTransitionConfig[] }[]; states?: Record<string, XStateNodeConfig> }`. The part of an xstate v5 state node `fromXState` reads: its `id`, `type` and `meta`, and the ways it moves on (`on`, `always`, `after`, and each `invoke`'s `onDone` and `onError`). Typed by shape, so the package does not depend on xstate.
|
package/dist/FlowDiagram.d.ts
CHANGED
|
@@ -67,9 +67,17 @@ export declare function FlowDiagram({ label, nodes, edges, direction, active, ov
|
|
|
67
67
|
} & LoadingProps & Passthrough): ReactElement;
|
|
68
68
|
/** A box's tone in the internal view: the public tones, and `muted` for a box that is over. */
|
|
69
69
|
export type ViewTone = Tone | "muted";
|
|
70
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* A box of the internal view: a `FlowNode` that may also be `muted`, and may carry an `accent`, a
|
|
72
|
+
* text colour utility painted as a bar across its top (a `StateMachine` state's role).
|
|
73
|
+
*/
|
|
71
74
|
export type ViewNode = Omit<FlowNode, "tone"> & {
|
|
72
75
|
tone?: ViewTone | undefined;
|
|
76
|
+
accent?: string | undefined;
|
|
77
|
+
};
|
|
78
|
+
/** An arrow of the internal view: a `FlowEdge` that may carry a `colour`, the text colour utility its line and head are drawn in. */
|
|
79
|
+
export type ViewEdge = FlowEdge & {
|
|
80
|
+
colour?: string | undefined;
|
|
73
81
|
};
|
|
74
82
|
/**
|
|
75
83
|
* The laid-out diagram, drawn: `FlowDiagram` without its loading bone, and with `layer`, which
|
|
@@ -77,16 +85,19 @@ export type ViewNode = Omit<FlowNode, "tone"> & {
|
|
|
77
85
|
* `canvas` ref and the layout `drawn`. Unlike `overlay`, the layer is read by a screen reader and its
|
|
78
86
|
* children may take the pointer (`pointer-events-auto`), so they can be links. With a layer, the
|
|
79
87
|
* image is the drawing alone and the scrolling box round it is a group named by `label`. With `fit`,
|
|
80
|
-
* a drawing wider than its room is shrunk to the room's width instead of scrolling.
|
|
88
|
+
* a drawing wider than its room is shrunk to the room's width instead of scrolling. With `uniform`
|
|
89
|
+
* (laid out by `useFlowLayout`'s `uniform`), every box fills its slot: its label wraps to the box's
|
|
90
|
+
* width and its content sits at the bottom. Internal.
|
|
81
91
|
*/
|
|
82
|
-
export declare function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit, ...rest }: {
|
|
92
|
+
export declare function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit, uniform, ...rest }: {
|
|
83
93
|
label: string;
|
|
84
94
|
nodes: readonly ViewNode[];
|
|
85
|
-
edges: readonly
|
|
95
|
+
edges: readonly ViewEdge[];
|
|
86
96
|
active: string | undefined;
|
|
87
97
|
overlay?: ((layout: FlowLayout) => ReactNode) | undefined;
|
|
88
98
|
layer?: ((layout: FlowLayout) => ReactNode) | undefined;
|
|
89
99
|
canvas: ReturnType<typeof useFlowLayout>["canvas"];
|
|
90
100
|
drawn: FlowLayout;
|
|
91
101
|
fit?: boolean | undefined;
|
|
102
|
+
uniform?: boolean | undefined;
|
|
92
103
|
} & Passthrough): ReactElement;
|
package/dist/FlowDiagram.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useId } from "react";
|
|
3
|
-
import {
|
|
4
|
-
import { cx } from "./cx.js";
|
|
3
|
+
import { FlowBoxView } from "./flowBox.js";
|
|
5
4
|
import { Bone, BusyLabel, busy, useLoading } from "./skeleton.js";
|
|
6
|
-
import { edgeKey,
|
|
7
|
-
/** A box's frame by tone: the Callout's soft surfaces, a white box for `neutral`, a grey one for `muted`. */
|
|
8
|
-
const NODE_TONE = {
|
|
9
|
-
...TONE_SURFACE,
|
|
10
|
-
neutral: "border-slate-300 bg-white",
|
|
11
|
-
muted: "border-slate-200 bg-slate-50",
|
|
12
|
-
};
|
|
13
|
-
/** A box's ink by tone; `neutral` is the primary ink, `muted` the quiet one. */
|
|
14
|
-
const NODE_INK = {
|
|
15
|
-
...TONE_TEXT,
|
|
16
|
-
neutral: "text-slate-900",
|
|
17
|
-
muted: "text-slate-500",
|
|
18
|
-
};
|
|
5
|
+
import { edgeKey, useFit, useFlowLayout } from "./useFlowLayout.js";
|
|
19
6
|
/**
|
|
20
7
|
* Boxes joined by arrows, laid out for you: a flow, a pipeline, a state machine. Give it the
|
|
21
8
|
* `nodes` and the `edges` between them and it places every box in ranks `direction` (`down` or
|
|
@@ -47,9 +34,11 @@ export function FlowDiagram({ label, nodes, edges, direction = "down", active, o
|
|
|
47
34
|
* `canvas` ref and the layout `drawn`. Unlike `overlay`, the layer is read by a screen reader and its
|
|
48
35
|
* children may take the pointer (`pointer-events-auto`), so they can be links. With a layer, the
|
|
49
36
|
* image is the drawing alone and the scrolling box round it is a group named by `label`. With `fit`,
|
|
50
|
-
* a drawing wider than its room is shrunk to the room's width instead of scrolling.
|
|
37
|
+
* a drawing wider than its room is shrunk to the room's width instead of scrolling. With `uniform`
|
|
38
|
+
* (laid out by `useFlowLayout`'s `uniform`), every box fills its slot: its label wraps to the box's
|
|
39
|
+
* width and its content sits at the bottom. Internal.
|
|
51
40
|
*/
|
|
52
|
-
export function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit = false, ...rest }) {
|
|
41
|
+
export function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit = false, uniform = false, ...rest }) {
|
|
53
42
|
const { frame, scale } = useFit(fit ? drawn.width : 0);
|
|
54
43
|
const known = new Set(nodes.map((n) => n.id));
|
|
55
44
|
const shown = edges.flatMap((edge, index) => known.has(edge.from) && known.has(edge.to) ? [{ edge, index }] : []);
|
|
@@ -57,7 +46,7 @@ export function FlowView({ label, nodes, edges, active, overlay, layer, canvas,
|
|
|
57
46
|
role: "img",
|
|
58
47
|
"aria-label": `${label}: ${count(known.size, "box", "boxes")} and ${count(shown.length, "arrow", "arrows")}`,
|
|
59
48
|
};
|
|
60
|
-
const drawing = _jsx(FlowCanvas, { drawn: drawn, nodes: nodes, edges: shown, active: active });
|
|
49
|
+
const drawing = (_jsx(FlowCanvas, { drawn: drawn, nodes: nodes, edges: shown, active: active, uniform: uniform }));
|
|
61
50
|
return (_jsxs("div", { ...rest, className: "flex min-w-0 flex-col", children: [_jsx("div", { ...(layer ? { role: "group", "aria-label": label } : image),
|
|
62
51
|
// A diagram wider than its room scrolls, so it takes the focus for the arrow keys.
|
|
63
52
|
tabIndex: 0, ref: frame, className: "max-w-full overflow-auto rounded-lg outline-offset-2 focus-visible:outline-2 focus-visible:outline-violet-600", children: _jsx("div", { className: "mx-auto", style: { width: drawn.width * scale, height: drawn.height * scale }, children: _jsxs("div", { ref: canvas, className: "relative origin-top-left", style: {
|
|
@@ -69,26 +58,32 @@ export function FlowView({ label, nodes, edges, active, overlay, layer, canvas,
|
|
|
69
58
|
/** `1 box`, `3 boxes`. */
|
|
70
59
|
const count = (n, one, many) => `${n} ${n === 1 ? one : many}`;
|
|
71
60
|
/** The drawing itself: the group frames, the arrows, their labels and the boxes. */
|
|
72
|
-
function FlowCanvas({ drawn, nodes, edges, active, }) {
|
|
61
|
+
function FlowCanvas({ drawn, nodes, edges, active, uniform, }) {
|
|
73
62
|
const marker = `flow-arrow-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
|
|
74
63
|
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
75
64
|
const paths = new Map(drawn.edges.map((e) => [e.index, e]));
|
|
76
|
-
return (_jsxs(_Fragment, { children: [drawn.groups.map((g) => (_jsx("div", { className: "absolute rounded-xl border border-dashed border-slate-300", style: { left: g.x, top: g.y, width: g.width, height: g.height }, children: _jsx("span", { className: "absolute left-3 top-1 text-xs font-medium text-slate-500", children: g.name }) }, g.name))), _jsx(FlowArrows, { drawn: drawn, marker: marker }), edges.map(({ edge, index }) => {
|
|
65
|
+
return (_jsxs(_Fragment, { children: [drawn.groups.map((g) => (_jsx("div", { className: "absolute rounded-xl border border-dashed border-slate-300", style: { left: g.x, top: g.y, width: g.width, height: g.height }, children: _jsx("span", { className: "absolute left-3 top-1 text-xs font-medium text-slate-500", children: g.name }) }, g.name))), _jsx(FlowArrows, { drawn: drawn, marker: marker, colours: new Map(edges.map(({ edge, index }) => [index, edge.colour])) }), edges.map(({ edge, index }) => {
|
|
77
66
|
const at = paths.get(index)?.labelAt;
|
|
78
67
|
return edge.label && at ? (_jsx("span", { "data-measure": edgeKey(index), className: "absolute -translate-x-1/2 -translate-y-1/2 whitespace-nowrap rounded bg-white px-1 text-xs text-slate-600", style: { left: at.x, top: at.y }, children: edge.label }, index)) : null;
|
|
79
68
|
}), drawn.nodes.map((b) => {
|
|
80
69
|
const node = byId.get(b.id);
|
|
81
|
-
return node ? (_jsx(FlowBoxView, { box: b, node: node, active: active === b.id }, b.id)) : null;
|
|
70
|
+
return node ? (_jsx(FlowBoxView, { box: b, node: node, active: active === b.id, uniform: uniform }, b.id)) : null;
|
|
82
71
|
})] }));
|
|
83
72
|
}
|
|
84
|
-
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
73
|
+
/** The default arrow colour, for an arrow with no `colour`. */
|
|
74
|
+
const ARROW = "text-slate-400";
|
|
75
|
+
/**
|
|
76
|
+
* Every arrow as an SVG path, with its arrowhead, each in its `colours` entry or the default grey.
|
|
77
|
+
* A marker's fill follows the colour where the marker is defined, not the path that uses it, so
|
|
78
|
+
* each colour gets its own arrowhead.
|
|
79
|
+
*/
|
|
80
|
+
function FlowArrows({ drawn, marker, colours, }) {
|
|
81
|
+
const used = [...new Set([ARROW, ...colours.values()].filter((c) => c !== undefined))];
|
|
82
|
+
const headOf = (colour) => `${marker}-${used.indexOf(colour)}`;
|
|
83
|
+
return (_jsxs("svg", { width: drawn.width, height: drawn.height, className: "absolute inset-0 overflow-visible", children: [_jsx("defs", { children: used.map((colour) => (_jsx("marker", { id: headOf(colour), className: colour, viewBox: "0 0 10 10", refX: "9", refY: "5", markerWidth: "8", markerHeight: "8", markerUnits: "userSpaceOnUse", orient: "auto-start-reverse", children: _jsx("path", { d: "M0,0L10,5L0,10z", fill: "currentColor" }) }, colour))) }), drawn.edges.map((e) => {
|
|
84
|
+
const colour = colours.get(e.index) ?? ARROW;
|
|
85
|
+
return (_jsx("path", { d: e.path, className: colour, fill: "none", stroke: "currentColor", strokeWidth: 1.5, markerEnd: `url(#${headOf(colour)})` }, e.index));
|
|
86
|
+
})] }));
|
|
92
87
|
}
|
|
93
88
|
/** The diagram as text for a screen reader: each box, its content, and the arrows out of it. */
|
|
94
89
|
function FlowList({ label, nodes, edges, active, }) {
|
package/dist/StateMachine.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ReactElement } from "react";
|
|
2
2
|
import type { Passthrough } from "./props.js";
|
|
3
3
|
import { type LoadingProps } from "./skeleton.js";
|
|
4
|
+
import { type CategoryTone } from "./StackedBar.js";
|
|
4
5
|
/**
|
|
5
6
|
* What a state is: `initial` (where things start), `normal`, `human` (waiting on a person),
|
|
6
7
|
* `terminal` (done, nothing leaves it) or `failed` (stopped on an error).
|
|
@@ -12,14 +13,22 @@ export type StateMachineState = {
|
|
|
12
13
|
label: string;
|
|
13
14
|
/** @default normal */
|
|
14
15
|
kind?: StateMachineKind | undefined;
|
|
15
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* The role or lane that acts in this state (`reviewer`, `billing`): a tag on it in the role's
|
|
18
|
+
* colour, and a bar of that colour across the box's top.
|
|
19
|
+
*/
|
|
16
20
|
owner?: string | undefined;
|
|
17
21
|
};
|
|
18
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* One move between states, with the `event` that makes it, shown on the arrow, and the `actor`,
|
|
24
|
+
* the role that makes the move, whose colour the arrow is drawn in.
|
|
25
|
+
*/
|
|
19
26
|
export type StateMachineTransition = {
|
|
20
27
|
from: string;
|
|
21
28
|
to: string;
|
|
22
29
|
event?: string | undefined;
|
|
30
|
+
/** The role that makes this move. Left out, it is the `owner` of the `from` state. */
|
|
31
|
+
actor?: string | undefined;
|
|
23
32
|
};
|
|
24
33
|
/** The machine: its states and the transitions between them. `fromXState` makes one from an xstate machine. */
|
|
25
34
|
export type StateMachineDefinition = {
|
|
@@ -38,12 +47,19 @@ export type StateMachineItem = {
|
|
|
38
47
|
};
|
|
39
48
|
/**
|
|
40
49
|
* A state machine, drawn, with the things moving through it: each state a box, each transition an
|
|
41
|
-
* arrow named by its event, laid out like a `FlowDiagram`. A state's `kind`
|
|
42
|
-
*
|
|
43
|
-
*
|
|
50
|
+
* arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` is a tag that says it:
|
|
51
|
+
* `human`, `failed` (which also takes the danger tone), `terminal` (muted) and `initial` (start).
|
|
52
|
+
* Every box is one size, the label wrapping inside it, and the boxes sit close.
|
|
53
|
+
*
|
|
54
|
+
* Each role gets its own colour from the categorical scale (`cat1` to `cat8`, remapped by every
|
|
55
|
+
* theme): a state's `owner` is a tag with its role's colour, and a bar of it across the box's top. A
|
|
56
|
+
* transition's arrow is drawn in its `actor`'s colour, else in the colour of the role that owns the
|
|
57
|
+
* state it leaves. The roles take the colours in order, the states' owners first, so the same
|
|
58
|
+
* machine always gets the same colours; `roleTones` pins a role to one. A legend under the machine
|
|
59
|
+
* names each role and its colour.
|
|
44
60
|
*
|
|
45
61
|
* Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
|
|
46
|
-
* counts them; a box shows up to
|
|
62
|
+
* counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
|
|
47
63
|
* full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
|
|
48
64
|
* a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
|
|
49
65
|
* dot runs along the arrow between the two (straight across when there is none); with reduced
|
|
@@ -51,7 +67,7 @@ export type StateMachineItem = {
|
|
|
51
67
|
*
|
|
52
68
|
* `fromXState` makes `states` and `transitions` from an xstate machine.
|
|
53
69
|
*/
|
|
54
|
-
export declare function StateMachine({ label, states, transitions, items, active, loading, loadingLabel, ...rest }: {
|
|
70
|
+
export declare function StateMachine({ label, states, transitions, items, active, roleTones: pinned, loading, loadingLabel, ...rest }: {
|
|
55
71
|
/** What the machine is: its accessible name. */
|
|
56
72
|
label: string;
|
|
57
73
|
/** The states, each with a unique `id`. */
|
|
@@ -62,4 +78,9 @@ export declare function StateMachine({ label, states, transitions, items, active
|
|
|
62
78
|
items?: readonly StateMachineItem[] | undefined;
|
|
63
79
|
/** The `id` of the one state to ring as current. */
|
|
64
80
|
active?: string | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Pins a role to a colour (`{ owner: "cat2" }`). A role left out takes the next colour in order
|
|
83
|
+
* that no role is pinned to.
|
|
84
|
+
*/
|
|
85
|
+
roleTones?: Readonly<Record<string, CategoryTone>> | undefined;
|
|
65
86
|
} & LoadingProps & Passthrough): ReactElement;
|
package/dist/StateMachine.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useState } from "react";
|
|
3
|
+
import { cx } from "./cx.js";
|
|
3
4
|
import { FlowView } from "./FlowDiagram.js";
|
|
4
5
|
import { Pill } from "./Pill.js";
|
|
5
6
|
import { Bone, BusyLabel, busy, useLoading } from "./skeleton.js";
|
|
6
|
-
import {
|
|
7
|
+
import { Legend, VIZ_TONE } from "./StackedBar.js";
|
|
8
|
+
import { moverOf, toneByRole } from "./stateMachineRoles.js";
|
|
9
|
+
import { boxContentSize, MACHINE_SPACING, dotArea, dotLayer, placeOf, slotsFor, UNPLACED, useMoves, } from "./stateMachineDots.js";
|
|
7
10
|
import { useFlowLayout } from "./useFlowLayout.js";
|
|
8
11
|
/** Each kind's box tone, and the tag that says it in words. */
|
|
9
12
|
const KIND_TONE = {
|
|
10
13
|
initial: "neutral",
|
|
11
14
|
normal: "neutral",
|
|
12
|
-
human: "
|
|
15
|
+
human: "neutral",
|
|
13
16
|
terminal: "muted",
|
|
14
17
|
failed: "danger",
|
|
15
18
|
};
|
|
16
19
|
const KIND_TAG = {
|
|
17
20
|
initial: { text: "start", tone: "neutral" },
|
|
18
21
|
normal: null,
|
|
19
|
-
human: { text: "human", tone: "
|
|
22
|
+
human: { text: "human", tone: "neutral" },
|
|
20
23
|
terminal: { text: "end", tone: "neutral" },
|
|
21
24
|
failed: { text: "failed", tone: "danger" },
|
|
22
25
|
};
|
|
@@ -26,14 +29,15 @@ const KIND_TAG = {
|
|
|
26
29
|
* one are the same height.
|
|
27
30
|
*/
|
|
28
31
|
function boxTags(tag, owner, count) {
|
|
29
|
-
return (_jsxs("span", { className: "flex flex-wrap items-center justify-center gap-1", children: [tag && (_jsx(Pill, { size: "sm", tone: tag.tone, children: tag.text })), owner && _jsx(Pill, { size: "sm", children: owner }), count > 0 ? (_jsx(Pill, { size: "sm", tone: "accent", dot: true, children: count === 1 ? "1 item" : `${count} items` })) : (_jsx("span", { "aria-hidden": "true", className: "flex invisible", children: _jsx(Pill, { size: "sm", tone: "accent", dot: true, children: "0 items" }) }))] }));
|
|
32
|
+
return (_jsxs("span", { className: "flex flex-wrap items-center justify-center gap-1", children: [tag && (_jsx(Pill, { size: "sm", tone: tag.tone, children: tag.text })), owner && (_jsx(Pill, { size: "sm", children: _jsxs("span", { className: "inline-flex items-center gap-1", children: [_jsx("span", { "aria-hidden": "true", className: cx("size-2 shrink-0 rounded-full bg-current", VIZ_TONE[owner.tone]) }), owner.role] }) })), count > 0 ? (_jsx(Pill, { size: "sm", tone: "accent", dot: true, children: count === 1 ? "1 item" : `${count} items` })) : (_jsx("span", { "aria-hidden": "true", className: "flex invisible", children: _jsx(Pill, { size: "sm", tone: "accent", dot: true, children: "0 items" }) }))] }));
|
|
30
33
|
}
|
|
31
34
|
/**
|
|
32
35
|
* What a box holds under its label: its tags, and the dot area it always reserves (the full grid,
|
|
33
36
|
* even empty), so a box keeps its size whatever its item count.
|
|
34
37
|
*/
|
|
35
|
-
function boxContent(state, count, slots) {
|
|
36
|
-
const
|
|
38
|
+
function boxContent(state, tone, count, slots) {
|
|
39
|
+
const owner = state?.owner && tone ? { role: state.owner, tone } : undefined;
|
|
40
|
+
const tags = boxTags(state ? KIND_TAG[state.kind ?? "normal"] : null, owner, count);
|
|
37
41
|
return (_jsxs(_Fragment, { children: [tags, _jsx("span", { "aria-hidden": "true", className: "mx-auto mt-1 block", style: dotArea(slots) })] }));
|
|
38
42
|
}
|
|
39
43
|
/** The items each box holds: its state's, and the unplaced box's for an unknown state. */
|
|
@@ -47,15 +51,19 @@ function itemsByBox(items, known) {
|
|
|
47
51
|
return inBox;
|
|
48
52
|
}
|
|
49
53
|
/** The boxes to draw: one per state, and the unplaced box when an item is in no state. */
|
|
50
|
-
function boxesFor(states, inBox, open) {
|
|
54
|
+
function boxesFor(states, inBox, open, tones) {
|
|
51
55
|
const slots = (id) => slotsFor(inBox.get(id)?.length ?? 0, open.has(id));
|
|
52
|
-
const boxes = states.map((s) =>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const boxes = states.map((s) => {
|
|
57
|
+
const tone = s.owner ? tones.get(s.owner) : undefined;
|
|
58
|
+
return {
|
|
59
|
+
id: s.id,
|
|
60
|
+
label: s.label,
|
|
61
|
+
tone: KIND_TONE[s.kind ?? "normal"],
|
|
62
|
+
...(tone ? { accent: VIZ_TONE[tone] } : {}),
|
|
63
|
+
content: boxContent(s, tone, inBox.get(s.id)?.length ?? 0, slots(s.id)),
|
|
64
|
+
contentSize: boxContentSize(slots(s.id)),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
59
67
|
const unplaced = inBox.get(UNPLACED)?.length ?? 0;
|
|
60
68
|
if (unplaced === 0)
|
|
61
69
|
return boxes;
|
|
@@ -65,7 +73,7 @@ function boxesFor(states, inBox, open) {
|
|
|
65
73
|
id: UNPLACED,
|
|
66
74
|
label: "Not in any state",
|
|
67
75
|
tone: "warn",
|
|
68
|
-
content: boxContent(null, unplaced, slots(UNPLACED)),
|
|
76
|
+
content: boxContent(null, undefined, unplaced, slots(UNPLACED)),
|
|
69
77
|
contentSize: boxContentSize(slots(UNPLACED)),
|
|
70
78
|
},
|
|
71
79
|
];
|
|
@@ -86,12 +94,19 @@ function useOpenBoxes(opened) {
|
|
|
86
94
|
}
|
|
87
95
|
/**
|
|
88
96
|
* A state machine, drawn, with the things moving through it: each state a box, each transition an
|
|
89
|
-
* arrow named by its event, laid out like a `FlowDiagram`. A state's `kind`
|
|
90
|
-
*
|
|
91
|
-
*
|
|
97
|
+
* arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` is a tag that says it:
|
|
98
|
+
* `human`, `failed` (which also takes the danger tone), `terminal` (muted) and `initial` (start).
|
|
99
|
+
* Every box is one size, the label wrapping inside it, and the boxes sit close.
|
|
100
|
+
*
|
|
101
|
+
* Each role gets its own colour from the categorical scale (`cat1` to `cat8`, remapped by every
|
|
102
|
+
* theme): a state's `owner` is a tag with its role's colour, and a bar of it across the box's top. A
|
|
103
|
+
* transition's arrow is drawn in its `actor`'s colour, else in the colour of the role that owns the
|
|
104
|
+
* state it leaves. The roles take the colours in order, the states' owners first, so the same
|
|
105
|
+
* machine always gets the same colours; `roleTones` pins a role to one. A legend under the machine
|
|
106
|
+
* names each role and its colour.
|
|
92
107
|
*
|
|
93
108
|
* Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
|
|
94
|
-
* counts them; a box shows up to
|
|
109
|
+
* counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
|
|
95
110
|
* full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
|
|
96
111
|
* a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
|
|
97
112
|
* dot runs along the arrow between the two (straight across when there is none); with reduced
|
|
@@ -99,18 +114,26 @@ function useOpenBoxes(opened) {
|
|
|
99
114
|
*
|
|
100
115
|
* `fromXState` makes `states` and `transitions` from an xstate machine.
|
|
101
116
|
*/
|
|
102
|
-
export function StateMachine({ label, states, transitions, items = [], active, loading, loadingLabel, ...rest }) {
|
|
117
|
+
export function StateMachine({ label, states, transitions, items = [], active, roleTones: pinned, loading, loadingLabel, ...rest }) {
|
|
103
118
|
const { loading: waiting, owner } = useLoading(loading);
|
|
104
119
|
const known = new Set(states.map((s) => s.id));
|
|
105
120
|
const { moves, done, clear } = useMoves(items, known);
|
|
106
121
|
const [open, toggle] = useOpenBoxes(clear);
|
|
107
122
|
const inBox = itemsByBox(items, known);
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
const
|
|
123
|
+
const tones = toneByRole(states, transitions, pinned);
|
|
124
|
+
const nodes = boxesFor(states, inBox, open, tones);
|
|
125
|
+
const owners = new Map(states.map((s) => [s.id, s.owner]));
|
|
126
|
+
const edges = transitions.map((t) => {
|
|
127
|
+
const tone = tones.get(moverOf(t, owners) ?? "");
|
|
128
|
+
return { from: t.from, to: t.to, label: t.event, ...(tone ? { colour: VIZ_TONE[tone] } : {}) };
|
|
129
|
+
});
|
|
130
|
+
const { canvas, drawn } = useFlowLayout(nodes, edges, "down", {
|
|
131
|
+
uniform: true,
|
|
132
|
+
spacing: MACHINE_SPACING,
|
|
133
|
+
});
|
|
111
134
|
if (waiting && states.length === 0) {
|
|
112
135
|
return (_jsxs("figure", { ...rest, ...busy(owner, waiting), "aria-label": label, className: "flex flex-col", children: [_jsx(Bone, { shape: "block h-48 rounded-lg" }), _jsx(BusyLabel, { owner: owner, loading: waiting, label: loadingLabel })] }));
|
|
113
136
|
}
|
|
114
137
|
const names = new Map(nodes.map((n) => [n.id, n.label]));
|
|
115
|
-
return (
|
|
138
|
+
return (_jsxs("div", { ...rest, className: "flex min-w-0 flex-col gap-2", children: [_jsx(FlowView, { label: label, nodes: nodes, edges: edges, active: active, layer: (layout) => dotLayer(layout, { inBox, names, open, toggle, moves, done }), canvas: canvas, drawn: drawn, fit: true, uniform: true }), tones.size > 0 && (_jsx("div", { className: "flex justify-center", children: _jsx(Legend, { items: [...tones].map(([role, tone]) => ({ label: role, tone })), "aria-label": `${label}: roles` }) }))] }));
|
|
116
139
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** One box of a `FlowDiagram` or a `StateMachine`, placed. Internal. */
|
|
2
|
+
import type { ReactElement } from "react";
|
|
3
|
+
import type { ViewNode } from "./FlowDiagram.js";
|
|
4
|
+
import type { FlowNodeBox } from "./flowLayout.js";
|
|
5
|
+
/**
|
|
6
|
+
* One box, placed: its label and content, sized by them, centred in the slot the layout gave it.
|
|
7
|
+
* A `uniform` box fills its slot instead, its label measured alone and wrapped to the box's width,
|
|
8
|
+
* its content at the bottom.
|
|
9
|
+
*/
|
|
10
|
+
export declare function FlowBoxView({ box, node, active, uniform, }: {
|
|
11
|
+
box: FlowNodeBox;
|
|
12
|
+
node: ViewNode;
|
|
13
|
+
active: boolean;
|
|
14
|
+
uniform: boolean;
|
|
15
|
+
}): ReactElement;
|
package/dist/flowBox.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { TONE_SURFACE, TONE_TEXT } from "./Callout.js";
|
|
3
|
+
import { cx } from "./cx.js";
|
|
4
|
+
import { contentKey, labelKey, nodeKey } from "./useFlowLayout.js";
|
|
5
|
+
/** A box's frame by tone: the Callout's soft surfaces, a white box for `neutral`, a grey one for `muted`. */
|
|
6
|
+
const NODE_TONE = {
|
|
7
|
+
...TONE_SURFACE,
|
|
8
|
+
neutral: "border-slate-300 bg-white",
|
|
9
|
+
muted: "border-slate-200 bg-slate-50",
|
|
10
|
+
};
|
|
11
|
+
/** A box's ink by tone; `neutral` is the primary ink, `muted` the quiet one. */
|
|
12
|
+
const NODE_INK = {
|
|
13
|
+
...TONE_TEXT,
|
|
14
|
+
neutral: "text-slate-900",
|
|
15
|
+
muted: "text-slate-500",
|
|
16
|
+
};
|
|
17
|
+
/** A bar of `colour` (a text colour utility) across a box's top edge. */
|
|
18
|
+
function AccentBar({ colour }) {
|
|
19
|
+
return (_jsx("span", { "aria-hidden": "true", className: cx("absolute inset-x-0 top-0 h-1 bg-current", colour) }));
|
|
20
|
+
}
|
|
21
|
+
/** A box's frame: its tone's surface and ink, its size, the ring when `active`, and room for its accent bar. */
|
|
22
|
+
function frameClass(node, active, uniform) {
|
|
23
|
+
const tone = node.tone ?? "neutral";
|
|
24
|
+
return cx("rounded-lg border px-3 py-2 text-sm", node.accent && "relative overflow-hidden", uniform ? "flex size-full flex-col" : "w-max max-w-72 shrink-0", NODE_TONE[tone], NODE_INK[tone], active && "outline-2 outline-offset-2 outline-violet-600");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One box, placed: its label and content, sized by them, centred in the slot the layout gave it.
|
|
28
|
+
* A `uniform` box fills its slot instead, its label measured alone and wrapped to the box's width,
|
|
29
|
+
* its content at the bottom.
|
|
30
|
+
*/
|
|
31
|
+
export function FlowBoxView({ box, node, active, uniform, }) {
|
|
32
|
+
return (_jsx("div", { className: "absolute flex items-center justify-center", style: { left: box.x, top: box.y, width: box.width, height: box.height }, children: _jsxs("div", { ...(uniform ? {} : { "data-measure": nodeKey(box.id) }), className: frameClass(node, active, uniform), children: [node.accent && _jsx(AccentBar, { colour: node.accent }), _jsx("div", { ...(uniform ? { "data-measure": labelKey(box.id) } : {}), className: cx("font-medium", uniform && "break-words"), children: node.label }), node.content != null && (_jsx("div", { ...(uniform ? { "data-measure": contentKey(box.id) } : {}), className: uniform ? "mt-auto pt-2" : "mt-2", children: node.content }))] }) }));
|
|
33
|
+
}
|
package/dist/flowLayout.d.ts
CHANGED
|
@@ -61,8 +61,14 @@ export type LayoutEdge = {
|
|
|
61
61
|
};
|
|
62
62
|
/** `down` reads from the top, `right` from the start edge. */
|
|
63
63
|
export type FlowDirection = "down" | "right";
|
|
64
|
+
/** The gaps dagre keeps: between boxes in a rank, between ranks, and between arrows in a rank. */
|
|
65
|
+
export type FlowSpacing = {
|
|
66
|
+
node: number;
|
|
67
|
+
rank: number;
|
|
68
|
+
edge: number;
|
|
69
|
+
};
|
|
64
70
|
/**
|
|
65
71
|
* Lays the nodes and arrows out with dagre. An arrow whose end is not a node is left out, so a
|
|
66
72
|
* typo draws no stray box. Pure: the same input gives the same layout.
|
|
67
73
|
*/
|
|
68
|
-
export declare function layoutFlow(nodes: readonly LayoutNode[], edges: readonly LayoutEdge[], direction: FlowDirection): FlowLayout;
|
|
74
|
+
export declare function layoutFlow(nodes: readonly LayoutNode[], edges: readonly LayoutEdge[], direction: FlowDirection, spacing?: FlowSpacing): FlowLayout;
|
package/dist/flowLayout.js
CHANGED
|
@@ -11,6 +11,7 @@ const MARGIN = 16;
|
|
|
11
11
|
const NODE_GAP = 32;
|
|
12
12
|
const RANK_GAP = 48;
|
|
13
13
|
const EDGE_GAP = 16;
|
|
14
|
+
const LOOSE = { node: NODE_GAP, rank: RANK_GAP, edge: EDGE_GAP };
|
|
14
15
|
/** A group's dagre id, kept apart from every node id. */
|
|
15
16
|
const groupKey = (name) => `\u0000group:${name}`;
|
|
16
17
|
const round = (n) => Math.round(n * 10) / 10;
|
|
@@ -18,7 +19,7 @@ const round = (n) => Math.round(n * 10) / 10;
|
|
|
18
19
|
* Lays the nodes and arrows out with dagre. An arrow whose end is not a node is left out, so a
|
|
19
20
|
* typo draws no stray box. Pure: the same input gives the same layout.
|
|
20
21
|
*/
|
|
21
|
-
export function layoutFlow(nodes, edges, direction) {
|
|
22
|
+
export function layoutFlow(nodes, edges, direction, spacing = LOOSE) {
|
|
22
23
|
const groups = [...new Set(nodes.flatMap((n) => (n.group ? [n.group] : [])))];
|
|
23
24
|
const g = new Graph({
|
|
24
25
|
multigraph: true,
|
|
@@ -26,9 +27,9 @@ export function layoutFlow(nodes, edges, direction) {
|
|
|
26
27
|
});
|
|
27
28
|
g.setGraph({
|
|
28
29
|
rankdir: direction === "right" ? "LR" : "TB",
|
|
29
|
-
nodesep:
|
|
30
|
-
ranksep:
|
|
31
|
-
edgesep:
|
|
30
|
+
nodesep: spacing.node,
|
|
31
|
+
ranksep: spacing.rank,
|
|
32
|
+
edgesep: spacing.edge,
|
|
32
33
|
marginx: MARGIN,
|
|
33
34
|
marginy: MARGIN,
|
|
34
35
|
});
|
package/dist/fromXState.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export type XStateStateInput = {
|
|
|
59
59
|
* `initial` for the machine's `initial`, and `normal` for the rest.
|
|
60
60
|
* - Each `on` event, `always`, each `after` delay and each `invoke`'s `onDone` and `onError` is a
|
|
61
61
|
* transition. Its `event` is its `meta.label`, else the event's name (`always`, `after 500`,
|
|
62
|
-
* `done`, `error`). A transition with no target
|
|
62
|
+
* `done`, `error`), and its `actor` its `meta.actor`. A transition with no target
|
|
63
63
|
* (it stays where it is), one that stays inside its own state, or one whose target is not a
|
|
64
64
|
* top-level state is left out.
|
|
65
65
|
* - `state` adjusts each state, for a machine whose meta says it another way: return the state to
|
package/dist/fromXState.js
CHANGED
|
@@ -69,7 +69,7 @@ function targets(config) {
|
|
|
69
69
|
* `initial` for the machine's `initial`, and `normal` for the rest.
|
|
70
70
|
* - Each `on` event, `always`, each `after` delay and each `invoke`'s `onDone` and `onError` is a
|
|
71
71
|
* transition. Its `event` is its `meta.label`, else the event's name (`always`, `after 500`,
|
|
72
|
-
* `done`, `error`). A transition with no target
|
|
72
|
+
* `done`, `error`), and its `actor` its `meta.actor`. A transition with no target
|
|
73
73
|
* (it stays where it is), one that stays inside its own state, or one whose target is not a
|
|
74
74
|
* top-level state is left out.
|
|
75
75
|
* - `state` adjusts each state, for a machine whose meta says it another way: return the state to
|
|
@@ -101,10 +101,16 @@ export function fromXState(machine, options = {}) {
|
|
|
101
101
|
const transitions = nodes.flatMap(([from, node]) => movesOf(node).flatMap(([event, on]) => listOf(on).flatMap((one) => {
|
|
102
102
|
const { to, meta } = targets(one);
|
|
103
103
|
const name = text(meta?.["label"]) ?? event;
|
|
104
|
+
const actor = text(meta?.["actor"]);
|
|
104
105
|
return to
|
|
105
106
|
.map((t) => targetState(t, from, ids))
|
|
106
107
|
.filter((t) => t !== from && keys.has(t))
|
|
107
|
-
.map((t) => ({
|
|
108
|
+
.map((t) => ({
|
|
109
|
+
from,
|
|
110
|
+
to: t,
|
|
111
|
+
event: name,
|
|
112
|
+
...(actor === undefined ? {} : { actor }),
|
|
113
|
+
}));
|
|
108
114
|
})));
|
|
109
115
|
return { states, transitions };
|
|
110
116
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ReactElement } from "react";
|
|
2
|
-
import type { FlowLayout, FlowNodeBox } from "./flowLayout.js";
|
|
2
|
+
import type { FlowLayout, FlowNodeBox, FlowSpacing } from "./flowLayout.js";
|
|
3
3
|
import type { StateMachineItem } from "./StateMachine.js";
|
|
4
4
|
/**
|
|
5
5
|
* The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
|
|
@@ -32,19 +32,24 @@ export declare function useMoves(items: readonly StateMachineItem[], known: Read
|
|
|
32
32
|
/** How many slots a box with `count` items draws: all of them, or the first few and a `+N`; one more for "fewer" when open. */
|
|
33
33
|
export declare function slotsFor(count: number, open: boolean): number;
|
|
34
34
|
/**
|
|
35
|
-
* The rows a box reserves for its dots, whatever its count: the
|
|
35
|
+
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
36
36
|
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
37
37
|
* its size does not depend on how many items it holds.
|
|
38
38
|
*/
|
|
39
39
|
export declare function rowsFor(slots: number): number;
|
|
40
40
|
/**
|
|
41
41
|
* The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
|
|
42
|
-
* height, so it is the same at 0, 1,
|
|
42
|
+
* height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
|
|
43
43
|
*/
|
|
44
44
|
export declare function dotArea(slots: number): {
|
|
45
45
|
width: number;
|
|
46
46
|
height: number;
|
|
47
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
|
|
50
|
+
* two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
|
|
51
|
+
*/
|
|
52
|
+
export declare const MACHINE_SPACING: FlowSpacing;
|
|
48
53
|
/** A box's tag row: one small pill's height. */
|
|
49
54
|
export declare const TAG_ROW = 20;
|
|
50
55
|
/** The gap between the tag row and the dot area (`mt-1`). */
|
package/dist/stateMachineDots.js
CHANGED
|
@@ -14,7 +14,7 @@ export const SLOT_W = 76;
|
|
|
14
14
|
export const SLOT_H = 22;
|
|
15
15
|
export const COLS = 3;
|
|
16
16
|
/** Up to this many dots show in a box; past it, one fewer and a `+N` that shows the rest. */
|
|
17
|
-
const LIMIT =
|
|
17
|
+
const LIMIT = 6;
|
|
18
18
|
/** From a box's bottom edge to its dots' bottom: its border and its bottom padding. */
|
|
19
19
|
const BOX_INSET = 9;
|
|
20
20
|
/** From a slot's left edge to its dot's centre: the chip's inset, its padding and half the dot. */
|
|
@@ -84,7 +84,7 @@ export function slotsFor(count, open) {
|
|
|
84
84
|
return open ? count + 1 : LIMIT;
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
* The rows a box reserves for its dots, whatever its count: the
|
|
87
|
+
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
88
88
|
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
89
89
|
* its size does not depend on how many items it holds.
|
|
90
90
|
*/
|
|
@@ -93,11 +93,16 @@ export function rowsFor(slots) {
|
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
95
|
* The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
|
|
96
|
-
* height, so it is the same at 0, 1,
|
|
96
|
+
* height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
|
|
97
97
|
*/
|
|
98
98
|
export function dotArea(slots) {
|
|
99
99
|
return { width: COLS * SLOT_W, height: rowsFor(slots) * SLOT_H };
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
|
|
103
|
+
* two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
|
|
104
|
+
*/
|
|
105
|
+
export const MACHINE_SPACING = { node: 24, rank: 32, edge: 8 };
|
|
101
106
|
/** A box's tag row: one small pill's height. */
|
|
102
107
|
export const TAG_ROW = 20;
|
|
103
108
|
/** The gap between the tag row and the dot area (`mt-1`). */
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CategoryTone } from "./StackedBar.js";
|
|
2
|
+
import type { StateMachineState, StateMachineTransition } from "./StateMachine.js";
|
|
3
|
+
/**
|
|
4
|
+
* Each role's colour. The roles are the states' owners in state order, then any transition actor
|
|
5
|
+
* that owns no state, in transition order. A role in `pinned` takes its pinned tone; every other
|
|
6
|
+
* takes the next tone in `cat1` to `cat8` order that no role is pinned to, wrapping past the last.
|
|
7
|
+
* So the same definition and pins always give the same colours.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toneByRole(states: readonly StateMachineState[], transitions: readonly StateMachineTransition[], pinned?: Readonly<Record<string, CategoryTone>>): Map<string, CategoryTone>;
|
|
10
|
+
/** The role a transition's line is coloured by: its `actor`, else the owner of its `from` state. */
|
|
11
|
+
export declare function moverOf(transition: StateMachineTransition, owners: ReadonlyMap<string, string | undefined>): string | undefined;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The categorical scale in its order: the colours a role takes, `cat1` first. */
|
|
2
|
+
const ROLE_TONES = [
|
|
3
|
+
"cat1",
|
|
4
|
+
"cat2",
|
|
5
|
+
"cat3",
|
|
6
|
+
"cat4",
|
|
7
|
+
"cat5",
|
|
8
|
+
"cat6",
|
|
9
|
+
"cat7",
|
|
10
|
+
"cat8",
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Each role's colour. The roles are the states' owners in state order, then any transition actor
|
|
14
|
+
* that owns no state, in transition order. A role in `pinned` takes its pinned tone; every other
|
|
15
|
+
* takes the next tone in `cat1` to `cat8` order that no role is pinned to, wrapping past the last.
|
|
16
|
+
* So the same definition and pins always give the same colours.
|
|
17
|
+
*/
|
|
18
|
+
export function toneByRole(states, transitions, pinned = {}) {
|
|
19
|
+
const roles = [
|
|
20
|
+
...new Set([...states.map((s) => s.owner), ...transitions.map((t) => t.actor)].filter((r) => r !== undefined && r !== "")),
|
|
21
|
+
];
|
|
22
|
+
const taken = new Set(Object.values(pinned));
|
|
23
|
+
const free = ROLE_TONES.filter((t) => !taken.has(t));
|
|
24
|
+
const pool = free.length > 0 ? free : ROLE_TONES;
|
|
25
|
+
const tones = new Map();
|
|
26
|
+
let next = 0;
|
|
27
|
+
for (const role of roles) {
|
|
28
|
+
const pin = Object.hasOwn(pinned, role) ? pinned[role] : undefined;
|
|
29
|
+
tones.set(role, pin ?? pool[next++ % pool.length] ?? "cat1");
|
|
30
|
+
}
|
|
31
|
+
return tones;
|
|
32
|
+
}
|
|
33
|
+
/** The role a transition's line is coloured by: its `actor`, else the owner of its `from` state. */
|
|
34
|
+
export function moverOf(transition, owners) {
|
|
35
|
+
return transition.actor || owners.get(transition.from) || undefined;
|
|
36
|
+
}
|
package/dist/useFlowLayout.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type RefObject } from "react";
|
|
2
2
|
import type { FlowEdge, FlowNode } from "./FlowDiagram.js";
|
|
3
|
-
import { type FlowDirection, type FlowLayout } from "./flowLayout.js";
|
|
3
|
+
import { type FlowDirection, type FlowLayout, type FlowSpacing } from "./flowLayout.js";
|
|
4
4
|
type Size = {
|
|
5
5
|
width: number;
|
|
6
6
|
height: number;
|
|
@@ -16,6 +16,17 @@ export type SizedNode = Pick<FlowNode, "id" | "label" | "group" | "content"> & {
|
|
|
16
16
|
export declare function guessNode(node: SizedNode): Size;
|
|
17
17
|
export declare const nodeKey: (id: string) => string;
|
|
18
18
|
export declare const edgeKey: (index: number) => string;
|
|
19
|
+
export declare const labelKey: (id: string) => string;
|
|
20
|
+
export declare const contentKey: (id: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Every box the same size (`uniform`): as wide as the widest content, its label wrapped to that
|
|
23
|
+
* width, and as tall as the tallest label and the tallest content make a box. A box's label
|
|
24
|
+
* (`labelKey`) and content (`contentKey`, its gap above included) are measured, not the box: their
|
|
25
|
+
* width is fixed, so their height does not depend on the layout, and a box drawn at its slot's size
|
|
26
|
+
* is never read back as its own size. A content measured taller than its `contentSize` (a row of
|
|
27
|
+
* tags that wraps) makes every box taller.
|
|
28
|
+
*/
|
|
29
|
+
export declare function uniformSizes(nodes: readonly SizedNode[], measured: Readonly<Record<string, Size>>): Map<string, Size>;
|
|
19
30
|
/**
|
|
20
31
|
* How far to shrink a diagram `width` wide so it fits the box `frame` refers to: 1 when it fits
|
|
21
32
|
* or the room is not known yet (the server render, a test's DOM), else the room over the width.
|
|
@@ -29,9 +40,13 @@ export declare function useFit(width: number): {
|
|
|
29
40
|
* Lays the diagram out, first at guessed sizes, then at the sizes the browser draws each box and
|
|
30
41
|
* arrow label at: every render measures them, and lays out again when one's size is not the one
|
|
31
42
|
* the layout used. A box's size never depends on where it is placed, so this settles after one
|
|
32
|
-
* pass; the observer catches a later change (a web font arriving).
|
|
43
|
+
* pass; the observer catches a later change (a web font arriving). `uniform` draws every box at one
|
|
44
|
+
* size (`uniformSizes`), and `spacing` sets dagre's gaps (`layoutFlow`'s default when not given).
|
|
33
45
|
*/
|
|
34
|
-
export declare function useFlowLayout(nodes: readonly SizedNode[], edges: readonly FlowEdge[], direction: FlowDirection
|
|
46
|
+
export declare function useFlowLayout(nodes: readonly SizedNode[], edges: readonly FlowEdge[], direction: FlowDirection, options?: {
|
|
47
|
+
uniform?: boolean;
|
|
48
|
+
spacing?: FlowSpacing;
|
|
49
|
+
}): {
|
|
35
50
|
canvas: RefObject<HTMLDivElement | null>;
|
|
36
51
|
drawn: FlowLayout;
|
|
37
52
|
};
|
package/dist/useFlowLayout.js
CHANGED
|
@@ -17,9 +17,11 @@ const CONTENT_GAP = 8;
|
|
|
17
17
|
const EDGE_CHAR = 6.6;
|
|
18
18
|
const EDGE_PAD = 8;
|
|
19
19
|
const EDGE_LINE = 16;
|
|
20
|
+
/** How many lines a box's label wraps to at `room` wide, guessed from its characters. */
|
|
21
|
+
const guessLines = (label, room) => Math.max(1, Math.ceil(Math.ceil(label.length * NODE_CHAR) / room));
|
|
20
22
|
export function guessNode(node) {
|
|
21
23
|
const text = Math.ceil(node.label.length * NODE_CHAR);
|
|
22
|
-
const lines =
|
|
24
|
+
const lines = guessLines(node.label, NODE_MAX - NODE_PAD_X);
|
|
23
25
|
const content = node.contentSize ?? { width: 0, height: CONTENT_GUESS - CONTENT_GAP };
|
|
24
26
|
return {
|
|
25
27
|
width: Math.min(NODE_MAX, Math.max(text, content.width) + NODE_PAD_X),
|
|
@@ -32,6 +34,30 @@ const guessLabel = (label) => ({
|
|
|
32
34
|
});
|
|
33
35
|
export const nodeKey = (id) => `n:${id}`;
|
|
34
36
|
export const edgeKey = (index) => `e:${index}`;
|
|
37
|
+
export const labelKey = (id) => `l:${id}`;
|
|
38
|
+
export const contentKey = (id) => `c:${id}`;
|
|
39
|
+
/**
|
|
40
|
+
* Every box the same size (`uniform`): as wide as the widest content, its label wrapped to that
|
|
41
|
+
* width, and as tall as the tallest label and the tallest content make a box. A box's label
|
|
42
|
+
* (`labelKey`) and content (`contentKey`, its gap above included) are measured, not the box: their
|
|
43
|
+
* width is fixed, so their height does not depend on the layout, and a box drawn at its slot's size
|
|
44
|
+
* is never read back as its own size. A content measured taller than its `contentSize` (a row of
|
|
45
|
+
* tags that wraps) makes every box taller.
|
|
46
|
+
*/
|
|
47
|
+
export function uniformSizes(nodes, measured) {
|
|
48
|
+
const room = Math.max(0, ...nodes.map((n) => n.contentSize?.width ?? 0));
|
|
49
|
+
const tallest = Math.max(NODE_LINE, ...nodes.map((n) => measured[labelKey(n.id)]?.height ?? guessLines(n.label, room) * NODE_LINE));
|
|
50
|
+
const content = Math.max(0, ...nodes.map((n) => n.content == null
|
|
51
|
+
? 0
|
|
52
|
+
: Math.max(CONTENT_GAP + (n.contentSize?.height ?? 0), measured[contentKey(n.id)]?.height ?? 0)));
|
|
53
|
+
return new Map(nodes.map((n) => [
|
|
54
|
+
n.id,
|
|
55
|
+
{
|
|
56
|
+
width: room + NODE_PAD_X,
|
|
57
|
+
height: tallest + NODE_PAD_Y + (n.content == null ? 0 : content),
|
|
58
|
+
},
|
|
59
|
+
]));
|
|
60
|
+
}
|
|
35
61
|
/**
|
|
36
62
|
* Reads the drawn size of every box and arrow label under `root`, keyed by `data-measure`, in the
|
|
37
63
|
* diagram's own space: a canvas scaled to fit (`useFit`) is drawn smaller, so its sizes are read
|
|
@@ -83,25 +109,28 @@ const sameSizes = (a, b) => {
|
|
|
83
109
|
* Lays the diagram out, first at guessed sizes, then at the sizes the browser draws each box and
|
|
84
110
|
* arrow label at: every render measures them, and lays out again when one's size is not the one
|
|
85
111
|
* the layout used. A box's size never depends on where it is placed, so this settles after one
|
|
86
|
-
* pass; the observer catches a later change (a web font arriving).
|
|
112
|
+
* pass; the observer catches a later change (a web font arriving). `uniform` draws every box at one
|
|
113
|
+
* size (`uniformSizes`), and `spacing` sets dagre's gaps (`layoutFlow`'s default when not given).
|
|
87
114
|
*/
|
|
88
|
-
export function useFlowLayout(nodes, edges, direction) {
|
|
115
|
+
export function useFlowLayout(nodes, edges, direction, options = {}) {
|
|
89
116
|
const canvas = useRef(null);
|
|
90
117
|
const [measured, setMeasured] = useState({});
|
|
118
|
+
const uniform = options.uniform ? uniformSizes(nodes, measured) : undefined;
|
|
91
119
|
const layoutNodes = nodes.map((n) => ({
|
|
92
120
|
id: n.id,
|
|
93
121
|
group: n.group,
|
|
94
|
-
...(measured[nodeKey(n.id)] ?? guessNode(n)),
|
|
122
|
+
...(uniform?.get(n.id) ?? measured[nodeKey(n.id)] ?? guessNode(n)),
|
|
95
123
|
}));
|
|
96
124
|
const layoutEdges = edges.map((e, index) => ({
|
|
97
125
|
from: e.from,
|
|
98
126
|
to: e.to,
|
|
99
127
|
label: e.label ? (measured[edgeKey(index)] ?? guessLabel(e.label)) : undefined,
|
|
100
128
|
}));
|
|
101
|
-
const key = JSON.stringify([direction, layoutNodes, layoutEdges]);
|
|
129
|
+
const key = JSON.stringify([direction, options.spacing, layoutNodes, layoutEdges]);
|
|
102
130
|
// The key holds every input the layout reads, so the layout reruns only when one changes.
|
|
131
|
+
const drawn = useMemo(() => layoutFlow(layoutNodes, layoutEdges, direction, options.spacing),
|
|
103
132
|
// oxlint-disable-next-line react-hooks/exhaustive-deps -- keyed on the serialised input above
|
|
104
|
-
|
|
133
|
+
[key]);
|
|
105
134
|
useLayoutEffect(() => {
|
|
106
135
|
const root = canvas.current;
|
|
107
136
|
if (!root)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slashsuite/ui-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "UISlash React components: primitives, typography roles and a safe Markdown renderer, written against Tailwind v4 utilities. Pair with a theme from @slashsuite/themes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"components",
|