@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,575 @@
1
+ import type { Dragged, Drop, Moved } from "@slotform/ui-core/descriptors";
2
+ import {
3
+ accepts,
4
+ type Band,
5
+ bandAt,
6
+ DRAG_DELAY,
7
+ DROP_HOLD,
8
+ edgeDepth,
9
+ edgeSpeed,
10
+ moveOf,
11
+ pressed,
12
+ sideOf,
13
+ } from "@slotform/ui-core/drag";
14
+ import { createStore } from "@slotform/ui-core/store";
15
+ import { counted, type Words } from "@slotform/ui-core/tokens";
16
+ import {
17
+ type PointerEvent as ReactPointerEvent,
18
+ useEffect,
19
+ useId,
20
+ useMemo,
21
+ useRef,
22
+ useSyncExternalStore,
23
+ } from "react";
24
+ import { type Act, useAct } from "./act.ts";
25
+
26
+ // Drag and drop on pointer events: docs/architecture/ui-core/drag.md, "Web drag".
27
+
28
+ /** What a press on a row hands the store, once it becomes a drag. */
29
+ interface Source {
30
+ /** What is dragged to a drop; none for a row that is only reordered. */
31
+ drag?: Dragged;
32
+ /** What the label that follows the pointer reads: the row's name, or the count dragged. */
33
+ label: string;
34
+ /** The row is dragged within its collection too. */
35
+ reorder?: Reordering;
36
+ }
37
+
38
+ /** A collection's order, as a drag within it reads and moves it. */
39
+ interface Reordering {
40
+ /** The row dragged. */
41
+ id: string;
42
+ /** The collection's box: only rows inside it are places to land. */
43
+ root: () => HTMLElement | null;
44
+ /** The ids in the order they stand now (undefined where a window has not loaded a row). */
45
+ ids: () => readonly (string | undefined)[];
46
+ move: (id: string, to: Moved) => void;
47
+ }
48
+
49
+ /** Where the line that marks the landing is, in the viewport. */
50
+ interface Line {
51
+ left: number;
52
+ top: number;
53
+ width: number;
54
+ }
55
+
56
+ /** What a drag in progress looks like, for the layer that draws it. */
57
+ interface View {
58
+ label: string;
59
+ x: number;
60
+ y: number;
61
+ over: string | undefined;
62
+ line: Line | undefined;
63
+ }
64
+
65
+ interface Live {
66
+ source: Source;
67
+ /** The folded branch the pointer has stood over, and since when (ms). */
68
+ holding: string | undefined;
69
+ since: number;
70
+ x: number;
71
+ y: number;
72
+ over: string | undefined;
73
+ moved: Moved | undefined;
74
+ line: Line | undefined;
75
+ }
76
+
77
+ /** A drop as the store takes it: the data it declares, and how to run a drop on it with its wiring. */
78
+ interface Target {
79
+ id: string;
80
+ drop: () => Drop | undefined;
81
+ run: (drag: Dragged) => void;
82
+ /** The place's path, which names it where labels clash; none for a row. */
83
+ path: string;
84
+ /** The route the target leads to (a place's route, a row's `href`), which says two registrations are one target. */
85
+ route: string | undefined;
86
+ }
87
+
88
+ /** A drop that is only offered by menu: a place's, whether or not its row stands. */
89
+ interface Listing {
90
+ drop: () => Drop | undefined;
91
+ path: string;
92
+ route: string | undefined;
93
+ }
94
+
95
+ /** A branch a drag opens by holding over it. */
96
+ interface Hold {
97
+ wants: (kind: string) => boolean;
98
+ open: () => void;
99
+ }
100
+
101
+ const targets = new Map<string, Target>();
102
+ const listed = new Map<string, Listing>();
103
+ const holds = new Map<string, Hold>();
104
+ const store = createStore<View | undefined>(undefined);
105
+ // Counts every change to the drops standing, so what lists them draws again.
106
+ let version = 0;
107
+
108
+ function same(a: View | undefined, b: View | undefined): boolean {
109
+ if (!a || !b) return a === b;
110
+ return (
111
+ a.x === b.x &&
112
+ a.y === b.y &&
113
+ a.over === b.over &&
114
+ a.label === b.label &&
115
+ a.line?.top === b.line?.top &&
116
+ a.line?.left === b.line?.left &&
117
+ a.line?.width === b.line?.width
118
+ );
119
+ }
120
+
121
+ /** The drag in progress, for the layer that draws its label and its line. */
122
+ export function useDrag(): View | undefined {
123
+ return useSyncExternalStore(store.subscribe, store.get, () => undefined);
124
+ }
125
+
126
+ // WebKit ignores a `preventDefault` from a `touchmove` listener added after
127
+ // the touch began, and a finger's drag begins after its hold: so the one
128
+ // non-passive listener stands while any draggable stands, before any touch,
129
+ // and acts only while a drag stands.
130
+ let holders = 0;
131
+
132
+ /** Holds the page still under a finger's drag while `on`, a part `press` drags being mounted: the first holder registers the one non-passive `touchmove` listener, the last to unmount removes it. */
133
+ export function useTouchHold(on: boolean): void {
134
+ useEffect(() => {
135
+ if (!on) return undefined;
136
+ if (holders++ === 0)
137
+ window.addEventListener("touchmove", holdPage, { passive: false });
138
+ return () => {
139
+ if (--holders === 0) window.removeEventListener("touchmove", holdPage);
140
+ };
141
+ }, [on]);
142
+ }
143
+
144
+ function holdPage(event: TouchEvent) {
145
+ if (standing === "drag" && event.cancelable) event.preventDefault();
146
+ }
147
+
148
+ /** Whether an element is a scroller that can still scroll toward `side` (below 0 its start, above 0 its end). */
149
+ function canScroll(element: Element, side: number): boolean {
150
+ if (!/auto|scroll/.test(getComputedStyle(element).overflowY)) return false;
151
+ return side < 0
152
+ ? element.scrollTop > 0
153
+ : Math.ceil(element.scrollTop) + element.clientHeight <
154
+ element.scrollHeight;
155
+ }
156
+
157
+ /** Opens the folded branch a drag of a kind a drop under it accepts has stood over for `DROP_HOLD`. */
158
+ function hold(live: Live, found: Element[]) {
159
+ const kind = live.source.drag?.kind;
160
+ let holding: string | undefined;
161
+ if (kind !== undefined) {
162
+ for (const element of found) {
163
+ const id = element.closest<HTMLElement>("[data-hold]")?.dataset.hold;
164
+ if (id !== undefined && holds.get(id)?.wants(kind)) {
165
+ holding = id;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ const now = performance.now();
171
+ if (holding !== live.holding) {
172
+ live.holding = holding;
173
+ live.since = now;
174
+ } else if (holding !== undefined && now - live.since >= DROP_HOLD) {
175
+ holds.get(holding)?.open();
176
+ live.since = now;
177
+ }
178
+ }
179
+
180
+ /** The press that follows a drag on the element that started it is not a click: the first click after the release is swallowed. */
181
+ function swallowClick() {
182
+ const stop = (event: Event) => {
183
+ event.stopPropagation();
184
+ event.preventDefault();
185
+ };
186
+ window.addEventListener("click", stop, { capture: true, once: true });
187
+ // No event says a release brings no click: the click fires in the release's
188
+ // task or never, so the next task ends the wait.
189
+ setTimeout(() => window.removeEventListener("click", stop, true), 0);
190
+ }
191
+
192
+ // A press that lands on a control of its own is that control's; the row's hit
193
+ // is the one control a press on the row lands on.
194
+ const OWN =
195
+ "button:not([data-hit]), input, textarea, select, [contenteditable], [role=menuitem], [role=checkbox], [role=slider], [data-nodrag]";
196
+
197
+ // What stands: a press, or the drag it became.
198
+ let standing: "press" | "drag" | undefined;
199
+
200
+ /** Starts a press on a row. Past the slop (a finger: after its hold) it becomes a drag, which `make` describes lazily, since the rows are read when the drag begins. Escape and a cancelled pointer end it with no drop. Its part calls `useTouchHold`, so the page holds still under a finger's drag. */
201
+ export function press(
202
+ event: ReactPointerEvent<Element>,
203
+ make: () => Source | undefined,
204
+ ): void {
205
+ if (standing) return;
206
+ if (event.button !== 0 || !event.isPrimary) return;
207
+ if (event.target instanceof Element && event.target.closest(OWN)) return;
208
+ const pointer = event.pointerType;
209
+ // Only the pointer that started the press moves, drops or ends it: a second
210
+ // finger's touch is not this drag's.
211
+ const { pointerId } = event;
212
+ const mine = (next: PointerEvent) => next.pointerId === pointerId;
213
+ const start = { x: event.clientX, y: event.clientY };
214
+ const at = { ...start };
215
+ let held = pointer !== "touch";
216
+ let live: Live | undefined;
217
+ let frame = 0;
218
+ // The edge band the drag stands in, which its speed ramps from (`bandAt`).
219
+ let band: Band | undefined;
220
+ let timer: ReturnType<typeof setTimeout> | undefined;
221
+
222
+ // A held finger's long press would open the browser's menu on a link.
223
+ const stop = (event: Event) => event.preventDefault();
224
+
225
+ const probe = (found: Element[]) => {
226
+ if (!live) return;
227
+ const { source } = live;
228
+ let over: string | undefined;
229
+ for (const element of found) {
230
+ const mark = element.closest<HTMLElement>("[data-drop]");
231
+ const target = mark && targets.get(mark.dataset.drop ?? "");
232
+ const drop = target?.drop();
233
+ if (
234
+ target &&
235
+ drop &&
236
+ drop.blocked === undefined &&
237
+ accepts(drop.accepts, source.drag?.kind)
238
+ ) {
239
+ over = target.id;
240
+ break;
241
+ }
242
+ }
243
+ let moved: Moved | undefined;
244
+ let line: Line | undefined;
245
+ const order = source.reorder;
246
+ const root = order?.root();
247
+ if (order && root && over === undefined) {
248
+ for (const element of found) {
249
+ const mark = element.closest<HTMLElement>("[data-row]");
250
+ if (!mark || !root.contains(mark)) continue;
251
+ const row = mark.closest("tr") ?? mark;
252
+ const box = row.getBoundingClientRect();
253
+ const side = sideOf(live.y, box.top, box.height);
254
+ moved = moveOf(order.ids(), order.id, mark.dataset.row ?? "", side);
255
+ if (moved) {
256
+ line = {
257
+ left: box.left,
258
+ width: box.width,
259
+ top: side === "before" ? box.top : box.bottom,
260
+ };
261
+ }
262
+ break;
263
+ }
264
+ }
265
+ live.over = over;
266
+ live.moved = moved;
267
+ live.line = line;
268
+ };
269
+
270
+ // The innermost scroller under the pointer whose edge band it stands in and
271
+ // that can still move that way is carried, whichever list holds it; one at
272
+ // its end passes the drag to the next one out.
273
+ const carry = (y: number, found: Element[], time: number) => {
274
+ for (const scroller of found) {
275
+ const box = scroller.getBoundingClientRect();
276
+ const depth = edgeDepth(y, box.top, box.bottom);
277
+ const side = Math.sign(depth);
278
+ if (side === 0 || !canScroll(scroller, side)) continue;
279
+ const step = bandAt(band, scroller, side, time);
280
+ band = step.band;
281
+ scroller.scrollTop += edgeSpeed(depth, step.elapsed, step.since);
282
+ return;
283
+ }
284
+ band = undefined;
285
+ };
286
+
287
+ const tick = (time: number) => {
288
+ frame = requestAnimationFrame(tick);
289
+ if (!live) return;
290
+ const found = document.elementsFromPoint(live.x, live.y);
291
+ carry(live.y, found, time);
292
+ // A branch held over opens.
293
+ hold(live, found);
294
+ probe(found);
295
+ const next: View = {
296
+ label: live.source.label,
297
+ x: live.x,
298
+ y: live.y,
299
+ over: live.over,
300
+ line: live.line,
301
+ };
302
+ if (!same(store.get(), next)) store.set(next);
303
+ };
304
+
305
+ const begin = () => {
306
+ const source = make();
307
+ if (!source) return cleanup(false);
308
+ live = {
309
+ source,
310
+ holding: undefined,
311
+ since: 0,
312
+ x: at.x,
313
+ y: at.y,
314
+ over: undefined,
315
+ moved: undefined,
316
+ line: undefined,
317
+ };
318
+ standing = "drag";
319
+ window.getSelection()?.removeAllRanges();
320
+ window.addEventListener("selectstart", stop);
321
+ frame = requestAnimationFrame(tick);
322
+ };
323
+
324
+ const move = (next: PointerEvent) => {
325
+ if (!mine(next)) return;
326
+ at.x = next.clientX;
327
+ at.y = next.clientY;
328
+ if (live) {
329
+ live.x = at.x;
330
+ live.y = at.y;
331
+ return;
332
+ }
333
+ const verdict = pressed(
334
+ pointer,
335
+ Math.hypot(at.x - start.x, at.y - start.y),
336
+ held,
337
+ );
338
+ if (verdict === "cancel") cleanup(false);
339
+ else if (verdict === "drag") begin();
340
+ };
341
+
342
+ const up = (next: PointerEvent) => {
343
+ if (mine(next)) cleanup(true);
344
+ };
345
+ const cancel = (next: PointerEvent) => {
346
+ if (mine(next)) cleanup(false);
347
+ };
348
+ const key = (event: KeyboardEvent) => {
349
+ if (event.key === "Escape") cleanup(false);
350
+ };
351
+
352
+ function cleanup(commit: boolean) {
353
+ clearTimeout(timer);
354
+ cancelAnimationFrame(frame);
355
+ window.removeEventListener("pointermove", move);
356
+ window.removeEventListener("pointerup", up);
357
+ window.removeEventListener("pointercancel", cancel);
358
+ window.removeEventListener("keydown", key, true);
359
+ window.removeEventListener("contextmenu", stop);
360
+ window.removeEventListener("selectstart", stop);
361
+ standing = undefined;
362
+ const done = live;
363
+ live = undefined;
364
+ if (!done) return;
365
+ store.set(undefined);
366
+ swallowClick();
367
+ if (!commit) return;
368
+ const { over, moved, source } = done;
369
+ if (over !== undefined && source.drag) targets.get(over)?.run(source.drag);
370
+ else if (moved && source.reorder) {
371
+ source.reorder.move(source.reorder.id, moved);
372
+ }
373
+ }
374
+
375
+ standing = "press";
376
+ window.addEventListener("pointermove", move);
377
+ window.addEventListener("pointerup", up);
378
+ window.addEventListener("pointercancel", cancel);
379
+ window.addEventListener("keydown", key, true);
380
+ window.addEventListener("contextmenu", stop);
381
+ if (pointer === "touch") {
382
+ timer = setTimeout(() => {
383
+ held = true;
384
+ begin();
385
+ }, DRAG_DELAY);
386
+ }
387
+ }
388
+
389
+ /** What the label that follows the pointer reads: the row's name, or how many rows the pick holds (the counted `dragged` word). */
390
+ export function draggedLabel(
391
+ words: Words,
392
+ name: string,
393
+ count: number,
394
+ ): string {
395
+ return count <= 1 ? name : counted(words.dragged, count);
396
+ }
397
+
398
+ // The key of what a drop shows of itself to the rows that offer it by menu.
399
+ function keyOf(drop: Drop | undefined): string {
400
+ if (!drop) return "";
401
+ const kinds =
402
+ typeof drop.accepts === "string" ? drop.accepts : drop.accepts.join(",");
403
+ return `${drop.label}|${drop.blocked ?? ""}|${kinds}`;
404
+ }
405
+
406
+ /** A place that takes a drag (`drop`, data): its `data-drop` mark, which the drag finds it by, and whether a drag it accepts is over it now. Released on, it runs `onDrop` as an act does: its `confirm` asked, its `done` toasted, its `to` gone to, a rejection toasted. `route` is where the place or row leads (a place's route, a row's `href`) and `path` names a place among those that share a label (a menu entry names each by it). */
407
+ export function useDrop(
408
+ drop: Drop | undefined,
409
+ route?: string,
410
+ path = "",
411
+ ): {
412
+ mark: { "data-drop": string } | Record<string, never>;
413
+ over: boolean;
414
+ } {
415
+ const id = useId();
416
+ const acting = useAct<true>("toast");
417
+ const latest = useRef({ drop, acting });
418
+ latest.current = { drop, acting };
419
+ const has = drop !== undefined;
420
+ useEffect(() => {
421
+ if (!has) return undefined;
422
+ targets.set(id, {
423
+ id,
424
+ path,
425
+ route,
426
+ drop: () => latest.current.drop,
427
+ run: (dragged) => {
428
+ const now = latest.current.drop;
429
+ if (!now) return;
430
+ latest.current.acting.attempt(true, {
431
+ label: now.label,
432
+ onAct: () => now.onDrop(dragged),
433
+ confirm: now.confirm,
434
+ done: now.done,
435
+ to: now.to,
436
+ });
437
+ },
438
+ });
439
+ version++;
440
+ store.emit();
441
+ return () => {
442
+ targets.delete(id);
443
+ version++;
444
+ store.emit();
445
+ };
446
+ }, [id, has, path, route]);
447
+ const shows = keyOf(drop);
448
+ const first = useRef(true);
449
+ // What the drop says of itself changed: the rows that offer it by menu draw again.
450
+ useEffect(() => {
451
+ if (first.current) {
452
+ first.current = false;
453
+ return;
454
+ }
455
+ version++;
456
+ store.emit();
457
+ }, [shows]);
458
+ const over = useSyncExternalStore(
459
+ store.subscribe,
460
+ () => store.get()?.over === id,
461
+ () => false,
462
+ );
463
+ return { mark: has ? { "data-drop": id } : {}, over };
464
+ }
465
+
466
+ /** The drops of a set of places, offered by menu whether or not the place stands (a folded branch's children, a place a window has not drawn): the owner of the places' specs lists them, each with its `path`. */
467
+ export function useListedDrops(
468
+ items: readonly { drop: Drop; path: string; route: string }[],
469
+ ): void {
470
+ const id = useId();
471
+ const latest = useRef(items);
472
+ latest.current = items;
473
+ const shows = items
474
+ .map((each) => `${each.path}|${keyOf(each.drop)}`)
475
+ .join("\n");
476
+ useEffect(() => {
477
+ const keys = latest.current.map((each, at) => {
478
+ const key = `${id}:${at}`;
479
+ listed.set(key, {
480
+ path: each.path,
481
+ route: each.route,
482
+ drop: () => latest.current[at]?.drop,
483
+ });
484
+ return key;
485
+ });
486
+ version++;
487
+ store.emit();
488
+ return () => {
489
+ for (const key of keys) listed.delete(key);
490
+ version++;
491
+ store.emit();
492
+ };
493
+ }, [id, shows]);
494
+ }
495
+
496
+ /** A folded branch a drag opens by a hold: the `data-hold` mark the drag finds it by. While a drag of a kind `wants` accepts stands over the branch for a moment, `open` runs. Given none, the branch is no hold. */
497
+ export function useHold(hold: Hold | undefined): {
498
+ "data-hold"?: string;
499
+ } {
500
+ const id = useId();
501
+ const latest = useRef(hold);
502
+ latest.current = hold;
503
+ const has = hold !== undefined;
504
+ useEffect(() => {
505
+ if (!has) return undefined;
506
+ holds.set(id, {
507
+ wants: (kind) => latest.current?.wants(kind) === true,
508
+ open: () => latest.current?.open(),
509
+ });
510
+ return () => {
511
+ holds.delete(id);
512
+ };
513
+ }, [id, has]);
514
+ return has ? { "data-hold": id } : {};
515
+ }
516
+
517
+ /** The acts that do by menu what a drag of `kind` does by pointer: one for each drop standing that accepts it, running the drop with what `payload` reads when the act is pressed. Nothing is merged by label. Places that share a label are each named by their path (`Add to Warm-up (Sets › Warm-up)`). A row and a place are one target only where the row's `href` is the place's route, and then one entry; every other row stands under its label, beside a place of that label too, and a row with no `href` always stands. Appended to the row's own more menu, so a drag is never the only way. */
518
+ export function useOffers(
519
+ kind: string | undefined,
520
+ payload: () => Dragged,
521
+ ): readonly Act[] {
522
+ const stood = useSyncExternalStore(
523
+ store.subscribe,
524
+ () => version,
525
+ () => 0,
526
+ );
527
+ const read = useRef(payload);
528
+ read.current = payload;
529
+ return useMemo(() => {
530
+ if (kind === undefined) return [];
531
+ // Every drop standing that takes the kind: a place's with its path, a row's
532
+ // with where it leads.
533
+ const entries: { drop: Drop; path: string; route: string | undefined }[] =
534
+ [];
535
+ for (const each of [...targets.values(), ...listed.values()]) {
536
+ const drop = each.drop();
537
+ if (drop && accepts(drop.accepts, kind)) {
538
+ entries.push({ drop, path: each.path, route: each.route });
539
+ }
540
+ }
541
+ const places = entries.filter((each) => each.path);
542
+ const stands = (label: string) =>
543
+ places.filter((each) => each.drop.label === label);
544
+ const acts: Act[] = [];
545
+ const seen = new Set<string>();
546
+ for (const each of entries) {
547
+ const { drop, path, route } = each;
548
+ // A row and a place are one target where both lead to one route; a row
549
+ // with no route is its own. Every other drop stands.
550
+ const same = path
551
+ ? places.find(
552
+ (one) => one.path === path && one.drop.label === drop.label,
553
+ ) !== each
554
+ : route !== undefined &&
555
+ places.some(
556
+ (one) => one.route === route && one.drop.label === drop.label,
557
+ );
558
+ const key = `${drop.label}\0${path || route || ""}`;
559
+ if (same || ((path || route) && seen.has(key))) continue;
560
+ seen.add(key);
561
+ // Places that share a label are named by their paths.
562
+ const clash =
563
+ path && new Set(stands(drop.label).map((one) => one.path)).size > 1;
564
+ acts.push({
565
+ label: clash ? `${drop.label} (${path})` : drop.label,
566
+ blocked: drop.blocked,
567
+ confirm: drop.confirm,
568
+ done: drop.done,
569
+ to: drop.to,
570
+ onAct: () => drop.onDrop(read.current()),
571
+ });
572
+ }
573
+ return acts;
574
+ }, [kind, stood]);
575
+ }
@@ -0,0 +1,85 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import type { LinkAct } from "@slotform/ui-core/descriptors";
3
+ import type { Datum } from "@slotform/ui-core/read";
4
+ import {
5
+ CARD_PART,
6
+ EMPTY_LINE,
7
+ EMPTY_PART,
8
+ EMPTY_SENTENCE,
9
+ text,
10
+ } from "@slotform/ui-core/variants";
11
+ import { use } from "react";
12
+ import { Icon } from "../atoms/icon/index.tsx";
13
+ import { ActFace } from "../components/button/face.tsx";
14
+ import type { Act } from "./act.ts";
15
+ import { PageTitle } from "./container.ts";
16
+
17
+ /** Where an empty state stands: `part` inside the card whose rows it stands for (a Section's sheet, a Form's, a Sheet's body), at the card's inset; `frame` on a card of its own (a Section's or a Pane's body). The drawer that owns the card says which. */
18
+ export type EmptyFit = "part" | "frame";
19
+
20
+ // A chart's loaded height keeps its height; the line stands at its top start.
21
+ const FILL = "grow";
22
+ const MARK = "inline-flex items-center shrink-0 text-danger";
23
+ // The act stands across a column at the empty width, centred under the
24
+ // sentence: a block that narrow stacks its acts across it, as an `ActionBar`
25
+ // at or under the auth width does.
26
+ const ACT = "w-full max-w-empty";
27
+
28
+ /** What a frame says when the read it holds is empty: the sentence, and the act that makes the first one. It stands only for the frame's own read, so a frame given none has no empty state. */
29
+ export interface Empty {
30
+ /** What is missing and what the act makes (a sentence; wraps). A selection (a pick of the read the frame holds) draws once it lands, the generic sentence until then. */
31
+ sentence: Datum<string>;
32
+ /** The way to make the first one: an act that runs, or a `{ label, href }` that goes to a route (an anchor), either drawn as the create act. */
33
+ act?: Act | LinkAct;
34
+ }
35
+
36
+ /** The empty state every empty `Pane`, `Section` and collection, a failed read and a missing one draw: one sentence in the meta ink at the body role over its act as an outlined button across a column at the empty width, both centred, at a card's inset: inside the card it stands for, or on a card of its own. Outside the package's exports: `fit` and `tone` are the drawers' alone. */
37
+ export function EmptyBase(props: {
38
+ fit: EmptyFit;
39
+ tone: "rest" | "failed" | "missing";
40
+ // None is the Section's empty that offers an act in place of a sentence.
41
+ sentence?: string;
42
+ // A missing shape's way back goes to a route, so it is a link; so is a create
43
+ // act that navigates.
44
+ act?: Act | LinkAct;
45
+ // The line fills the box it stands in and stands at its top start (a
46
+ // chart's loaded height).
47
+ fill?: boolean;
48
+ // A failed shape that stands for its Pane (a Pane's own frame).
49
+ headed?: boolean;
50
+ }) {
51
+ // A missing or failed shape without a title is the Pane's `h2`: docs/architecture/ui-core/reads.md, "Missing and failed shapes".
52
+ const headed =
53
+ (props.tone === "missing" || (props.tone === "failed" && props.headed)) &&
54
+ use(PageTitle) === undefined;
55
+ const Heading = "h2" as const;
56
+ const look = cn(text({ role: "body" }), EMPTY_SENTENCE);
57
+ const sentence =
58
+ props.sentence === undefined ? null : headed ? (
59
+ <Heading className={look}>{props.sentence}</Heading>
60
+ ) : (
61
+ <span className={look}>{props.sentence}</span>
62
+ );
63
+ return (
64
+ <div
65
+ className={cn(
66
+ EMPTY_LINE,
67
+ props.fit === "part" ? EMPTY_PART : CARD_PART,
68
+ props.fill && FILL,
69
+ )}
70
+ >
71
+ {/* Only a failed read draws a mark: the alert. */}
72
+ {props.tone === "failed" ? (
73
+ <span className={MARK}>
74
+ <Icon name="CircleAlert" fit="control" />
75
+ </span>
76
+ ) : null}
77
+ {sentence}
78
+ {props.act ? (
79
+ <div className={ACT}>
80
+ <ActFace act={props.act} kind="secondary" fit="field" />
81
+ </div>
82
+ ) : null}
83
+ </div>
84
+ );
85
+ }
@@ -0,0 +1,39 @@
1
+ import type { Run, Runs } from "@slotform/ui-core/descriptors";
2
+ import { createContext } from "react";
3
+
4
+ // A toggle a row's label targets: docs/architecture/ui-core/acts.md, "Fields".
5
+ export interface LabelFit {
6
+ labelledBy?: string;
7
+ }
8
+
9
+ /** What every control takes as a field: its name, and what a field draws around it. */
10
+ export interface Fielded {
11
+ /** What it holds, the control's name (a short phrase; wraps). Placed alone the control draws as its field, this over it; where an owner names it (a `Form`'s field, a `Toolbar`), and at a bar fit that reads it in its box, it is the accessible name. */
12
+ label: string;
13
+ /** Under the control placed alone (a sentence; wraps). */
14
+ description?: string;
15
+ /** The value may be left empty: the word `optional` stands under the label. */
16
+ optional?: boolean;
17
+ /** The message that takes the description's place, the control drawn in error (a sentence; wraps): words, or runs of one sentence, so a machine name in it is a `{ code }` run boxed in the mono (`["No project at ", { code: "apps/billing" }, "."]`). A save the server refused stands there on its own until the next change, a span of its message between backticks boxed the same (`markedRuns`). */
18
+ error?: Run | Runs;
19
+ }
20
+
21
+ /** The keys a `FormField` supplies to the control it draws. */
22
+ export type FieldedKey = keyof Fielded;
23
+
24
+ // The label that names a group of controls: docs/architecture/ui-core/acts.md, "Fields".
25
+ export const GroupName = createContext<{ labelledBy: string } | undefined>(
26
+ undefined,
27
+ );
28
+
29
+ // A `Table` cell's edit: docs/architecture/ui-core/acts.md, "Fields".
30
+ export interface CellFit {
31
+ label: string;
32
+ starts: boolean;
33
+ }
34
+
35
+ // An `Input` a row draws without a `FormField`: docs/architecture/ui-core/acts.md, "Fields".
36
+ export interface InlineFit {
37
+ label: string;
38
+ focus?: boolean;
39
+ }