@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,246 @@
1
+ import { Input as Control } from "@base-ui/react/input";
2
+ import { cn } from "@slotform/ui-core/cn";
3
+ import type { Draft } from "@slotform/ui-core/react/acts";
4
+ import { FieldDisabled } from "@slotform/ui-core/react/field";
5
+ import { useWords } from "@slotform/ui-core/react/words";
6
+ import {
7
+ FIELD_GLYPH,
8
+ FIELD_UNIT,
9
+ FIGURES,
10
+ type FieldKind,
11
+ field,
12
+ fieldValue,
13
+ } from "@slotform/ui-core/variants";
14
+ import type { KeyboardEvent, MouseEvent } from "react";
15
+ import { IconButtonBase } from "../../atoms/button/glyph.tsx";
16
+ import { Icon } from "../../atoms/icon/index.tsx";
17
+ import { useAct, useHeldDraft } from "../../lib/act.ts";
18
+ import { caretAtEnd } from "../../lib/caret.ts";
19
+ import { useCommit } from "../../lib/commit.ts";
20
+ import type { CellFit, InlineFit } from "../../lib/field.ts";
21
+ import { BOX_FOCUS, useModality } from "../../lib/modality.ts";
22
+ import { PAINT_CONTROL_HIDDEN } from "../../lib/paint.tsx";
23
+ import type { InputKind, TextInputProps } from "./index.tsx";
24
+
25
+ // The box draws the field's states: docs/architecture/plugins/react-ui.md, "Field box and focus rings".
26
+ export const BOX = "flex items-center";
27
+ // A focused field takes the accent edge, under the pointer and the keyboard
28
+ // alike (the keyboard adds the ring); the hover edge yields to it.
29
+ const FOCUS_EDGE = "has-focus-visible:border-accent";
30
+ export const BOX_HOVER = `not-has-focus-visible:hover:border-edge-hover ${FOCUS_EDGE}`;
31
+ // The in-field act answers its own pointer, so the box keeps its edge under it.
32
+ export const BOX_HOVER_VALUE = `not-has-[button:hover]:not-has-focus-visible:hover:border-edge-hover ${FOCUS_EDGE}`;
33
+ export const BOX_DISABLED = "bg-fill-disabled text-ink-disabled";
34
+ // The value fills the box's height, border included (as an act in the box
35
+ // does), so the element a pointer lands on is the box's size, not a line's.
36
+ const VALUE =
37
+ "min-w-0 grow self-stretch -my-hairline truncate outline-none placeholder:text-ink-meta disabled:text-ink-disabled";
38
+ // A `source` value is a command or a path that outruns a phone, so at the
39
+ // field's own fit it is a one-line text area that wraps at any character and
40
+ // grows the box with its lines, where a line of text would cut its end and
41
+ // leave the rest unread (touch has no tooltip). It fills the box, border
42
+ // included, as the input does, so the element a pointer lands on is the box's
43
+ // size (the target floor on touch), and its padding stands one line in the
44
+ // middle of a control's height, where the input's line stands.
45
+ const WRAPPED =
46
+ "block min-w-0 grow self-stretch -my-hairline resize-none field-sizing-content py-[calc((var(--spacing-control)-1lh)/2)] wrap-anywhere outline-none placeholder:text-ink-meta disabled:text-ink-disabled";
47
+ // A search box grows to fill the region a toolbar gives it.
48
+ const SEARCH_BOX = "grow";
49
+ // A number in a table cell keeps the cell's end-aligned figures.
50
+ const CELL_NUMBER = "text-end";
51
+ const UNIT = "shrink-0 truncate";
52
+ const UNIT_DISABLED = "text-ink-disabled";
53
+
54
+ // The whole box focuses the value, which is one line tall inside it; a press
55
+ // on the value or on the act inside keeps its own.
56
+ function focusValue(event: MouseEvent<HTMLDivElement>) {
57
+ if (
58
+ event.target instanceof Element &&
59
+ event.target.closest("input, textarea, button")
60
+ )
61
+ return;
62
+ event.preventDefault();
63
+ event.currentTarget.querySelector<HTMLElement>("input, textarea")?.focus();
64
+ }
65
+
66
+ const TYPES: Record<InputKind, string> = {
67
+ text: "text",
68
+ search: "text",
69
+ secret: "password",
70
+ source: "text",
71
+ number: "text",
72
+ email: "email",
73
+ date: "date",
74
+ };
75
+
76
+ const SURFACE: Record<InputKind, FieldKind> = {
77
+ text: "text",
78
+ search: "search",
79
+ secret: "text",
80
+ source: "code",
81
+ number: "text",
82
+ email: "text",
83
+ date: "text",
84
+ };
85
+
86
+ /** The field box every Input renders, on the `draft` its host holds: a field hosts the draft of its input and draws a rejection in its own line. `cell` is the `Table` cell it edits in, `inline` the `Row` entry or `Rules` term it stands in: each sets its bar fit and name. `named` is the name of an Input a field owns (a `FormField`'s child), which keeps the field fit. `painted` is the box while its data is not here: filled at its loaded size, the value not drawn, inert. Outside the package's exports. */
87
+ export function InputBase({
88
+ draft,
89
+ kind,
90
+ onChange,
91
+ onCommit,
92
+ placeholder,
93
+ unit,
94
+ act,
95
+ autoFocus,
96
+ cell,
97
+ inline,
98
+ named,
99
+ painted: given,
100
+ }: Omit<TextInputProps, "value"> & {
101
+ draft: Draft<string>;
102
+ cell?: CellFit;
103
+ inline?: InlineFit;
104
+ named?: string;
105
+ painted?: boolean;
106
+ }) {
107
+ useModality();
108
+ // The act runs through one holder, whether Enter or its icon presses it.
109
+ const acting = useAct("toast");
110
+ // A read that ended leaves the box empty and blocked: a keystroke says why
111
+ // and changes nothing.
112
+ const ended = draft.ended;
113
+ const painted = given || (draft.painted && !ended);
114
+ const shown = painted || ended ? "" : (draft.value ?? "");
115
+ const which = kind ?? "text";
116
+ // The wrapped form is a text area standing for one line: Enter never starts
117
+ // a second, and a pasted newline is a space, as an input takes it.
118
+ const wraps = which === "source" && !cell && !inline;
119
+ const edit = (next: string) => {
120
+ const one = wraps ? next.replace(/\r?\n/g, " ") : next;
121
+ draft.change(one);
122
+ if (!ended) onChange(one);
123
+ };
124
+ // A commit that returns its save's promise holds the value while it pends.
125
+ const save =
126
+ onCommit && ((next: string) => draft.change(next, () => onCommit(next)));
127
+ const words = useWords();
128
+ const surface = SURFACE[which];
129
+ const search = which === "search";
130
+ const commit = useCommit(shown, edit, save, true);
131
+ // Enter presses the act in place of a submit; on a secret it commits, since
132
+ // its act (remove) is never what typing ends with.
133
+ const keys = (event: KeyboardEvent<HTMLElement>) => {
134
+ commit.onKeyDown(event);
135
+ if (event.key !== "Enter") return;
136
+ // A text area has no implicit submit, which a line of text has; under
137
+ // `wraps` the key's element is that text area.
138
+ if (wraps && !act) {
139
+ event.preventDefault();
140
+ (event.currentTarget as HTMLTextAreaElement).form?.requestSubmit();
141
+ return;
142
+ }
143
+ if (!act || which === "secret") return;
144
+ event.preventDefault();
145
+ if (act.blocked === undefined && !acting.holds) acting.attempt(true, act);
146
+ };
147
+ return (
148
+ <Control
149
+ value={shown}
150
+ onValueChange={edit}
151
+ onFocus={commit.onFocus}
152
+ onBlur={commit.onBlur}
153
+ onKeyDown={keys}
154
+ type={wraps ? undefined : TYPES[which]}
155
+ inputMode={which === "number" ? "decimal" : undefined}
156
+ autoComplete={which === "email" ? "email" : undefined}
157
+ autoCapitalize={which === "text" ? undefined : "off"}
158
+ spellCheck={which === "text" ? undefined : false}
159
+ placeholder={placeholder ?? (search ? words.search : undefined)}
160
+ aria-label={
161
+ cell?.label ??
162
+ inline?.label ??
163
+ named ??
164
+ (search ? words.search : undefined)
165
+ }
166
+ tabIndex={cell ? -1 : undefined}
167
+ autoFocus={autoFocus ?? (cell?.starts || inline?.focus)}
168
+ ref={autoFocus ? caretAtEnd : undefined}
169
+ // Base UI's Field wires the control: docs/architecture/plugins/react-ui.md, "Field box and focus rings".
170
+ render={(control, state) => (
171
+ // biome-ignore lint/a11y/noStaticElementInteractions: the press forwards focus to the input inside, which the keyboard reaches on its own
172
+ <div
173
+ onMouseDown={focusValue}
174
+ aria-disabled={state.disabled || ended || undefined}
175
+ inert={painted || undefined}
176
+ className={cn(
177
+ field({
178
+ trailing: act ? "act" : "none",
179
+ state: state.valid === false ? "error" : "rest",
180
+ }),
181
+ FIELD_GLYPH,
182
+ BOX,
183
+ BOX_FOCUS,
184
+ search && SEARCH_BOX,
185
+ state.disabled
186
+ ? BOX_DISABLED
187
+ : state.valid !== false && BOX_HOVER_VALUE,
188
+ painted && PAINT_CONTROL_HIDDEN,
189
+ )}
190
+ >
191
+ {search ? <Icon name="Search" fit="control" /> : null}
192
+ {wraps ? (
193
+ <textarea
194
+ {...control}
195
+ rows={1}
196
+ className={cn(fieldValue({ kind: surface }), WRAPPED)}
197
+ />
198
+ ) : (
199
+ <input
200
+ {...control}
201
+ className={cn(
202
+ fieldValue({ kind: surface }),
203
+ cell && which === "number" && FIGURES,
204
+ VALUE,
205
+ cell && which === "number" && CELL_NUMBER,
206
+ )}
207
+ />
208
+ )}
209
+ {which === "number" && unit ? (
210
+ <span
211
+ className={cn(FIELD_UNIT, UNIT, state.disabled && UNIT_DISABLED)}
212
+ >
213
+ {unit}
214
+ </span>
215
+ ) : null}
216
+ {act ? (
217
+ <FieldDisabled value={state.disabled}>
218
+ <IconButtonBase
219
+ icon={act.icon}
220
+ fit="field"
221
+ label={act.label}
222
+ destructive={act.destructive}
223
+ disabled={act.blocked !== undefined}
224
+ pending={acting.pending !== undefined}
225
+ onClick={() => acting.attempt(true, act)}
226
+ />
227
+ </FieldDisabled>
228
+ ) : null}
229
+ </div>
230
+ )}
231
+ />
232
+ );
233
+ }
234
+
235
+ /** An Input that holds its own draft, as a bare one, a cell and a rule's term do; a rejection toasts. Outside the package's exports. */
236
+ export function HeldInput({
237
+ value,
238
+ ...rest
239
+ }: TextInputProps & {
240
+ cell?: CellFit;
241
+ inline?: InlineFit;
242
+ named?: string;
243
+ painted?: boolean;
244
+ }) {
245
+ return <InputBase draft={useHeldDraft(value)} {...rest} />;
246
+ }
@@ -0,0 +1,108 @@
1
+ import { Input as Control } from "@base-ui/react/input";
2
+ import { drew, isThenable } from "@slotform/ui-core/acts";
3
+ import { cn } from "@slotform/ui-core/cn";
4
+ import { useAttempt } from "@slotform/ui-core/react/acts";
5
+ import { usePort } from "@slotform/ui-core/react/port";
6
+ import { useWords } from "@slotform/ui-core/react/words";
7
+ import {
8
+ FLOW_IN,
9
+ OTP,
10
+ OTP_DIGIT,
11
+ otpBox,
12
+ text,
13
+ } from "@slotform/ui-core/variants";
14
+ import { Spinner } from "../../atoms/spinner/index.tsx";
15
+ import { useModality } from "../../lib/modality.ts";
16
+ import type { CodeInputProps } from "./index.tsx";
17
+ import { useRefusal } from "./refusal.tsx";
18
+
19
+ // The checking line stands one `pair` under the row.
20
+ const STACK = "flex flex-col gap-gap";
21
+ const ROW = "relative flex items-center";
22
+ // One real input lies over the boxes, invisible: a click anywhere focuses it,
23
+ // and the browser's code suggestion or a paste fills it at once. It goes
24
+ // first so the box it types into can ring on its focus.
25
+ const INPUT = "peer absolute inset-0 opacity-0";
26
+ const BOX = "flex items-center justify-center";
27
+ const BOX_FOCUS =
28
+ "peer-focus-visible:in-data-[modality=keyboard]:outline-2 peer-focus-visible:in-data-[modality=keyboard]:outline-offset-(--focus-ring-edge-offset) peer-focus-visible:in-data-[modality=keyboard]:outline-ring";
29
+ const DIGIT = "text-center";
30
+ const LINE = "flex items-center gap-gap";
31
+
32
+ /** The row of digit boxes over one input a `code` Input draws; the box the next digit lands in rings on focus. Outside the package's exports. */
33
+ export function CodeField({
34
+ length,
35
+ value,
36
+ onChange,
37
+ onComplete,
38
+ }: CodeInputProps) {
39
+ useModality();
40
+ const words = useWords();
41
+ const acting = useAttempt();
42
+ const port = usePort();
43
+ const { refused, refuse, wired, line } = useRefusal();
44
+ const checking = acting.pending !== undefined;
45
+ const active = Math.min(value.length, length - 1);
46
+ // The attempt a Retry runs again: its promise, so the toast's act holds it.
47
+ const attempt = (code: string): unknown => {
48
+ const result = onComplete?.(code);
49
+ if (!isThenable(result)) return result;
50
+ return Promise.resolve(result).catch((error: unknown) => {
51
+ drew(error);
52
+ refuse(port.refusal(error) ?? words.actFailed);
53
+ });
54
+ };
55
+ const complete = (code: string) => acting.attempt(true, () => attempt(code));
56
+ return (
57
+ <Control
58
+ value={value}
59
+ onValueChange={(raw) => {
60
+ const next = raw.replace(/\D/g, "").slice(0, length);
61
+ if (next === value) return;
62
+ refuse(undefined);
63
+ onChange(next);
64
+ if (next.length === length) complete(next);
65
+ }}
66
+ inputMode="numeric"
67
+ autoComplete="one-time-code"
68
+ maxLength={length}
69
+ // Read-only, not disabled, while the check runs: a disabled input
70
+ // drops its focus, so the digit after a refusal would go nowhere.
71
+ readOnly={checking}
72
+ // Base UI's Field wires the input; the render function hands over its
73
+ // props and state so the boxes draw the field's error.
74
+ render={(control, state) => (
75
+ <div className={STACK}>
76
+ <div className={cn(OTP, ROW)}>
77
+ <input {...control} {...wired(control)} className={INPUT} />
78
+ {Array.from({ length }, (_, index) => (
79
+ <span
80
+ key={index}
81
+ aria-hidden
82
+ className={cn(
83
+ otpBox({
84
+ state: state.valid === false || refused ? "error" : "rest",
85
+ }),
86
+ BOX,
87
+ index === active && !checking && BOX_FOCUS,
88
+ )}
89
+ >
90
+ <span className={cn(OTP_DIGIT, DIGIT)}>{value[index]}</span>
91
+ </span>
92
+ ))}
93
+ </div>
94
+ {checking ? (
95
+ <p
96
+ role="status"
97
+ className={cn(text({ role: "meta" }), LINE, FLOW_IN)}
98
+ >
99
+ <Spinner />
100
+ <span>{words.checking}</span>
101
+ </p>
102
+ ) : null}
103
+ {line}
104
+ </div>
105
+ )}
106
+ />
107
+ );
108
+ }
@@ -0,0 +1,37 @@
1
+ import type { ChangeEvent, DragEvent } from "react";
2
+
3
+ // The files a file input chose, the input cleared: the same file chosen again
4
+ // is a change the input would not report.
5
+ export function chosenFiles(event: ChangeEvent<HTMLInputElement>): File[] {
6
+ const files = Array.from(event.currentTarget.files ?? []);
7
+ event.currentTarget.value = "";
8
+ return files;
9
+ }
10
+
11
+ // A box that takes dropped files: a drag that carries files lights it
12
+ // (`setOver`) and is the box's to drop, so a file dropped on it never opens
13
+ // in the tab; any other drag (a text selection, a link) passes it by.
14
+ // Without `take` (the box is disabled) the drop is still the box's and takes
15
+ // nothing.
16
+ export function fileDrop(
17
+ setOver: (over: boolean) => void,
18
+ take: ((files: FileList) => void) | undefined,
19
+ ) {
20
+ return {
21
+ onDragOver: (event: DragEvent<HTMLElement>) => {
22
+ if (!event.dataTransfer.types.includes("Files")) return;
23
+ event.preventDefault();
24
+ if (take) setOver(true);
25
+ },
26
+ onDragLeave: (event: DragEvent<HTMLElement>) => {
27
+ const next = event.relatedTarget;
28
+ if (next instanceof Node && event.currentTarget.contains(next)) return;
29
+ setOver(false);
30
+ },
31
+ onDrop: (event: DragEvent<HTMLElement>) => {
32
+ event.preventDefault();
33
+ setOver(false);
34
+ take?.(event.dataTransfer.files);
35
+ },
36
+ };
37
+ }
@@ -0,0 +1,154 @@
1
+ import { Input as Control } from "@base-ui/react/input";
2
+ import { cn } from "@slotform/ui-core/cn";
3
+ import { accepts, fileSize, kindsOf } from "@slotform/ui-core/file";
4
+ import { pathCut } from "@slotform/ui-core/list-state";
5
+ import { FieldDisabled } from "@slotform/ui-core/react/field";
6
+ import { useWords } from "@slotform/ui-core/react/words";
7
+ import { filled } from "@slotform/ui-core/tokens";
8
+ import {
9
+ FIELD_GLYPH,
10
+ FIELD_PLACEHOLDER,
11
+ FIGURES,
12
+ field,
13
+ fieldValue,
14
+ text,
15
+ YIELDS_FIRST,
16
+ } from "@slotform/ui-core/variants";
17
+ import { type ChangeEvent, useState } from "react";
18
+ import { Icon } from "../../atoms/icon/index.tsx";
19
+ import { BOX_FOCUS, useModality } from "../../lib/modality.ts";
20
+ import { pickedFrom } from "../../lib/picked.ts";
21
+ import { Button } from "../button/index.tsx";
22
+ import { BOX, BOX_DISABLED, BOX_HOVER_VALUE } from "./base.tsx";
23
+ import { chosenFiles, fileDrop } from "./drop.ts";
24
+ import type { FilePickProps } from "./index.tsx";
25
+ import { useRefusal } from "./refusal.tsx";
26
+
27
+ // The native file input lies over the whole box, unseen: a press anywhere
28
+ // opens the system dialog and the keyboard reaches it, so the box rings on its
29
+ // focus as an `Input`'s does. The remove act stands above it.
30
+ const PICK = "absolute inset-0 opacity-0";
31
+ const ANCHOR = "relative";
32
+ const STACK = "flex flex-col gap-gap min-w-0";
33
+ const ABOVE = "relative flex";
34
+ const VALUE = "min-w-0 grow truncate";
35
+ // A chosen name cuts in its middle as a file row's does (`pathCut`): its stem
36
+ // truncates and its end, the extension and the characters before it, stands
37
+ // whole while the box holds it: the stem gives first (a shrink weight past the
38
+ // end's), and the end gives only once the stem is gone, cut with an ellipsis,
39
+ // so the box never outgrows its column.
40
+ const NAME = "flex min-w-0 grow";
41
+ const STEM = `min-w-0 truncate ${YIELDS_FIRST}`;
42
+ const END = "min-w-0 truncate";
43
+ const SIZE = "shrink-0 max-w-measure-short truncate";
44
+ const DISABLED = "text-ink-disabled";
45
+ // A file over the box draws the focus ring, the box being where it lands.
46
+ const OVER = "outline-2 outline-offset-(--focus-ring-edge-offset) outline-ring";
47
+
48
+ function Name(props: { name: string }) {
49
+ const { stem, tail } = pathCut(props.name);
50
+ return (
51
+ <>
52
+ <span className={STEM}>{stem}</span>
53
+ <span className={END}>{tail}</span>
54
+ </>
55
+ );
56
+ }
57
+
58
+ /** The field box a `file` Input draws. Outside the package's exports. */
59
+ export function FileField({ value, onChange, accept }: FilePickProps) {
60
+ useModality();
61
+ const words = useWords();
62
+ const { refused, refuse, wired, line } = useRefusal();
63
+ const [over, setOver] = useState(false);
64
+ const take = (file: File | undefined) => {
65
+ if (!file) return;
66
+ if (!accepts(file, accept)) {
67
+ refuse(
68
+ filled(words.wrongType, {
69
+ name: file.name,
70
+ types: kindsOf(accept, words).join(", "),
71
+ }),
72
+ );
73
+ return;
74
+ }
75
+ refuse(undefined);
76
+ onChange(pickedFrom(file));
77
+ };
78
+ const pick = (event: ChangeEvent<HTMLInputElement>) =>
79
+ take(chosenFiles(event)[0]);
80
+ const drops = (disabled: boolean) =>
81
+ fileDrop(setOver, disabled ? undefined : (files) => take(files[0]));
82
+ return (
83
+ <div className={STACK}>
84
+ <Control
85
+ type="file"
86
+ accept={accept.join(",")}
87
+ onChange={pick}
88
+ // Base UI's Field wires the control: docs/architecture/plugins/react-ui.md, "Field box and focus rings".
89
+ render={(control, state) => (
90
+ <div
91
+ aria-disabled={state.disabled || undefined}
92
+ {...drops(state.disabled)}
93
+ className={cn(
94
+ field({
95
+ trailing: value ? "act" : "none",
96
+ state: state.valid === false || refused ? "error" : "rest",
97
+ }),
98
+ FIELD_GLYPH,
99
+ BOX,
100
+ BOX_FOCUS,
101
+ ANCHOR,
102
+ over && OVER,
103
+ state.disabled
104
+ ? BOX_DISABLED
105
+ : state.valid !== false && !refused && BOX_HOVER_VALUE,
106
+ )}
107
+ >
108
+ <input {...control} {...wired(control)} className={PICK} />
109
+ <Icon name={value ? "File" : "FileUp"} fit="control" />
110
+ <span
111
+ className={cn(
112
+ fieldValue({ kind: "text" }),
113
+ !value && FIELD_PLACEHOLDER,
114
+ value ? NAME : VALUE,
115
+ state.disabled && DISABLED,
116
+ )}
117
+ >
118
+ {value ? <Name name={value.name} /> : words.chooseFile}
119
+ </span>
120
+ {value ? (
121
+ <>
122
+ <span
123
+ className={cn(
124
+ text({ role: "meta" }),
125
+ FIGURES,
126
+ SIZE,
127
+ state.disabled && DISABLED,
128
+ )}
129
+ >
130
+ {fileSize(value.size)}
131
+ </span>
132
+ <span className={ABOVE}>
133
+ <FieldDisabled value={state.disabled}>
134
+ <Button
135
+ glyph
136
+ icon="X"
137
+ label={`${words.remove} ${value.name}`}
138
+ onAct={() => {
139
+ refuse(undefined);
140
+ onChange(null);
141
+ }}
142
+ fit="field"
143
+ />
144
+ </FieldDisabled>
145
+ </span>
146
+ </>
147
+ ) : null}
148
+ </div>
149
+ )}
150
+ />
151
+ {line}
152
+ </div>
153
+ );
154
+ }
@@ -0,0 +1,132 @@
1
+ import type { PickedFile } from "@slotform/ui-core/descriptors";
2
+ import { FieldOwned } from "@slotform/ui-core/react/field";
3
+ import type { Datum } from "@slotform/ui-core/read";
4
+ import { use } from "react";
5
+ import type { IconAct } from "../../lib/act.ts";
6
+ import type { Closed } from "../../lib/closed.ts";
7
+ import type { Fielded } from "../../lib/field.ts";
8
+ import { FieldBody } from "../form-field/index.tsx";
9
+ import { HeldInput } from "./base.tsx";
10
+ import { CodeField } from "./code.tsx";
11
+ import { FileField } from "./file.tsx";
12
+ import { HeldProse } from "./prose.tsx";
13
+
14
+ export type InputKind =
15
+ | "text"
16
+ | "search"
17
+ | "secret"
18
+ | "source"
19
+ | "number"
20
+ | "email"
21
+ | "date";
22
+
23
+ /** One line of typing. */
24
+ export interface TextInputProps extends Closed {
25
+ /** What is typed: `text` (the default), `search` (a toolbar box with its glyph), `secret`, `source` (mono, a machine reads it; at the field's own fit it wraps at any character, the box growing by its lines, and Enter never starts a second), `number` (with `unit`), `email`, or `date` (a day, `<input type="date">`, the value `YYYY-MM-DD`). */
26
+ kind?: InputKind;
27
+ /** The value (text; one line, scrolls in the field, a `source` one wraps in it). A selection paints the box, inert, until its read lands and follows it until the viewer types. Once its read ended the control stands with no value and a change says why instead. */
28
+ value: Datum<string>;
29
+ /** Hears every keystroke's value. */
30
+ onChange: (value: string) => void;
31
+ /** Hears the value once the viewer is done with it: on leaving the field or on Enter, only when it changed since focus; Escape then puts back the value at focus and leaves the field. Return the save's promise and the viewer's value holds while it pends; a refusal puts it back, with its reason. */
32
+ onCommit?: (value: string) => unknown;
33
+ /** The hint drawn while the value is empty; never the field's name (a short phrase; truncates). */
34
+ placeholder?: string;
35
+ /** A `number`'s unit, drawn after the value (a word; truncates past the short measure, 20 characters). */
36
+ unit?: string;
37
+ /** An icon act inside the field's end (copy, reveal, add); Enter in the field presses it, except in a `secret`, where Enter commits and only a press runs the act (a Remove). */
38
+ act?: IconAct;
39
+ /** Takes the focus when it mounts, with the caret at the end of its value: pass it for a field that replaces what the viewer was reading. A field on a form that loads with the page leaves it off. */
40
+ autoFocus?: boolean;
41
+ }
42
+
43
+ /** Many lines of typing, in a box that grows with its value from three lines. */
44
+ export interface ProseProps extends Closed {
45
+ /** The value (text; wraps). A selection paints the box, inert, until its read lands and follows it until the viewer types. Once its read ended the control stands with no value and a change says why instead. */
46
+ value: Datum<string>;
47
+ /** Hears every keystroke's value. */
48
+ onChange: (value: string) => void;
49
+ /** Hears the value once the viewer leaves the field having changed it since focus (Enter is a new line); Escape then puts back the value at focus and leaves the field. Return the save's promise and the viewer's value holds while it pends; a refusal puts it back, with its reason. */
50
+ onCommit?: (value: string) => unknown;
51
+ /** The hint drawn while the value is empty; never the field's name (a short phrase; wraps). */
52
+ placeholder?: string;
53
+ /** Mono, for text a machine reads, with its indentation kept. */
54
+ source?: boolean;
55
+ /** A word budget: draws the count against it, in the error ink once over. */
56
+ budget?: number;
57
+ /** Takes the focus when it mounts, with the caret at the end of its value: pass it for a field that replaces what the viewer was reading. A field on a form that loads with the page leaves it off. */
58
+ autoFocus?: boolean;
59
+ }
60
+
61
+ /** A one-time code, one box per digit. */
62
+ export interface CodeInputProps extends Closed {
63
+ /** How many digits the code has, one box each. */
64
+ length: number;
65
+ /** The digits typed so far. */
66
+ value: string;
67
+ /** Hears the digits on every change; anything but a digit is dropped. */
68
+ onChange: (value: string) => void;
69
+ /** Hears the code once its last digit lands. Return the check's promise and, while it pends, the boxes hold the code at rest, the input is disabled and a spinner line stands under it; a refusal stands in a line under the boxes, with its reason, and the next digit clears it. */
70
+ onComplete?: (value: string) => unknown;
71
+ }
72
+
73
+ /** One file chosen or dropped. */
74
+ export interface FilePickProps extends Closed {
75
+ /** The chosen file; `null` while none is. */
76
+ value: PickedFile | null;
77
+ /** Hears the chosen file, or `null` once it is removed. A file `accept` does not name never reaches it. */
78
+ onChange: (file: PickedFile | null) => void;
79
+ /** The types it takes: MIME types or families (`text/csv`, `image/*`) and extensions (`.har`). */
80
+ accept: readonly string[];
81
+ }
82
+
83
+ /** What is typed or picked, by `kind`: a line of text (the default), `prose`, a one-time `code`, or a `file`. Each kind takes the props only it can use, and every kind its field's: its `label`, and the `description`, `optional` and `error` it draws placed alone. */
84
+ export type InputProps = Fielded &
85
+ (
86
+ | TextInputProps
87
+ | (ProseProps & { kind: "prose" })
88
+ | (CodeInputProps & { kind: "code" })
89
+ | (FilePickProps & { kind: "file" })
90
+ );
91
+
92
+ // The control alone, named by its label: where an owner draws the field
93
+ // around it, and a search, which names itself in its box.
94
+ function Bare({ props, label }: { props: InputProps; label: string }) {
95
+ switch (props.kind) {
96
+ case "prose":
97
+ return <HeldProse {...props} />;
98
+ case "code":
99
+ return <CodeField {...props} />;
100
+ case "file":
101
+ return <FileField {...props} />;
102
+ default:
103
+ return <HeldInput {...props} named={label} />;
104
+ }
105
+ }
106
+
107
+ /** A field, named by its `label`: placed alone it draws its label over the box and its `description`, or its `error` (a refused save's reason too, until the next change), under it; in a `Form`'s field or a `Toolbar` the owner names it and nothing is drawn around it, and a `search` names itself. A line of text is a field box on the surface: hairline at rest, `edge-hover` under the pointer, the ring on focus, `edge-error` when its `FormField` is in error, the disabled fill when it is disabled; in a `Table` cell it stands at the bar fit, named by the cell; in a `Row`'s entry or a `Rules` term, named by its label. `prose` grows with its value from three lines, the budget's count under it. `code` is a row of digit boxes over one input; the box the next digit lands in rings on focus. `file` is a box that is the act: empty it reads Choose file, chosen it shows the file's name and size with an act that removes it; a file dropped on the box is taken as one chosen, and a file of another type is refused in a line under the box, which clears on the next pick. */
108
+ export function Input(allProps: InputProps) {
109
+ const { label, description, optional, error, ...props } = allProps;
110
+ const owned = use(FieldOwned);
111
+ // A rest of the union is not distributed over its kinds, so it loses the
112
+ // `kind` each switch below narrows by; the cast puts the union back.
113
+ const control = props as InputProps;
114
+ if (owned || control.kind === "search")
115
+ return <Bare props={control} label={label} />;
116
+ const field = { label, description, optional, error, alone: true };
117
+ switch (control.kind) {
118
+ case "prose": {
119
+ const { kind: _, ...prose } = control;
120
+ return <FieldBody {...field} textArea={prose} />;
121
+ }
122
+ case "code":
123
+ case "file":
124
+ return (
125
+ <FieldBody {...field}>
126
+ <Bare props={control} label={label} />
127
+ </FieldBody>
128
+ );
129
+ default:
130
+ return <FieldBody {...field} input={control} />;
131
+ }
132
+ }