@slashsuite/ui-react 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,7 +31,7 @@ import { StateMachine } from "@slashsuite/ui-react";
31
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 to its new box; there is nothing to call.
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
 
@@ -64,11 +64,11 @@ import { StateMachine } from "@slashsuite/ui-react";
64
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.
65
65
  - Each dot is named by its item's label and its state (`Fix the login, in Review`), and takes the focus; one with `href` is a link. A box with more than six shows a `+N` button that shows the rest.
66
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 a dot moves to its new state at once, with no run.
67
+ - With reduced motion every dot moves to its new place at once, with no run and no slide.
68
68
 
69
69
  ### Props
70
70
  ### StateMachine
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
+ 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.
72
72
  - active: string. The `id` of the one state to ring as current.
73
73
  - id: string. The root element's `id`: an anchor, or the target of a `for` or an `aria-describedby`.
74
74
  - items: readonly StateMachineItem[] (default []). The things in the machine, each in the state its `state` names.
@@ -62,8 +62,10 @@ export type StateMachineItem = {
62
62
  * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
63
63
  * full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
64
64
  * a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
65
- * dot runs along the arrow between the two (straight across when there is none); with reduced
66
- * motion it moves at once. `active` rings the one state that is current.
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.
67
69
  *
68
70
  * `fromXState` makes `states` and `transitions` from an xstate machine.
69
71
  */
@@ -6,7 +6,9 @@ import { Pill } from "./Pill.js";
6
6
  import { Bone, BusyLabel, busy, useLoading } from "./skeleton.js";
7
7
  import { Legend, VIZ_TONE } from "./StackedBar.js";
8
8
  import { moverOf, toneByRole } from "./stateMachineRoles.js";
9
- import { boxContentSize, MACHINE_SPACING, dotArea, dotLayer, placeOf, slotsFor, UNPLACED, useMoves, } from "./stateMachineDots.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";
10
12
  import { useFlowLayout } from "./useFlowLayout.js";
11
13
  /** Each kind's box tone, and the tag that says it in words. */
12
14
  const KIND_TONE = {
@@ -79,10 +81,9 @@ function boxesFor(states, inBox, open, tones) {
79
81
  ];
80
82
  }
81
83
  /** Which boxes show all their dots, and the switch that opens or closes one. */
82
- function useOpenBoxes(opened) {
84
+ function useOpenBoxes() {
83
85
  const [open, setOpen] = useState(new Set());
84
86
  const toggle = (id) => {
85
- opened();
86
87
  setOpen((current) => {
87
88
  const next = new Set(current);
88
89
  if (!next.delete(id))
@@ -109,16 +110,18 @@ function useOpenBoxes(opened) {
109
110
  * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
110
111
  * full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
111
112
  * a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
112
- * dot runs along the arrow between the two (straight across when there is none); with reduced
113
- * motion it moves at once. `active` rings the one state that is current.
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.
114
117
  *
115
118
  * `fromXState` makes `states` and `transitions` from an xstate machine.
116
119
  */
117
120
  export function StateMachine({ label, states, transitions, items = [], active, roleTones: pinned, loading, loadingLabel, ...rest }) {
118
121
  const { loading: waiting, owner } = useLoading(loading);
119
122
  const known = new Set(states.map((s) => s.id));
120
- const { moves, done, clear } = useMoves(items, known);
121
- const [open, toggle] = useOpenBoxes(clear);
123
+ const [open, toggle] = useOpenBoxes();
124
+ const { moves, done } = useMoves(items, known, open);
122
125
  const inBox = itemsByBox(items, known);
123
126
  const tones = toneByRole(states, transitions, pinned);
124
127
  const nodes = boxesFor(states, inBox, open, tones);
@@ -127,6 +130,14 @@ export function StateMachine({ label, states, transitions, items = [], active, r
127
130
  const tone = tones.get(moverOf(t, owners) ?? "");
128
131
  return { from: t.from, to: t.to, label: t.event, ...(tone ? { colour: VIZ_TONE[tone] } : {}) };
129
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
+ };
130
141
  const { canvas, drawn } = useFlowLayout(nodes, edges, "down", {
131
142
  uniform: true,
132
143
  spacing: MACHINE_SPACING,
@@ -135,5 +146,5 @@ export function StateMachine({ label, states, transitions, items = [], active, r
135
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 })] }));
136
147
  }
137
148
  const names = new Map(nodes.map((n) => [n.id, n.label]));
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` }) }))] }));
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` }) }))] }));
139
150
  }
@@ -1,74 +1,8 @@
1
- import { type ReactElement } from "react";
2
- import type { FlowLayout, FlowNodeBox, FlowSpacing } from "./flowLayout.js";
1
+ import type { ReactElement } from "react";
2
+ import type { FlowLayout } from "./flowLayout.js";
3
3
  import type { StateMachineItem } from "./StateMachine.js";
4
- /**
5
- * The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
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;
@@ -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 = { ...props.style, left: left + 2, top: y - 10, width: SLOT_W - 4 };
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
- // A run needs the box it leaves; with that box gone (the last unplaced item placed) it just lands.
166
- const arriving = move?.to === box.id && places.layout.nodes.some((n) => n.id === move.from);
167
- const dot = (_jsx(Dot, { item: item, where: places.names.get(box.id) ?? box.id, left: at.left, y: at.y, arriving: arriving }, `${item.id}:${box.id}`));
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
- /** A box's dots: every item, or the first few and the `+N` that shows the rest. */
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: css }), layout.nodes.flatMap((box) => boxDots(box, { ...places, layout }))] }));
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slashsuite/ui-react",
3
- "version": "0.14.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",