@slashsuite/ui-react 0.13.3 → 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 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`: it stands out, and its tag says so.
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,9 @@ 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 (the warn tone), `failed` for an error (danger), `terminal` for done (muted), `initial` for the start. Each is also a tag in words. Leave the rest `normal`.
31
- - `owner` is the role or lane that acts in a state, a tag on its box.
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
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.
33
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.
34
34
  - Change an item's `state` and its dot runs along the arrow to its new box; there is nothing to call.
@@ -38,8 +38,9 @@ import { StateMachine } from "@slashsuite/ui-react";
38
38
  ### Don't
39
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.
40
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.
41
42
 
42
- Every state marked `human`: nothing stands out, and the tags say nothing.
43
+ Every state marked `human`: the tags say nothing.
43
44
  ```tsx
44
45
  import { StateMachine } from "@slashsuite/ui-react";
45
46
 
@@ -62,12 +63,12 @@ import { StateMachine } from "@slashsuite/ui-react";
62
63
  ### Accessibility
63
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.
64
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.
65
- - A visually hidden list after it carries every state, its tags and the transitions out of it, as `FlowDiagram`'s does.
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.
66
67
  - With reduced motion a dot moves to its new state at once, with no run.
67
68
 
68
69
  ### Props
69
70
  ### StateMachine
70
- 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` sets its look and a tag that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping inside it, and the boxes sit close. 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.
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.
71
72
  - active: string. The `id` of the one state to ring as current.
72
73
  - id: string. The root element's `id`: an anchor, or the target of a `for` or an `aria-describedby`.
73
74
  - items: readonly StateMachineItem[] (default []). The things in the machine, each in the state its `state` names.
@@ -75,6 +76,8 @@ A state machine, drawn, with the things moving through it: each state a box, eac
75
76
  - loading: boolean. The data is not here yet: an empty slot draws a bone of its shape, a filled one draws as usual.
76
77
  Left out, the part takes the nearest loading layout's or part's; `false` opts it out inside one.
77
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.
78
81
  - states: readonly StateMachineState[] (required). The states, each with a unique `id`.
79
82
  - transitions: readonly StateMachineTransition[] (required). The moves between them. One whose `from` or `to` is not a state is not drawn.
80
83
 
@@ -82,7 +85,7 @@ Left out, the part takes the nearest loading layout's or part's; `false` opts it
82
85
  - StateMachineDefinition: `{ states: readonly StateMachineState[]; transitions: readonly StateMachineTransition[] }`. The machine: its states and the transitions between them. `fromXState` makes one from an xstate machine.
83
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.
84
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).
85
- - 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.
86
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.
87
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`).
88
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.
@@ -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
- /** A box of the internal view: a `FlowNode` that may also be `muted`. */
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
@@ -84,7 +92,7 @@ export type ViewNode = Omit<FlowNode, "tone"> & {
84
92
  export declare function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit, uniform, ...rest }: {
85
93
  label: string;
86
94
  nodes: readonly ViewNode[];
87
- edges: readonly FlowEdge[];
95
+ edges: readonly ViewEdge[];
88
96
  active: string | undefined;
89
97
  overlay?: ((layout: FlowLayout) => ReactNode) | undefined;
90
98
  layer?: ((layout: FlowLayout) => ReactNode) | undefined;
@@ -62,7 +62,7 @@ function FlowCanvas({ drawn, nodes, edges, active, uniform, }) {
62
62
  const marker = `flow-arrow-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
63
63
  const byId = new Map(nodes.map((n) => [n.id, n]));
64
64
  const paths = new Map(drawn.edges.map((e) => [e.index, e]));
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 }), 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 }) => {
66
66
  const at = paths.get(index)?.labelAt;
67
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;
68
68
  }), drawn.nodes.map((b) => {
@@ -70,9 +70,20 @@ function FlowCanvas({ drawn, nodes, edges, active, uniform, }) {
70
70
  return node ? (_jsx(FlowBoxView, { box: b, node: node, active: active === b.id, uniform: uniform }, b.id)) : null;
71
71
  })] }));
72
72
  }
73
- /** Every arrow as an SVG path, with its arrowhead. */
74
- function FlowArrows({ drawn, marker }) {
75
- return (_jsxs("svg", { width: drawn.width, height: drawn.height, className: "absolute inset-0 overflow-visible text-slate-400", children: [_jsx("defs", { children: _jsx("marker", { id: marker, 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" }) }) }), drawn.edges.map((e) => (_jsx("path", { d: e.path, fill: "none", stroke: "currentColor", strokeWidth: 1.5, markerEnd: `url(#${marker})` }, e.index)))] }));
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
+ })] }));
76
87
  }
77
88
  /** The diagram as text for a screen reader: each box, its content, and the arrows out of it. */
78
89
  function FlowList({ label, nodes, edges, active, }) {
@@ -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
- /** The role or lane that acts in this state (`reviewer`, `billing`), shown as a tag on it. */
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
- /** One move between states, with the `event` that makes it, shown on the arrow. */
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,10 +47,16 @@ 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` sets its look and a tag
42
- * that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and
43
- * `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping
44
- * inside it, and the boxes sit close.
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.
45
60
  *
46
61
  * Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
47
62
  * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
@@ -52,7 +67,7 @@ export type StateMachineItem = {
52
67
  *
53
68
  * `fromXState` makes `states` and `transitions` from an xstate machine.
54
69
  */
55
- 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 }: {
56
71
  /** What the machine is: its accessible name. */
57
72
  label: string;
58
73
  /** The states, each with a unique `id`. */
@@ -63,4 +78,9 @@ export declare function StateMachine({ label, states, transitions, items, active
63
78
  items?: readonly StateMachineItem[] | undefined;
64
79
  /** The `id` of the one state to ring as current. */
65
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;
66
86
  } & LoadingProps & Passthrough): ReactElement;
@@ -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";
7
+ import { Legend, VIZ_TONE } from "./StackedBar.js";
8
+ import { moverOf, toneByRole } from "./stateMachineRoles.js";
6
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: "warn",
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: "warn" },
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 tags = boxTags(state ? KIND_TAG[state.kind ?? "normal"] : null, state?.owner, count);
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
- id: s.id,
54
- label: s.label,
55
- tone: KIND_TONE[s.kind ?? "normal"],
56
- content: boxContent(s, inBox.get(s.id)?.length ?? 0, slots(s.id)),
57
- contentSize: boxContentSize(slots(s.id)),
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,10 +94,16 @@ 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` sets its look and a tag
90
- * that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and
91
- * `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping
92
- * inside it, and the boxes sit close.
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.
93
107
  *
94
108
  * Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
95
109
  * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
@@ -100,14 +114,19 @@ function useOpenBoxes(opened) {
100
114
  *
101
115
  * `fromXState` makes `states` and `transitions` from an xstate machine.
102
116
  */
103
- 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 }) {
104
118
  const { loading: waiting, owner } = useLoading(loading);
105
119
  const known = new Set(states.map((s) => s.id));
106
120
  const { moves, done, clear } = useMoves(items, known);
107
121
  const [open, toggle] = useOpenBoxes(clear);
108
122
  const inBox = itemsByBox(items, known);
109
- const nodes = boxesFor(states, inBox, open);
110
- const edges = transitions.map((t) => ({ from: t.from, to: t.to, label: t.event }));
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
+ });
111
130
  const { canvas, drawn } = useFlowLayout(nodes, edges, "down", {
112
131
  uniform: true,
113
132
  spacing: MACHINE_SPACING,
@@ -116,5 +135,5 @@ export function StateMachine({ label, states, transitions, items = [], active, l
116
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 })] }));
117
136
  }
118
137
  const names = new Map(nodes.map((n) => [n.id, n.label]));
119
- return (_jsx(FlowView, { ...rest, 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 }));
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` }) }))] }));
120
139
  }
package/dist/flowBox.js CHANGED
@@ -14,12 +14,20 @@ const NODE_INK = {
14
14
  neutral: "text-slate-900",
15
15
  muted: "text-slate-500",
16
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
+ }
17
26
  /**
18
27
  * One box, placed: its label and content, sized by them, centred in the slot the layout gave it.
19
28
  * A `uniform` box fills its slot instead, its label measured alone and wrapped to the box's width,
20
29
  * its content at the bottom.
21
30
  */
22
31
  export function FlowBoxView({ box, node, active, uniform, }) {
23
- const tone = node.tone ?? "neutral";
24
- 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: cx("rounded-lg border px-3 py-2 text-sm", 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"), children: [_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 }))] }) }));
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 }))] }) }));
25
33
  }
@@ -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
@@ -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) => ({ from, to: t, event: name }));
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
  }
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slashsuite/ui-react",
3
- "version": "0.13.3",
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",