@miadi/stateloom-canvas 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Guillaume Isabelle (jgwill)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # @miadi/stateloom-canvas
2
+
3
+ The SMDF design surface, as a component anyone can mount.
4
+
5
+ This is the stateloom designer's board with its store taken out: pan, zoom,
6
+ drill into composites, drag boxes, routed edges, settled event labels, touch
7
+ gestures and a navigation HUD — all driven by props, all styled by CSS
8
+ variables. Two applications draw the same board from it (the stateloom web
9
+ designer and forgewright), which is the whole reason it exists as a package.
10
+
11
+ ```bash
12
+ npm install @miadi/stateloom-canvas
13
+ ```
14
+
15
+ ```tsx
16
+ import { useMemo, useState } from "react";
17
+ import { StateMachineCanvas, autoLayout, IDENTITY_VIEWPORT } from "@miadi/stateloom-canvas";
18
+ import "@miadi/stateloom-canvas/styles.css";
19
+
20
+ export function Board({ definition }) {
21
+ const [viewport, setViewport] = useState(IDENTITY_VIEWPORT);
22
+ const [path, setPath] = useState([]);
23
+ const [positions, setPositions] = useState(() => autoLayout(definition));
24
+
25
+ return (
26
+ <StateMachineCanvas
27
+ definition={definition}
28
+ positions={positions}
29
+ path={path}
30
+ viewport={viewport}
31
+ onViewportChange={setViewport}
32
+ onStateMove={(name, box) => setPositions((p) => ({ ...p, [name]: box }))}
33
+ onNavigateInto={(name) => setPath((p) => [...p, name])}
34
+ onNavigateTo={(depth) => setPath((p) => p.slice(0, depth))}
35
+ />
36
+ );
37
+ }
38
+ ```
39
+
40
+ ## Navigation
41
+
42
+ | Gesture | What it does |
43
+ |---|---|
44
+ | Wheel / trackpad | Scroll the board |
45
+ | Shift + wheel | Scroll sideways |
46
+ | Ctrl/⌘ + wheel | Zoom, anchored at the cursor |
47
+ | Middle-drag, or Space + drag | Pan |
48
+ | Drag a box | Move that state (needs `onStateMove`) |
49
+ | Double-click a composite | Drill into it |
50
+ | Right-click | Context menu (needs `onContextMenu`) |
51
+ | One finger | Pan · **two fingers** pinch-zoom · **hold** opens the menu |
52
+ | ⤢ Fit in the HUD | Frame every box of the current level |
53
+
54
+ `Ctrl+Z` / `Ctrl+Shift+Z` / `Delete` are forwarded to `onUndo`, `onRedo` and
55
+ `onDeleteState` when those props are given, and are inert when they are not.
56
+
57
+ ## Props
58
+
59
+ Required: `definition`, `positions`, `viewport`, `onViewportChange`.
60
+
61
+ Everything else is optional and additive — a canvas with no callbacks is a
62
+ read-only board you can still pan, zoom and drill. `readOnly` turns off dragging
63
+ and transition-drawing while leaving navigation intact. `fitKey` frames the
64
+ board whenever its value changes (pass a document id, so loading a diagram
65
+ lands it centred). A `ref` exposes `fit()`, `zoomIn()`, `zoomOut()`,
66
+ `resetZoom()` and `element()` for a toolbar that lives outside the canvas.
67
+
68
+ ## Theming
69
+
70
+ Every colour is a CSS custom property declared on `.slc-pane` — none is written
71
+ into an SVG attribute. Re-skin the whole surface either with a partial `theme`
72
+ prop or by redeclaring the variables on any ancestor:
73
+
74
+ ```css
75
+ .my-app {
76
+ --slc-surface: #131110; /* warm coal instead of slate */
77
+ --slc-accent: #FF6D3B;
78
+ --slc-node-fill: #1E1A17;
79
+ }
80
+ ```
81
+
82
+ `CANVAS_THEME_VARS` names every variable the component reads.
83
+
84
+ ## What comes with it
85
+
86
+ The layout, routing and viewport arithmetic lives in
87
+ [`@miadi/stateloom-protocol`](https://www.npmjs.com/package/@miadi/stateloom-protocol)
88
+ and is re-exported here — `autoLayout`, `routeEdges`, `placeLabels`, `fitToBoxes`,
89
+ `zoomAt`, `viewportTransform` — so a host needs one dependency to draw a board.
90
+
91
+ Part of [smcraft / stateloom](https://github.com/jgwill/smcraft). MIT.
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The design surface, as a component anyone can mount.
3
+ *
4
+ * This is the smcraft designer's canvas with its store taken out. Everything
5
+ * it knows arrives as props — the definition, where the boxes sit, which way
6
+ * the view is pointed — and everything it wants arrives back as a callback.
7
+ * Nothing here reaches for a zustand store, a Next.js route, a stylesheet
8
+ * framework, or a socket, which is the whole reason a second application can
9
+ * mount the same board and get the same navigation.
10
+ *
11
+ * What it still owns, because none of it is application state:
12
+ *
13
+ * · the gesture layer — wheel, ⌃wheel, middle-drag, Space+drag, one-finger
14
+ * pan, two-finger pinch, held-press menu — with the pointer bookkeeping
15
+ * that keeps a second finger from being read as a rival drag;
16
+ * · the drawing — edges routed so they leave a box through the gap that is
17
+ * actually there, labels settled so two chips never print through each
18
+ * other, both from `@miadi/stateloom-protocol`;
19
+ * · the enter/exit animation of nodes and edges as a definition changes.
20
+ *
21
+ * Colour is CSS, never an attribute (see theme.ts): a host re-skins the whole
22
+ * board by declaring `--slc-*` variables, and the same component is at home on
23
+ * slate and on warm coal.
24
+ */
25
+ import { type CSSProperties, type ReactNode } from "react";
26
+ import type { LayoutBox, StateMachineDefinition, Viewport } from "@miadi/stateloom-protocol";
27
+ import { type CanvasTheme } from "./theme.js";
28
+ export type DrawMode = "select" | "transition";
29
+ /** What a context menu was asked for. */
30
+ export interface CanvasTarget {
31
+ kind: "state" | "transition" | "canvas";
32
+ id?: string;
33
+ }
34
+ export interface CanvasSelection {
35
+ kind: "state" | "transition" | null;
36
+ id: string | null;
37
+ }
38
+ /** Imperative navigation, for a host whose toolbar lives outside the canvas. */
39
+ export interface StateMachineCanvasHandle {
40
+ /** Frame every box of the level currently drawn. */
41
+ fit(): void;
42
+ zoomIn(): void;
43
+ zoomOut(): void;
44
+ resetZoom(): void;
45
+ /** The rendered `<svg>` — what an exporter serializes. */
46
+ element(): SVGSVGElement | null;
47
+ }
48
+ export interface StateMachineCanvasProps {
49
+ definition: StateMachineDefinition;
50
+ /** Where every box sits, in world units. Derive with `autoLayout`. */
51
+ positions: Record<string, LayoutBox>;
52
+ /** Names below the root state — the drill path. `[]` draws the top level. */
53
+ path?: readonly string[];
54
+ /** What the breadcrumb calls the root. Defaults to the root state's name. */
55
+ rootLabel?: string;
56
+ viewport: Viewport;
57
+ onViewportChange: (viewport: Viewport) => void;
58
+ selection?: CanvasSelection;
59
+ /** States the runtime is currently in — drawn with the active glow. */
60
+ activeStates?: readonly string[];
61
+ /** Names that failed validation — drawn with the danger outline. */
62
+ errorElements?: readonly string[];
63
+ drawMode?: DrawMode;
64
+ drawSource?: string | null;
65
+ /** No dragging, no drawing, no menu. Pan, zoom, select and drill remain. */
66
+ readOnly?: boolean;
67
+ showBreadcrumb?: boolean;
68
+ showHud?: boolean;
69
+ /**
70
+ * Frame the board whenever this value changes — a document id, a load
71
+ * counter. Left undefined, the view is never moved on the host's behalf.
72
+ */
73
+ fitKey?: string | number;
74
+ onSelect?: (kind: "state" | "transition", id: string) => void;
75
+ onClearSelection?: () => void;
76
+ onStateMove?: (name: string, box: LayoutBox) => void;
77
+ onNavigateInto?: (name: string) => void;
78
+ /** Breadcrumb click. `depth` is 0 for the root, 1 for its child, and so on. */
79
+ onNavigateTo?: (depth: number) => void;
80
+ onContextMenu?: (clientX: number, clientY: number, target: CanvasTarget) => void;
81
+ onCreateTransition?: (from: string, to: string, event: string) => void;
82
+ /** Escape, or the picker's Cancel — the host clears its own draw mode. */
83
+ onCancelDraw?: () => void;
84
+ onDeleteState?: (name: string) => void;
85
+ onUndo?: () => void;
86
+ onRedo?: () => void;
87
+ /** Extra controls appended inside the HUD pill. */
88
+ hudExtras?: ReactNode;
89
+ /** What an empty level says. */
90
+ emptyHint?: ReactNode;
91
+ theme?: Partial<CanvasTheme>;
92
+ className?: string;
93
+ style?: CSSProperties;
94
+ /** DOM id of the `<svg>`, so an exporter can find it without a ref. */
95
+ svgId?: string;
96
+ /**
97
+ * Marks the pane for a host that runs its own two-finger gesture elsewhere
98
+ * in the chrome and must disqualify itself while a finger is on the board.
99
+ */
100
+ pinchOwner?: string;
101
+ }
102
+ export declare const StateMachineCanvas: import("react").ForwardRefExoticComponent<StateMachineCanvasProps & import("react").RefAttributes<StateMachineCanvasHandle>>;
103
+ export default StateMachineCanvas;