@slashsuite/ui-react 0.13.2 → 0.13.3

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.
@@ -29,6 +29,7 @@ import { StateMachine } from "@slashsuite/ui-react";
29
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
30
  - `kind` says what a state is: `human` for a state waiting on a person (the warn tone), `failed` for an error (danger), `terminal` for done (muted), `initial` for the start. Each is also a tag in words. Leave the rest `normal`.
31
31
  - `owner` is the role or lane that acts in a state, a tag on its box.
32
+ - Keep a state's `label` short: every box is drawn one size, so the boxes line up, and a long label wraps inside it and makes every box a line taller.
32
33
  - An item's `id` is its short ref, shown by its dot; its `label` is the full name, its tooltip and what a screen reader says. Give `href` and the dot is a link.
33
34
  - Change an item's `state` and its dot runs along the arrow to its new box; there is nothing to call.
34
35
  - `active` rings the one state that is current, for a machine with one thing in it.
@@ -60,13 +61,13 @@ import { StateMachine } from "@slashsuite/ui-react";
60
61
 
61
62
  ### Accessibility
62
63
  - The drawing is an image named by `label`, with the count of states and transitions; the box round it is a group named by `label`, and takes the keyboard focus so a machine that scrolls can be scrolled with the arrow keys.
63
- - Each dot is named by its item's label and its state (`Fix the login, in Review`), and takes the focus; one with `href` is a link. A box with more than nine shows a `+N` button that shows the rest.
64
+ - 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.
64
65
  - A visually hidden list after it carries every state, its tags and the transitions out of it, as `FlowDiagram`'s does.
65
66
  - With reduced motion a dot moves to its new state at once, with no run.
66
67
 
67
68
  ### Props
68
69
  ### StateMachine
69
- A state machine, drawn, with the things moving through it: each state a box, each transition an arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` sets its look and a tag that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and `initial` is tagged start. Its `owner` is a tag beside it. 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 nine and a `+N` for the rest. The dot's name and tooltip is its full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its dot runs along the arrow between the two (straight across when there is none); with reduced motion it moves at once. `active` rings the one state that is current. `fromXState` makes `states` and `transitions` from an xstate machine.
70
+ A state machine, drawn, with the things moving through it: each state a box, each transition an arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` sets its look and a tag that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping inside it, and the boxes sit close. Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its dot runs along the arrow between the two (straight across when there is none); with reduced motion it moves at once. `active` rings the one state that is current. `fromXState` makes `states` and `transitions` from an xstate machine.
70
71
  - active: string. The `id` of the one state to ring as current.
71
72
  - id: string. The root element's `id`: an anchor, or the target of a `for` or an `aria-describedby`.
72
73
  - items: readonly StateMachineItem[] (default []). The things in the machine, each in the state its `state` names.
@@ -77,9 +77,11 @@ export type ViewNode = Omit<FlowNode, "tone"> & {
77
77
  * `canvas` ref and the layout `drawn`. Unlike `overlay`, the layer is read by a screen reader and its
78
78
  * children may take the pointer (`pointer-events-auto`), so they can be links. With a layer, the
79
79
  * image is the drawing alone and the scrolling box round it is a group named by `label`. With `fit`,
80
- * a drawing wider than its room is shrunk to the room's width instead of scrolling. Internal.
80
+ * a drawing wider than its room is shrunk to the room's width instead of scrolling. With `uniform`
81
+ * (laid out by `useFlowLayout`'s `uniform`), every box fills its slot: its label wraps to the box's
82
+ * width and its content sits at the bottom. Internal.
81
83
  */
82
- export declare function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit, ...rest }: {
84
+ export declare function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit, uniform, ...rest }: {
83
85
  label: string;
84
86
  nodes: readonly ViewNode[];
85
87
  edges: readonly FlowEdge[];
@@ -89,4 +91,5 @@ export declare function FlowView({ label, nodes, edges, active, overlay, layer,
89
91
  canvas: ReturnType<typeof useFlowLayout>["canvas"];
90
92
  drawn: FlowLayout;
91
93
  fit?: boolean | undefined;
94
+ uniform?: boolean | undefined;
92
95
  } & Passthrough): ReactElement;
@@ -1,21 +1,8 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useId } from "react";
3
- import { TONE_SURFACE, TONE_TEXT } from "./Callout.js";
4
- import { cx } from "./cx.js";
3
+ import { FlowBoxView } from "./flowBox.js";
5
4
  import { Bone, BusyLabel, busy, useLoading } from "./skeleton.js";
6
- import { edgeKey, nodeKey, useFit, useFlowLayout } from "./useFlowLayout.js";
7
- /** A box's frame by tone: the Callout's soft surfaces, a white box for `neutral`, a grey one for `muted`. */
8
- const NODE_TONE = {
9
- ...TONE_SURFACE,
10
- neutral: "border-slate-300 bg-white",
11
- muted: "border-slate-200 bg-slate-50",
12
- };
13
- /** A box's ink by tone; `neutral` is the primary ink, `muted` the quiet one. */
14
- const NODE_INK = {
15
- ...TONE_TEXT,
16
- neutral: "text-slate-900",
17
- muted: "text-slate-500",
18
- };
5
+ import { edgeKey, useFit, useFlowLayout } from "./useFlowLayout.js";
19
6
  /**
20
7
  * Boxes joined by arrows, laid out for you: a flow, a pipeline, a state machine. Give it the
21
8
  * `nodes` and the `edges` between them and it places every box in ranks `direction` (`down` or
@@ -47,9 +34,11 @@ export function FlowDiagram({ label, nodes, edges, direction = "down", active, o
47
34
  * `canvas` ref and the layout `drawn`. Unlike `overlay`, the layer is read by a screen reader and its
48
35
  * children may take the pointer (`pointer-events-auto`), so they can be links. With a layer, the
49
36
  * image is the drawing alone and the scrolling box round it is a group named by `label`. With `fit`,
50
- * a drawing wider than its room is shrunk to the room's width instead of scrolling. Internal.
37
+ * a drawing wider than its room is shrunk to the room's width instead of scrolling. With `uniform`
38
+ * (laid out by `useFlowLayout`'s `uniform`), every box fills its slot: its label wraps to the box's
39
+ * width and its content sits at the bottom. Internal.
51
40
  */
52
- export function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit = false, ...rest }) {
41
+ export function FlowView({ label, nodes, edges, active, overlay, layer, canvas, drawn, fit = false, uniform = false, ...rest }) {
53
42
  const { frame, scale } = useFit(fit ? drawn.width : 0);
54
43
  const known = new Set(nodes.map((n) => n.id));
55
44
  const shown = edges.flatMap((edge, index) => known.has(edge.from) && known.has(edge.to) ? [{ edge, index }] : []);
@@ -57,7 +46,7 @@ export function FlowView({ label, nodes, edges, active, overlay, layer, canvas,
57
46
  role: "img",
58
47
  "aria-label": `${label}: ${count(known.size, "box", "boxes")} and ${count(shown.length, "arrow", "arrows")}`,
59
48
  };
60
- const drawing = _jsx(FlowCanvas, { drawn: drawn, nodes: nodes, edges: shown, active: active });
49
+ const drawing = (_jsx(FlowCanvas, { drawn: drawn, nodes: nodes, edges: shown, active: active, uniform: uniform }));
61
50
  return (_jsxs("div", { ...rest, className: "flex min-w-0 flex-col", children: [_jsx("div", { ...(layer ? { role: "group", "aria-label": label } : image),
62
51
  // A diagram wider than its room scrolls, so it takes the focus for the arrow keys.
63
52
  tabIndex: 0, ref: frame, className: "max-w-full overflow-auto rounded-lg outline-offset-2 focus-visible:outline-2 focus-visible:outline-violet-600", children: _jsx("div", { className: "mx-auto", style: { width: drawn.width * scale, height: drawn.height * scale }, children: _jsxs("div", { ref: canvas, className: "relative origin-top-left", style: {
@@ -69,7 +58,7 @@ export function FlowView({ label, nodes, edges, active, overlay, layer, canvas,
69
58
  /** `1 box`, `3 boxes`. */
70
59
  const count = (n, one, many) => `${n} ${n === 1 ? one : many}`;
71
60
  /** The drawing itself: the group frames, the arrows, their labels and the boxes. */
72
- function FlowCanvas({ drawn, nodes, edges, active, }) {
61
+ function FlowCanvas({ drawn, nodes, edges, active, uniform, }) {
73
62
  const marker = `flow-arrow-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
74
63
  const byId = new Map(nodes.map((n) => [n.id, n]));
75
64
  const paths = new Map(drawn.edges.map((e) => [e.index, e]));
@@ -78,18 +67,13 @@ function FlowCanvas({ drawn, nodes, edges, active, }) {
78
67
  return edge.label && at ? (_jsx("span", { "data-measure": edgeKey(index), className: "absolute -translate-x-1/2 -translate-y-1/2 whitespace-nowrap rounded bg-white px-1 text-xs text-slate-600", style: { left: at.x, top: at.y }, children: edge.label }, index)) : null;
79
68
  }), drawn.nodes.map((b) => {
80
69
  const node = byId.get(b.id);
81
- return node ? (_jsx(FlowBoxView, { box: b, node: node, active: active === b.id }, b.id)) : null;
70
+ return node ? (_jsx(FlowBoxView, { box: b, node: node, active: active === b.id, uniform: uniform }, b.id)) : null;
82
71
  })] }));
83
72
  }
84
73
  /** Every arrow as an SVG path, with its arrowhead. */
85
74
  function FlowArrows({ drawn, marker }) {
86
75
  return (_jsxs("svg", { width: drawn.width, height: drawn.height, className: "absolute inset-0 overflow-visible text-slate-400", children: [_jsx("defs", { children: _jsx("marker", { id: marker, viewBox: "0 0 10 10", refX: "9", refY: "5", markerWidth: "8", markerHeight: "8", markerUnits: "userSpaceOnUse", orient: "auto-start-reverse", children: _jsx("path", { d: "M0,0L10,5L0,10z", fill: "currentColor" }) }) }), drawn.edges.map((e) => (_jsx("path", { d: e.path, fill: "none", stroke: "currentColor", strokeWidth: 1.5, markerEnd: `url(#${marker})` }, e.index)))] }));
87
76
  }
88
- /** One box, placed: its label and content, sized by them, centred in the slot the layout gave it. */
89
- function FlowBoxView({ box, node, active, }) {
90
- const tone = node.tone ?? "neutral";
91
- 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", { "data-measure": nodeKey(box.id), className: cx("w-max max-w-72 shrink-0 rounded-lg border px-3 py-2 text-sm", NODE_TONE[tone], NODE_INK[tone], active && "outline-2 outline-offset-2 outline-violet-600"), children: [_jsx("div", { className: "font-medium", children: node.label }), node.content != null && _jsx("div", { className: "mt-2", children: node.content })] }) }));
92
- }
93
77
  /** The diagram as text for a screen reader: each box, its content, and the arrows out of it. */
94
78
  function FlowList({ label, nodes, edges, active, }) {
95
79
  const names = new Map(nodes.map((n) => [n.id, n.label]));
@@ -40,10 +40,11 @@ export type StateMachineItem = {
40
40
  * A state machine, drawn, with the things moving through it: each state a box, each transition an
41
41
  * arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` sets its look and a tag
42
42
  * that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and
43
- * `initial` is tagged start. Its `owner` is a tag beside it.
43
+ * `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping
44
+ * inside it, and the boxes sit close.
44
45
  *
45
46
  * Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
46
- * counts them; a box shows up to nine and a `+N` for the rest. The dot's name and tooltip is its
47
+ * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
47
48
  * full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
48
49
  * a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
49
50
  * dot runs along the arrow between the two (straight across when there is none); with reduced
@@ -3,7 +3,7 @@ import { useState } from "react";
3
3
  import { FlowView } from "./FlowDiagram.js";
4
4
  import { Pill } from "./Pill.js";
5
5
  import { Bone, BusyLabel, busy, useLoading } from "./skeleton.js";
6
- import { boxContentSize, dotArea, dotLayer, placeOf, slotsFor, UNPLACED, useMoves, } from "./stateMachineDots.js";
6
+ import { boxContentSize, MACHINE_SPACING, dotArea, dotLayer, placeOf, slotsFor, UNPLACED, useMoves, } from "./stateMachineDots.js";
7
7
  import { useFlowLayout } from "./useFlowLayout.js";
8
8
  /** Each kind's box tone, and the tag that says it in words. */
9
9
  const KIND_TONE = {
@@ -88,10 +88,11 @@ function useOpenBoxes(opened) {
88
88
  * A state machine, drawn, with the things moving through it: each state a box, each transition an
89
89
  * arrow named by its event, laid out like a `FlowDiagram`. A state's `kind` sets its look and a tag
90
90
  * that says it: `human` takes the warn tone, `failed` the danger tone, `terminal` is muted, and
91
- * `initial` is tagged start. Its `owner` is a tag beside it.
91
+ * `initial` is tagged start. Its `owner` is a tag beside it. Every box is one size, the label wrapping
92
+ * inside it, and the boxes sit close.
92
93
  *
93
94
  * Each of the `items` is a coloured dot with its short `id`, in the box of its `state`, which
94
- * counts them; a box shows up to nine and a `+N` for the rest. The dot's name and tooltip is its
95
+ * counts them; a box shows up to six and a `+N` for the rest. The dot's name and tooltip is its
95
96
  * full `label`, and with an `href` it is a link. An item whose state is not in the machine goes in
96
97
  * a box of its own, "Not in any state", so nothing is dropped. When an item's state changes, its
97
98
  * dot runs along the arrow between the two (straight across when there is none); with reduced
@@ -107,10 +108,13 @@ export function StateMachine({ label, states, transitions, items = [], active, l
107
108
  const inBox = itemsByBox(items, known);
108
109
  const nodes = boxesFor(states, inBox, open);
109
110
  const edges = transitions.map((t) => ({ from: t.from, to: t.to, label: t.event }));
110
- const { canvas, drawn } = useFlowLayout(nodes, edges, "down");
111
+ const { canvas, drawn } = useFlowLayout(nodes, edges, "down", {
112
+ uniform: true,
113
+ spacing: MACHINE_SPACING,
114
+ });
111
115
  if (waiting && states.length === 0) {
112
116
  return (_jsxs("figure", { ...rest, ...busy(owner, waiting), "aria-label": label, className: "flex flex-col", children: [_jsx(Bone, { shape: "block h-48 rounded-lg" }), _jsx(BusyLabel, { owner: owner, loading: waiting, label: loadingLabel })] }));
113
117
  }
114
118
  const names = new Map(nodes.map((n) => [n.id, n.label]));
115
- return (_jsx(FlowView, { ...rest, label: label, nodes: nodes, edges: edges, active: active, layer: (layout) => dotLayer(layout, { inBox, names, open, toggle, moves, done }), canvas: canvas, drawn: drawn, fit: true }));
119
+ return (_jsx(FlowView, { ...rest, label: label, nodes: nodes, edges: edges, active: active, layer: (layout) => dotLayer(layout, { inBox, names, open, toggle, moves, done }), canvas: canvas, drawn: drawn, fit: true, uniform: true }));
116
120
  }
@@ -0,0 +1,15 @@
1
+ /** One box of a `FlowDiagram` or a `StateMachine`, placed. Internal. */
2
+ import type { ReactElement } from "react";
3
+ import type { ViewNode } from "./FlowDiagram.js";
4
+ import type { FlowNodeBox } from "./flowLayout.js";
5
+ /**
6
+ * One box, placed: its label and content, sized by them, centred in the slot the layout gave it.
7
+ * A `uniform` box fills its slot instead, its label measured alone and wrapped to the box's width,
8
+ * its content at the bottom.
9
+ */
10
+ export declare function FlowBoxView({ box, node, active, uniform, }: {
11
+ box: FlowNodeBox;
12
+ node: ViewNode;
13
+ active: boolean;
14
+ uniform: boolean;
15
+ }): ReactElement;
@@ -0,0 +1,25 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { TONE_SURFACE, TONE_TEXT } from "./Callout.js";
3
+ import { cx } from "./cx.js";
4
+ import { contentKey, labelKey, nodeKey } from "./useFlowLayout.js";
5
+ /** A box's frame by tone: the Callout's soft surfaces, a white box for `neutral`, a grey one for `muted`. */
6
+ const NODE_TONE = {
7
+ ...TONE_SURFACE,
8
+ neutral: "border-slate-300 bg-white",
9
+ muted: "border-slate-200 bg-slate-50",
10
+ };
11
+ /** A box's ink by tone; `neutral` is the primary ink, `muted` the quiet one. */
12
+ const NODE_INK = {
13
+ ...TONE_TEXT,
14
+ neutral: "text-slate-900",
15
+ muted: "text-slate-500",
16
+ };
17
+ /**
18
+ * One box, placed: its label and content, sized by them, centred in the slot the layout gave it.
19
+ * A `uniform` box fills its slot instead, its label measured alone and wrapped to the box's width,
20
+ * its content at the bottom.
21
+ */
22
+ export function FlowBoxView({ box, node, active, uniform, }) {
23
+ const tone = node.tone ?? "neutral";
24
+ return (_jsx("div", { className: "absolute flex items-center justify-center", style: { left: box.x, top: box.y, width: box.width, height: box.height }, children: _jsxs("div", { ...(uniform ? {} : { "data-measure": nodeKey(box.id) }), className: cx("rounded-lg border px-3 py-2 text-sm", uniform ? "flex size-full flex-col" : "w-max max-w-72 shrink-0", NODE_TONE[tone], NODE_INK[tone], active && "outline-2 outline-offset-2 outline-violet-600"), children: [_jsx("div", { ...(uniform ? { "data-measure": labelKey(box.id) } : {}), className: cx("font-medium", uniform && "break-words"), children: node.label }), node.content != null && (_jsx("div", { ...(uniform ? { "data-measure": contentKey(box.id) } : {}), className: uniform ? "mt-auto pt-2" : "mt-2", children: node.content }))] }) }));
25
+ }
@@ -61,8 +61,14 @@ export type LayoutEdge = {
61
61
  };
62
62
  /** `down` reads from the top, `right` from the start edge. */
63
63
  export type FlowDirection = "down" | "right";
64
+ /** The gaps dagre keeps: between boxes in a rank, between ranks, and between arrows in a rank. */
65
+ export type FlowSpacing = {
66
+ node: number;
67
+ rank: number;
68
+ edge: number;
69
+ };
64
70
  /**
65
71
  * Lays the nodes and arrows out with dagre. An arrow whose end is not a node is left out, so a
66
72
  * typo draws no stray box. Pure: the same input gives the same layout.
67
73
  */
68
- export declare function layoutFlow(nodes: readonly LayoutNode[], edges: readonly LayoutEdge[], direction: FlowDirection): FlowLayout;
74
+ export declare function layoutFlow(nodes: readonly LayoutNode[], edges: readonly LayoutEdge[], direction: FlowDirection, spacing?: FlowSpacing): FlowLayout;
@@ -11,6 +11,7 @@ const MARGIN = 16;
11
11
  const NODE_GAP = 32;
12
12
  const RANK_GAP = 48;
13
13
  const EDGE_GAP = 16;
14
+ const LOOSE = { node: NODE_GAP, rank: RANK_GAP, edge: EDGE_GAP };
14
15
  /** A group's dagre id, kept apart from every node id. */
15
16
  const groupKey = (name) => `\u0000group:${name}`;
16
17
  const round = (n) => Math.round(n * 10) / 10;
@@ -18,7 +19,7 @@ const round = (n) => Math.round(n * 10) / 10;
18
19
  * Lays the nodes and arrows out with dagre. An arrow whose end is not a node is left out, so a
19
20
  * typo draws no stray box. Pure: the same input gives the same layout.
20
21
  */
21
- export function layoutFlow(nodes, edges, direction) {
22
+ export function layoutFlow(nodes, edges, direction, spacing = LOOSE) {
22
23
  const groups = [...new Set(nodes.flatMap((n) => (n.group ? [n.group] : [])))];
23
24
  const g = new Graph({
24
25
  multigraph: true,
@@ -26,9 +27,9 @@ export function layoutFlow(nodes, edges, direction) {
26
27
  });
27
28
  g.setGraph({
28
29
  rankdir: direction === "right" ? "LR" : "TB",
29
- nodesep: NODE_GAP,
30
- ranksep: RANK_GAP,
31
- edgesep: EDGE_GAP,
30
+ nodesep: spacing.node,
31
+ ranksep: spacing.rank,
32
+ edgesep: spacing.edge,
32
33
  marginx: MARGIN,
33
34
  marginy: MARGIN,
34
35
  });
@@ -1,5 +1,5 @@
1
1
  import { type ReactElement } from "react";
2
- import type { FlowLayout, FlowNodeBox } from "./flowLayout.js";
2
+ import type { FlowLayout, FlowNodeBox, FlowSpacing } from "./flowLayout.js";
3
3
  import type { StateMachineItem } from "./StateMachine.js";
4
4
  /**
5
5
  * The box items whose `state` is no state of the machine go in. Printable, since it reaches the DOM
@@ -32,19 +32,24 @@ export declare function useMoves(items: readonly StateMachineItem[], known: Read
32
32
  /** How many slots a box with `count` items draws: all of them, or the first few and a `+N`; one more for "fewer" when open. */
33
33
  export declare function slotsFor(count: number, open: boolean): number;
34
34
  /**
35
- * The rows a box reserves for its dots, whatever its count: the nine it shows (the `+N` included),
35
+ * The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
36
36
  * or the rows of an open box's full list. A box with few or no items reserves the same rows, so
37
37
  * its size does not depend on how many items it holds.
38
38
  */
39
39
  export declare function rowsFor(slots: number): number;
40
40
  /**
41
41
  * The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
42
- * height, so it is the same at 0, 1, 9 or 12 items (closed) and grows only when a box is opened.
42
+ * height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
43
43
  */
44
44
  export declare function dotArea(slots: number): {
45
45
  width: number;
46
46
  height: number;
47
47
  };
48
+ /**
49
+ * A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
50
+ * two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
51
+ */
52
+ export declare const MACHINE_SPACING: FlowSpacing;
48
53
  /** A box's tag row: one small pill's height. */
49
54
  export declare const TAG_ROW = 20;
50
55
  /** The gap between the tag row and the dot area (`mt-1`). */
@@ -14,7 +14,7 @@ export const SLOT_W = 76;
14
14
  export const SLOT_H = 22;
15
15
  export const COLS = 3;
16
16
  /** Up to this many dots show in a box; past it, one fewer and a `+N` that shows the rest. */
17
- const LIMIT = 9;
17
+ const LIMIT = 6;
18
18
  /** From a box's bottom edge to its dots' bottom: its border and its bottom padding. */
19
19
  const BOX_INSET = 9;
20
20
  /** From a slot's left edge to its dot's centre: the chip's inset, its padding and half the dot. */
@@ -84,7 +84,7 @@ export function slotsFor(count, open) {
84
84
  return open ? count + 1 : LIMIT;
85
85
  }
86
86
  /**
87
- * The rows a box reserves for its dots, whatever its count: the nine it shows (the `+N` included),
87
+ * The rows a box reserves for its dots, whatever its count: the six it shows (the `+N` included),
88
88
  * or the rows of an open box's full list. A box with few or no items reserves the same rows, so
89
89
  * its size does not depend on how many items it holds.
90
90
  */
@@ -93,11 +93,16 @@ export function rowsFor(slots) {
93
93
  }
94
94
  /**
95
95
  * The room a box reserves under its tags for `slots` dots: the full grid's width and `rowsFor`'s
96
- * height, so it is the same at 0, 1, 9 or 12 items (closed) and grows only when a box is opened.
96
+ * height, so it is the same at 0, 1, 6 or 12 items (closed) and grows only when a box is opened.
97
97
  */
98
98
  export function dotArea(slots) {
99
99
  return { width: COLS * SLOT_W, height: rowsFor(slots) * SLOT_H };
100
100
  }
101
+ /**
102
+ * A machine's gaps, closer than `FlowDiagram`'s: its boxes are all one size, and every gap between
103
+ * two ranks also holds a rank of arrow labels, so the loose gaps leave wide empty bands.
104
+ */
105
+ export const MACHINE_SPACING = { node: 24, rank: 32, edge: 8 };
101
106
  /** A box's tag row: one small pill's height. */
102
107
  export const TAG_ROW = 20;
103
108
  /** The gap between the tag row and the dot area (`mt-1`). */
@@ -1,6 +1,6 @@
1
1
  import { type RefObject } from "react";
2
2
  import type { FlowEdge, FlowNode } from "./FlowDiagram.js";
3
- import { type FlowDirection, type FlowLayout } from "./flowLayout.js";
3
+ import { type FlowDirection, type FlowLayout, type FlowSpacing } from "./flowLayout.js";
4
4
  type Size = {
5
5
  width: number;
6
6
  height: number;
@@ -16,6 +16,17 @@ export type SizedNode = Pick<FlowNode, "id" | "label" | "group" | "content"> & {
16
16
  export declare function guessNode(node: SizedNode): Size;
17
17
  export declare const nodeKey: (id: string) => string;
18
18
  export declare const edgeKey: (index: number) => string;
19
+ export declare const labelKey: (id: string) => string;
20
+ export declare const contentKey: (id: string) => string;
21
+ /**
22
+ * Every box the same size (`uniform`): as wide as the widest content, its label wrapped to that
23
+ * width, and as tall as the tallest label and the tallest content make a box. A box's label
24
+ * (`labelKey`) and content (`contentKey`, its gap above included) are measured, not the box: their
25
+ * width is fixed, so their height does not depend on the layout, and a box drawn at its slot's size
26
+ * is never read back as its own size. A content measured taller than its `contentSize` (a row of
27
+ * tags that wraps) makes every box taller.
28
+ */
29
+ export declare function uniformSizes(nodes: readonly SizedNode[], measured: Readonly<Record<string, Size>>): Map<string, Size>;
19
30
  /**
20
31
  * How far to shrink a diagram `width` wide so it fits the box `frame` refers to: 1 when it fits
21
32
  * or the room is not known yet (the server render, a test's DOM), else the room over the width.
@@ -29,9 +40,13 @@ export declare function useFit(width: number): {
29
40
  * Lays the diagram out, first at guessed sizes, then at the sizes the browser draws each box and
30
41
  * arrow label at: every render measures them, and lays out again when one's size is not the one
31
42
  * the layout used. A box's size never depends on where it is placed, so this settles after one
32
- * pass; the observer catches a later change (a web font arriving).
43
+ * pass; the observer catches a later change (a web font arriving). `uniform` draws every box at one
44
+ * size (`uniformSizes`), and `spacing` sets dagre's gaps (`layoutFlow`'s default when not given).
33
45
  */
34
- export declare function useFlowLayout(nodes: readonly SizedNode[], edges: readonly FlowEdge[], direction: FlowDirection): {
46
+ export declare function useFlowLayout(nodes: readonly SizedNode[], edges: readonly FlowEdge[], direction: FlowDirection, options?: {
47
+ uniform?: boolean;
48
+ spacing?: FlowSpacing;
49
+ }): {
35
50
  canvas: RefObject<HTMLDivElement | null>;
36
51
  drawn: FlowLayout;
37
52
  };
@@ -17,9 +17,11 @@ const CONTENT_GAP = 8;
17
17
  const EDGE_CHAR = 6.6;
18
18
  const EDGE_PAD = 8;
19
19
  const EDGE_LINE = 16;
20
+ /** How many lines a box's label wraps to at `room` wide, guessed from its characters. */
21
+ const guessLines = (label, room) => Math.max(1, Math.ceil(Math.ceil(label.length * NODE_CHAR) / room));
20
22
  export function guessNode(node) {
21
23
  const text = Math.ceil(node.label.length * NODE_CHAR);
22
- const lines = Math.max(1, Math.ceil(text / (NODE_MAX - NODE_PAD_X)));
24
+ const lines = guessLines(node.label, NODE_MAX - NODE_PAD_X);
23
25
  const content = node.contentSize ?? { width: 0, height: CONTENT_GUESS - CONTENT_GAP };
24
26
  return {
25
27
  width: Math.min(NODE_MAX, Math.max(text, content.width) + NODE_PAD_X),
@@ -32,6 +34,30 @@ const guessLabel = (label) => ({
32
34
  });
33
35
  export const nodeKey = (id) => `n:${id}`;
34
36
  export const edgeKey = (index) => `e:${index}`;
37
+ export const labelKey = (id) => `l:${id}`;
38
+ export const contentKey = (id) => `c:${id}`;
39
+ /**
40
+ * Every box the same size (`uniform`): as wide as the widest content, its label wrapped to that
41
+ * width, and as tall as the tallest label and the tallest content make a box. A box's label
42
+ * (`labelKey`) and content (`contentKey`, its gap above included) are measured, not the box: their
43
+ * width is fixed, so their height does not depend on the layout, and a box drawn at its slot's size
44
+ * is never read back as its own size. A content measured taller than its `contentSize` (a row of
45
+ * tags that wraps) makes every box taller.
46
+ */
47
+ export function uniformSizes(nodes, measured) {
48
+ const room = Math.max(0, ...nodes.map((n) => n.contentSize?.width ?? 0));
49
+ const tallest = Math.max(NODE_LINE, ...nodes.map((n) => measured[labelKey(n.id)]?.height ?? guessLines(n.label, room) * NODE_LINE));
50
+ const content = Math.max(0, ...nodes.map((n) => n.content == null
51
+ ? 0
52
+ : Math.max(CONTENT_GAP + (n.contentSize?.height ?? 0), measured[contentKey(n.id)]?.height ?? 0)));
53
+ return new Map(nodes.map((n) => [
54
+ n.id,
55
+ {
56
+ width: room + NODE_PAD_X,
57
+ height: tallest + NODE_PAD_Y + (n.content == null ? 0 : content),
58
+ },
59
+ ]));
60
+ }
35
61
  /**
36
62
  * Reads the drawn size of every box and arrow label under `root`, keyed by `data-measure`, in the
37
63
  * diagram's own space: a canvas scaled to fit (`useFit`) is drawn smaller, so its sizes are read
@@ -83,25 +109,28 @@ const sameSizes = (a, b) => {
83
109
  * Lays the diagram out, first at guessed sizes, then at the sizes the browser draws each box and
84
110
  * arrow label at: every render measures them, and lays out again when one's size is not the one
85
111
  * the layout used. A box's size never depends on where it is placed, so this settles after one
86
- * pass; the observer catches a later change (a web font arriving).
112
+ * pass; the observer catches a later change (a web font arriving). `uniform` draws every box at one
113
+ * size (`uniformSizes`), and `spacing` sets dagre's gaps (`layoutFlow`'s default when not given).
87
114
  */
88
- export function useFlowLayout(nodes, edges, direction) {
115
+ export function useFlowLayout(nodes, edges, direction, options = {}) {
89
116
  const canvas = useRef(null);
90
117
  const [measured, setMeasured] = useState({});
118
+ const uniform = options.uniform ? uniformSizes(nodes, measured) : undefined;
91
119
  const layoutNodes = nodes.map((n) => ({
92
120
  id: n.id,
93
121
  group: n.group,
94
- ...(measured[nodeKey(n.id)] ?? guessNode(n)),
122
+ ...(uniform?.get(n.id) ?? measured[nodeKey(n.id)] ?? guessNode(n)),
95
123
  }));
96
124
  const layoutEdges = edges.map((e, index) => ({
97
125
  from: e.from,
98
126
  to: e.to,
99
127
  label: e.label ? (measured[edgeKey(index)] ?? guessLabel(e.label)) : undefined,
100
128
  }));
101
- const key = JSON.stringify([direction, layoutNodes, layoutEdges]);
129
+ const key = JSON.stringify([direction, options.spacing, layoutNodes, layoutEdges]);
102
130
  // The key holds every input the layout reads, so the layout reruns only when one changes.
131
+ const drawn = useMemo(() => layoutFlow(layoutNodes, layoutEdges, direction, options.spacing),
103
132
  // oxlint-disable-next-line react-hooks/exhaustive-deps -- keyed on the serialised input above
104
- const drawn = useMemo(() => layoutFlow(layoutNodes, layoutEdges, direction), [key]);
133
+ [key]);
105
134
  useLayoutEffect(() => {
106
135
  const root = canvas.current;
107
136
  if (!root)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slashsuite/ui-react",
3
- "version": "0.13.2",
3
+ "version": "0.13.3",
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",