@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,145 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import {
3
+ GATE_LOCKUP,
4
+ GATE_LOGO,
5
+ text,
6
+ textStrong,
7
+ } from "@slotform/ui-core/variants";
8
+ import { createContext, type ReactNode, use, useState } from "react";
9
+
10
+ /** The app's lockup: its icon, in the light mode and, when the app gave one, the dark, and its name; and, when the app drew one, its logo (symbol and name in one image). */
11
+ export interface AppLockup {
12
+ /** The icon in the light mode, and in the dark when `dark` is absent. */
13
+ src: string;
14
+ /** The icon in the dark mode. */
15
+ dark?: string;
16
+ /** The app's drawn lockup in the light mode, and in the dark when `logoDark` is absent: drawn in place of the icon and the name. */
17
+ logo?: string;
18
+ /** The drawn lockup in the dark mode. */
19
+ logoDark?: string;
20
+ /** The app's name, drawn beside the icon, and the logo's alternative text. */
21
+ name: string;
22
+ }
23
+
24
+ // The app's lockup: docs/architecture/plugins/react-ui.md, "App lockup".
25
+ const LockupContext = createContext<AppLockup | null>(null);
26
+
27
+ export function LockupProvider(props: {
28
+ lockup: AppLockup | null;
29
+ children: ReactNode;
30
+ }) {
31
+ return <LockupContext value={props.lockup}>{props.children}</LockupContext>;
32
+ }
33
+
34
+ export function useLockup(): AppLockup | null {
35
+ return use(LockupContext);
36
+ }
37
+
38
+ const IMAGE = "shrink-0 object-contain";
39
+ // A lockup with a dark form draws one logo per mode, each hidden under the
40
+ // other's: the theme's own `.dark` scope, not the image's media.
41
+ const LIGHT = "[.dark_&]:hidden";
42
+ const DARK = "hidden [.dark_&]:block";
43
+ // A drawn lockup stands at the row's left, its own proportion kept when the
44
+ // row holds it narrower.
45
+ const DRAWN = "shrink-0 object-contain object-left";
46
+ const ROW = "flex items-center";
47
+ const LABEL = "truncate min-w-0";
48
+
49
+ // A logo that fails to load is gone and the name stands alone: the address
50
+ // that failed is kept, so a new `src` is tried again.
51
+ function Logo(props: { src: string; className?: string }) {
52
+ const [failed, setFailed] = useState<string>();
53
+ if (props.src === failed) return null;
54
+ return (
55
+ <img
56
+ src={props.src}
57
+ alt=""
58
+ onError={() => setFailed(props.src)}
59
+ className={cn(GATE_LOCKUP, IMAGE, props.className)}
60
+ />
61
+ );
62
+ }
63
+
64
+ // The drawn lockup carries the app's name as its alt. A pair draws one image
65
+ // per mode, the other form hidden, not removed: either failing to load leaves
66
+ // the icon and the name.
67
+ function Drawn(props: {
68
+ src: string;
69
+ name: string;
70
+ size: string;
71
+ onError: () => void;
72
+ className?: string;
73
+ }) {
74
+ return (
75
+ <img
76
+ src={props.src}
77
+ alt={props.name}
78
+ onError={props.onError}
79
+ className={cn(props.size, DRAWN, props.className)}
80
+ />
81
+ );
82
+ }
83
+
84
+ /** The app's lockup as one row: the drawn lockup when the app has one (at the Gate's `GATE_LOGO` height, its width from its own proportion, held to the row, the app's name as its alt) and otherwise the icon at the avatar's size, then the name at the body role and 500, truncating. A lockup that fails to load leaves the icon and the name. The dark form, when the app has one, is drawn under the theme's own `.dark` scope. The caller reads `useLockup` first and draws no row when the app has no icon. */
85
+ export function Lockup(props: { lockup: AppLockup; className: string }) {
86
+ const { lockup } = props;
87
+ const [failed, setFailed] = useState<string>();
88
+ if (
89
+ lockup.logo !== undefined &&
90
+ failed !== lockup.logo &&
91
+ (lockup.logoDark === undefined || failed !== lockup.logoDark)
92
+ ) {
93
+ const fail = (src: string) => () => setFailed(src);
94
+ return (
95
+ <div className={cn(props.className, ROW)}>
96
+ {lockup.logoDark === undefined ? (
97
+ <Drawn
98
+ src={lockup.logo}
99
+ name={lockup.name}
100
+ size={GATE_LOGO}
101
+ onError={fail(lockup.logo)}
102
+ />
103
+ ) : (
104
+ <>
105
+ <Drawn
106
+ src={lockup.logo}
107
+ name={lockup.name}
108
+ size={GATE_LOGO}
109
+ onError={fail(lockup.logo)}
110
+ className={LIGHT}
111
+ />
112
+ <Drawn
113
+ src={lockup.logoDark}
114
+ name={lockup.name}
115
+ size={GATE_LOGO}
116
+ onError={fail(lockup.logoDark)}
117
+ className={DARK}
118
+ />
119
+ </>
120
+ )}
121
+ </div>
122
+ );
123
+ }
124
+ return (
125
+ <div className={cn(props.className, ROW)}>
126
+ {lockup.dark === undefined ? (
127
+ <Logo src={lockup.src} />
128
+ ) : (
129
+ <>
130
+ <Logo src={lockup.src} className={LIGHT} />
131
+ <Logo src={lockup.dark} className={DARK} />
132
+ </>
133
+ )}
134
+ <span
135
+ className={cn(
136
+ text({ role: "body" }),
137
+ textStrong({ role: "body" }),
138
+ LABEL,
139
+ )}
140
+ >
141
+ {lockup.name}
142
+ </span>
143
+ </div>
144
+ );
145
+ }
@@ -0,0 +1,22 @@
1
+ import type { Size, SpacingRole } from "@slotform/ui-core/tokens";
2
+
3
+ // A spacing role or a size in pixels at the current density (a popup's
4
+ // offset, a glyph's side), read off the root so it follows the density.
5
+ export function spacing(role: SpacingRole | Size): number {
6
+ return Number.parseFloat(
7
+ getComputedStyle(document.documentElement).getPropertyValue(
8
+ `--spacing-${role}`,
9
+ ),
10
+ );
11
+ }
12
+
13
+ // A type role's size in pixels at the current density, read off `node` (the
14
+ // root by default) so the theme's size knob moves it.
15
+ export function textSize(
16
+ role: "meta",
17
+ node: Element = document.documentElement,
18
+ ): number {
19
+ return Number.parseFloat(
20
+ getComputedStyle(node).getPropertyValue(`--text-${role}`),
21
+ );
22
+ }
@@ -0,0 +1,63 @@
1
+ import { DESKTOP_MEDIA } from "@slotform/react-ui/density";
2
+ import { createStore } from "@slotform/ui-core/store";
3
+ import { createContext, use, useSyncExternalStore } from "react";
4
+
5
+ // The density rule, read by one store: docs/architecture/plugins/react-ui.md, "Density".
6
+ let desktop: MediaQueryList | undefined;
7
+
8
+ function desktopQuery(): MediaQueryList {
9
+ desktop ??= matchMedia(DESKTOP_MEDIA);
10
+ return desktop;
11
+ }
12
+
13
+ function touchNow(): boolean {
14
+ const pinned = document.documentElement.dataset.density;
15
+ if (pinned === "room" || pinned === "touch") return true;
16
+ if (pinned === "desktop") return false;
17
+ return !desktopQuery().matches;
18
+ }
19
+
20
+ const density = createStore(undefined, {
21
+ source: (store) => {
22
+ const query = desktopQuery();
23
+ query.addEventListener("change", store.emit);
24
+ const pin = new MutationObserver(store.emit);
25
+ pin.observe(document.documentElement, {
26
+ attributeFilter: ["data-density"],
27
+ });
28
+ return () => {
29
+ query.removeEventListener("change", store.emit);
30
+ pin.disconnect();
31
+ };
32
+ },
33
+ });
34
+
35
+ // A `Panes` that declares its `distance` as `room` hands it down, so what
36
+ // stands inside draws the touch tree whatever the pointer.
37
+ export const DistanceContext = createContext(false);
38
+
39
+ export function useTouch(): boolean {
40
+ const far = use(DistanceContext);
41
+ const touch = useSyncExternalStore(density.subscribe, touchNow, () => false);
42
+ return far || touch;
43
+ }
44
+
45
+ const REDUCED = "(prefers-reduced-motion: reduce)";
46
+
47
+ const reduced = createStore(undefined, {
48
+ source: (store) => {
49
+ const query = matchMedia(REDUCED);
50
+ query.addEventListener("change", store.emit);
51
+ return () => query.removeEventListener("change", store.emit);
52
+ },
53
+ });
54
+
55
+ // Whether the viewer asks for reduced motion now: what is read once, as a
56
+ // script starts (a scramble, a decode), rather than followed.
57
+ export const reducedNow = (): boolean => matchMedia(REDUCED).matches;
58
+
59
+ // Whether the viewer asked for reduced motion: a part that moves by script
60
+ // (a Web Animation, which CSS cannot still) reads it here.
61
+ export function useReducedMotion(): boolean {
62
+ return useSyncExternalStore(reduced.subscribe, reducedNow, () => false);
63
+ }
@@ -0,0 +1,69 @@
1
+ import { useEffect } from "react";
2
+
3
+ // How the viewer last drove the page: docs/architecture/plugins/react-ui.md, "Field box and focus rings".
4
+ let mounted = 0;
5
+ let remove: (() => void) | undefined;
6
+
7
+ const MODIFIERS = new Set(["Shift", "Control", "Alt", "Meta"]);
8
+ const LEAVES_FIELD = new Set(["Tab", "Escape"]);
9
+ // The input types that take no typing.
10
+ const UNTYPED = new Set([
11
+ "checkbox",
12
+ "radio",
13
+ "range",
14
+ "color",
15
+ "file",
16
+ "image",
17
+ "button",
18
+ "submit",
19
+ "reset",
20
+ ]);
21
+
22
+ function typesInto(target: EventTarget | null): boolean {
23
+ if (target instanceof HTMLTextAreaElement) return true;
24
+ if (target instanceof HTMLInputElement) return !UNTYPED.has(target.type);
25
+ return target instanceof HTMLElement && target.isContentEditable;
26
+ }
27
+
28
+ function moves(event: KeyboardEvent): boolean {
29
+ if (event.metaKey || event.ctrlKey || event.altKey) return false;
30
+ if (MODIFIERS.has(event.key)) return false;
31
+ return !typesInto(event.target) || LEAVES_FIELD.has(event.key);
32
+ }
33
+
34
+ function install(): () => void {
35
+ const root = document.documentElement;
36
+ const pointer = () => {
37
+ root.dataset.modality = "pointer";
38
+ };
39
+ const keyboard = (event: KeyboardEvent) => {
40
+ if (moves(event)) root.dataset.modality = "keyboard";
41
+ };
42
+ document.addEventListener("pointerdown", pointer, true);
43
+ document.addEventListener("keydown", keyboard, true);
44
+ return () => {
45
+ document.removeEventListener("pointerdown", pointer, true);
46
+ document.removeEventListener("keydown", keyboard, true);
47
+ };
48
+ }
49
+
50
+ export function keyed(): boolean {
51
+ return document.documentElement.dataset.modality === "keyboard";
52
+ }
53
+
54
+ // A frame or a field calls this once; the listeners follow them.
55
+ export function useModality() {
56
+ useEffect(() => {
57
+ if (mounted++ === 0) remove = install();
58
+ return () => {
59
+ if (--mounted === 0) {
60
+ remove?.();
61
+ remove = undefined;
62
+ }
63
+ };
64
+ }, []);
65
+ }
66
+
67
+ // A field box's ring: docs/architecture/plugins/react-ui.md, "Field box and focus rings".
68
+ export const BOX_FOCUS =
69
+ "not-has-[button:focus-visible]:in-data-[modality=keyboard]:has-focus-visible:outline-2 not-has-[button:focus-visible]:in-data-[modality=keyboard]:has-focus-visible:outline-offset-(--focus-ring-edge-offset) not-has-[button:focus-visible]:in-data-[modality=keyboard]:has-focus-visible:outline-ring";
@@ -0,0 +1,13 @@
1
+ import { momentWords } from "@slotform/ui-core/format";
2
+ import { pageLang } from "./lang.ts";
3
+
4
+ // An ISO moment as a reader reads it, in the document's language
5
+ // (`momentWords`): the time alone today, the day's word and the time a day
6
+ // either side, the date and time, its year only when it is not this year's.
7
+ export function moment(
8
+ iso: string,
9
+ now = new Date(),
10
+ lang = pageLang(),
11
+ ): string {
12
+ return momentWords(iso, now, lang);
13
+ }
@@ -0,0 +1,76 @@
1
+ import { cn } from "@slotform/ui-core/cn";
2
+ import { usePort } from "@slotform/ui-core/react/port";
3
+ import { useWindowMore } from "@slotform/ui-core/react/read";
4
+ import { useWords } from "@slotform/ui-core/react/words";
5
+ import { fetchesMore, type More, type Read } from "@slotform/ui-core/read";
6
+ import type { ReactNode } from "react";
7
+ import { useEffect, useRef } from "react";
8
+ import { ButtonBase } from "../components/button/base.tsx";
9
+ import { StateLine } from "./read.tsx";
10
+
11
+ // The room under a row of the viewer's own pressing, as a state line carries.
12
+ const SHOW = "flex items-center pb-gap";
13
+ // A collection that bleeds the card's inset (a Section's sheet) has its
14
+ // rows pad themselves with it, so its more row does the same; the quiet act
15
+ // hangs its wash out by the control's own padding, its label at the rows' text,
16
+ // and gives way to the row's width, its label truncating.
17
+ const INSET = "px-pad";
18
+ const HANG = "-mx-pad flex min-w-0 max-w-full";
19
+
20
+ interface MoreProps {
21
+ read: Read<unknown>;
22
+ empty: boolean;
23
+ paint: () => ReactNode;
24
+ /** The collection's rows pad themselves with the card's inset, so this row does too. */
25
+ inset?: boolean;
26
+ /** The rows mounted reach the last one loaded (`useReach`): a long collection loads its next page without a press. */
27
+ scrolled?: boolean;
28
+ }
29
+
30
+ /** A collection's last row over a read that pages (a pages read, or a window read the collection does not size to its total): "Show more" while a next page remains (pressed by the viewer; a long windowed list loads it once its mounted rows reach the end, docs/architecture/ui-core/reads.md, "Paging"), the painted rows `paint` draws while it loads, "Couldn't load more" with Retry when it failed. An empty first page that has more is not empty: the next page is fetched at once. Only this row renders on the next page's fetch; the frame is not busy then. */
31
+ export function MoreRow(props: MoreProps) {
32
+ return props.read.kind === "window" ? (
33
+ <WindowMoreRow {...props} />
34
+ ) : (
35
+ <PagesMoreRow {...props} />
36
+ );
37
+ }
38
+
39
+ function PagesMoreRow(props: MoreProps) {
40
+ return <MoreLine {...props} more={usePort().useNextPage(props.read)} />;
41
+ }
42
+
43
+ function WindowMoreRow(props: MoreProps) {
44
+ return <MoreLine {...props} more={useWindowMore(props.read)} />;
45
+ }
46
+
47
+ function MoreLine(props: MoreProps & { more: More }) {
48
+ const { more, empty, paint, inset, scrolled } = props;
49
+ const words = useWords();
50
+ const { next, fetching, failed } = more;
51
+ const fetch = useRef(more.fetch);
52
+ fetch.current = more.fetch;
53
+ const fetches = fetchesMore(more, empty || scrolled === true);
54
+ useEffect(() => {
55
+ if (fetches) fetch.current();
56
+ }, [fetches]);
57
+ if (fetching || fetches) return paint();
58
+ if (failed) {
59
+ return (
60
+ <div className={inset ? INSET : undefined}>
61
+ <StateLine
62
+ sentence={words.moreFailed}
63
+ act={{ label: words.retry, onAct: more.fetch }}
64
+ />
65
+ </div>
66
+ );
67
+ }
68
+ if (!next) return null;
69
+ return (
70
+ <div className={cn(SHOW, inset && INSET)}>
71
+ <div className={HANG}>
72
+ <ButtonBase act="quiet" label={words.showMore} onAct={more.fetch} />
73
+ </div>
74
+ </div>
75
+ );
76
+ }
@@ -0,0 +1,210 @@
1
+ import type { Leave } from "@slotform/ui-core/leave";
2
+ import { createStore } from "@slotform/ui-core/store";
3
+ import {
4
+ createContext,
5
+ type MouseEvent,
6
+ use,
7
+ useSyncExternalStore,
8
+ } from "react";
9
+ import { NOOP } from "./noop.ts";
10
+
11
+ // The slice of the router that routing in place needs: docs/architecture/plugins/react-ui.md, "Router bindings".
12
+ interface InPlaceRouter {
13
+ navigate(options: { href: string }): unknown;
14
+ preloadRoute(options: { href: string }): unknown;
15
+ subscribe(event: "onResolved", listener: () => void): () => void;
16
+ state: {
17
+ location: { href: string };
18
+ resolvedLocation?: { href: string };
19
+ // The route chain of the page drawn, root first.
20
+ matches?: readonly { routeId: string; pathname: string }[];
21
+ };
22
+ // The route tree by id: a route is a page when it has no children or one
23
+ // of them is its index (`path: "/"`).
24
+ routesById?: Record<
25
+ string,
26
+ { children?: readonly { path?: string }[] | Record<string, unknown> }
27
+ >;
28
+ // A blocker resolves `true` to hold a navigation, as the router's own does.
29
+ history: {
30
+ block(blocker: {
31
+ blockerFn: () => Promise<boolean>;
32
+ enableBeforeUnload: () => boolean;
33
+ }): () => void;
34
+ };
35
+ }
36
+
37
+ // One router and one location listener: docs/architecture/plugins/react-ui.md, "Navigation".
38
+ let bound: InPlaceRouter | undefined;
39
+ // The bound router's `onResolved` subscription, ended when another binds.
40
+ let unbind: (() => void) | undefined;
41
+ // Bound, the router resolves a history move and `onResolved` tells the
42
+ // readers, so the window's events are heard only while unbound.
43
+ const changes = createStore(undefined, {
44
+ source: (store) => {
45
+ const hearWindow = () => {
46
+ if (!bound) store.emit();
47
+ };
48
+ addEventListener("popstate", hearWindow);
49
+ addEventListener("hashchange", hearWindow);
50
+ return () => {
51
+ removeEventListener("popstate", hearWindow);
52
+ removeEventListener("hashchange", hearWindow);
53
+ };
54
+ },
55
+ });
56
+
57
+ // How long a pointer or focus rests on a route's anchor before its route is
58
+ // preloaded: TanStack's own intent delay, so a pass across a list loads
59
+ // nothing and a rest on one row loads that row.
60
+ const INTENT_DELAY = 50;
61
+ let intent: ReturnType<typeof setTimeout> | undefined;
62
+ let listening = false;
63
+
64
+ // One delegated listener for every anchor, and preload: docs/architecture/plugins/react-ui.md, "Navigation".
65
+ function wantsRoute(event: Event): void {
66
+ clearTimeout(intent);
67
+ intent = undefined;
68
+ const link =
69
+ event.target instanceof Element ? event.target.closest("a[href]") : null;
70
+ const href = link?.getAttribute("href");
71
+ if (!href || !isRoute(href) || href === drawn()) return;
72
+ intent = setTimeout(() => {
73
+ intent = undefined;
74
+ bound?.preloadRoute({ href });
75
+ }, INTENT_DELAY);
76
+ }
77
+
78
+ export function bindRouter(router: InPlaceRouter): void {
79
+ bound = router;
80
+ if (!listening) {
81
+ listening = true;
82
+ document.addEventListener("pointerover", wantsRoute);
83
+ document.addEventListener("focusin", wantsRoute);
84
+ }
85
+ unbind?.();
86
+ unbind = router.subscribe("onResolved", changes.emit);
87
+ changes.emit();
88
+ }
89
+
90
+ const unheard = () => NOOP;
91
+
92
+ // Set by a Section around its rows: the route it read once, which each row
93
+ // reads instead of subscribing on its own.
94
+ export const ListedRoute = createContext<string | undefined>(undefined);
95
+
96
+ // The route drawn: the router's resolved location, so a place's selection
97
+ // flips with the page; unbound, the location's path, query and hash.
98
+ function drawn(): string {
99
+ if (bound) return (bound.state.resolvedLocation ?? bound.state.location).href;
100
+ return location.pathname + location.search + location.hash;
101
+ }
102
+
103
+ // The current route: the location's path, its query and its hash.
104
+ export function useRoute(): string {
105
+ const listed = use(ListedRoute);
106
+ const own = useSyncExternalStore(
107
+ listed === undefined ? changes.subscribe : unheard,
108
+ drawn,
109
+ () => "/",
110
+ );
111
+ return listed ?? own;
112
+ }
113
+
114
+ // The route the page above this one is at: the nearest ancestor in the route
115
+ // chain whose pathname differs from the page's own (a pathless layout, or a
116
+ // layout the page shares its path with, is the page itself) and that is a page
117
+ // itself. Unbound, or on a router that hands over no chain, or at the top of
118
+ // the tree, it cannot be known.
119
+ function parentOf(): string | undefined {
120
+ const matches = bound?.state.matches;
121
+ const routes = bound?.routesById;
122
+ if (!matches || !routes) return undefined;
123
+ const page = matches.at(-1)?.pathname;
124
+ for (let at = matches.length - 2; at >= 0; at--) {
125
+ const match = matches[at];
126
+ if (!match || match.pathname === page) continue;
127
+ const children = routes[match.routeId]?.children;
128
+ const list: readonly unknown[] = Array.isArray(children)
129
+ ? children
130
+ : Object.values(children ?? {});
131
+ if (
132
+ list.length === 0 ||
133
+ list.some(
134
+ (child) =>
135
+ typeof child === "object" &&
136
+ child !== null &&
137
+ "path" in child &&
138
+ child.path === "/",
139
+ )
140
+ )
141
+ return match.pathname;
142
+ }
143
+ return undefined;
144
+ }
145
+
146
+ // The route the page above this one is at, read with the route drawn so a
147
+ // move re-reads it (see `parentOf`); `undefined` where the router cannot say.
148
+ export function useParentRoute(): string | undefined {
149
+ useRoute();
150
+ return parentOf();
151
+ }
152
+
153
+ // The route a record's Pane goes back to: the page above it with the address's
154
+ // query, which holds the place's tab and filters, so the place's Pane stands as
155
+ // it stood when the record opened.
156
+ export function useReturnRoute(): string | undefined {
157
+ const at = useRoute();
158
+ const parent = parentOf();
159
+ return parent === undefined
160
+ ? undefined
161
+ : parent + (at.match(/\?[^#]*/)?.[0] ?? "");
162
+ }
163
+
164
+ // An href from the app's root is a route; a scheme (`https:`, `mailto:`), a
165
+ // protocol-relative `//` or a bare `#hash` is somewhere else.
166
+ export function isRoute(href: string): boolean {
167
+ return /^\/(?!\/)/.test(href);
168
+ }
169
+
170
+ // A part of a trail is a page (a link) when its route is one, else a folder
171
+ // (text): a `#spot` or no route at all.
172
+ export function isPage(route: string | undefined): route is string {
173
+ return route !== undefined && isRoute(route);
174
+ }
175
+
176
+ // Bound, a route opens in the router with no document load, and the promise
177
+ // settles once the router has resolved it; elsewhere the document loads.
178
+ export async function navigate(route: string): Promise<void> {
179
+ if (bound && isRoute(route)) await bound.navigate({ href: route });
180
+ else location.assign(route);
181
+ }
182
+
183
+ // A form that stands edited holds its leave: docs/architecture/ui-core/acts.md, "Leave".
184
+ export function blockLeave(
185
+ leave: Leave,
186
+ ask: () => Promise<boolean>,
187
+ ): () => void {
188
+ if (bound) {
189
+ return bound.history.block({
190
+ blockerFn: async () => !(await leave.attempt(ask)),
191
+ enableBeforeUnload: leave.asks,
192
+ });
193
+ }
194
+ const unload = (event: BeforeUnloadEvent) => {
195
+ if (leave.asks()) event.preventDefault();
196
+ };
197
+ addEventListener("beforeunload", unload);
198
+ return () => removeEventListener("beforeunload", unload);
199
+ }
200
+
201
+ // An anchor's click: docs/architecture/plugins/react-ui.md, "Navigation".
202
+ export function follow(event: MouseEvent<HTMLAnchorElement>): void {
203
+ if (!bound || event.defaultPrevented) return;
204
+ if (event.button !== 0) return;
205
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
206
+ const href = event.currentTarget.getAttribute("href");
207
+ if (href === null || !isRoute(href)) return;
208
+ event.preventDefault();
209
+ navigate(href);
210
+ }
@@ -0,0 +1,3 @@
1
+ // A function that does nothing: a handler or a cleanup with nothing to do, a
2
+ // rejection the caller has nothing to do about.
3
+ export const NOOP = () => {};
@@ -0,0 +1,40 @@
1
+ import { ShellHome } from "@slotform/ui-core/react/frame";
2
+ import { useWords } from "@slotform/ui-core/react/words";
3
+ import type { ReactNode } from "react";
4
+ import { use } from "react";
5
+ import { Button } from "../components/button/index.tsx";
6
+ import { Gate } from "../components/gate/index.tsx";
7
+ import { MissingBase } from "../components/missing/base.tsx";
8
+ import { Pane } from "../components/pane/index.tsx";
9
+ import { InPane } from "./container.ts";
10
+
11
+ // The slice of the router the not-found page needs: docs/architecture/plugins/react-ui.md, "Router bindings".
12
+ interface NotFoundRouter {
13
+ update(options: { defaultNotFoundComponent: () => ReactNode }): void;
14
+ }
15
+
16
+ // The page for an address nothing serves: docs/architecture/plugins/react-ui.md, "Router bindings".
17
+ function NotFound() {
18
+ const words = useWords();
19
+ const home = use(ShellHome);
20
+ const inPane = use(InPane);
21
+ if (home === undefined)
22
+ return (
23
+ <Gate title={words.notFound} description={[words.nowhere]}>
24
+ <Button href="/" label={words.back} />
25
+ </Gate>
26
+ );
27
+ const missing = <MissingBase sentence={words.nowhere} act={home} />;
28
+ if (inPane) return missing;
29
+ return (
30
+ <Pane title={words.notFound} record={{}}>
31
+ {missing}
32
+ </Pane>
33
+ );
34
+ }
35
+
36
+ // The router's default not-found page, set once the app's own routes are
37
+ // known: a route's own `notFoundComponent` (an app's catch-all) wins over it.
38
+ export function bindNotFound(router: NotFoundRouter): void {
39
+ router.update({ defaultNotFoundComponent: NotFound });
40
+ }