@slotform/react-ui 0.0.0-stage → 0.1.1

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,356 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import {
3
+ folding,
4
+ type SheetEntry,
5
+ sheetEntries,
6
+ toggled,
7
+ treeMove,
8
+ treeRows,
9
+ treeStop,
10
+ } from "@slotform/ui-core/list-state";
11
+ import { PAINT_ROWS } from "@slotform/ui-core/paint";
12
+ import { useArrivals } from "@slotform/ui-core/react/arrive";
13
+ import { useReach } from "@slotform/ui-core/react/read";
14
+ import { type Drawn, placeholderData, type Read } from "@slotform/ui-core/read";
15
+ import { isRun, partText } from "@slotform/ui-core/runs";
16
+ import { VIRTUAL_FROM } from "@slotform/ui-core/window";
17
+ import {
18
+ type ComponentProps,
19
+ type FocusEvent,
20
+ type KeyboardEvent,
21
+ type ReactElement,
22
+ type ReactNode,
23
+ type RefObject,
24
+ useCallback,
25
+ useEffect,
26
+ useLayoutEffect,
27
+ useRef,
28
+ useState,
29
+ } from "react";
30
+ import { TREE_DIVIDER } from "../../atoms/section/card.tsx";
31
+ import { MoreRow } from "../../lib/more.tsx";
32
+ import { ListedRoute, useRoute } from "../../lib/navigate.ts";
33
+ import { edgeOf, type Movable } from "../../lib/reorder.ts";
34
+ import { Window } from "../../lib/window.tsx";
35
+ import { FileRowItem } from "../file-row/index.tsx";
36
+ import { RowItem } from "../row/index.tsx";
37
+ import { BandHead } from "./band.tsx";
38
+ import type { Band, Row, RowItem as RowItemData } from "./index.tsx";
39
+
40
+ /** What the Section hands its rows; no app sets it, so it is outside `SectionProps`. */
41
+ interface RowsInternal {
42
+ /** The items as the Section subscribed to them: real, or the placeholder while the read waits, which paints `PAINT_ROWS` rows. */
43
+ drawn: Drawn<readonly Row[]>;
44
+ /** The pages read the items come from, when it has pages: the sheet ends in its more row. */
45
+ more?: Read<unknown>;
46
+ /** The sheet the rows stand in, which the rows window against; a painted next page stands in its Section's. */
47
+ box?: RefObject<HTMLElement | null>;
48
+ /** The rows are the painted next page of another sheet: its rows alone, standing in that sheet's box. */
49
+ tail?: boolean;
50
+ /** The Section's `groups`. */
51
+ groups?: readonly Band[];
52
+ /** The Section's head counts the rows, so a lone band holding every row draws no count of its own: one band, one count. */
53
+ counted?: boolean;
54
+ /** The row the painted rows take their shape from, a list row or a file row; a title line over a facts line when none is given. */
55
+ paint?: Row;
56
+ /** The sheet reorders (the caller has put `drawn` in the order the viewer moved it to): its rows read the order and move by this. A flat, ungrouped sheet only. */
57
+ movable?: Movable;
58
+ }
59
+
60
+ // The painted row: a title line over a facts line, whatever kind the loaded rows
61
+ // are.
62
+ const PAINTED: RowItemData = { key: "", title: "", facts: [""] };
63
+
64
+ const isList = (item: Row): item is RowItemData => !("path" in item);
65
+ /** A row's key in its sheet: its `key`, else its label's text, else its title's words. */
66
+ export const keyOf = (item: Row) => {
67
+ if (item.key !== undefined) return item.key;
68
+ if (isList(item)) {
69
+ if (typeof item.label === "string") return item.label;
70
+ if (item.label) return item.label.code;
71
+ const { title } = item;
72
+ if (Array.isArray(title) || isRun(title)) return partText(title);
73
+ }
74
+ return "";
75
+ };
76
+ const childrenOf = (item: Row) => (isList(item) ? item.children : undefined);
77
+
78
+ // A tree has no box of its own but the card: its rows stand in the tree's
79
+ // element on it, a hairline falling once between them.
80
+ const GROUP_TREE = "flex flex-col";
81
+
82
+ // A tree's keyboard: the container's role and the keys and focus its rows'
83
+ // `treeitem` stops send up.
84
+ interface TreeNav {
85
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
86
+ onFocus: (event: FocusEvent<HTMLElement>) => void;
87
+ }
88
+
89
+ /** The rows of a Section's sheet, of whatever kinds, with its bands and its tree. Outside the package's exports: `drawn` is the Section's alone. */
90
+ export function Rows(props: RowsInternal) {
91
+ const at = useRoute();
92
+ // The keys of the tree's folded branches; every branch starts open.
93
+ const [folded, setFolded] = useState<readonly string[]>([]);
94
+ // The key of the tree's row that holds its one tab stop.
95
+ const [active, setActive] = useState<string>();
96
+ const fold = useCallback(
97
+ (key: string) => setFolded((keys) => toggled(keys, key)),
98
+ [],
99
+ );
100
+ const own = useRef<HTMLDivElement>(null);
101
+ const box = props.box ?? own;
102
+ const treeBox = useRef<HTMLDivElement>(null);
103
+ // The visible row a key is moving the focus to, by its key; it stands
104
+ // mounted by then, the window keeping the tab stop's row, and takes the focus.
105
+ const [wanted, setWanted] = useState<{ key: string }>();
106
+ useLayoutEffect(() => {
107
+ if (!wanted) return;
108
+ treeBox.current
109
+ ?.querySelectorAll<HTMLElement>('[role="treeitem"]')
110
+ .forEach((each) => {
111
+ if (each.dataset.key === wanted.key) each.focus();
112
+ });
113
+ }, [wanted]);
114
+ const { drawn } = props;
115
+ const painted = drawn.painted;
116
+ // Painted, as many list items as the sheet last loaded (`PAINT_ROWS` while it
117
+ // has not, never more than a sheet draws whole), so the rows it waits with
118
+ // are the rows that land; the data's depths are not there, so a painted tree
119
+ // draws flat.
120
+ const last = useRef<number>(undefined);
121
+ useEffect(() => {
122
+ if (!drawn.painted && drawn.value.length > 0)
123
+ last.current = drawn.value.length;
124
+ }, [drawn]);
125
+ const loaded: readonly Row[] = drawn.painted
126
+ ? Array.from(
127
+ { length: Math.min(last.current ?? PAINT_ROWS, VIRTUAL_FROM - 1) },
128
+ () => props.paint ?? PAINTED,
129
+ )
130
+ : drawn.value;
131
+ // The groups' bands stand over a loaded standalone list of rows. Rows of no
132
+ // group stand before the first band, the rest in their group's order, each
133
+ // group's rows in the items' order.
134
+ const groups = painted ? undefined : props.groups;
135
+ const rank = (item: Row) =>
136
+ groups?.findIndex(
137
+ (each) => each.key === (isList(item) ? item.group : undefined),
138
+ ) ?? -1;
139
+ const items: readonly Row[] = groups
140
+ ? [...loaded].sort((a, b) => rank(a) - rank(b))
141
+ : loaded;
142
+ // Every index below is one a loop, the window or `treeMove` hands out over
143
+ // the list it reads, so it is in range: the index casts are what
144
+ // `noUncheckedIndexedAccess` cannot see.
145
+ // A sheet is a tree once any of its list items declares `children`.
146
+ const tree = !painted && items.some((item) => childrenOf(item) !== undefined);
147
+ // The rows that came on their own once the list was drawn fade in as they
148
+ // mount; what it held when first drawn is not new.
149
+ const arrived = useArrivals(painted ? [] : items.map(keyOf), !painted);
150
+ // A painted row's key is its place, the painted rows sharing one item.
151
+ // Only a painted row takes its place as a prop (it varies the filler): a
152
+ // loaded row's place moves when a row lands above it, which would render
153
+ // every row below.
154
+ const paintedAt = (index: number) => (painted ? index : undefined);
155
+ const keyAt = (index: number) =>
156
+ painted ? `waiting-${index}` : keyOf(items[index] as Row);
157
+ // A tree's rows are the visible ones: each item, then its open children.
158
+ const visible = tree
159
+ ? treeRows(items, { key: keyOf, children: childrenOf }, folded)
160
+ : undefined;
161
+ // The sheet's entries: its rows, and with groups a band before the first
162
+ // row of each group (and every empty group's, after the last); a tree's
163
+ // child stands with its parent.
164
+ const entries = sheetEntries(
165
+ visible
166
+ ? visible.map((each) => ({
167
+ key: each.key,
168
+ depth: each.depth,
169
+ group: each.depth === 0 ? rank(each.item) : -1,
170
+ }))
171
+ : items.map((item, index) => ({
172
+ key: keyAt(index),
173
+ depth: 0,
174
+ group: rank(item),
175
+ })),
176
+ groups,
177
+ props.counted === true,
178
+ );
179
+ // The next page loads as the window reaches the sheet's last entry.
180
+ const reach = useReach(entries.length);
181
+ // One row by its kind: a file row alone has a shape of its own; a tree's rows take their place in it.
182
+ const rowOf = (
183
+ item: Row,
184
+ key: string,
185
+ index: number | undefined,
186
+ place?: Omit<
187
+ ComponentProps<typeof RowItem>,
188
+ "item" | "id" | "painted" | "arrived"
189
+ >,
190
+ ) => {
191
+ if ("path" in item)
192
+ return (
193
+ <FileRowItem key={key} item={item} index={index} painted={painted} />
194
+ );
195
+ return (
196
+ <RowItem
197
+ key={key}
198
+ id={key}
199
+ {...place}
200
+ item={item}
201
+ index={index}
202
+ painted={painted}
203
+ arrived={arrived.has(key)}
204
+ />
205
+ );
206
+ };
207
+ // The sheet is the rows' box: they stand in it directly, so its hairline
208
+ // falls once between them, a tree's too (its rails run unbroken); facts draw
209
+ // none. The rows read the route the Section read once, through `ListedRoute`.
210
+ // The entries are built by their index, so a long Window builds only the
211
+ // ones it mounts; the others draw all of them.
212
+ const frame = (
213
+ rowAt: (index: number) => ReactElement,
214
+ nav?: TreeNav,
215
+ keep?: number,
216
+ ) => {
217
+ const entryAt = (at: number) => {
218
+ const entry = entries[at] as SheetEntry;
219
+ if (entry.kind === "row") return rowAt(entry.index);
220
+ const group = groups?.[entry.index] as Band;
221
+ return (
222
+ <BandHead
223
+ key={entry.key}
224
+ label={group.label}
225
+ count={entry.count}
226
+ act={group.act}
227
+ />
228
+ );
229
+ };
230
+ const rows = () =>
231
+ Array.from({ length: entries.length }, (_, at) => entryAt(at));
232
+ if (props.tail) return <ListedRoute value={at}>{rows()}</ListedRoute>;
233
+ // The more row ends the box, so the rows it paints stand at the box's
234
+ // rhythm; a tree's box holds treeitems alone, so its row follows it.
235
+ const more =
236
+ props.more && !painted ? (
237
+ <MoreRow
238
+ read={props.more}
239
+ empty={items.length === 0}
240
+ inset
241
+ scrolled={reach.reached}
242
+ paint={() => (
243
+ <Rows
244
+ {...props}
245
+ more={undefined}
246
+ tail={!nav}
247
+ // The next page paints in the shape of the rows that stand.
248
+ paint={props.paint ?? items[0]}
249
+ drawn={{
250
+ painted: true,
251
+ phase: "waiting",
252
+ value: placeholderData(),
253
+ }}
254
+ />
255
+ )}
256
+ />
257
+ ) : null;
258
+ // A tree's rows window too, the one holding the tab stop kept mounted.
259
+ const shown = (
260
+ <Window
261
+ count={entries.length}
262
+ keyOf={(at) => (entries[at] as SheetEntry).key}
263
+ row={entryAt}
264
+ box={nav ? treeBox : box}
265
+ keep={
266
+ keep === undefined
267
+ ? undefined
268
+ : entries.findIndex(
269
+ (entry) => entry.kind === "row" && entry.index === keep,
270
+ )
271
+ }
272
+ onRange={(indices) => reach.onLast(indices.at(-1) ?? -1)}
273
+ />
274
+ );
275
+ const stand: ReactNode = nav ? (
276
+ <div
277
+ ref={treeBox}
278
+ role="tree"
279
+ onKeyDown={nav.onKeyDown}
280
+ onFocus={nav.onFocus}
281
+ className={cn(GROUP_TREE, TREE_DIVIDER)}
282
+ >
283
+ {shown}
284
+ </div>
285
+ ) : (
286
+ shown
287
+ );
288
+ return (
289
+ <ListedRoute value={at}>
290
+ {stand}
291
+ {more}
292
+ </ListedRoute>
293
+ );
294
+ };
295
+ if (!visible) {
296
+ return frame((index) =>
297
+ rowOf(
298
+ items[index] as Row,
299
+ keyAt(index),
300
+ paintedAt(index),
301
+ props.movable && {
302
+ movable: props.movable,
303
+ edge: edgeOf(index, items.length),
304
+ },
305
+ ),
306
+ );
307
+ }
308
+ // Each row of the tree reads its depth, fold and tab stop from its own
309
+ // provider; the keys and focus its `treeitem` stops send up move the stop and
310
+ // fold the branches (the WAI-ARIA tree pattern, `treeMove`).
311
+ const stop = treeStop(visible, active);
312
+ // A row carries its key (`data-key`), so a key finds the row's place over
313
+ // `visible` when it is pressed, whichever rows the window mounts.
314
+ const indexOf = (target: HTMLElement) => {
315
+ const key = target.closest<HTMLElement>('[role="treeitem"]')?.dataset.key;
316
+ return visible.findIndex((each) => each.key === key);
317
+ };
318
+ const nav: TreeNav = {
319
+ onKeyDown: (event) => {
320
+ // A key's target is the focused element, always an HTML one here.
321
+ const target = event.target as HTMLElement;
322
+ if (target.getAttribute("role") !== "treeitem") return;
323
+ const move = treeMove(visible, indexOf(target), event.key);
324
+ if (!move) return;
325
+ event.preventDefault();
326
+ if ("focus" in move) {
327
+ // The tab stop moves first: the window keeps its row, mounted by
328
+ // this render if it was not, and the focus follows it there.
329
+ setActive(visible[move.focus]?.key);
330
+ setWanted({ key: visible[move.focus]?.key as string });
331
+ } else setFolded((keys) => folding(keys, move.fold, move.open));
332
+ },
333
+ onFocus: (event) => {
334
+ // A focus event's target is the element that took focus, an HTML one here.
335
+ const target = event.target as HTMLElement;
336
+ if (!target.closest('[role="treeitem"]')) return;
337
+ setActive(visible[indexOf(target)]?.key);
338
+ },
339
+ };
340
+ return frame(
341
+ (index) => {
342
+ const each = visible[index] as (typeof visible)[number];
343
+ return rowOf(each.item, each.key, undefined, {
344
+ fold,
345
+ depth: each.depth,
346
+ under: visible[index + 1]?.depth ?? 0,
347
+ size: each.size,
348
+ place: each.place,
349
+ open: each.branch ? each.open : undefined,
350
+ tabbable: index === stop,
351
+ });
352
+ },
353
+ nav,
354
+ stop,
355
+ );
356
+ }
@@ -0,0 +1,272 @@
1
+ import type { Dialog } from "@base-ui/react/dialog";
2
+ import { cn } from "@slotform/ui-core/cn";
3
+ import type { Acting } from "@slotform/ui-core/react/acts";
4
+ import { askDiscard } from "@slotform/ui-core/react/confirm";
5
+ import { hasBody, submitOf, useFrame } from "@slotform/ui-core/react/read";
6
+ import {
7
+ TouchedContext,
8
+ usePageTurn,
9
+ useTouchState,
10
+ } from "@slotform/ui-core/react/touched";
11
+ import { useWords } from "@slotform/ui-core/react/words";
12
+ import type { Selection } from "@slotform/ui-core/read";
13
+ import {
14
+ SHEET_FOOT,
15
+ SHEET_PARTS,
16
+ SHEET_PARTS_END,
17
+ } from "@slotform/ui-core/variants";
18
+ import type { ReactNode } from "react";
19
+ import { DIVIDER } from "../../atoms/section/card.tsx";
20
+ import type { Act } from "../../lib/act.ts";
21
+ import { useAct } from "../../lib/act.ts";
22
+ import type { Empty } from "../../lib/empty.tsx";
23
+ import { FIELDS, firstTabbable } from "../../lib/focus.ts";
24
+ import { useTouch } from "../../lib/media.ts";
25
+ import { FrameBody, stillOf } from "../../lib/read.tsx";
26
+ import { useScrolls } from "../../lib/scrolls.ts";
27
+ import { Scroller, useScroller } from "../../lib/window.tsx";
28
+ import { ActionBarBase } from "../action-bar/base.tsx";
29
+ import { SheetHead } from "./head.tsx";
30
+ import { SheetLayer } from "./layer.tsx";
31
+
32
+ // The body takes a tab stop only while it scrolls with nothing tabbable inside.
33
+ const BODY =
34
+ "flex flex-col grow min-h-0 overflow-y-auto overscroll-contain focus-visible:-outline-offset-2";
35
+ const PARTS = "flex flex-col";
36
+ // The card's foot carries the raised ground to the box's corners, so it follows
37
+ // the box's radius; on touch the box rounds its top only. The foot is a row
38
+ // on the desktop, a stack on touch, its acts at its end. It stands after the
39
+ // body, so a submit follows the fields it submits at both densities.
40
+ const FOOT =
41
+ "flex items-center rounded-b-sheet touch:flex-col touch:items-stretch touch:rounded-b-none";
42
+ const END_ONLY = "justify-end";
43
+
44
+ /** A submit that runs nothing: the viewer says they have taken in what the sheet shows ("I saved them"), and the press closes the sheet. */
45
+ export interface Acknowledge {
46
+ /** The acknowledgement's words (a short phrase; truncates). */
47
+ label: string;
48
+ onAct?: never;
49
+ sheet?: never;
50
+ }
51
+
52
+ /** What completes a sheet: an act, or an acknowledgement that closes it. */
53
+ export type SheetSubmit = Act | Acknowledge;
54
+
55
+ /** Whether a sheet's submit acknowledges: it neither runs nor opens anything. Outside the package's exports. */
56
+ function acknowledges(submit: SheetSubmit): submit is Acknowledge {
57
+ return submit.onAct === undefined && submit.sheet === undefined;
58
+ }
59
+
60
+ /** The act a sheet's submit runs: its own, or the sheet's close where it acknowledges. Outside the package's exports. */
61
+ export function submitAct(submit: SheetSubmit, close: () => void): Act {
62
+ return acknowledges(submit) ? { label: submit.label, onAct: close } : submit;
63
+ }
64
+
65
+ /** What a task sheet draws: the public `Sheet`, the confirm and the toolbar's touch filters. Outside the package's exports. */
66
+ interface SheetBaseProps {
67
+ open: boolean;
68
+ onClose: () => void;
69
+ /** Hears a trigger tied by `handle` opening the sheet. */
70
+ onOpen?: () => void;
71
+ title: string;
72
+ description?: string;
73
+ /** A task sheet's second page: its way back stands first in the head (`Sheet`'s `back`). */
74
+ back?: () => void;
75
+ submit?: SheetSubmit;
76
+ /** A decision's acts, the foot's `ActionBar` at both densities. */
77
+ acts?: Act[];
78
+ /** A decision is centred on the desktop. */
79
+ centred?: boolean;
80
+ /** What the body holds, read once for the frame (`Sheet`'s `read`): the submit paints, inert, while it waits or stands still, and stands blocked with the failed sentence once it failed or is missing. */
81
+ read?: Selection<unknown>;
82
+ /** The body's sentence when the `read` is empty. */
83
+ empty?: Empty;
84
+ /** The holder of the sheet's acts' attempt, when a form in its body has a bar of its own (the one holder keeps the dismissal held while either runs). Absent, the sheet hosts its own. */
85
+ acting?: Acting<number>;
86
+ /** What the sheet opens focused on: its first field (a confirm's typed name); a decision without it opens on its first act, the way out, and a task's body on its first field, else its first control, else the close act. */
87
+ focus?: "field";
88
+ /** Ties a trigger elsewhere (a `Dialog.Trigger`) to the sheet. */
89
+ handle?: Dialog.Handle<unknown>;
90
+ /** The body's parts, at the card inset. */
91
+ children?: ReactNode;
92
+ /** The body's form (a `SheetForm`), after the parts under a hairline: its rows flush on the sheet, its foot stuck to the body's. */
93
+ fields?: ReactNode;
94
+ /** The sheet opened after this one (the sheet outlet's), drawn inside this one's dialog. */
95
+ nested?: ReactNode;
96
+ }
97
+
98
+ /** A sheet over the scrim: on touch raised from the bottom edge, its submit alone in the foot after the body, a blocked submit's reason under it; on the desktop a card centred across the page, its top a static inset under the viewport's top, capped at the viewport less the page inset, its body scrolling, with the submit after Cancel in the foot (alone where it acknowledges), or a decision centred. Base UI's dialog traps focus and closes on Escape and on a press outside. */
99
+ export function SheetBase({
100
+ open,
101
+ onClose,
102
+ onOpen,
103
+ title,
104
+ description,
105
+ back,
106
+ submit: given,
107
+ acts,
108
+ centred: decision,
109
+ handle,
110
+ acting: holder,
111
+ focus,
112
+ read,
113
+ empty,
114
+ children,
115
+ fields,
116
+ nested,
117
+ }: SheetBaseProps) {
118
+ const touch = useTouch();
119
+ const frame = useFrame(read);
120
+ const words = useWords();
121
+ const own = useAct<number>("line");
122
+ // The submit waits only on the sheet's own read: painted while it waits or
123
+ // stands still, blocked with the sentence once it ended.
124
+ const { waiting, ended } = submitOf(words, frame.shape);
125
+ // A submit that acknowledges closes the sheet, and stands alone: there is
126
+ // nothing to cancel.
127
+ const acknowledging = given !== undefined && acknowledges(given);
128
+ const offered = given && submitAct(given, onClose);
129
+ // The sheet hosts its acts' attempt: its foot bar and its form's bar share one,
130
+ // so one act pends at a time and the dismissal is held while it runs.
131
+ const acting = holder ?? own;
132
+ // A sheet with no foot (no submit, no bar of acts) ends on its last part, which
133
+ // keeps a sections gap under it (`SHEET_PARTS_END`).
134
+ const footless = acts === undefined && offered === undefined;
135
+ // The body is the frame's: its failure and its emptiness stand in its place,
136
+ // at the sheet's inset. Its parts stand at the card inset; the form after
137
+ // them is rows flush on the sheet, the hairline between the two drawn once.
138
+ const body =
139
+ hasBody(frame, children) || fields ? (
140
+ <FrameBody frame={frame} fit="part" empty={empty}>
141
+ {children ? (
142
+ <div
143
+ className={cn(
144
+ SHEET_PARTS,
145
+ PARTS,
146
+ footless && !fields && SHEET_PARTS_END,
147
+ )}
148
+ >
149
+ {children}
150
+ </div>
151
+ ) : null}
152
+ {fields}
153
+ </FrameBody>
154
+ ) : null;
155
+ const [touchedValue, setTouched] = useTouchState();
156
+ const { leave } = touchedValue;
157
+ const scroll = useScroller();
158
+ useScrolls(scroll.node);
159
+ // A sheet whose `form` stands edited asks before it closes: "Discard your
160
+ // edit?", as a page's form does when its page is left, since the form's
161
+ // edit is dropped as the sheet opens again. A bare control in the body holds
162
+ // the app's value, which the sheet keeps, so it asks nothing for it. A
163
+ // decision holds no edit of the sheet's: what it takes is its own pick.
164
+ const asks = !decision && leave.asks();
165
+ const dismiss = () => {
166
+ if (!asks) return onClose();
167
+ void leave
168
+ .attempt(() => askDiscard(words))
169
+ .then((discard) => {
170
+ if (discard) onClose();
171
+ });
172
+ };
173
+ const centred = decision === true && !touch;
174
+ usePageTurn(open, title, description, () => setTouched(false));
175
+ // A decision draws no close act: its acts dismiss it.
176
+ const head = (
177
+ <SheetHead
178
+ title={title}
179
+ description={description}
180
+ back={back}
181
+ frame={frame}
182
+ centred={centred}
183
+ close={!acts}
184
+ holds={acting.holds}
185
+ asks={asks}
186
+ onClose={onClose}
187
+ />
188
+ );
189
+ // The submit stands in the foot's bar after the body, after Cancel on the
190
+ // desktop and alone on touch, where the close act leads the head, and where
191
+ // it acknowledges. The bar stands it blocked once the read ended and draws
192
+ // the sentence on a press only; a blocked reason and a failed run's
193
+ // sentence stand under it.
194
+ const bar =
195
+ acts ??
196
+ (offered
197
+ ? touch || acknowledging
198
+ ? [offered]
199
+ : [{ label: words.cancel, onAct: dismiss }, offered]
200
+ : undefined);
201
+ const actionBar = bar ? (
202
+ <ActionBarBase
203
+ acts={bar}
204
+ acting={acting}
205
+ painted={waiting}
206
+ ended={acts ? undefined : ended}
207
+ />
208
+ ) : null;
209
+ const footer = actionBar ? (
210
+ <div className={cn(SHEET_FOOT, FOOT, END_ONLY)}>{actionBar}</div>
211
+ ) : null;
212
+ const content = centred ? (
213
+ <>
214
+ {head}
215
+ {body}
216
+ {actionBar}
217
+ </>
218
+ ) : (
219
+ <>
220
+ {head}
221
+ {body ? (
222
+ <div
223
+ ref={scroll.ref}
224
+ aria-busy={(frame.holds && frame.shape === "waiting") || undefined}
225
+ data-still={stillOf(frame)}
226
+ className={cn(BODY, DIVIDER)}
227
+ >
228
+ <Scroller value={scroll}>{body}</Scroller>
229
+ </div>
230
+ ) : null}
231
+ {footer}
232
+ </>
233
+ );
234
+ return (
235
+ <SheetLayer
236
+ handle={handle}
237
+ open={open}
238
+ onOpen={onOpen}
239
+ onClose={() => {
240
+ if (!acting.holds) dismiss();
241
+ }}
242
+ place={decision ? "centred" : undefined}
243
+ // A field and an act take no ref (their props are closed), so the
244
+ // popup finds them. A decision opens on its way out: on touch the
245
+ // stack draws the filled act first, so the first tabbable is not it.
246
+ focus={
247
+ focus === "field"
248
+ ? (popup) => popup?.querySelector("input") ?? true
249
+ : acts
250
+ ? (popup) =>
251
+ [...(popup?.querySelectorAll("button") ?? [])].find(
252
+ (act) => act.textContent === acts[0]?.label,
253
+ ) ?? true
254
+ : // The head's close act stands first in the tab order, but
255
+ // the task is the body's: its first field, else its first
256
+ // control.
257
+ () =>
258
+ firstTabbable(scroll.holder.current, FIELDS) ??
259
+ firstTabbable(scroll.holder.current) ??
260
+ true
261
+ }
262
+ // A field inside takes input: a blocked act says its reason.
263
+ // Only the sheet's `form` edits its leave (through its own
264
+ // form's change): a bare control holds the app's value, which
265
+ // the sheet does not drop, so it asks nothing for it.
266
+ onChange={() => setTouched(true)}
267
+ nested={nested}
268
+ >
269
+ <TouchedContext value={touchedValue}>{content}</TouchedContext>
270
+ </SheetLayer>
271
+ );
272
+ }