@slashsuite/ui-react 0.13.3 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATING.md +8 -0
- package/agent-docs/components/state-machine.md +13 -10
- package/dist/FlowDiagram.d.ts +10 -2
- package/dist/FlowDiagram.js +15 -4
- package/dist/StateMachine.d.ts +31 -9
- package/dist/StateMachine.js +59 -29
- package/dist/flowBox.js +10 -2
- package/dist/fromXState.d.ts +1 -1
- package/dist/fromXState.js +8 -2
- package/dist/stateMachineDots.d.ts +6 -70
- package/dist/stateMachineDots.js +36 -129
- package/dist/stateMachineGrid.d.ts +56 -0
- package/dist/stateMachineGrid.js +67 -0
- package/dist/stateMachineMoves.d.ts +80 -0
- package/dist/stateMachineMoves.js +118 -0
- package/dist/stateMachineRoles.d.ts +11 -0
- package/dist/stateMachineRoles.js +36 -0
- 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,20 +26,21 @@ 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
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
|
-
- Change an item's `state` and its dot runs along the arrow
|
|
34
|
+
- Change an item's `state` and its dot runs from its slot along the arrow, in the arrow's colour, into its new box (straight across when no arrow joins the two), while the dots it leaves slide into its place; there is nothing to call.
|
|
35
35
|
- `active` rings the one state that is current, for a machine with one thing in it.
|
|
36
36
|
- Made with xstate? `fromXState(machine)` gives `states` and `transitions`; its `state` option maps your own `meta` onto `kind` and `owner`.
|
|
37
37
|
|
|
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`:
|
|
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
|
-
- With reduced motion
|
|
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.
|
|
67
|
+
- With reduced motion every dot moves to its new place at once, with no run and no slide.
|
|
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`
|
|
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 from its slot along the arrow between the two, in the arrow's colour (straight across, in the colour of the old state's owner, when there is none), into its new slot, and the dots that stay slide into the slots it freed; with reduced motion every dot 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.
|
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
|
|
@@ -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
|
|
95
|
+
edges: readonly ViewEdge[];
|
|
88
96
|
active: string | undefined;
|
|
89
97
|
overlay?: ((layout: FlowLayout) => ReactNode) | undefined;
|
|
90
98
|
layer?: ((layout: FlowLayout) => ReactNode) | undefined;
|
package/dist/FlowDiagram.js
CHANGED
|
@@ -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
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
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, }) {
|
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,21 +47,29 @@ 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
|
-
*
|
|
44
|
-
*
|
|
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
|
|
48
63
|
* full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
|
|
49
64
|
* a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
|
|
50
|
-
* dot runs along the arrow between the two
|
|
51
|
-
*
|
|
65
|
+
* dot runs from its slot along the arrow between the two, in the arrow's colour (straight across,
|
|
66
|
+
* in the colour of the old state's owner, when there is none), into its new slot, and the dots
|
|
67
|
+
* that stay slide into the slots it freed; with reduced motion every dot moves at once. `active`
|
|
68
|
+
* rings the one state that is current.
|
|
52
69
|
*
|
|
53
70
|
* `fromXState` makes `states` and `transitions` from an xstate machine.
|
|
54
71
|
*/
|
|
55
|
-
export declare function StateMachine({ label, states, transitions, items, active, loading, loadingLabel, ...rest }: {
|
|
72
|
+
export declare function StateMachine({ label, states, transitions, items, active, roleTones: pinned, loading, loadingLabel, ...rest }: {
|
|
56
73
|
/** What the machine is: its accessible name. */
|
|
57
74
|
label: string;
|
|
58
75
|
/** The states, each with a unique `id`. */
|
|
@@ -63,4 +80,9 @@ export declare function StateMachine({ label, states, transitions, items, active
|
|
|
63
80
|
items?: readonly StateMachineItem[] | undefined;
|
|
64
81
|
/** The `id` of the one state to ring as current. */
|
|
65
82
|
active?: string | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Pins a role to a colour (`{ owner: "cat2" }`). A role left out takes the next colour in order
|
|
85
|
+
* that no role is pinned to.
|
|
86
|
+
*/
|
|
87
|
+
roleTones?: Readonly<Record<string, CategoryTone>> | undefined;
|
|
66
88
|
} & LoadingProps & Passthrough): ReactElement;
|
package/dist/StateMachine.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
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 { dotLayer } from "./stateMachineDots.js";
|
|
10
|
+
import { boxContentSize, MACHINE_SPACING, dotArea, placeOf, slotsFor, UNPLACED, } from "./stateMachineGrid.js";
|
|
11
|
+
import { useMoves } from "./stateMachineMoves.js";
|
|
7
12
|
import { useFlowLayout } from "./useFlowLayout.js";
|
|
8
13
|
/** Each kind's box tone, and the tag that says it in words. */
|
|
9
14
|
const KIND_TONE = {
|
|
10
15
|
initial: "neutral",
|
|
11
16
|
normal: "neutral",
|
|
12
|
-
human: "
|
|
17
|
+
human: "neutral",
|
|
13
18
|
terminal: "muted",
|
|
14
19
|
failed: "danger",
|
|
15
20
|
};
|
|
16
21
|
const KIND_TAG = {
|
|
17
22
|
initial: { text: "start", tone: "neutral" },
|
|
18
23
|
normal: null,
|
|
19
|
-
human: { text: "human", tone: "
|
|
24
|
+
human: { text: "human", tone: "neutral" },
|
|
20
25
|
terminal: { text: "end", tone: "neutral" },
|
|
21
26
|
failed: { text: "failed", tone: "danger" },
|
|
22
27
|
};
|
|
@@ -26,14 +31,15 @@ const KIND_TAG = {
|
|
|
26
31
|
* one are the same height.
|
|
27
32
|
*/
|
|
28
33
|
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" }) }))] }));
|
|
34
|
+
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
35
|
}
|
|
31
36
|
/**
|
|
32
37
|
* What a box holds under its label: its tags, and the dot area it always reserves (the full grid,
|
|
33
38
|
* even empty), so a box keeps its size whatever its item count.
|
|
34
39
|
*/
|
|
35
|
-
function boxContent(state, count, slots) {
|
|
36
|
-
const
|
|
40
|
+
function boxContent(state, tone, count, slots) {
|
|
41
|
+
const owner = state?.owner && tone ? { role: state.owner, tone } : undefined;
|
|
42
|
+
const tags = boxTags(state ? KIND_TAG[state.kind ?? "normal"] : null, owner, count);
|
|
37
43
|
return (_jsxs(_Fragment, { children: [tags, _jsx("span", { "aria-hidden": "true", className: "mx-auto mt-1 block", style: dotArea(slots) })] }));
|
|
38
44
|
}
|
|
39
45
|
/** The items each box holds: its state's, and the unplaced box's for an unknown state. */
|
|
@@ -47,15 +53,19 @@ function itemsByBox(items, known) {
|
|
|
47
53
|
return inBox;
|
|
48
54
|
}
|
|
49
55
|
/** The boxes to draw: one per state, and the unplaced box when an item is in no state. */
|
|
50
|
-
function boxesFor(states, inBox, open) {
|
|
56
|
+
function boxesFor(states, inBox, open, tones) {
|
|
51
57
|
const slots = (id) => slotsFor(inBox.get(id)?.length ?? 0, open.has(id));
|
|
52
|
-
const boxes = states.map((s) =>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
const boxes = states.map((s) => {
|
|
59
|
+
const tone = s.owner ? tones.get(s.owner) : undefined;
|
|
60
|
+
return {
|
|
61
|
+
id: s.id,
|
|
62
|
+
label: s.label,
|
|
63
|
+
tone: KIND_TONE[s.kind ?? "normal"],
|
|
64
|
+
...(tone ? { accent: VIZ_TONE[tone] } : {}),
|
|
65
|
+
content: boxContent(s, tone, inBox.get(s.id)?.length ?? 0, slots(s.id)),
|
|
66
|
+
contentSize: boxContentSize(slots(s.id)),
|
|
67
|
+
};
|
|
68
|
+
});
|
|
59
69
|
const unplaced = inBox.get(UNPLACED)?.length ?? 0;
|
|
60
70
|
if (unplaced === 0)
|
|
61
71
|
return boxes;
|
|
@@ -65,16 +75,15 @@ function boxesFor(states, inBox, open) {
|
|
|
65
75
|
id: UNPLACED,
|
|
66
76
|
label: "Not in any state",
|
|
67
77
|
tone: "warn",
|
|
68
|
-
content: boxContent(null, unplaced, slots(UNPLACED)),
|
|
78
|
+
content: boxContent(null, undefined, unplaced, slots(UNPLACED)),
|
|
69
79
|
contentSize: boxContentSize(slots(UNPLACED)),
|
|
70
80
|
},
|
|
71
81
|
];
|
|
72
82
|
}
|
|
73
83
|
/** Which boxes show all their dots, and the switch that opens or closes one. */
|
|
74
|
-
function useOpenBoxes(
|
|
84
|
+
function useOpenBoxes() {
|
|
75
85
|
const [open, setOpen] = useState(new Set());
|
|
76
86
|
const toggle = (id) => {
|
|
77
|
-
opened();
|
|
78
87
|
setOpen((current) => {
|
|
79
88
|
const next = new Set(current);
|
|
80
89
|
if (!next.delete(id))
|
|
@@ -86,28 +95,49 @@ function useOpenBoxes(opened) {
|
|
|
86
95
|
}
|
|
87
96
|
/**
|
|
88
97
|
* 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
|
-
*
|
|
92
|
-
*
|
|
98
|
+
* arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` is a tag that says it:
|
|
99
|
+
* `human`, `failed` (which also takes the danger tone), `terminal` (muted) and `initial` (start).
|
|
100
|
+
* Every box is one size, the label wrapping inside it, and the boxes sit close.
|
|
101
|
+
*
|
|
102
|
+
* Each role gets its own colour from the categorical scale (`cat1` to `cat8`, remapped by every
|
|
103
|
+
* theme): a state's `owner` is a tag with its role's colour, and a bar of it across the box's top. A
|
|
104
|
+
* transition's arrow is drawn in its `actor`'s colour, else in the colour of the role that owns the
|
|
105
|
+
* state it leaves. The roles take the colours in order, the states' owners first, so the same
|
|
106
|
+
* machine always gets the same colours; `roleTones` pins a role to one. A legend under the machine
|
|
107
|
+
* names each role and its colour.
|
|
93
108
|
*
|
|
94
109
|
* Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
|
|
95
110
|
* counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
|
|
96
111
|
* full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
|
|
97
112
|
* a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
|
|
98
|
-
* dot runs along the arrow between the two
|
|
99
|
-
*
|
|
113
|
+
* dot runs from its slot along the arrow between the two, in the arrow's colour (straight across,
|
|
114
|
+
* in the colour of the old state's owner, when there is none), into its new slot, and the dots
|
|
115
|
+
* that stay slide into the slots it freed; with reduced motion every dot moves at once. `active`
|
|
116
|
+
* rings the one state that is current.
|
|
100
117
|
*
|
|
101
118
|
* `fromXState` makes `states` and `transitions` from an xstate machine.
|
|
102
119
|
*/
|
|
103
|
-
export function StateMachine({ label, states, transitions, items = [], active, loading, loadingLabel, ...rest }) {
|
|
120
|
+
export function StateMachine({ label, states, transitions, items = [], active, roleTones: pinned, loading, loadingLabel, ...rest }) {
|
|
104
121
|
const { loading: waiting, owner } = useLoading(loading);
|
|
105
122
|
const known = new Set(states.map((s) => s.id));
|
|
106
|
-
const
|
|
107
|
-
const
|
|
123
|
+
const [open, toggle] = useOpenBoxes();
|
|
124
|
+
const { moves, done } = useMoves(items, known, open);
|
|
108
125
|
const inBox = itemsByBox(items, known);
|
|
109
|
-
const
|
|
110
|
-
const
|
|
126
|
+
const tones = toneByRole(states, transitions, pinned);
|
|
127
|
+
const nodes = boxesFor(states, inBox, open, tones);
|
|
128
|
+
const owners = new Map(states.map((s) => [s.id, s.owner]));
|
|
129
|
+
const edges = transitions.map((t) => {
|
|
130
|
+
const tone = tones.get(moverOf(t, owners) ?? "");
|
|
131
|
+
return { from: t.from, to: t.to, label: t.event, ...(tone ? { colour: VIZ_TONE[tone] } : {}) };
|
|
132
|
+
});
|
|
133
|
+
// A run takes its arrow's colour; with no arrow, the colour of the role that owns the state it leaves.
|
|
134
|
+
const runColour = (from, to) => {
|
|
135
|
+
const edge = edges.find((e) => e.from === from && e.to === to);
|
|
136
|
+
if (edge)
|
|
137
|
+
return edge.colour;
|
|
138
|
+
const tone = tones.get(owners.get(from) ?? "");
|
|
139
|
+
return tone ? VIZ_TONE[tone] : undefined;
|
|
140
|
+
};
|
|
111
141
|
const { canvas, drawn } = useFlowLayout(nodes, edges, "down", {
|
|
112
142
|
uniform: true,
|
|
113
143
|
spacing: MACHINE_SPACING,
|
|
@@ -116,5 +146,5 @@ export function StateMachine({ label, states, transitions, items = [], active, l
|
|
|
116
146
|
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
147
|
}
|
|
118
148
|
const names = new Map(nodes.map((n) => [n.id, n.label]));
|
|
119
|
-
return (
|
|
149
|
+
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, runColour }), 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
150
|
}
|
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
|
-
|
|
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
|
}
|
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,74 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FlowLayout
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import type { FlowLayout } from "./flowLayout.js";
|
|
3
3
|
import type { StateMachineItem } from "./StateMachine.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* (a box's `data-measure`), where a server render would turn a control character into U+FFFD.
|
|
7
|
-
*/
|
|
8
|
-
export declare const UNPLACED = "hc-sm:unplaced";
|
|
9
|
-
/** A dot's slot in its box: three to a row, each wide enough for a short ref. */
|
|
10
|
-
export declare const SLOT_W = 76;
|
|
11
|
-
export declare const SLOT_H = 22;
|
|
12
|
-
export declare const COLS = 3;
|
|
13
|
-
/** An item on its way from one state to another; `seq` restarts the run when it moves again. */
|
|
14
|
-
type Move = {
|
|
15
|
-
from: string;
|
|
16
|
-
to: string;
|
|
17
|
-
seq: number;
|
|
18
|
-
};
|
|
19
|
-
/** Which box each item sits in: its state's, else the unplaced box. */
|
|
20
|
-
export declare function placeOf(items: readonly StateMachineItem[], known: ReadonlySet<string>): Map<string, string>;
|
|
21
|
-
/**
|
|
22
|
-
* Remembers where every item was, and when one's place changes records the move, during render
|
|
23
|
-
* (React's "adjust state when a prop changes"), so the re-render that measures the boxes does not
|
|
24
|
-
* lose it. A move ends on its run's `animationend` (`done`), at the next change of places (which
|
|
25
|
-
* records only its own moves), or on `clear` (a box opened, which would start a run long over).
|
|
26
|
-
*/
|
|
27
|
-
export declare function useMoves(items: readonly StateMachineItem[], known: ReadonlySet<string>): {
|
|
28
|
-
moves: ReadonlyMap<string, Move>;
|
|
29
|
-
done: (id: string, seq: number) => void;
|
|
30
|
-
clear: () => void;
|
|
31
|
-
};
|
|
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
|
-
export declare function slotsFor(count: number, open: boolean): number;
|
|
34
|
-
/**
|
|
35
|
-
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
36
|
-
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
37
|
-
* its size does not depend on how many items it holds.
|
|
38
|
-
*/
|
|
39
|
-
export declare function rowsFor(slots: number): number;
|
|
40
|
-
/**
|
|
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, 6 or 12 items (closed) and grows only when a box is opened.
|
|
43
|
-
*/
|
|
44
|
-
export declare function dotArea(slots: number): {
|
|
45
|
-
width: number;
|
|
46
|
-
height: number;
|
|
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;
|
|
53
|
-
/** A box's tag row: one small pill's height. */
|
|
54
|
-
export declare const TAG_ROW = 20;
|
|
55
|
-
/** The gap between the tag row and the dot area (`mt-1`). */
|
|
56
|
-
export declare const TAG_GAP = 4;
|
|
57
|
-
/**
|
|
58
|
-
* The size of what a box holds under its label: its tag row, then its dot area. The layout takes
|
|
59
|
-
* it before the box can be measured (a server render), so the dots, placed up from the box's
|
|
60
|
-
* bottom, already sit under the tags and never on them.
|
|
61
|
-
*/
|
|
62
|
-
export declare function boxContentSize(slots: number): {
|
|
63
|
-
width: number;
|
|
64
|
-
height: number;
|
|
65
|
-
};
|
|
66
|
-
/** Where slot `i` of `slots` sits in `box`: its left edge and its vertical centre, on the box's fixed grid. */
|
|
67
|
-
export declare function slotAt(box: FlowNodeBox, i: number, slots: number): {
|
|
68
|
-
left: number;
|
|
69
|
-
y: number;
|
|
70
|
-
};
|
|
71
|
-
/** What the dot layer draws from: the items in each box, the boxes' names, which boxes are open, and the moves. */
|
|
4
|
+
import { type Move } from "./stateMachineMoves.js";
|
|
5
|
+
/** What the dot layer draws from: the items in each box, the boxes' names, which boxes are open, the moves, and each run's colour. */
|
|
72
6
|
type DotPlaces = {
|
|
73
7
|
inBox: ReadonlyMap<string, readonly StateMachineItem[]>;
|
|
74
8
|
names: ReadonlyMap<string, string>;
|
|
@@ -76,6 +10,8 @@ type DotPlaces = {
|
|
|
76
10
|
toggle: (id: string) => void;
|
|
77
11
|
moves: ReadonlyMap<string, Move>;
|
|
78
12
|
done: (id: string, seq: number) => void;
|
|
13
|
+
/** The text colour utility of a run from one box to another, if its move has a role. */
|
|
14
|
+
runColour: (from: string, to: string) => string | undefined;
|
|
79
15
|
};
|
|
80
16
|
/** Every box's dots, over the drawn diagram. */
|
|
81
17
|
export declare function dotLayer(layout: FlowLayout, places: DotPlaces): ReactElement;
|
package/dist/stateMachineDots.js
CHANGED
|
@@ -1,24 +1,10 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useState } from "react";
|
|
3
2
|
import { cx, focusRing } from "./cx.js";
|
|
4
3
|
import { renderLink } from "./link.js";
|
|
5
4
|
import { VIZ_TONE } from "./StackedBar.js";
|
|
5
|
+
import { DOT_X, LIMIT, SLOT_W, slotAt, slotsFor } from "./stateMachineGrid.js";
|
|
6
|
+
import { moveCss, Travel } from "./stateMachineMoves.js";
|
|
6
7
|
import { useTooltip } from "./Tooltip.js";
|
|
7
|
-
/**
|
|
8
|
-
* The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
|
|
9
|
-
* (a box's `data-measure`), where a server render would turn a control character into U+FFFD.
|
|
10
|
-
*/
|
|
11
|
-
export const UNPLACED = "hc-sm:unplaced";
|
|
12
|
-
/** A dot's slot in its box: three to a row, each wide enough for a short ref. */
|
|
13
|
-
export const SLOT_W = 76;
|
|
14
|
-
export const SLOT_H = 22;
|
|
15
|
-
export const COLS = 3;
|
|
16
|
-
/** Up to this many dots show in a box; past it, one fewer and a `+N` that shows the rest. */
|
|
17
|
-
const LIMIT = 6;
|
|
18
|
-
/** From a box's bottom edge to its dots' bottom: its border and its bottom padding. */
|
|
19
|
-
const BOX_INSET = 9;
|
|
20
|
-
/** From a slot's left edge to its dot's centre: the chip's inset, its padding and half the dot. */
|
|
21
|
-
const DOT_X = 2 + 4 + 5;
|
|
22
8
|
const CATEGORIES = [
|
|
23
9
|
"cat1",
|
|
24
10
|
"cat2",
|
|
@@ -36,105 +22,29 @@ function colourOf(id) {
|
|
|
36
22
|
sum = (sum * 31 + (ch.codePointAt(0) ?? 0)) % 997;
|
|
37
23
|
return VIZ_TONE[CATEGORIES[sum % CATEGORIES.length] ?? "cat1"];
|
|
38
24
|
}
|
|
39
|
-
// The move is CSS: a dot runs along the arrow's path (`offset-path`) in 600ms while the dot at
|
|
40
|
-
// its new place waits hidden for the same time. Nothing is timed in script: the run ends on its
|
|
41
|
-
// own `animationend`. With reduced motion the run is hidden and over at once (so it still ends),
|
|
42
|
-
// and the dot is in its new place at once.
|
|
43
|
-
const css = `[data-hc-sm-travel]{offset-anchor:center;offset-rotate:0deg;animation:hc-sm-travel .6s ease-in-out forwards}[data-hc-sm-arrive]{animation:hc-sm-arrive .6s linear}@keyframes hc-sm-travel{from{offset-distance:0%}to{offset-distance:100%}}@keyframes hc-sm-arrive{from,to{visibility:hidden}}@media (prefers-reduced-motion:reduce){[data-hc-sm-travel]{visibility:hidden;animation-duration:1ms}[data-hc-sm-arrive]{animation:none}}`;
|
|
44
|
-
/** Which box each item sits in: its state's, else the unplaced box. */
|
|
45
|
-
export function placeOf(items, known) {
|
|
46
|
-
return new Map(items.map((i) => [i.id, known.has(i.state) ? i.state : UNPLACED]));
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Remembers where every item was, and when one's place changes records the move, during render
|
|
50
|
-
* (React's "adjust state when a prop changes"), so the re-render that measures the boxes does not
|
|
51
|
-
* lose it. A move ends on its run's `animationend` (`done`), at the next change of places (which
|
|
52
|
-
* records only its own moves), or on `clear` (a box opened, which would start a run long over).
|
|
53
|
-
*/
|
|
54
|
-
export function useMoves(items, known) {
|
|
55
|
-
const now = placeOf(items, known);
|
|
56
|
-
const key = JSON.stringify([...now]);
|
|
57
|
-
const [seen, setSeen] = useState({ key, places: now });
|
|
58
|
-
const [moves, setMoves] = useState(new Map());
|
|
59
|
-
// Compared by value: a caller's `items` is often a new array on every render.
|
|
60
|
-
if (seen.key !== key) {
|
|
61
|
-
const next = new Map();
|
|
62
|
-
for (const [id, to] of now) {
|
|
63
|
-
const from = seen.places.get(id);
|
|
64
|
-
if (from !== undefined && from !== to) {
|
|
65
|
-
next.set(id, { from, to, seq: (moves.get(id)?.seq ?? 0) + 1 });
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
setSeen({ key, places: now });
|
|
69
|
-
setMoves(next);
|
|
70
|
-
}
|
|
71
|
-
const done = (id, seq) => setMoves((current) => {
|
|
72
|
-
if (current.get(id)?.seq !== seq)
|
|
73
|
-
return current;
|
|
74
|
-
const next = new Map(current);
|
|
75
|
-
next.delete(id);
|
|
76
|
-
return next;
|
|
77
|
-
});
|
|
78
|
-
return { moves, done, clear: () => setMoves(new Map()) };
|
|
79
|
-
}
|
|
80
|
-
/** 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. */
|
|
81
|
-
export function slotsFor(count, open) {
|
|
82
|
-
if (count <= LIMIT)
|
|
83
|
-
return count;
|
|
84
|
-
return open ? count + 1 : LIMIT;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
88
|
-
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
89
|
-
* its size does not depend on how many items it holds.
|
|
90
|
-
*/
|
|
91
|
-
export function rowsFor(slots) {
|
|
92
|
-
return Math.ceil(Math.max(LIMIT, slots) / COLS);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
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, 6 or 12 items (closed) and grows only when a box is opened.
|
|
97
|
-
*/
|
|
98
|
-
export function dotArea(slots) {
|
|
99
|
-
return { width: COLS * SLOT_W, height: rowsFor(slots) * SLOT_H };
|
|
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 };
|
|
106
|
-
/** A box's tag row: one small pill's height. */
|
|
107
|
-
export const TAG_ROW = 20;
|
|
108
|
-
/** The gap between the tag row and the dot area (`mt-1`). */
|
|
109
|
-
export const TAG_GAP = 4;
|
|
110
|
-
/**
|
|
111
|
-
* The size of what a box holds under its label: its tag row, then its dot area. The layout takes
|
|
112
|
-
* it before the box can be measured (a server render), so the dots, placed up from the box's
|
|
113
|
-
* bottom, already sit under the tags and never on them.
|
|
114
|
-
*/
|
|
115
|
-
export function boxContentSize(slots) {
|
|
116
|
-
const area = dotArea(slots);
|
|
117
|
-
return { width: area.width, height: TAG_ROW + TAG_GAP + area.height };
|
|
118
|
-
}
|
|
119
|
-
/** Where slot `i` of `slots` sits in `box`: its left edge and its vertical centre, on the box's fixed grid. */
|
|
120
|
-
export function slotAt(box, i, slots) {
|
|
121
|
-
const top = box.y + box.height - BOX_INSET - rowsFor(slots) * SLOT_H;
|
|
122
|
-
return {
|
|
123
|
-
left: box.x + box.width / 2 - (COLS * SLOT_W) / 2 + (i % COLS) * SLOT_W,
|
|
124
|
-
y: top + Math.floor(i / COLS) * SLOT_H + SLOT_H / 2,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
25
|
/** One dot: its colour, its short ref, its full label as name and tooltip, a link when it has `href`. */
|
|
128
|
-
function Dot({ item, where, left, y, arriving, }) {
|
|
26
|
+
function Dot({ item, where, left, y, arriving, shift, shifted, }) {
|
|
129
27
|
const { props, tooltip } = useTooltip(item.label, { describes: false, tap: item.href === undefined }, {});
|
|
130
28
|
const className = cx("pointer-events-auto absolute flex h-5 items-center gap-1 rounded px-1 text-xs text-slate-700", focusRing);
|
|
131
|
-
const position = {
|
|
29
|
+
const position = {
|
|
30
|
+
...props.style,
|
|
31
|
+
left: left + 2,
|
|
32
|
+
top: y - 10,
|
|
33
|
+
width: SLOT_W - 4,
|
|
34
|
+
...(shift ? { "--hc-sm-dx": `${shift.dx}px`, "--hc-sm-dy": `${shift.dy}px` } : {}),
|
|
35
|
+
};
|
|
132
36
|
const body = (_jsxs(_Fragment, { children: [_jsx("span", { "aria-hidden": "true", className: cx("size-2.5 shrink-0 rounded-full bg-current", colourOf(item.id)) }), _jsx("span", { "aria-hidden": "true", className: "truncate", children: item.id })] }));
|
|
133
37
|
const own = {
|
|
134
38
|
...props,
|
|
135
39
|
style: position,
|
|
136
40
|
"aria-label": `${item.label}, in ${where}`,
|
|
137
41
|
...(arriving ? { "data-hc-sm-arrive": "" } : {}),
|
|
42
|
+
...(shift ? { "data-hc-sm-shift": shift.seq % 2 === 0 ? "b" : "a" } : {}),
|
|
43
|
+
onAnimationEnd: (event) => {
|
|
44
|
+
if (event.target === event.currentTarget && event.animationName.startsWith("hc-sm-shift")) {
|
|
45
|
+
shifted();
|
|
46
|
+
}
|
|
47
|
+
},
|
|
138
48
|
};
|
|
139
49
|
return (_jsxs(_Fragment, { children: [item.href === undefined ? (_jsx("span", { ...own, role: "img", tabIndex: 0, className: className, children: body })) : (renderLink({
|
|
140
50
|
...own,
|
|
@@ -143,36 +53,31 @@ function Dot({ item, where, left, y, arriving, }) {
|
|
|
143
53
|
children: body,
|
|
144
54
|
})), tooltip] }));
|
|
145
55
|
}
|
|
146
|
-
/** The dot that runs from an item's old box to its new one: along the arrow between them, else straight. */
|
|
147
|
-
function Travel({ item, move, drawn, to, done, }) {
|
|
148
|
-
const from = drawn.nodes.find((n) => n.id === move.from);
|
|
149
|
-
if (!from)
|
|
150
|
-
return null;
|
|
151
|
-
const start = slotAt(from, 0, 1);
|
|
152
|
-
const x0 = start.left + DOT_X;
|
|
153
|
-
const edge = drawn.edges.find((e) => e.from === move.from && e.to === move.to);
|
|
154
|
-
const via = edge ? ` L${edge.path.slice(1)}` : "";
|
|
155
|
-
const d = `M${x0},${start.y}${via} L${to.x},${to.y}`;
|
|
156
|
-
return (_jsx("span", { "aria-hidden": "true", "data-hc-sm-travel": "", onAnimationEnd: done, className: cx("absolute left-0 top-0 size-2.5 rounded-full bg-current", colourOf(item.id)), style: { offsetPath: `path("${d}")` } }));
|
|
157
|
-
}
|
|
158
56
|
/** The `+N` (or "fewer") switch in a full box's last slot. */
|
|
159
57
|
function MoreToggle({ hidden, open, where, left, y, toggle, }) {
|
|
160
58
|
return (_jsx("button", { type: "button", "aria-expanded": open, "aria-label": open ? `Show fewer in ${where}` : `Show ${hidden} more in ${where}`, onClick: toggle, className: cx("pointer-events-auto absolute flex h-5 cursor-pointer items-center rounded px-1 text-xs font-medium text-violet-600 hover:text-violet-700", focusRing), style: { left: left + 2, top: y - 10 }, children: open ? "fewer" : `+${hidden}` }));
|
|
161
59
|
}
|
|
60
|
+
/** The run of an item that has just come into `box`, ending at `end`, if it has one. */
|
|
61
|
+
function runInto(item, box, end, places) {
|
|
62
|
+
const move = places.moves.get(item.id);
|
|
63
|
+
// A run needs the box it leaves; with that box gone (the last unplaced item placed) it just lands.
|
|
64
|
+
if (move?.kind !== "travel" || move.to !== box.id)
|
|
65
|
+
return null;
|
|
66
|
+
if (!places.layout.nodes.some((n) => n.id === move.from))
|
|
67
|
+
return null;
|
|
68
|
+
return (_jsx(Travel, { move: move, drawn: places.layout, to: { x: end.left + DOT_X, y: end.y }, colour: places.runColour(move.from, move.to) ?? colourOf(item.id), done: () => places.done(item.id, move.seq) }, `${item.id}:${move.seq}`));
|
|
69
|
+
}
|
|
162
70
|
/** One item's dot in `box`, and its run there when it has just moved in. */
|
|
163
71
|
function placeDot(item, at, box, places) {
|
|
164
72
|
const move = places.moves.get(item.id);
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
if (!move || !arriving)
|
|
169
|
-
return [dot];
|
|
170
|
-
return [
|
|
171
|
-
dot,
|
|
172
|
-
_jsx(Travel, { item: item, move: move, drawn: places.layout, to: { x: at.left + DOT_X, y: at.y }, done: () => places.done(item.id, move.seq) }, `${item.id}:${move.seq}`),
|
|
173
|
-
];
|
|
73
|
+
const run = runInto(item, box, at, places);
|
|
74
|
+
const dot = (_jsx(Dot, { item: item, where: places.names.get(box.id) ?? box.id, left: at.left, y: at.y, arriving: run !== null, shift: move?.kind === "shift" ? move : undefined, shifted: () => move && places.done(item.id, move.seq) }, `${item.id}:${box.id}`));
|
|
75
|
+
return run ? [dot, run] : [dot];
|
|
174
76
|
}
|
|
175
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* A box's dots: every item, or the first few and the `+N` that shows the rest. An item that comes
|
|
79
|
+
* in behind the `+N` runs to it.
|
|
80
|
+
*/
|
|
176
81
|
function boxDots(box, places) {
|
|
177
82
|
const here = places.inBox.get(box.id) ?? [];
|
|
178
83
|
const open = places.open.has(box.id);
|
|
@@ -182,12 +87,14 @@ function boxDots(box, places) {
|
|
|
182
87
|
if (here.length <= LIMIT)
|
|
183
88
|
return dots;
|
|
184
89
|
const at = slotAt(box, count - 1, count);
|
|
90
|
+
const behind = here.slice(shown.length).flatMap((item) => runInto(item, box, at, places) ?? []);
|
|
185
91
|
return [
|
|
186
92
|
...dots,
|
|
93
|
+
...behind,
|
|
187
94
|
_jsx(MoreToggle, { hidden: here.length - shown.length, open: open, where: places.names.get(box.id) ?? box.id, left: at.left, y: at.y, toggle: () => places.toggle(box.id) }, `more:${box.id}`),
|
|
188
95
|
];
|
|
189
96
|
}
|
|
190
97
|
/** Every box's dots, over the drawn diagram. */
|
|
191
98
|
export function dotLayer(layout, places) {
|
|
192
|
-
return (_jsxs(_Fragment, { children: [_jsx("style", { children:
|
|
99
|
+
return (_jsxs(_Fragment, { children: [_jsx("style", { children: moveCss }), layout.nodes.flatMap((box) => boxDots(box, { ...places, layout }))] }));
|
|
193
100
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { FlowNodeBox, FlowSpacing } from "./flowLayout.js";
|
|
2
|
+
import type { StateMachineItem } from "./StateMachine.js";
|
|
3
|
+
/**
|
|
4
|
+
* The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
|
|
5
|
+
* (a box's `data-measure`), where a server render would turn a control character into U+FFFD.
|
|
6
|
+
*/
|
|
7
|
+
export declare const UNPLACED = "hc-sm:unplaced";
|
|
8
|
+
/** A dot's slot in its box: three to a row, each wide enough for a short ref. */
|
|
9
|
+
export declare const SLOT_W = 76;
|
|
10
|
+
export declare const SLOT_H = 22;
|
|
11
|
+
export declare const COLS = 3;
|
|
12
|
+
/** Up to this many dots show in a box; past it, one fewer and a `+N` that shows the rest. */
|
|
13
|
+
export declare const LIMIT = 6;
|
|
14
|
+
/** From a slot's left edge to its dot's centre: the chip's inset, its padding and half the dot. */
|
|
15
|
+
export declare const DOT_X: number;
|
|
16
|
+
/** Which box each item sits in: its state's, else the unplaced box. */
|
|
17
|
+
export declare function placeOf(items: readonly StateMachineItem[], known: ReadonlySet<string>): Map<string, string>;
|
|
18
|
+
/** 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. */
|
|
19
|
+
export declare function slotsFor(count: number, open: boolean): number;
|
|
20
|
+
/**
|
|
21
|
+
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
22
|
+
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
23
|
+
* its size does not depend on how many items it holds.
|
|
24
|
+
*/
|
|
25
|
+
export declare function rowsFor(slots: number): number;
|
|
26
|
+
/**
|
|
27
|
+
* The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
|
|
28
|
+
* height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
|
|
29
|
+
*/
|
|
30
|
+
export declare function dotArea(slots: number): {
|
|
31
|
+
width: number;
|
|
32
|
+
height: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
|
|
36
|
+
* two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
|
|
37
|
+
*/
|
|
38
|
+
export declare const MACHINE_SPACING: FlowSpacing;
|
|
39
|
+
/** A box's tag row: one small pill's height. */
|
|
40
|
+
export declare const TAG_ROW = 20;
|
|
41
|
+
/** The gap between the tag row and the dot area (`mt-1`). */
|
|
42
|
+
export declare const TAG_GAP = 4;
|
|
43
|
+
/**
|
|
44
|
+
* The size of what a box holds under its label: its tag row, then its dot area. The layout takes
|
|
45
|
+
* it before the box can be measured (a server render), so the dots, placed up from the box's
|
|
46
|
+
* bottom, already sit under the tags and never on them.
|
|
47
|
+
*/
|
|
48
|
+
export declare function boxContentSize(slots: number): {
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
};
|
|
52
|
+
/** Where slot `i` of `slots` sits in `box`: its left edge and its vertical centre, on the box's fixed grid. */
|
|
53
|
+
export declare function slotAt(box: FlowNodeBox, i: number, slots: number): {
|
|
54
|
+
left: number;
|
|
55
|
+
y: number;
|
|
56
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// A StateMachine box's fixed grid of dot slots, and which box each item sits in.
|
|
2
|
+
/**
|
|
3
|
+
* The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
|
|
4
|
+
* (a box's `data-measure`), where a server render would turn a control character into U+FFFD.
|
|
5
|
+
*/
|
|
6
|
+
export const UNPLACED = "hc-sm:unplaced";
|
|
7
|
+
/** A dot's slot in its box: three to a row, each wide enough for a short ref. */
|
|
8
|
+
export const SLOT_W = 76;
|
|
9
|
+
export const SLOT_H = 22;
|
|
10
|
+
export const COLS = 3;
|
|
11
|
+
/** Up to this many dots show in a box; past it, one fewer and a `+N` that shows the rest. */
|
|
12
|
+
export const LIMIT = 6;
|
|
13
|
+
/** From a box's bottom edge to its dots' bottom: its border and its bottom padding. */
|
|
14
|
+
const BOX_INSET = 9;
|
|
15
|
+
/** From a slot's left edge to its dot's centre: the chip's inset, its padding and half the dot. */
|
|
16
|
+
export const DOT_X = 2 + 4 + 5;
|
|
17
|
+
/** Which box each item sits in: its state's, else the unplaced box. */
|
|
18
|
+
export function placeOf(items, known) {
|
|
19
|
+
return new Map(items.map((i) => [i.id, known.has(i.state) ? i.state : UNPLACED]));
|
|
20
|
+
}
|
|
21
|
+
/** 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. */
|
|
22
|
+
export function slotsFor(count, open) {
|
|
23
|
+
if (count <= LIMIT)
|
|
24
|
+
return count;
|
|
25
|
+
return open ? count + 1 : LIMIT;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
|
|
29
|
+
* or the rows of an open box's full list. A box with few or no items reserves the same rows, so
|
|
30
|
+
* its size does not depend on how many items it holds.
|
|
31
|
+
*/
|
|
32
|
+
export function rowsFor(slots) {
|
|
33
|
+
return Math.ceil(Math.max(LIMIT, slots) / COLS);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
|
|
37
|
+
* height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
|
|
38
|
+
*/
|
|
39
|
+
export function dotArea(slots) {
|
|
40
|
+
return { width: COLS * SLOT_W, height: rowsFor(slots) * SLOT_H };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
|
|
44
|
+
* two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
|
|
45
|
+
*/
|
|
46
|
+
export const MACHINE_SPACING = { node: 24, rank: 32, edge: 8 };
|
|
47
|
+
/** A box's tag row: one small pill's height. */
|
|
48
|
+
export const TAG_ROW = 20;
|
|
49
|
+
/** The gap between the tag row and the dot area (`mt-1`). */
|
|
50
|
+
export const TAG_GAP = 4;
|
|
51
|
+
/**
|
|
52
|
+
* The size of what a box holds under its label: its tag row, then its dot area. The layout takes
|
|
53
|
+
* it before the box can be measured (a server render), so the dots, placed up from the box's
|
|
54
|
+
* bottom, already sit under the tags and never on them.
|
|
55
|
+
*/
|
|
56
|
+
export function boxContentSize(slots) {
|
|
57
|
+
const area = dotArea(slots);
|
|
58
|
+
return { width: area.width, height: TAG_ROW + TAG_GAP + area.height };
|
|
59
|
+
}
|
|
60
|
+
/** Where slot `i` of `slots` sits in `box`: its left edge and its vertical centre, on the box's fixed grid. */
|
|
61
|
+
export function slotAt(box, i, slots) {
|
|
62
|
+
const top = box.y + box.height - BOX_INSET - rowsFor(slots) * SLOT_H;
|
|
63
|
+
return {
|
|
64
|
+
left: box.x + box.width / 2 - (COLS * SLOT_W) / 2 + (i % COLS) * SLOT_W,
|
|
65
|
+
y: top + Math.floor(i / COLS) * SLOT_H + SLOT_H / 2,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
import type { FlowLayout } from "./flowLayout.js";
|
|
3
|
+
import type { StateMachineItem } from "./StateMachine.js";
|
|
4
|
+
export declare const moveCss = "[data-hc-sm-travel]{offset-anchor:center;offset-rotate:0deg;animation:hc-sm-travel .6s ease-in-out forwards}[data-hc-sm-arrive]{animation:hc-sm-arrive .6s linear}[data-hc-sm-shift=\"a\"]{animation:hc-sm-shift-a .6s ease-in-out}[data-hc-sm-shift=\"b\"]{animation:hc-sm-shift-b .6s ease-in-out}@keyframes hc-sm-travel{from{offset-distance:0%}to{offset-distance:100%}}@keyframes hc-sm-arrive{from,to{visibility:hidden}}@keyframes hc-sm-shift-a{from{translate:var(--hc-sm-dx) var(--hc-sm-dy)}}@keyframes hc-sm-shift-b{from{translate:var(--hc-sm-dx) var(--hc-sm-dy)}}@media (prefers-reduced-motion:reduce){[data-hc-sm-travel]{visibility:hidden;animation-duration:1ms}[data-hc-sm-arrive],[data-hc-sm-shift]{animation:none}}";
|
|
5
|
+
/**
|
|
6
|
+
* Where an item's dot sits: its box, its slot there (`null` when it is one of the dots a full box
|
|
7
|
+
* hides behind its `+N`), and the number of slots the box draws.
|
|
8
|
+
*/
|
|
9
|
+
type Seat = {
|
|
10
|
+
box: string;
|
|
11
|
+
slot: number | null;
|
|
12
|
+
slots: number;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* An item's move, `seq` restarting its animation when it moves again: a run from its old box
|
|
16
|
+
* (`from`, from its old `slot` of `slots` there) to its new one (`to`), or a slide inside its box
|
|
17
|
+
* by `dx` and `dy` pixels, from its old slot to its new one.
|
|
18
|
+
*/
|
|
19
|
+
export type Move = {
|
|
20
|
+
kind: "travel";
|
|
21
|
+
from: string;
|
|
22
|
+
to: string;
|
|
23
|
+
slot: number;
|
|
24
|
+
slots: number;
|
|
25
|
+
seq: number;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "shift";
|
|
28
|
+
dx: number;
|
|
29
|
+
dy: number;
|
|
30
|
+
seq: number;
|
|
31
|
+
};
|
|
32
|
+
/** Every item's seat: its box, in item order, and its slot there. */
|
|
33
|
+
export declare function seatsOf(items: readonly StateMachineItem[], known: ReadonlySet<string>, open: ReadonlySet<string>): Map<string, Seat>;
|
|
34
|
+
/**
|
|
35
|
+
* The move from `was` to `now`, if the item moved: a run between boxes, or a slide between two
|
|
36
|
+
* shown slots of one. A run starts from its old slot in the grid its old box draws now
|
|
37
|
+
* (`fromSlots`), since the run is drawn on the new layout.
|
|
38
|
+
*/
|
|
39
|
+
export declare function moveOf(was: Seat, now: Seat, seq: number, fromSlots: number): Move | null;
|
|
40
|
+
/**
|
|
41
|
+
* Remembers where every dot sat, and when the seats change records each dot's move, during render
|
|
42
|
+
* (React's "adjust state when a prop changes"), so the re-render that measures the boxes does not
|
|
43
|
+
* lose it. A move ends on its animation's `animationend` (`done`). A change of seats records its own
|
|
44
|
+
* moves and keeps the ones under way whose dot kept its seat (opening a box records none).
|
|
45
|
+
*/
|
|
46
|
+
export declare function useMoves(items: readonly StateMachineItem[], known: ReadonlySet<string>, open: ReadonlySet<string>): {
|
|
47
|
+
moves: ReadonlyMap<string, Move>;
|
|
48
|
+
done: (id: string, seq: number) => void;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* The dot that runs from an item's old slot to its new one: out of its old box, along the arrow
|
|
52
|
+
* between the two boxes (straight across when there is none), and into its slot, in `colour`.
|
|
53
|
+
*/
|
|
54
|
+
export declare function Travel({ move, drawn, to, colour, done, }: {
|
|
55
|
+
move: Extract<Move, {
|
|
56
|
+
kind: "travel";
|
|
57
|
+
}>;
|
|
58
|
+
drawn: FlowLayout;
|
|
59
|
+
to: {
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
};
|
|
63
|
+
colour: string;
|
|
64
|
+
done: () => void;
|
|
65
|
+
}): ReactElement | null;
|
|
66
|
+
/**
|
|
67
|
+
* The SVG path of a run: from the item's old slot in the box it left, along the drawn arrow from
|
|
68
|
+
* that box to the new one (every bend of it) when there is one, else straight, to `to`, the centre
|
|
69
|
+
* of its new dot. `null` when the box it left is not drawn.
|
|
70
|
+
*/
|
|
71
|
+
export declare function runPath(drawn: FlowLayout, move: {
|
|
72
|
+
from: string;
|
|
73
|
+
to: string;
|
|
74
|
+
slot: number;
|
|
75
|
+
slots: number;
|
|
76
|
+
}, to: {
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
}): string | null;
|
|
80
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { cx } from "./cx.js";
|
|
4
|
+
import { COLS, DOT_X, LIMIT, placeOf, SLOT_H, SLOT_W, slotAt, slotsFor, } from "./stateMachineGrid.js";
|
|
5
|
+
// Every move is CSS. A dot runs along the arrow's path (`offset-path`) in 600ms while the dot at
|
|
6
|
+
// its new place waits hidden for the same time; a dot that stays in its box but changes slot (one
|
|
7
|
+
// before it left, or one came in) slides from its old slot to its new one (`translate`, from the
|
|
8
|
+
// `--hc-sm-dx` and `--hc-sm-dy` it is given), under two keyframe names taken in turn so a second
|
|
9
|
+
// slide restarts. Nothing is timed in script: each ends on its own `animationend`. With reduced
|
|
10
|
+
// motion the run is hidden and over at once (so it still ends), and every dot is in its new place
|
|
11
|
+
// at once.
|
|
12
|
+
export const moveCss = `[data-hc-sm-travel]{offset-anchor:center;offset-rotate:0deg;animation:hc-sm-travel .6s ease-in-out forwards}[data-hc-sm-arrive]{animation:hc-sm-arrive .6s linear}[data-hc-sm-shift="a"]{animation:hc-sm-shift-a .6s ease-in-out}[data-hc-sm-shift="b"]{animation:hc-sm-shift-b .6s ease-in-out}@keyframes hc-sm-travel{from{offset-distance:0%}to{offset-distance:100%}}@keyframes hc-sm-arrive{from,to{visibility:hidden}}@keyframes hc-sm-shift-a{from{translate:var(--hc-sm-dx) var(--hc-sm-dy)}}@keyframes hc-sm-shift-b{from{translate:var(--hc-sm-dx) var(--hc-sm-dy)}}@media (prefers-reduced-motion:reduce){[data-hc-sm-travel]{visibility:hidden;animation-duration:1ms}[data-hc-sm-arrive],[data-hc-sm-shift]{animation:none}}`;
|
|
13
|
+
/** The slot of the `i`th of `count` items in a box: its own, or `null` when the `+N` hides it. */
|
|
14
|
+
function slotOf(i, count, open) {
|
|
15
|
+
return count <= LIMIT || open || i < LIMIT - 1 ? i : null;
|
|
16
|
+
}
|
|
17
|
+
/** Every item's seat: its box, in item order, and its slot there. */
|
|
18
|
+
export function seatsOf(items, known, open) {
|
|
19
|
+
const placed = placeOf(items, known);
|
|
20
|
+
const counts = new Map();
|
|
21
|
+
for (const box of placed.values())
|
|
22
|
+
counts.set(box, (counts.get(box) ?? 0) + 1);
|
|
23
|
+
const taken = new Map();
|
|
24
|
+
const seats = new Map();
|
|
25
|
+
for (const [id, box] of placed) {
|
|
26
|
+
const i = taken.get(box) ?? 0;
|
|
27
|
+
taken.set(box, i + 1);
|
|
28
|
+
const count = counts.get(box) ?? 0;
|
|
29
|
+
const isOpen = open.has(box);
|
|
30
|
+
seats.set(id, { box, slot: slotOf(i, count, isOpen), slots: slotsFor(count, isOpen) });
|
|
31
|
+
}
|
|
32
|
+
return seats;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The move from `was` to `now`, if the item moved: a run between boxes, or a slide between two
|
|
36
|
+
* shown slots of one. A run starts from its old slot in the grid its old box draws now
|
|
37
|
+
* (`fromSlots`), since the run is drawn on the new layout.
|
|
38
|
+
*/
|
|
39
|
+
export function moveOf(was, now, seq, fromSlots) {
|
|
40
|
+
if (was.box !== now.box) {
|
|
41
|
+
const slot = was.slot ?? LIMIT - 1;
|
|
42
|
+
return { kind: "travel", from: was.box, to: now.box, slot, slots: fromSlots, seq };
|
|
43
|
+
}
|
|
44
|
+
if (was.slot === null || now.slot === null || was.slot === now.slot)
|
|
45
|
+
return null;
|
|
46
|
+
const dx = ((was.slot % COLS) - (now.slot % COLS)) * SLOT_W;
|
|
47
|
+
const dy = (Math.floor(was.slot / COLS) - Math.floor(now.slot / COLS)) * SLOT_H;
|
|
48
|
+
return { kind: "shift", dx, dy, seq };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The moves after the seats change from `was` to `now`: each dot that changed seat gets a new move,
|
|
52
|
+
* and a move under way is kept while its dot keeps its seat.
|
|
53
|
+
*/
|
|
54
|
+
function nextMoves(was, now, moves) {
|
|
55
|
+
const slotsNow = new Map([...now.values()].map((seat) => [seat.box, seat.slots]));
|
|
56
|
+
const next = new Map();
|
|
57
|
+
for (const [id, seat] of now) {
|
|
58
|
+
const from = was.get(id);
|
|
59
|
+
if (!from)
|
|
60
|
+
continue;
|
|
61
|
+
const going = moves.get(id);
|
|
62
|
+
const move = moveOf(from, seat, (going?.seq ?? 0) + 1, slotsNow.get(from.box) ?? 0);
|
|
63
|
+
if (move)
|
|
64
|
+
next.set(id, move);
|
|
65
|
+
else if (going && from.box === seat.box && from.slot === seat.slot)
|
|
66
|
+
next.set(id, going);
|
|
67
|
+
}
|
|
68
|
+
return next;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Remembers where every dot sat, and when the seats change records each dot's move, during render
|
|
72
|
+
* (React's "adjust state when a prop changes"), so the re-render that measures the boxes does not
|
|
73
|
+
* lose it. A move ends on its animation's `animationend` (`done`). A change of seats records its own
|
|
74
|
+
* moves and keeps the ones under way whose dot kept its seat (opening a box records none).
|
|
75
|
+
*/
|
|
76
|
+
export function useMoves(items, known, open) {
|
|
77
|
+
const now = seatsOf(items, known, open);
|
|
78
|
+
const key = JSON.stringify([...now]);
|
|
79
|
+
const [seen, setSeen] = useState({ key, seats: now });
|
|
80
|
+
const [moves, setMoves] = useState(new Map());
|
|
81
|
+
// Compared by value: a caller's `items` is often a new array on every render.
|
|
82
|
+
if (seen.key !== key) {
|
|
83
|
+
setSeen({ key, seats: now });
|
|
84
|
+
setMoves(nextMoves(seen.seats, now, moves));
|
|
85
|
+
}
|
|
86
|
+
const done = (id, seq) => setMoves((current) => {
|
|
87
|
+
if (current.get(id)?.seq !== seq)
|
|
88
|
+
return current;
|
|
89
|
+
const next = new Map(current);
|
|
90
|
+
next.delete(id);
|
|
91
|
+
return next;
|
|
92
|
+
});
|
|
93
|
+
return { moves, done };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The dot that runs from an item's old slot to its new one: out of its old box, along the arrow
|
|
97
|
+
* between the two boxes (straight across when there is none), and into its slot, in `colour`.
|
|
98
|
+
*/
|
|
99
|
+
export function Travel({ move, drawn, to, colour, done, }) {
|
|
100
|
+
const d = runPath(drawn, move, to);
|
|
101
|
+
if (d === null)
|
|
102
|
+
return null;
|
|
103
|
+
return (_jsx("span", { "aria-hidden": "true", "data-hc-sm-travel": "", onAnimationEnd: done, className: cx("absolute left-0 top-0 size-2.5 rounded-full bg-current", colour), style: { offsetPath: `path("${d}")` } }));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The SVG path of a run: from the item's old slot in the box it left, along the drawn arrow from
|
|
107
|
+
* that box to the new one (every bend of it) when there is one, else straight, to `to`, the centre
|
|
108
|
+
* of its new dot. `null` when the box it left is not drawn.
|
|
109
|
+
*/
|
|
110
|
+
export function runPath(drawn, move, to) {
|
|
111
|
+
const from = drawn.nodes.find((n) => n.id === move.from);
|
|
112
|
+
if (!from)
|
|
113
|
+
return null;
|
|
114
|
+
const start = slotAt(from, move.slot, move.slots);
|
|
115
|
+
const edge = drawn.edges.find((e) => e.from === move.from && e.to === move.to)?.path;
|
|
116
|
+
const via = edge ? ` L${edge.slice(1)}` : "";
|
|
117
|
+
return `M${start.left + DOT_X},${start.y}${via} L${to.x},${to.y}`;
|
|
118
|
+
}
|
|
@@ -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.
|
|
3
|
+
"version": "0.15.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",
|