@slotform/react-ui 0.0.0-stage → 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.
Files changed (280) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -2
  3. package/dist/density.d.ts +4 -0
  4. package/dist/density.d.ts.map +1 -0
  5. package/dist/density.js +8 -0
  6. package/dist/density.js.map +1 -0
  7. package/dist/index.d.ts +400 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +388 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/node/canvas.d.ts +3 -0
  12. package/dist/node/canvas.d.ts.map +1 -0
  13. package/dist/node/canvas.js +13 -0
  14. package/dist/node/canvas.js.map +1 -0
  15. package/dist/node/codegen.d.ts +6 -0
  16. package/dist/node/codegen.d.ts.map +1 -0
  17. package/dist/node/codegen.js +70 -0
  18. package/dist/node/codegen.js.map +1 -0
  19. package/dist/node/css-escape.d.ts +8 -0
  20. package/dist/node/css-escape.d.ts.map +1 -0
  21. package/dist/node/css-escape.js +12 -0
  22. package/dist/node/css-escape.js.map +1 -0
  23. package/dist/node/fonts.d.ts +6 -0
  24. package/dist/node/fonts.d.ts.map +1 -0
  25. package/dist/node/fonts.js +219 -0
  26. package/dist/node/fonts.js.map +1 -0
  27. package/dist/node/lint.d.ts +3 -0
  28. package/dist/node/lint.d.ts.map +1 -0
  29. package/dist/node/lint.js +23 -0
  30. package/dist/node/lint.js.map +1 -0
  31. package/dist/node/mode.d.ts +9 -0
  32. package/dist/node/mode.d.ts.map +1 -0
  33. package/dist/node/mode.js +38 -0
  34. package/dist/node/mode.js.map +1 -0
  35. package/dist/node/theme.d.ts +13 -0
  36. package/dist/node/theme.d.ts.map +1 -0
  37. package/dist/node/theme.js +160 -0
  38. package/dist/node/theme.js.map +1 -0
  39. package/dist/types.d.ts +250 -0
  40. package/dist/types.d.ts.map +1 -0
  41. package/dist/types.js +80 -0
  42. package/dist/types.js.map +1 -0
  43. package/guide/acts.md +61 -0
  44. package/guide/audio.md +77 -0
  45. package/guide/blocks.md +82 -0
  46. package/guide/canvas-choosing.md +34 -0
  47. package/guide/canvas-editing.md +36 -0
  48. package/guide/canvas-layout.md +46 -0
  49. package/guide/canvas-opening.md +47 -0
  50. package/guide/canvas-overview.md +24 -0
  51. package/guide/canvas-states.md +37 -0
  52. package/guide/canvas-view.md +46 -0
  53. package/guide/canvas.md +94 -0
  54. package/guide/containers.md +60 -0
  55. package/guide/content.md +40 -0
  56. package/guide/controls.md +89 -0
  57. package/guide/density.md +48 -0
  58. package/guide/dock.md +42 -0
  59. package/guide/drag.md +38 -0
  60. package/guide/figures.md +25 -0
  61. package/guide/forms.md +82 -0
  62. package/guide/pane-bar.md +11 -0
  63. package/guide/pane.md +50 -0
  64. package/guide/reference.md +74 -0
  65. package/guide/rules.md +109 -0
  66. package/guide/section.md +29 -0
  67. package/guide/shell.md +26 -0
  68. package/guide/shots.md +37 -0
  69. package/guide/sidebar.md +30 -0
  70. package/guide/table.md +119 -0
  71. package/lint/no-arbitrary-value.grit +9 -0
  72. package/lint/no-class-on-component.grit +12 -0
  73. package/lint/no-density.grit +8 -0
  74. package/lint/no-host-look.grit +38 -0
  75. package/lint/no-img.grit +10 -0
  76. package/lint/no-navigate-act.grit +22 -0
  77. package/lint/no-raw-style.grit +8 -0
  78. package/package.json +150 -3
  79. package/src/density.ts +9 -0
  80. package/src/index.ts +503 -0
  81. package/src/node/canvas.ts +14 -0
  82. package/src/node/codegen.ts +100 -0
  83. package/src/node/css-escape.ts +27 -0
  84. package/src/node/fonts.ts +260 -0
  85. package/src/node/lint.ts +25 -0
  86. package/src/node/mode.ts +64 -0
  87. package/src/node/theme.ts +232 -0
  88. package/src/types.ts +120 -0
  89. package/src/ui/atoms/avatar/base.tsx +42 -0
  90. package/src/ui/atoms/button/glyph.tsx +244 -0
  91. package/src/ui/atoms/button/link.tsx +81 -0
  92. package/src/ui/atoms/button/look.ts +32 -0
  93. package/src/ui/atoms/button/reason.tsx +69 -0
  94. package/src/ui/atoms/chip/base.tsx +85 -0
  95. package/src/ui/atoms/count/base.tsx +27 -0
  96. package/src/ui/atoms/icon/index.tsx +51 -0
  97. package/src/ui/atoms/message/waiting.tsx +19 -0
  98. package/src/ui/atoms/prose/painted.tsx +35 -0
  99. package/src/ui/atoms/row/badges.tsx +226 -0
  100. package/src/ui/atoms/row/leading.tsx +166 -0
  101. package/src/ui/atoms/row/lock.tsx +17 -0
  102. package/src/ui/atoms/row/marks.tsx +79 -0
  103. package/src/ui/atoms/section/card.tsx +27 -0
  104. package/src/ui/atoms/spinner/index.tsx +42 -0
  105. package/src/ui/atoms/status/base.tsx +51 -0
  106. package/src/ui/atoms/status/change.tsx +29 -0
  107. package/src/ui/atoms/status/dot.tsx +45 -0
  108. package/src/ui/atoms/switch/checkbox.tsx +121 -0
  109. package/src/ui/atoms/turn.tsx +28 -0
  110. package/src/ui/components/action-bar/base.tsx +276 -0
  111. package/src/ui/components/action-bar/index.tsx +20 -0
  112. package/src/ui/components/avatar/index.tsx +21 -0
  113. package/src/ui/components/banner/index.tsx +207 -0
  114. package/src/ui/components/banner/pending.tsx +109 -0
  115. package/src/ui/components/bar-chart/index.tsx +701 -0
  116. package/src/ui/components/button/base.tsx +214 -0
  117. package/src/ui/components/button/face.tsx +84 -0
  118. package/src/ui/components/button/index.tsx +206 -0
  119. package/src/ui/components/canvas/connection.tsx +26 -0
  120. package/src/ui/components/canvas/edges.tsx +116 -0
  121. package/src/ui/components/canvas/elk.ts +260 -0
  122. package/src/ui/components/canvas/floor.ts +173 -0
  123. package/src/ui/components/canvas/geometry.ts +772 -0
  124. package/src/ui/components/canvas/ground.tsx +97 -0
  125. package/src/ui/components/canvas/group.tsx +156 -0
  126. package/src/ui/components/canvas/hit.ts +40 -0
  127. package/src/ui/components/canvas/index.tsx +497 -0
  128. package/src/ui/components/canvas/label.tsx +53 -0
  129. package/src/ui/components/canvas/layout.ts +543 -0
  130. package/src/ui/components/canvas/lift.ts +272 -0
  131. package/src/ui/components/canvas/look.ts +129 -0
  132. package/src/ui/components/canvas/node.tsx +689 -0
  133. package/src/ui/components/canvas/open.ts +243 -0
  134. package/src/ui/components/canvas/opening.ts +240 -0
  135. package/src/ui/components/canvas/view.ts +621 -0
  136. package/src/ui/components/canvas/viewport.ts +417 -0
  137. package/src/ui/components/canvas/zoom.tsx +113 -0
  138. package/src/ui/components/card/index.tsx +20 -0
  139. package/src/ui/components/chip/index.tsx +24 -0
  140. package/src/ui/components/code/frame.ts +2 -0
  141. package/src/ui/components/code/index.tsx +234 -0
  142. package/src/ui/components/columns/index.tsx +51 -0
  143. package/src/ui/components/count/index.tsx +17 -0
  144. package/src/ui/components/diff/index.tsx +249 -0
  145. package/src/ui/components/diff/word.tsx +104 -0
  146. package/src/ui/components/dock/index.tsx +262 -0
  147. package/src/ui/components/file-row/index.tsx +213 -0
  148. package/src/ui/components/form/base.tsx +280 -0
  149. package/src/ui/components/form/derive.tsx +269 -0
  150. package/src/ui/components/form/index.tsx +49 -0
  151. package/src/ui/components/form/saved.tsx +39 -0
  152. package/src/ui/components/form-field/index.tsx +658 -0
  153. package/src/ui/components/gallery/index.tsx +83 -0
  154. package/src/ui/components/gate/index.tsx +183 -0
  155. package/src/ui/components/image/base.tsx +245 -0
  156. package/src/ui/components/image/index.tsx +58 -0
  157. package/src/ui/components/input/base.tsx +246 -0
  158. package/src/ui/components/input/code.tsx +108 -0
  159. package/src/ui/components/input/drop.ts +37 -0
  160. package/src/ui/components/input/file.tsx +154 -0
  161. package/src/ui/components/input/index.tsx +132 -0
  162. package/src/ui/components/input/prose.tsx +107 -0
  163. package/src/ui/components/input/refusal.tsx +30 -0
  164. package/src/ui/components/item-header/base.tsx +410 -0
  165. package/src/ui/components/menu/base.tsx +319 -0
  166. package/src/ui/components/message/attachments.tsx +101 -0
  167. package/src/ui/components/message/base.tsx +266 -0
  168. package/src/ui/components/message-input/index.tsx +377 -0
  169. package/src/ui/components/missing/base.tsx +45 -0
  170. package/src/ui/components/pane/head.tsx +399 -0
  171. package/src/ui/components/pane/index.tsx +325 -0
  172. package/src/ui/components/picker/base.tsx +1551 -0
  173. package/src/ui/components/picker/index.tsx +224 -0
  174. package/src/ui/components/picker/list.tsx +423 -0
  175. package/src/ui/components/picker/segments.tsx +183 -0
  176. package/src/ui/components/prose/index.tsx +259 -0
  177. package/src/ui/components/qr-code/index.tsx +65 -0
  178. package/src/ui/components/row/acts.tsx +126 -0
  179. package/src/ui/components/row/fact.tsx +54 -0
  180. package/src/ui/components/row/index.tsx +1370 -0
  181. package/src/ui/components/row/progress.tsx +38 -0
  182. package/src/ui/components/row/status.tsx +54 -0
  183. package/src/ui/components/row/title.ts +7 -0
  184. package/src/ui/components/rules/index.tsx +290 -0
  185. package/src/ui/components/section/band.tsx +39 -0
  186. package/src/ui/components/section/head.tsx +267 -0
  187. package/src/ui/components/section/index.tsx +409 -0
  188. package/src/ui/components/section/rows.tsx +356 -0
  189. package/src/ui/components/sheet/base.tsx +272 -0
  190. package/src/ui/components/sheet/confirm.tsx +79 -0
  191. package/src/ui/components/sheet/docked.tsx +218 -0
  192. package/src/ui/components/sheet/form.tsx +76 -0
  193. package/src/ui/components/sheet/head.tsx +120 -0
  194. package/src/ui/components/sheet/index.tsx +45 -0
  195. package/src/ui/components/sheet/layer.tsx +168 -0
  196. package/src/ui/components/sheet/outlet.tsx +58 -0
  197. package/src/ui/components/sheet/task.tsx +41 -0
  198. package/src/ui/components/shell/cut.tsx +116 -0
  199. package/src/ui/components/shell/drag.tsx +45 -0
  200. package/src/ui/components/shell/host.tsx +71 -0
  201. package/src/ui/components/shell/index.tsx +737 -0
  202. package/src/ui/components/shell/panes.tsx +76 -0
  203. package/src/ui/components/shell/preview.ts +230 -0
  204. package/src/ui/components/shell/rail.tsx +163 -0
  205. package/src/ui/components/shell/row.ts +11 -0
  206. package/src/ui/components/shell/switcher.tsx +143 -0
  207. package/src/ui/components/shell/tree.tsx +252 -0
  208. package/src/ui/components/slider/base.tsx +139 -0
  209. package/src/ui/components/slider/index.tsx +57 -0
  210. package/src/ui/components/stages/index.tsx +198 -0
  211. package/src/ui/components/stats/count-links.tsx +47 -0
  212. package/src/ui/components/stats/index.tsx +203 -0
  213. package/src/ui/components/stats/meter.tsx +213 -0
  214. package/src/ui/components/stats/parts.ts +10 -0
  215. package/src/ui/components/step-count/index.tsx +45 -0
  216. package/src/ui/components/switch/base.tsx +86 -0
  217. package/src/ui/components/switch/index.tsx +38 -0
  218. package/src/ui/components/table/columns.tsx +131 -0
  219. package/src/ui/components/table/index.tsx +2215 -0
  220. package/src/ui/components/text/index.tsx +37 -0
  221. package/src/ui/components/thread/index.tsx +503 -0
  222. package/src/ui/components/thread/latest.tsx +59 -0
  223. package/src/ui/components/toast/index.tsx +113 -0
  224. package/src/ui/components/toast/layer.tsx +26 -0
  225. package/src/ui/components/toolbar/filters.tsx +161 -0
  226. package/src/ui/components/toolbar/index.tsx +125 -0
  227. package/src/ui/components/waveform/index.tsx +205 -0
  228. package/src/ui/globals.css +427 -0
  229. package/src/ui/lib/act.ts +60 -0
  230. package/src/ui/lib/age.ts +49 -0
  231. package/src/ui/lib/bound.ts +16 -0
  232. package/src/ui/lib/canvas-layout.ts +11 -0
  233. package/src/ui/lib/caret.ts +9 -0
  234. package/src/ui/lib/chip-lines.ts +3 -0
  235. package/src/ui/lib/closed.ts +8 -0
  236. package/src/ui/lib/code.tsx +184 -0
  237. package/src/ui/lib/columns.ts +39 -0
  238. package/src/ui/lib/commit.ts +24 -0
  239. package/src/ui/lib/container.ts +112 -0
  240. package/src/ui/lib/copy.ts +29 -0
  241. package/src/ui/lib/drag.ts +575 -0
  242. package/src/ui/lib/empty.tsx +85 -0
  243. package/src/ui/lib/field.ts +39 -0
  244. package/src/ui/lib/focus.ts +175 -0
  245. package/src/ui/lib/folds.ts +67 -0
  246. package/src/ui/lib/follow.ts +220 -0
  247. package/src/ui/lib/form.ts +19 -0
  248. package/src/ui/lib/guards.ts +38 -0
  249. package/src/ui/lib/heading.ts +14 -0
  250. package/src/ui/lib/ink.ts +26 -0
  251. package/src/ui/lib/lang.ts +7 -0
  252. package/src/ui/lib/leave.ts +14 -0
  253. package/src/ui/lib/lockup.tsx +145 -0
  254. package/src/ui/lib/measure.ts +22 -0
  255. package/src/ui/lib/media.ts +63 -0
  256. package/src/ui/lib/modality.ts +69 -0
  257. package/src/ui/lib/moment.ts +13 -0
  258. package/src/ui/lib/more.tsx +76 -0
  259. package/src/ui/lib/navigate.ts +210 -0
  260. package/src/ui/lib/noop.ts +3 -0
  261. package/src/ui/lib/not-found.tsx +40 -0
  262. package/src/ui/lib/paint.tsx +55 -0
  263. package/src/ui/lib/parts-line.tsx +54 -0
  264. package/src/ui/lib/picked.ts +18 -0
  265. package/src/ui/lib/player-store.ts +38 -0
  266. package/src/ui/lib/player.ts +139 -0
  267. package/src/ui/lib/portal.ts +12 -0
  268. package/src/ui/lib/read.tsx +269 -0
  269. package/src/ui/lib/region.tsx +49 -0
  270. package/src/ui/lib/reorder.ts +154 -0
  271. package/src/ui/lib/rest.ts +16 -0
  272. package/src/ui/lib/saved.ts +29 -0
  273. package/src/ui/lib/scramble.tsx +147 -0
  274. package/src/ui/lib/scrolls.ts +70 -0
  275. package/src/ui/lib/style.ts +6 -0
  276. package/src/ui/lib/swap.tsx +107 -0
  277. package/src/ui/lib/toast.ts +39 -0
  278. package/src/ui/lib/walk-classes.ts +16 -0
  279. package/src/ui/lib/window.tsx +376 -0
  280. package/src/ui/worker.d.ts +4 -0
@@ -0,0 +1,497 @@
1
+ import { pathOrder } from "@slotform/ui-core/canvas";
2
+ import { cn } from "@slotform/ui-core/cn";
3
+ import type {
4
+ CanvasEdge,
5
+ CanvasGroup,
6
+ CanvasNode,
7
+ CanvasPath,
8
+ CanvasPoint,
9
+ LinkAct,
10
+ } from "@slotform/ui-core/descriptors";
11
+ import { useFrame, useSelections } from "@slotform/ui-core/react/read";
12
+ import { type Datum, isEnded, isSelection } from "@slotform/ui-core/read";
13
+ import {
14
+ BLOCK_IN,
15
+ CANVAS_GROUP_HEAD,
16
+ canvasGroup,
17
+ } from "@slotform/ui-core/variants";
18
+ import {
19
+ type FocusEvent,
20
+ type KeyboardEvent,
21
+ type MouseEvent,
22
+ useCallback,
23
+ useEffect,
24
+ useEffectEvent,
25
+ useLayoutEffect,
26
+ useMemo,
27
+ useRef,
28
+ useState,
29
+ } from "react";
30
+ import type { Act } from "../../lib/act.ts";
31
+ import type { Closed } from "../../lib/closed.ts";
32
+ import { spacing, textSize } from "../../lib/measure.ts";
33
+ import { useTouch } from "../../lib/media.ts";
34
+ import { FrameBody, FrameLine } from "../../lib/read.tsx";
35
+ import { ConnectionLine } from "./connection.tsx";
36
+ import { EdgeLayer } from "./edges.tsx";
37
+ import {
38
+ belowFloor,
39
+ nameCap,
40
+ overviewFloorFor,
41
+ TEXT_FLOOR,
42
+ UNZOOM_VAR,
43
+ } from "./floor.ts";
44
+ import { emptyGroups, type Size } from "./geometry.ts";
45
+ import { GROUND, Grid, PaintedNodes } from "./ground.tsx";
46
+ import { GroupBox } from "./group.tsx";
47
+ import { isGround } from "./hit.ts";
48
+ import { EdgeLabel } from "./label.tsx";
49
+ import { useGraphRun } from "./layout.ts";
50
+ import { type EdgeTone, edgeLook, nodeLook } from "./look.ts";
51
+ import { type NodeEdit, NodeView } from "./node.tsx";
52
+ import { useOpeningView } from "./opening.ts";
53
+ import { landAt, place, whole } from "./view.ts";
54
+ import { useViewport, useViewportValue } from "./viewport.ts";
55
+ import { ActFoot, ZoomPainted, ZoomStack } from "./zoom.tsx";
56
+
57
+ /** A graph of nodes and edges on a pannable, zoomable ground. */
58
+ export interface CanvasProps extends Closed {
59
+ /** What the graph is, read as its region's name (a short phrase; read aloud, never drawn). */
60
+ label: string;
61
+ /** The nodes; the canvas places those that carry no position. A selection, here or in `edges` or `groups`, stands the waiting ground (its dots, three painted node cards at a node's measure and its zoom stack) in place of the graph until its read lands, and over it while the landed graph is laid out and measured; every handler and the `act` are then ignored. A selection of nodes is the canvas's own frame: it holds its read and draws the failed, missing and empty shapes in the ground whatever stands above it, with the generic sentences, and the stale and paused line over it. */
62
+ nodes: Datum<readonly CanvasNode[]>;
63
+ /** The links between nodes, each drawn as an arrow from `from` to `to`. */
64
+ edges?: Datum<readonly CanvasEdge[]>;
65
+ /** Frames around the nodes they hold; a group holding none is a frame of its head alone that an edge may name, placed by the layout while no node has a position. */
66
+ groups?: Datum<readonly CanvasGroup[]>;
67
+ /** The selected node's or group's id. */
68
+ selected?: string;
69
+ /** Hears a node's or group's id when it is chosen, `null` when the choice is cleared; with it nodes and group heads are buttons. */
70
+ onSelect?: (id: string | null) => void;
71
+ /** A run's taken path; what it leaves out dims. */
72
+ path?: CanvasPath;
73
+ /** Hears each node's absolute position once the canvas places the nodes; with it nodes can be moved. */
74
+ onMove?: (id: string, position: CanvasPoint) => void;
75
+ /** Hears a drawn link, `to` being `null` when released on the ground; with it nodes can be connected. */
76
+ onConnect?: (from: string, to: string | null) => void;
77
+ /** The canvas's act, at the foot's centre (a `{ label, href }` is a link to a route, an anchor). */
78
+ act?: Act | LinkAct;
79
+ }
80
+
81
+ // What the graph draws once its data is here: the canvas's props with the data given plain.
82
+ type CanvasGraphProps = Omit<CanvasProps, "nodes" | "edges" | "groups"> & {
83
+ nodes: readonly CanvasNode[];
84
+ edges?: readonly CanvasEdge[];
85
+ groups?: readonly CanvasGroup[];
86
+ };
87
+
88
+ const NO_EDGES: readonly CanvasEdge[] = [];
89
+ const NO_GROUPS: readonly CanvasGroup[] = [];
90
+ const NO_SIZES: ReadonlyMap<string, Size> = new Map();
91
+ const NO_POINTS: ReadonlyMap<string, CanvasPoint> = new Map();
92
+
93
+ // Between its read landing and its layout the graph is measured but not shown:
94
+ // every part of the region but its ground and the waiting shape over it stands
95
+ // transparent and takes no pointer, so the waiting shape is what shows until the
96
+ // layout lands.
97
+ const MEASURING =
98
+ "*:not-data-ground:not-data-cover:opacity-0 *:not-data-ground:not-data-cover:pointer-events-none";
99
+ const LAYER = "absolute left-0 top-0";
100
+ const COVER = "contents";
101
+ const FLOW = "flex items-center";
102
+ const COLUMN = "flex flex-col";
103
+ const PROBE = "invisible absolute flex flex-col items-start";
104
+ const NBSP = "\u00a0";
105
+
106
+ /** A graph of nodes and edges, laid out and pannable; read-only until it is given `onSelect`, `onMove` or `onConnect`. */
107
+ export function Canvas({ nodes, edges, groups, ...graph }: CanvasProps) {
108
+ const [placed, linked, framed] = useSelections([nodes, edges, groups]);
109
+ // The nodes are the canvas's frame; its edges and groups wait with it.
110
+ const frame = useFrame(isSelection(nodes) ? nodes : undefined);
111
+ if (isEnded(frame.shape) || frame.shape === "empty")
112
+ return (
113
+ <section data-wide="" aria-label={graph.label} className={GROUND}>
114
+ <FrameBody frame={frame} fill />
115
+ </section>
116
+ );
117
+ return (
118
+ <FrameBody frame={frame}>
119
+ <FrameLine frame={frame} />
120
+ {placed.painted || linked.painted || framed.painted ? (
121
+ <section data-wide="" aria-label={graph.label} inert className={GROUND}>
122
+ <Grid />
123
+ <PaintedNodes />
124
+ <ZoomPainted arrange={Boolean(graph.onMove)} />
125
+ </section>
126
+ ) : (
127
+ <CanvasGraph
128
+ {...graph}
129
+ nodes={placed.value}
130
+ edges={linked.value}
131
+ groups={framed.value}
132
+ />
133
+ )}
134
+ </FrameBody>
135
+ );
136
+ }
137
+
138
+ function CanvasGraph({
139
+ label,
140
+ nodes,
141
+ edges = NO_EDGES,
142
+ groups = NO_GROUPS,
143
+ selected,
144
+ onSelect,
145
+ path,
146
+ onMove,
147
+ onConnect,
148
+ act,
149
+ }: CanvasGraphProps) {
150
+ const region = useRef<HTMLElement>(null);
151
+ const viewport = useViewport(region);
152
+ const touch = useTouch();
153
+ // One flag for the whole canvas, true while the smallest text a node draws
154
+ // renders under the text floor: a crossing renders once, and a pan or a zoom
155
+ // within a side renders nothing.
156
+ const below = useViewportValue(viewport, (view) =>
157
+ belowFloor(view.k, TEXT_FLOOR, TEXT_FLOOR),
158
+ );
159
+ const [sizes, setSizes] = useState(NO_SIZES);
160
+ const [probe, setProbe] = useState<HTMLElement | null>(null);
161
+ // An empty group is a leaf an edge may name, so it stands in the path.
162
+ const hollow = useMemo(
163
+ () => emptyGroups(groups, new Set(nodes.map((node) => node.id))),
164
+ [groups, nodes],
165
+ );
166
+ const order = useMemo(
167
+ () => pathOrder([...nodes, ...hollow.map((id) => ({ id }))], edges),
168
+ [nodes, hollow, edges],
169
+ );
170
+ const chipped = edges.filter(
171
+ (edge) => edge.label !== undefined || edge.handoff,
172
+ );
173
+ const probed = groups.length > 0 || chipped.length > 0;
174
+ // The one live position of each node a drag holds, set only through `at` and
175
+ // cleared on release; where a landing put a node; and the link being drawn.
176
+ const [live, setLive] = useState(NO_POINTS);
177
+ const [landed, setLanded] = useState(NO_POINTS);
178
+ const [link, setLink] = useState<{
179
+ from: string;
180
+ to: CanvasPoint;
181
+ target: string | null;
182
+ } | null>(null);
183
+ // A glyph's size on screen, and the widest a name stands beside it, at the
184
+ // density in force.
185
+ const glyph = spacing("control");
186
+ const names = nameCap(textSize("meta"));
187
+ const run = useGraphRun({
188
+ nodes,
189
+ edges,
190
+ groups,
191
+ order,
192
+ hollow,
193
+ sizes,
194
+ region,
195
+ viewport,
196
+ probe,
197
+ probed,
198
+ live,
199
+ landed,
200
+ onMove,
201
+ ports: Boolean(onConnect),
202
+ glyph,
203
+ });
204
+ const {
205
+ boxes,
206
+ drawn,
207
+ standing,
208
+ about,
209
+ routed,
210
+ space,
211
+ arrange,
212
+ clearLeft,
213
+ clearStack,
214
+ } = run;
215
+ const { minZoom, ready, fit } = useOpeningView(run, viewport, {
216
+ region,
217
+ nodes,
218
+ order,
219
+ sizes,
220
+ glyph,
221
+ names,
222
+ selected,
223
+ });
224
+
225
+ // The zoom under which a node is its glyph alone: the overview's floor, raised
226
+ // until no two overview forms (glyph, a `pair`, the name at its cap) overlap
227
+ // with a stretch between them for an edge; 1 for a graph with no overview.
228
+ const overview = useMemo(
229
+ () =>
230
+ overviewFloorFor(
231
+ [...boxes.values()],
232
+ glyph,
233
+ glyph + space.pair + names,
234
+ 2 * space.pair,
235
+ ),
236
+ [boxes, glyph, names, space.pair],
237
+ );
238
+ // The second flag, beside `below`: true while the canvas is under that floor.
239
+ const bare = useViewportValue(viewport, (view) => view.k < overview);
240
+
241
+ // The glyph and a port's hit hold their size on screen at any zoom through
242
+ // this variable, set by script so a pinch renders nothing.
243
+ useEffect(() => {
244
+ const element = region.current;
245
+ const set = () =>
246
+ element?.style.setProperty(UNZOOM_VAR, String(1 / viewport.get().k));
247
+ set();
248
+ return viewport.subscribe(set);
249
+ }, [viewport]);
250
+
251
+ const at = useCallback((id: string, point: CanvasPoint | null) => {
252
+ setLive((last) => {
253
+ if (point === null && !last.has(id)) return last;
254
+ const next = new Map(last);
255
+ if (point === null) next.delete(id);
256
+ else next.set(id, point);
257
+ return next;
258
+ });
259
+ }, []);
260
+ // Every move a viewer makes (a drag, a lift, an arrow key) reports here, at
261
+ // whole pixels, so a node stored where it was dropped draws its border on
262
+ // one pixel column.
263
+ const drop = useCallback(
264
+ (id: string, point: CanvasPoint) => {
265
+ onMove?.(id, whole(point));
266
+ at(id, null);
267
+ },
268
+ [onMove, at],
269
+ );
270
+ const linking = useCallback(
271
+ (from: string, to: CanvasPoint | null, target: string | null) => {
272
+ setLink(to && { from, to, target });
273
+ },
274
+ [],
275
+ );
276
+
277
+ // A node with no position among nodes that have one lands with its centre on
278
+ // the viewport's, and is chosen. When none has a position the layout places
279
+ // them all, so nothing lands. The ids a landing has seen keep a node from
280
+ // landing twice; an id that leaves `nodes` leaves with it.
281
+ const seen = useRef(new Set<string>());
282
+ const positioned = nodes.some((node) => node.position);
283
+ useLayoutEffect(() => {
284
+ const present = new Set(nodes.map((node) => node.id));
285
+ const gone = [...seen.current].filter((id) => !present.has(id));
286
+ const pane = region.current?.getBoundingClientRect();
287
+ const fresh =
288
+ ready && pane && positioned
289
+ ? nodes.filter(
290
+ (node) =>
291
+ !node.position &&
292
+ !seen.current.has(node.id) &&
293
+ sizes.has(node.id),
294
+ )
295
+ : [];
296
+ if (gone.length === 0 && fresh.length === 0) return;
297
+ const spots = fresh.flatMap((node) => {
298
+ const size = sizes.get(node.id);
299
+ if (!size || !pane) return [];
300
+ const centre = viewport.screenToFlow({
301
+ x: pane.left + pane.width / 2,
302
+ y: pane.top + pane.height / 2,
303
+ });
304
+ return [[node.id, landAt(centre, size)] as const];
305
+ });
306
+ for (const id of gone) seen.current.delete(id);
307
+ setLanded((last) => {
308
+ const next = new Map(last);
309
+ for (const id of gone) next.delete(id);
310
+ for (const [id, spot] of spots) next.set(id, spot);
311
+ return next;
312
+ });
313
+ for (const [id, spot] of spots) {
314
+ seen.current.add(id);
315
+ onMove?.(id, spot);
316
+ onSelect?.(id);
317
+ }
318
+ }, [nodes, sizes, ready, positioned, viewport, onMove, onSelect]);
319
+
320
+ // A node writes its size only when it changed.
321
+ const resized = useCallback((id: string, size: Size) => {
322
+ setSizes((last) => {
323
+ const old = last.get(id);
324
+ if (old?.width === size.width && old.height === size.height) return last;
325
+ return new Map(last).set(id, size);
326
+ });
327
+ }, []);
328
+
329
+ const reveal = (id: string) => {
330
+ const box = standing(id);
331
+ if (box) viewport.centreOn(box);
332
+ };
333
+ // A selection from outside brings its node into view; one the pointer made
334
+ // is already in view.
335
+ const follow = useEffectEvent(reveal);
336
+ useEffect(() => {
337
+ if (selected !== undefined) follow(selected);
338
+ }, [selected]);
339
+
340
+ const byId = new Map(nodes.map((node) => [node.id, node]));
341
+ const source = link ? boxes.get(link.from) : undefined;
342
+ const tones = new Map<string, EdgeTone>(
343
+ edges.map((edge) => [edge.id, edgeLook(edge, byId, path)]),
344
+ );
345
+
346
+ // The node last focused inside the region, which the zoom buttons zoom about:
347
+ // focus moving to a button of the stack keeps it, anywhere else drops it, and
348
+ // so does a click on the ground. A ref, since nothing draws it.
349
+ const focused = useRef<string | null>(null);
350
+ const track = (event: FocusEvent) => {
351
+ const target = event.target;
352
+ if (!(target instanceof Element)) return;
353
+ const node = target.closest<HTMLElement>("[data-focus]");
354
+ if (node) focused.current = node.dataset.focus ?? null;
355
+ else if (!target.closest("[data-clear]")) focused.current = null;
356
+ };
357
+ const anchor = () => (focused.current ? about(focused.current) : undefined);
358
+
359
+ // Escape clears the selection from anywhere inside the region.
360
+ const clear = (event: KeyboardEvent) => {
361
+ if (event.key === "Escape" && selected !== undefined) onSelect?.(null);
362
+ };
363
+ // A click on the ground clears it. d3 swallows the click that ends a drag,
364
+ // so a pan never gets here.
365
+ const ground = (event: MouseEvent) => {
366
+ const target = event.target;
367
+ if (
368
+ region.current &&
369
+ target instanceof Element &&
370
+ isGround(target, region.current)
371
+ ) {
372
+ focused.current = null;
373
+ onSelect?.(null);
374
+ }
375
+ };
376
+ const edit: NodeEdit = {
377
+ viewport,
378
+ region,
379
+ touch,
380
+ draggable: Boolean(onMove) && !touch && !below,
381
+ liftable: Boolean(onMove) && touch && !below,
382
+ ports: Boolean(onConnect) && !below,
383
+ at,
384
+ drop,
385
+ linking,
386
+ onConnect,
387
+ about,
388
+ };
389
+
390
+ return (
391
+ <section
392
+ data-wide=""
393
+ ref={region}
394
+ aria-label={label}
395
+ onKeyDown={clear}
396
+ onFocus={track}
397
+ onClick={onSelect ? ground : undefined}
398
+ aria-busy={ready ? undefined : true}
399
+ className={cn(GROUND, !ready && MEASURING)}
400
+ >
401
+ <Grid />
402
+ {ready ? null : (
403
+ // The waiting shape stands over the measuring graph, as the loaded one
404
+ // will: the same ground, its nodes' cards and its zoom stack painted.
405
+ <div data-cover aria-hidden="true" inert className={COVER}>
406
+ <PaintedNodes />
407
+ <ZoomPainted arrange={Boolean(onMove)} />
408
+ </div>
409
+ )}
410
+ <div data-layer className={cn(LAYER, ready && BLOCK_IN)}>
411
+ {groups.map((group) => {
412
+ const frame = routed.frames.get(group.id);
413
+ return frame ? (
414
+ <GroupBox
415
+ key={group.id}
416
+ id={group.id}
417
+ head={group.head}
418
+ box={frame}
419
+ below={below}
420
+ clear={{ left: clearLeft, stack: clearStack }}
421
+ viewport={viewport}
422
+ selected={selected === group.id}
423
+ onSelect={onSelect}
424
+ onFocusVisible={reveal}
425
+ />
426
+ ) : null;
427
+ })}
428
+ <EdgeLayer
429
+ edges={edges}
430
+ routes={routed.routes}
431
+ tones={tones}
432
+ radius={space.pair}
433
+ below={below}
434
+ />
435
+ {order.map((id) => {
436
+ const node = byId.get(id);
437
+ if (!node) return null;
438
+ return (
439
+ <NodeView
440
+ key={id}
441
+ node={node}
442
+ look={nodeLook(node, selected, path)}
443
+ box={drawn.get(id) ?? place(node, { live, landed })}
444
+ landing={positioned && !node.position && !landed.has(id)}
445
+ lifted={live.has(id)}
446
+ targeted={link?.target === id}
447
+ below={below}
448
+ bare={bare}
449
+ edit={edit}
450
+ onSelect={onSelect}
451
+ onSize={resized}
452
+ onFocusVisible={reveal}
453
+ />
454
+ );
455
+ })}
456
+ {link && source ? (
457
+ <ConnectionLine from={source} to={link.to} pair={space.pair} />
458
+ ) : null}
459
+ </div>
460
+ <ZoomStack
461
+ viewport={viewport}
462
+ onFit={fit}
463
+ minZoom={minZoom}
464
+ anchor={anchor}
465
+ onArrange={onMove ? arrange : undefined}
466
+ />
467
+ {act ? <ActFoot act={act} /> : null}
468
+ {probed ? (
469
+ // The probe's parts name it: new parts are a new probe, which the
470
+ // layout measures afresh.
471
+ <div
472
+ key={`${groups.map((group) => group.id + group.head).join()}:${chipped.map((edge) => edge.id).join()}`}
473
+ ref={setProbe}
474
+ aria-hidden="true"
475
+ className={PROBE}
476
+ >
477
+ {groups.map((group) => (
478
+ <div
479
+ key={group.id}
480
+ data-head={group.id}
481
+ className={cn(canvasGroup({ state: "rest" }), COLUMN)}
482
+ >
483
+ <div className={cn(CANVAS_GROUP_HEAD, FLOW)}>
484
+ <span>{group.head || NBSP}</span>
485
+ </div>
486
+ </div>
487
+ ))}
488
+ {chipped.map((edge) => (
489
+ <div key={edge.id} data-label={edge.id}>
490
+ <EdgeLabel label={edge.label} handoff={edge.handoff ?? false} />
491
+ </div>
492
+ ))}
493
+ </div>
494
+ ) : null}
495
+ </section>
496
+ );
497
+ }
@@ -0,0 +1,53 @@
1
+ import { HANDOFF_GLYPH } from "@slotform/ui-core/canvas";
2
+ import { cn } from "@slotform/ui-core/cn";
3
+ import { PATH_TONE } from "@slotform/ui-core/variants";
4
+ import { ChipBase } from "../../atoms/chip/base.tsx";
5
+ import { Icon } from "../../atoms/icon/index.tsx";
6
+ import type { Box } from "./geometry.ts";
7
+ import type { EdgeTone } from "./look.ts";
8
+
9
+ const CHIP = "flex items-center gap-gap";
10
+ const INK = "text-ink-meta";
11
+ // The handoff glyph, a graphic, dims to the mark ink (3:1); the chip's words
12
+ // step down to the meta ink, the quietest a word has (4.5:1), as a node off
13
+ // the path's do.
14
+ const DIM = "text-ink-mark";
15
+ const PLACED = "absolute pointer-events-none";
16
+ const HIDDEN = "invisible";
17
+
18
+ // An edge's chip: its label in a neutral `Chip`, a handoff edge's glyph before
19
+ // it. The layer draws it at `at` and the hidden probe draws it where it flows,
20
+ // so what the router places is what was measured. Under the text floor it keeps
21
+ // its place and draws nothing.
22
+ export function EdgeLabel({
23
+ label,
24
+ handoff,
25
+ tone = "rest",
26
+ at,
27
+ below = false,
28
+ }: {
29
+ label?: string;
30
+ handoff: boolean;
31
+ tone?: EdgeTone;
32
+ at?: Box;
33
+ below?: boolean;
34
+ }) {
35
+ return (
36
+ // The chip's place is a coordinate of the flow, known at run time.
37
+ <div
38
+ className={cn(
39
+ CHIP,
40
+ PATH_TONE,
41
+ tone === "dimmed" ? DIM : INK,
42
+ at && PLACED,
43
+ below && HIDDEN,
44
+ )}
45
+ style={at && { left: at.x, top: at.y }}
46
+ >
47
+ {handoff ? <Icon name={HANDOFF_GLYPH} fit="meta" /> : null}
48
+ {label ? (
49
+ <ChipBase label={label} family="neutral" quiet={tone === "dimmed"} />
50
+ ) : null}
51
+ </div>
52
+ );
53
+ }