@yaks/ux 0.0.0-stage → 0.2.36

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 (286) hide show
  1. package/Answer.js +43 -0
  2. package/Answer.js.map +1 -0
  3. package/Answer.ts +84 -0
  4. package/Conversation.js +520 -0
  5. package/Conversation.js.map +1 -0
  6. package/Conversation.ts +649 -0
  7. package/ConversationBox.js +106 -0
  8. package/ConversationBox.js.map +1 -0
  9. package/ConversationBox.ts +198 -0
  10. package/ConversationLines.js +29 -0
  11. package/ConversationLines.js.map +1 -0
  12. package/ConversationLines.ts +48 -0
  13. package/Disclosure.js +79 -0
  14. package/Disclosure.js.map +1 -0
  15. package/Disclosure.ts +130 -0
  16. package/Edit.js +330 -0
  17. package/Edit.js.map +1 -0
  18. package/Edit.ts +488 -0
  19. package/EntityPopup.js +35 -0
  20. package/EntityPopup.js.map +1 -0
  21. package/EntityPopup.ts +52 -0
  22. package/Fields.js +161 -0
  23. package/Fields.js.map +1 -0
  24. package/Fields.ts +276 -0
  25. package/LICENSE +202 -0
  26. package/Order.js +105 -0
  27. package/Order.js.map +1 -0
  28. package/Order.ts +159 -0
  29. package/README.md +1023 -2
  30. package/Save.js +50 -0
  31. package/Save.js.map +1 -0
  32. package/Save.ts +115 -0
  33. package/Sort.js +49 -0
  34. package/Sort.js.map +1 -0
  35. package/Sort.ts +98 -0
  36. package/Submit.js +115 -0
  37. package/Submit.js.map +1 -0
  38. package/Submit.ts +174 -0
  39. package/Text.js +114 -0
  40. package/Text.js.map +1 -0
  41. package/Text.ts +140 -0
  42. package/TokenPicker.js +65 -0
  43. package/TokenPicker.js.map +1 -0
  44. package/TokenPicker.ts +117 -0
  45. package/_dist/Answer.d.ts +39 -0
  46. package/_dist/Answer.d.ts.map +1 -0
  47. package/_dist/Conversation.d.ts +131 -0
  48. package/_dist/Conversation.d.ts.map +1 -0
  49. package/_dist/ConversationBox.d.ts +29 -0
  50. package/_dist/ConversationBox.d.ts.map +1 -0
  51. package/_dist/ConversationLines.d.ts +13 -0
  52. package/_dist/ConversationLines.d.ts.map +1 -0
  53. package/_dist/Disclosure.d.ts +36 -0
  54. package/_dist/Disclosure.d.ts.map +1 -0
  55. package/_dist/Edit.d.ts +87 -0
  56. package/_dist/Edit.d.ts.map +1 -0
  57. package/_dist/EntityPopup.d.ts +12 -0
  58. package/_dist/EntityPopup.d.ts.map +1 -0
  59. package/_dist/Fields.d.ts +29 -0
  60. package/_dist/Fields.d.ts.map +1 -0
  61. package/_dist/Order.d.ts +40 -0
  62. package/_dist/Order.d.ts.map +1 -0
  63. package/_dist/Save.d.ts +28 -0
  64. package/_dist/Save.d.ts.map +1 -0
  65. package/_dist/Sort.d.ts +28 -0
  66. package/_dist/Sort.d.ts.map +1 -0
  67. package/_dist/Submit.d.ts +48 -0
  68. package/_dist/Submit.d.ts.map +1 -0
  69. package/_dist/Text.d.ts +27 -0
  70. package/_dist/Text.d.ts.map +1 -0
  71. package/_dist/TokenPicker.d.ts +19 -0
  72. package/_dist/TokenPicker.d.ts.map +1 -0
  73. package/_dist/anchor.d.ts +52 -0
  74. package/_dist/anchor.d.ts.map +1 -0
  75. package/_dist/anchored.d.ts +30 -0
  76. package/_dist/anchored.d.ts.map +1 -0
  77. package/_dist/completion-state.d.ts +65 -0
  78. package/_dist/completion-state.d.ts.map +1 -0
  79. package/_dist/completion.d.ts +73 -0
  80. package/_dist/completion.d.ts.map +1 -0
  81. package/_dist/composer-browser.d.ts +1 -0
  82. package/_dist/composer-browser.d.ts.map +1 -0
  83. package/_dist/composer.d.ts +31 -0
  84. package/_dist/composer.d.ts.map +1 -0
  85. package/_dist/context-menu-browser.d.ts +4 -0
  86. package/_dist/context-menu-browser.d.ts.map +1 -0
  87. package/_dist/context-menu.d.ts +95 -0
  88. package/_dist/context-menu.d.ts.map +1 -0
  89. package/_dist/drag.d.ts +29 -0
  90. package/_dist/drag.d.ts.map +1 -0
  91. package/_dist/edit-keys.d.ts +42 -0
  92. package/_dist/edit-keys.d.ts.map +1 -0
  93. package/_dist/emit.d.ts +7 -0
  94. package/_dist/emit.d.ts.map +1 -0
  95. package/_dist/entity-popup-browser.d.ts +3 -0
  96. package/_dist/entity-popup-browser.d.ts.map +1 -0
  97. package/_dist/face/FaceTagger.d.ts +25 -0
  98. package/_dist/face/FaceTagger.d.ts.map +1 -0
  99. package/_dist/face/review.d.ts +5 -0
  100. package/_dist/face/review.d.ts.map +1 -0
  101. package/_dist/face/tags.d.ts +99 -0
  102. package/_dist/face/tags.d.ts.map +1 -0
  103. package/_dist/field-values.d.ts +4 -0
  104. package/_dist/field-values.d.ts.map +1 -0
  105. package/_dist/fit-list.d.ts +4 -0
  106. package/_dist/fit-list.d.ts.map +1 -0
  107. package/_dist/hits.d.ts +27 -0
  108. package/_dist/hits.d.ts.map +1 -0
  109. package/_dist/host.d.ts +91 -0
  110. package/_dist/host.d.ts.map +1 -0
  111. package/_dist/image-fallback.d.ts +5 -0
  112. package/_dist/image-fallback.d.ts.map +1 -0
  113. package/_dist/keys.d.ts +95 -0
  114. package/_dist/keys.d.ts.map +1 -0
  115. package/_dist/kit.d.ts +23 -0
  116. package/_dist/kit.d.ts.map +1 -0
  117. package/_dist/lightbox-browser.d.ts +2 -0
  118. package/_dist/lightbox-browser.d.ts.map +1 -0
  119. package/_dist/lightbox.d.ts +42 -0
  120. package/_dist/lightbox.d.ts.map +1 -0
  121. package/_dist/live.d.ts +21 -0
  122. package/_dist/live.d.ts.map +1 -0
  123. package/_dist/mod.d.ts +67 -0
  124. package/_dist/mod.d.ts.map +1 -0
  125. package/_dist/read.d.ts +12 -0
  126. package/_dist/read.d.ts.map +1 -0
  127. package/_dist/resize.d.ts +43 -0
  128. package/_dist/resize.d.ts.map +1 -0
  129. package/_dist/rows.d.ts +31 -0
  130. package/_dist/rows.d.ts.map +1 -0
  131. package/_dist/save-browser.d.ts +29 -0
  132. package/_dist/save-browser.d.ts.map +1 -0
  133. package/_dist/select-browser.d.ts +1 -0
  134. package/_dist/select-browser.d.ts.map +1 -0
  135. package/_dist/select.d.ts +12 -0
  136. package/_dist/select.d.ts.map +1 -0
  137. package/_dist/sending.d.ts +10 -0
  138. package/_dist/sending.d.ts.map +1 -0
  139. package/_dist/sort-browser.d.ts +11 -0
  140. package/_dist/sort-browser.d.ts.map +1 -0
  141. package/_dist/state.d.ts +74 -0
  142. package/_dist/state.d.ts.map +1 -0
  143. package/_dist/suggest.d.ts +18 -0
  144. package/_dist/suggest.d.ts.map +1 -0
  145. package/_dist/swipe.d.ts +60 -0
  146. package/_dist/swipe.d.ts.map +1 -0
  147. package/_dist/ui.d.ts +6 -0
  148. package/_dist/ui.d.ts.map +1 -0
  149. package/_dist/upload-browser.d.ts +13 -0
  150. package/_dist/upload-browser.d.ts.map +1 -0
  151. package/_dist/views.d.ts +8 -0
  152. package/_dist/views.d.ts.map +1 -0
  153. package/_dist/vocab.d.ts +6 -0
  154. package/_dist/vocab.d.ts.map +1 -0
  155. package/anchor.js +58 -0
  156. package/anchor.js.map +1 -0
  157. package/anchor.ts +87 -0
  158. package/anchored.js +180 -0
  159. package/anchored.js.map +1 -0
  160. package/anchored.ts +232 -0
  161. package/completion-state.js +76 -0
  162. package/completion-state.js.map +1 -0
  163. package/completion-state.ts +120 -0
  164. package/completion.js +301 -0
  165. package/completion.js.map +1 -0
  166. package/completion.ts +433 -0
  167. package/composer-browser.js +4 -0
  168. package/composer-browser.js.map +1 -0
  169. package/composer-browser.ts +5 -0
  170. package/composer.js +42 -0
  171. package/composer.js.map +1 -0
  172. package/composer.ts +53 -0
  173. package/context-menu-browser.js +221 -0
  174. package/context-menu-browser.js.map +1 -0
  175. package/context-menu-browser.ts +231 -0
  176. package/context-menu.js +146 -0
  177. package/context-menu.js.map +1 -0
  178. package/context-menu.ts +194 -0
  179. package/deno.json +1 -0
  180. package/drag.js +33 -0
  181. package/drag.js.map +1 -0
  182. package/drag.ts +41 -0
  183. package/edit-keys.js +51 -0
  184. package/edit-keys.js.map +1 -0
  185. package/edit-keys.ts +81 -0
  186. package/emit.js +34 -0
  187. package/emit.js.map +1 -0
  188. package/emit.ts +55 -0
  189. package/entity-popup-browser.js +94 -0
  190. package/entity-popup-browser.js.map +1 -0
  191. package/entity-popup-browser.ts +100 -0
  192. package/face/FaceTagger.js +361 -0
  193. package/face/FaceTagger.js.map +1 -0
  194. package/face/FaceTagger.ts +507 -0
  195. package/face/NameProposal.js +61 -0
  196. package/face/NameProposal.js.map +1 -0
  197. package/face/NameProposal.ts +100 -0
  198. package/face/review.js +33 -0
  199. package/face/review.js.map +1 -0
  200. package/face/review.ts +49 -0
  201. package/face/tags.js +344 -0
  202. package/face/tags.js.map +1 -0
  203. package/face/tags.ts +357 -0
  204. package/field-values.js +20 -0
  205. package/field-values.js.map +1 -0
  206. package/field-values.ts +35 -0
  207. package/fit-list.js +66 -0
  208. package/fit-list.js.map +1 -0
  209. package/fit-list.ts +81 -0
  210. package/hits.js +65 -0
  211. package/hits.js.map +1 -0
  212. package/hits.ts +75 -0
  213. package/host.js +88 -0
  214. package/host.js.map +1 -0
  215. package/host.ts +212 -0
  216. package/image-fallback.js +32 -0
  217. package/image-fallback.js.map +1 -0
  218. package/image-fallback.ts +37 -0
  219. package/keys.js +418 -0
  220. package/keys.js.map +1 -0
  221. package/keys.ts +444 -0
  222. package/kit.js +41 -0
  223. package/kit.js.map +1 -0
  224. package/kit.ts +71 -0
  225. package/lightbox-browser.js +248 -0
  226. package/lightbox-browser.js.map +1 -0
  227. package/lightbox-browser.ts +232 -0
  228. package/lightbox.js +84 -0
  229. package/lightbox.js.map +1 -0
  230. package/lightbox.ts +103 -0
  231. package/live.js +111 -0
  232. package/live.js.map +1 -0
  233. package/live.ts +139 -0
  234. package/mod.js +67 -0
  235. package/mod.js.map +1 -0
  236. package/mod.ts +180 -0
  237. package/package.json +143 -4
  238. package/read.js +33 -0
  239. package/read.js.map +1 -0
  240. package/read.ts +56 -0
  241. package/resize.js +159 -0
  242. package/resize.js.map +1 -0
  243. package/resize.ts +203 -0
  244. package/rows.js +73 -0
  245. package/rows.js.map +1 -0
  246. package/rows.ts +97 -0
  247. package/save-browser.js +469 -0
  248. package/save-browser.js.map +1 -0
  249. package/save-browser.ts +530 -0
  250. package/select-browser.js +4 -0
  251. package/select-browser.js.map +1 -0
  252. package/select-browser.ts +5 -0
  253. package/select.js +296 -0
  254. package/select.js.map +1 -0
  255. package/select.ts +343 -0
  256. package/sending.js +36 -0
  257. package/sending.js.map +1 -0
  258. package/sending.ts +44 -0
  259. package/sort-browser.js +51 -0
  260. package/sort-browser.js.map +1 -0
  261. package/sort-browser.ts +70 -0
  262. package/state.js +99 -0
  263. package/state.js.map +1 -0
  264. package/state.ts +121 -0
  265. package/suggest.js +28 -0
  266. package/suggest.js.map +1 -0
  267. package/suggest.ts +32 -0
  268. package/swipe.js +186 -0
  269. package/swipe.js.map +1 -0
  270. package/swipe.ts +250 -0
  271. package/ui.js +507 -0
  272. package/ui.js.map +1 -0
  273. package/ui.ts +378 -0
  274. package/upload-browser.js +195 -0
  275. package/upload-browser.js.map +1 -0
  276. package/upload-browser.ts +303 -0
  277. package/upload-files.js +41 -0
  278. package/upload-files.js.map +1 -0
  279. package/upload-files.ts +49 -0
  280. package/views.js +8 -0
  281. package/views.js.map +1 -0
  282. package/views.ts +9 -0
  283. package/vocab.js +16 -0
  284. package/vocab.js.map +1 -0
  285. package/vocab.json +287 -0
  286. package/vocab.ts +17 -0
package/keys.ts ADDED
@@ -0,0 +1,444 @@
1
+ /**
2
+ * The keys that walk a page: vim's, one keymap and one cursor for every door.
3
+ *
4
+ * The **cursor** is the item the keys stand on, a mark of its own
5
+ * (`data-cursor`), never the browser's focus: standing on a field doesn't
6
+ * type into it, and nothing opening or closing moves it. In normal mode h, j,
7
+ * k and l move it to the item left of, below, above or right of where it
8
+ * stands; gg and G to the first and the last; Enter or Space presses the item
9
+ * (a link opens, a button presses); H and L go back and forward. An **item**
10
+ * is anything a person can press or type into.
11
+ *
12
+ * Insert mode is the browser's focus in a field: i, or Enter on a field,
13
+ * gives the item under the cursor the focus to type into, and every key is
14
+ * the field's until Esc leaves it, the cursor still on it.
15
+ *
16
+ * A **layer** is what floats over the page: a peek, a menu, a popover, a
17
+ * dialog (`role="dialog"`, `role="menu"`, `aria-modal`). The front-most one
18
+ * takes the keys: the cursor walks only its items, and Esc or q sends it
19
+ * `cancel`, which a layer answers by closing, and the cursor goes back to
20
+ * what it stood on when the layer opened. Without a layer the keys walk the
21
+ * whole surface, the sidebar beside the page included.
22
+ *
23
+ * A browser hears the keys as it hears any, and @yaks/tui's document
24
+ * dispatches the same keydown, measures what it painted, and presses and
25
+ * focuses as a browser does, so one `walk` walks a page in either.
26
+ *
27
+ * The core is pure: `chord` reads keys into moves, and `land` picks the item
28
+ * a move lands on from where the items sit. `walk` is the boundary that reads
29
+ * the document and moves the cursor.
30
+ *
31
+ * ```ts
32
+ * import { land } from '@yaks/ux/keys'
33
+ * import { equal } from '@yaks/testing'
34
+ *
35
+ * // Two rows of two: l goes right, j goes down, h from the left edge stays.
36
+ * let at = (left: number, top: number) =>
37
+ * ({ left, top, right: left + 10, bottom: top + 10 })
38
+ * let page = [at(0, 0), at(20, 0), at(0, 20), at(20, 20)]
39
+ * equal(land(page, 0, 'right'), 1)
40
+ * equal(land(page, 1, 'down'), 3)
41
+ * equal(land(page, 2, 'left'), undefined)
42
+ * ```
43
+ *
44
+ * @module
45
+ */
46
+
47
+ import {
48
+ dismissOverlay,
49
+ escapeOverlay,
50
+ frontOverlay,
51
+ openOverlay,
52
+ type OverlayOptions,
53
+ } from '@yaks/ui/overlays'
54
+
55
+ /** Where an item sits, in its platform's units: pixels, or a terminal's
56
+ * cells. */
57
+ export type Box = { left: number; top: number; right: number; bottom: number }
58
+
59
+ type Way = 'up' | 'down' | 'left' | 'right'
60
+
61
+ /** What a chord does. */
62
+ export type Move =
63
+ | Way
64
+ | 'first'
65
+ | 'last'
66
+ | 'back'
67
+ | 'forward'
68
+ | 'open'
69
+ | 'insert'
70
+ | 'close'
71
+
72
+ /** The keymap: a chord typed in normal mode, and its move. */
73
+ export let keymap: Record<string, Move> = {
74
+ h: 'left',
75
+ j: 'down',
76
+ k: 'up',
77
+ l: 'right',
78
+ gg: 'first',
79
+ G: 'last',
80
+ H: 'back',
81
+ L: 'forward',
82
+ Enter: 'open',
83
+ ' ': 'open',
84
+ i: 'insert',
85
+ Escape: 'close',
86
+ q: 'close',
87
+ }
88
+
89
+ /** The keymap as a keybinding card reads it. */
90
+ export let walkKeys: { keys: string[]; about: string }[] = [
91
+ {
92
+ keys: ['h', 'j', 'k', 'l'],
93
+ about: 'move the cursor to the item left, below, above or right',
94
+ },
95
+ { keys: ['gg', 'G'], about: 'move the cursor to the first or the last item' },
96
+ {
97
+ keys: ['Enter', 'Space'],
98
+ about: 'press the item: a link opens, a button presses, a field types',
99
+ },
100
+ { keys: ['i'], about: 'type into the item: insert mode, until Esc' },
101
+ { keys: ['Esc', 'q'], about: 'close the peek, menu or popover in front' },
102
+ { keys: ['H', 'L'], about: 'go back or forward' },
103
+ ]
104
+
105
+ /** A key read after the keys still pending: the move a whole chord makes, or
106
+ * the chord begun and still pending. A key that ends no chord starts over. */
107
+ /// chord('', 'j') -> {move: 'down', pending: ''}
108
+ /// chord('', 'g') -> {pending: 'g'}
109
+ /// chord('g', 'g') -> {move: 'first', pending: ''}
110
+ /// chord('g', 'G') -> {move: 'last', pending: ''}
111
+ /// chord('', 'x') -> {pending: ''}
112
+ export let chord = (
113
+ pending: string,
114
+ key: string,
115
+ ): { move?: Move; pending: string } => {
116
+ let typed = pending + key
117
+ if (Object.hasOwn(keymap, typed)) return { move: keymap[typed], pending: '' }
118
+ if (Object.keys(keymap).some((k) => k != typed && k.startsWith(typed))) {
119
+ return { pending: typed }
120
+ }
121
+ return pending ? chord('', key) : { pending: '' }
122
+ }
123
+
124
+ // Every way read as down: a box turned so the way it is walked runs down the
125
+ // page, so one set of rules serves all four.
126
+ let turn = (way: Way, b: Box): Box =>
127
+ way == 'down'
128
+ ? b
129
+ : way == 'up'
130
+ ? { left: b.left, right: b.right, top: -b.bottom, bottom: -b.top }
131
+ : way == 'right'
132
+ ? { left: b.top, right: b.bottom, top: b.left, bottom: b.right }
133
+ : { left: b.top, right: b.bottom, top: -b.right, bottom: -b.left }
134
+
135
+ // The rules Android's FocusFinder walks a screen by. A box lies down from
136
+ // another when its top starts lower and its bottom ends lower, so a box
137
+ // inside another, or beside it on the same line, never does. One in the beam,
138
+ // overlapping it across the way, wins over one outside it, unless that one
139
+ // ends before the beam's starts; otherwise the nearer wins, where how far
140
+ // down counts thirteen times over how far across. Across is measured start
141
+ // to start, not centre to centre as Android does, so a walk down from a wide
142
+ // title lands where the next line begins.
143
+ let down = (a: Box, b: Box) =>
144
+ (a.top < b.top || a.bottom <= b.top) && a.bottom < b.bottom
145
+ let beam = (a: Box, b: Box) => b.right > a.left && b.left < a.right
146
+ let gap = (a: Box, b: Box) => Math.max(0, b.top - a.bottom)
147
+ let far = (a: Box, b: Box) => Math.max(1, b.bottom - a.bottom)
148
+ let across = (a: Box, b: Box) => Math.abs(a.left - b.left)
149
+ let weight = (a: Box, b: Box) => 13 * gap(a, b) ** 2 + across(a, b) ** 2
150
+ let beats = (a: Box, b: Box, c: Box, sideways: boolean) =>
151
+ beam(a, b) && !beam(a, c) &&
152
+ (a.bottom > c.top || sideways || gap(a, b) < far(a, c))
153
+ let better = (a: Box, b: Box, c: Box | undefined, sideways: boolean) =>
154
+ down(a, b) && (
155
+ !c || !down(a, c) || beats(a, b, c, sideways) ||
156
+ !beats(a, c, b, sideways) && weight(a, b) < weight(a, c)
157
+ )
158
+
159
+ let inside = (b: Box, view?: Box) =>
160
+ !view || b.bottom > view.top && b.top < view.bottom &&
161
+ b.right > view.left && b.left < view.right
162
+
163
+ /** The item a move lands on from the one at `at` (-1 for none), among items
164
+ * in reading order with where each sits; undefined stays put. An item with no
165
+ * box is out of sight (a terminal paints only what its window shows): a walk
166
+ * up or down that finds nothing in sight reaches it in reading order. From
167
+ * nowhere, a walk starts at the first item in `view`, or the last walking up
168
+ * or left. */
169
+ /// land([], -1, 'down') -> undefined
170
+ /// land([{left: 0, top: 0, right: 9, bottom: 9}], -1, 'down') -> 0
171
+ /// land([{left: 0, top: 0, right: 9, bottom: 9}, undefined], 0, 'down') -> 1
172
+ /// land([undefined, undefined, undefined], 1, 'last') -> 2
173
+ export let land = (
174
+ boxes: (Box | undefined)[],
175
+ at: number,
176
+ move: Way | 'first' | 'last',
177
+ view?: Box,
178
+ ): number | undefined => {
179
+ let last = boxes.length - 1
180
+ if (last < 0) return
181
+ if (move == 'first') return 0
182
+ if (move == 'last') return last
183
+ let back = move == 'up' || move == 'left'
184
+ let sideways = move == 'left' || move == 'right'
185
+ let next = at + (back ? -1 : 1)
186
+ let order = !sideways && next >= 0 && next <= last && !boxes[next]
187
+ let here = boxes[at]
188
+ if (at < 0) {
189
+ let seen = boxes.flatMap((b, i) => b && inside(b, view) ? [i] : [])
190
+ return seen.length ? (back ? seen.at(-1) : seen[0]) : back ? last : 0
191
+ }
192
+ if (!here) return order ? next : undefined
193
+ let a = turn(move, here)
194
+ let best: number | undefined
195
+ boxes.forEach((b, i) => {
196
+ let held = best == null ? undefined : turn(move, boxes[best]!)
197
+ if (b && i != at && better(a, turn(move, b), held, sideways)) best = i
198
+ })
199
+ return best ?? (order ? next : undefined)
200
+ }
201
+
202
+ // What a person can press or type into; what floats over the page; the
203
+ // cursor's mark.
204
+ let reach = [
205
+ 'a[href]',
206
+ 'button',
207
+ 'input',
208
+ 'textarea',
209
+ 'select',
210
+ 'summary',
211
+ '[tabindex]',
212
+ '[contenteditable]',
213
+ '[role=button]',
214
+ '[role=link]',
215
+ '[role=option]',
216
+ '[role=menuitem]',
217
+ '[role=textbox]',
218
+ ].join(', ')
219
+ let layers = 'dialog[open], [role=dialog], [role=menu], [aria-modal=true]'
220
+ let mark = 'data-cursor'
221
+ let buttons = [
222
+ 'checkbox',
223
+ 'radio',
224
+ 'button',
225
+ 'submit',
226
+ 'reset',
227
+ 'range',
228
+ 'color',
229
+ 'file',
230
+ ]
231
+ let held = ['Shift', 'Control', 'Alt', 'Meta']
232
+
233
+ /** Whether `at` is being typed into: a field that takes every printable key,
234
+ * j and k among them. A select, a checkbox or a button lets the keys walk
235
+ * on. */
236
+ export let typing = (at: EventTarget | null | undefined): boolean => {
237
+ let el = at as HTMLElement | null | undefined
238
+ if (typeof el?.matches != 'function') return false
239
+ return !!el.isContentEditable || el.matches('textarea') ||
240
+ el.matches('input') && !buttons.includes(el.getAttribute('type') ?? 'text')
241
+ }
242
+
243
+ // Whether an item is typed into once entered: a field, or text typed over in
244
+ // place (`role="textbox"`), which opens as a double press opens it.
245
+ let editable = (n: Element) => typing(n) || n.matches('[role=textbox]')
246
+
247
+ let shown = (n: Element) =>
248
+ (n as { checkVisibility?: () => boolean }).checkVisibility?.() ?? true
249
+
250
+ /** The items under `root` the cursor stands on, in reading order: what can
251
+ * be pressed or typed into, and is shown. */
252
+ export let items = (root: Element): Element[] =>
253
+ [...root.querySelectorAll(reach)].filter((n) =>
254
+ !n.hasAttribute('disabled') && n.getAttribute('tabindex') != '-1' &&
255
+ n.getAttribute('contenteditable') != 'false' && shown(n)
256
+ )
257
+
258
+ /** The front-most layer for the cursor, using the shared overlay stack. */
259
+ export let front = (surface: Element): Element | undefined => {
260
+ let overlay = frontOverlay(
261
+ surface.ownerDocument,
262
+ (node) => node.matches(layers),
263
+ )
264
+ if (overlay) return overlay
265
+ let top = surface
266
+ while (top.parentElement) top = top.parentElement
267
+ return [...top.querySelectorAll(layers)].filter(shown).at(-1)
268
+ }
269
+
270
+ /** Put up a layer that no kit part draws (one a server drew, a game's glass,
271
+ * a frame's whole page standing over its parent's): while it is open and
272
+ * `shown` (default: not `hidden`), Esc in front of everything opened before
273
+ * it sends it `close`, which closes it or answers false to stay. A kit part
274
+ * is a layer by its `onCancel` instead. Returns what takes it down. */
275
+ export let layer = (
276
+ node: HTMLElement,
277
+ close: () => boolean | void,
278
+ o?: OverlayOptions,
279
+ ): () => void => openOverlay(node, close, o)
280
+
281
+ let box = (n: Element): Box | undefined => {
282
+ let r = n.getBoundingClientRect?.()
283
+ return r && (r.width > 0 || r.height > 0) ? r : undefined
284
+ }
285
+
286
+ let view = (): Box | undefined =>
287
+ typeof innerHeight == 'number' && innerHeight > 0
288
+ ? { left: 0, top: 0, right: innerWidth, bottom: innerHeight }
289
+ : undefined
290
+
291
+ // The item whose box holds where the cursor last stood: a cursor whose
292
+ // element was drawn anew stays where it was.
293
+ let near = (boxes: (Box | undefined)[], was: Box) => {
294
+ let x = (was.left + was.right) / 2, y = (was.top + was.bottom) / 2
295
+ return boxes.findIndex((b) =>
296
+ !!b && b.left <= x && x <= b.right && b.top <= y && y <= b.bottom
297
+ )
298
+ }
299
+
300
+ // Send `n` an event made by its own window, as its document takes only its
301
+ // own (a terminal's document has no window, and takes any).
302
+ let send = (n: Element, type: string, init: EventInit) => {
303
+ let view = n.ownerDocument?.defaultView as typeof globalThis | null
304
+ n.dispatchEvent(new (view?.Event ?? Event)(type, init))
305
+ }
306
+
307
+ // Enter an item: type into a field, choose in a select, open text typed over
308
+ // in place as a double press does, press anything else.
309
+ let enter = (n: Element) => {
310
+ let el = n as HTMLElement
311
+ if (typing(n) || n.matches('select')) return el.focus()
312
+ if (n.matches('[role=textbox]')) {
313
+ return send(n, 'dblclick', { bubbles: true })
314
+ }
315
+ el.click()
316
+ }
317
+
318
+ /** The walk over a surface (the app's frame: the sidebar and the page):
319
+ * `key` hears keydowns, and `point` hears presses, which carry the cursor,
320
+ * unmarked, to the item pressed. A walk from nowhere starts in `start` (the
321
+ * page) when there is one. A key someone took before it, a key with Ctrl,
322
+ * Alt or Meta, and every key while a field is typed into pass by. */
323
+ export let walk = (o: {
324
+ surface: () => Element | null | undefined
325
+ start?: () => Element | null | undefined
326
+ back?: () => void
327
+ forward?: () => void
328
+ }): { key: (e: KeyboardEvent) => void; point: (e: Event) => void } => {
329
+ let pending = ''
330
+ let at: Element | undefined
331
+ let was: Box | undefined
332
+ let stack: { layer: Element; opener?: Element }[] = []
333
+
334
+ let put = (n?: Element) => {
335
+ at?.removeAttribute(mark)
336
+ at = n
337
+ if (!n) return
338
+ n.setAttribute(mark, '')
339
+ was = box(n) ?? was
340
+ n.scrollIntoView?.({ block: 'nearest', inline: 'nearest' })
341
+ }
342
+
343
+ // The front layer. One that closed gives the cursor back to what it stood
344
+ // on when it opened, and one newly in front remembers that.
345
+ let settle = (surface: Element) => {
346
+ let top = front(surface)
347
+ while (stack.length && !stack.at(-1)!.layer.isConnected) {
348
+ let { opener } = stack.pop()!
349
+ if (!at?.isConnected) put(opener)
350
+ }
351
+ if (top && stack.at(-1)?.layer != top) {
352
+ stack.push({ layer: top, opener: at })
353
+ }
354
+ return top
355
+ }
356
+
357
+ // An item drawn anew as it closes (text typed over in place is) keeps the
358
+ // cursor: it goes to what now stands where it stood.
359
+ let heal = () => {
360
+ let surface = o.surface()
361
+ if (!surface || !was || at?.isConnected != false) return
362
+ let list = items(front(surface) ?? surface)
363
+ let i = near(list.map(box), was)
364
+ if (i >= 0) put(list[i])
365
+ }
366
+
367
+ let current = (list: Element[]) =>
368
+ at?.isConnected ? list.indexOf(at) : was ? near(list.map(box), was) : -1
369
+
370
+ let move = (root: Element, i: number, way: Way | 'first' | 'last') => {
371
+ let list = items(root)
372
+ let page = i < 0 && root == o.surface() && o.start?.()
373
+ let from = page ? items(page) : list
374
+ let to = land(from.map(box), page ? -1 : i, way, view())
375
+ if (to != null && from[to] != list[i]) put(from[to])
376
+ }
377
+
378
+ let key = (e: KeyboardEvent) => {
379
+ if (held.includes(e.key)) return
380
+ let surface = o.surface()
381
+ if (surface && e.key == 'Escape') {
382
+ let closed = escapeOverlay(surface.ownerDocument, e)
383
+ if (closed && stack.at(-1)?.layer == closed) put(stack.pop()?.opener)
384
+ if (e.defaultPrevented && typing(e.target) && !closed) {
385
+ put(e.target as Element)
386
+ setTimeout(heal)
387
+ }
388
+ }
389
+ let mine = !e.defaultPrevented && !e.ctrlKey && !e.metaKey && !e.altKey
390
+ if (!mine || typing(e.target)) {
391
+ pending = ''
392
+ // Esc leaves insert mode, the cursor where it was typed: the walk takes
393
+ // it, so a layer the field is in stays up.
394
+ if (mine && e.key == 'Escape') {
395
+ e.preventDefault()
396
+ put(e.target as Element)
397
+ setTimeout(heal)
398
+ }
399
+ return
400
+ }
401
+ let read = chord(pending, e.key)
402
+ pending = read.pending
403
+ if (pending) return e.preventDefault()
404
+ let way = read.move
405
+ if (way == 'back' || way == 'forward') {
406
+ e.preventDefault()
407
+ return o[way]?.()
408
+ }
409
+ if (!way || !surface) return
410
+ let top = settle(surface)
411
+ let root = top ?? surface
412
+ let list = items(root)
413
+ let i = current(list)
414
+ if (way == 'close') {
415
+ if (e.key == 'Escape' || !frontOverlay(surface.ownerDocument)) return
416
+ e.preventDefault()
417
+ if (dismissOverlay(surface.ownerDocument) == top) {
418
+ put(stack.pop()?.opener)
419
+ }
420
+ return
421
+ }
422
+ if (way == 'open') {
423
+ if (i < 0) return
424
+ e.preventDefault()
425
+ enter(list[i])
426
+ // A choice that closes its layer hands the cursor back once it has.
427
+ if (top) setTimeout(() => settle(surface))
428
+ return
429
+ }
430
+ e.preventDefault()
431
+ if (way != 'insert') return move(root, i, way)
432
+ let field = [...list.slice(Math.max(i, 0)), ...list].find(editable)
433
+ if (field) put(field), enter(field)
434
+ }
435
+
436
+ let point = (e: Event) => {
437
+ let hit = (e.target as Element | null)?.closest?.(reach) ?? undefined
438
+ at?.removeAttribute(mark)
439
+ at = hit
440
+ was = hit && box(hit)
441
+ }
442
+
443
+ return { key, point }
444
+ }
package/kit.js ADDED
@@ -0,0 +1,41 @@
1
+ /** UX kits describe controlled components and the page-only words they own.
2
+ * A kit carries declarations, not a store: the page loads its vocabulary and
3
+ * names each instance's entity. Drafts stay with the host, outside kit state.
4
+ * @module
5
+ */ /* @ts-types="npm:@types/react@19.3.0" */ import { storable } from "@yaks/vocab";
6
+ /** Check the kit boundary before exposing it to a page. Every state word
7
+ * belongs to the bringer, is CamelCase, and stays off the wire. */ export let defineKit = (kit)=>{
8
+ for (let [word, schema] of Object.entries(kit.vocab.$defs ?? {})){
9
+ if (schema.rule || schema.effect || schema.tool) {
10
+ throw new Error(`${word}: a kit brings state, not rules, effects or tools`);
11
+ }
12
+ if (!schema.component) continue;
13
+ if (!/^[A-Z][A-Za-z0-9]*$/.test(word)) {
14
+ throw new Error(`${word}: kit state must be a CamelCase component`);
15
+ }
16
+ if (schema.sync != 'none') {
17
+ throw new Error(`${word}: kit state must declare sync: none`);
18
+ }
19
+ if (!schema.durable || schema.durable == 'forever') {
20
+ throw new Error(`${word}: kit state must declare a page lifetime`);
21
+ }
22
+ }
23
+ let errors = storable(kit.vocab);
24
+ if (errors.length) throw new Error(errors.join('; '));
25
+ for (let [name, piece] of Object.entries(kit.components)){
26
+ for (let word of piece.state){
27
+ let schema = kit.vocab.$defs?.[word];
28
+ if (!/^[A-Z][A-Za-z0-9]*$/.test(word) || !schema?.component) {
29
+ throw new Error(`${name}: ${word} must declare a CamelCase component`);
30
+ }
31
+ if (schema.sync != 'none') {
32
+ throw new Error(`${name}: ${word} must declare sync: none`);
33
+ }
34
+ }
35
+ }
36
+ return kit;
37
+ };
38
+ /** The documents a page loads beside its domain vocabulary. */ export let kitDocs = (kits)=>[
39
+ ...new Set(Object.values(kits).map((kit)=>defineKit(kit).vocab))
40
+ ];
41
+ //# sourceMappingURL=kit.js.map
package/kit.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kit.js","sources":["./kit.ts"],"names":[],"mappings":"AAAA;;;;CAIC,GAED,uCAAuC,GACvC,SAAS,QAAQ,2BAA+C;AAsBhE;iEACiE,GACjE,OAAO,IAAI,YAAY,CAAgB;EACrC,KAAK,IAAI,CAAC,MAAM,OAAO,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,GAAI;IAChE,IAAI,OAAO,IAAI,IAAI,OAAO,MAAM,IAAI,OAAO,IAAI,EAAE;MAC/C,MAAM,IAAI,MACR,GAAG,KAAK,iDAAiD,CAAC;IAE9D;IACA,IAAI,CAAC,OAAO,SAAS,EAAE;IACvB,IAAI,CAAC,sBAAsB,IAAI,CAAC,OAAO;MACrC,MAAM,IAAI,MAAM,GAAG,KAAK,yCAAyC,CAAC;IACpE;IACA,IAAI,OAAO,IAAI,IAAI,QAAQ;MACzB,MAAM,IAAI,MAAM,GAAG,KAAK,mCAAmC,CAAC;IAC9D;IACA,IAAI,CAAC,OAAO,OAAO,IAAI,OAAO,OAAO,IAAI,WAAW;MAClD,MAAM,IAAI,MAAM,GAAG,KAAK,wCAAwC,CAAC;IACnE;EACF;EACA,IAAI,SAAS,SAAS,IAAI,KAAK;EAC/B,IAAI,OAAO,MAAM,EAAE,MAAM,IAAI,MAAM,OAAO,IAAI,CAAC;EAC/C,KAAK,IAAI,CAAC,MAAM,MAAM,IAAI,OAAO,OAAO,CAAC,IAAI,UAAU,EAAG;IACxD,KAAK,IAAI,QAAQ,MAAM,KAAK,CAAE;MAC5B,IAAI,SAAS,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK;MACpC,IAAI,CAAC,sBAAsB,IAAI,CAAC,SAAS,CAAC,QAAQ,WAAW;QAC3D,MAAM,IAAI,MAAM,GAAG,KAAK,EAAE,EAAE,KAAK,mCAAmC,CAAC;MACvE;MACA,IAAI,OAAO,IAAI,IAAI,QAAQ;QACzB,MAAM,IAAI,MAAM,GAAG,KAAK,EAAE,EAAE,KAAK,wBAAwB,CAAC;MAC5D;IACF;EACF;EACA,OAAO;AACT,EAAC;AAED,6DAA6D,GAC7D,OAAO,IAAI,UAAU,CACnB,OACe;OACZ,IAAI,IAAI,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,MAAQ,UAAU,KAAK,KAAK;GACjE,CAAA"}
package/kit.ts ADDED
@@ -0,0 +1,71 @@
1
+ /** UX kits describe controlled components and the page-only words they own.
2
+ * A kit carries declarations, not a store: the page loads its vocabulary and
3
+ * names each instance's entity. Drafts stay with the host, outside kit state.
4
+ * @module
5
+ */
6
+
7
+ /* @ts-types="npm:@types/react@19.3.0" */ import type { ReactElement } from 'npm:react@19.3.0'
8
+ import { storable, type VocabDoc } from '@yaks/vocab'
9
+
10
+ /** A labelled tree, drawn by the guide within its own composition. */
11
+ export type Specimen = [label: string, node: ReactElement]
12
+
13
+ /** Metadata for one controlled component. Props remain on its own export;
14
+ * this erased component reference is for discovery, not invocation. */
15
+ export type Piece = {
16
+ Component: (props: never) => ReactElement | null
17
+ description: string
18
+ specimens: () => Specimen[]
19
+ /** CamelCase state and event components declared in this kit's vocabulary. */
20
+ state: string[]
21
+ }
22
+
23
+ /** A bringer's components and vocabulary, usable without a plugin host. */
24
+ export type Kit = {
25
+ description: string
26
+ components: Record<string, Piece>
27
+ vocab: VocabDoc
28
+ }
29
+
30
+ /** Check the kit boundary before exposing it to a page. Every state word
31
+ * belongs to the bringer, is CamelCase, and stays off the wire. */
32
+ export let defineKit = <T extends Kit>(kit: T): T => {
33
+ for (let [word, schema] of Object.entries(kit.vocab.$defs ?? {})) {
34
+ if (schema.rule || schema.effect || schema.tool) {
35
+ throw new Error(
36
+ `${word}: a kit brings state, not rules, effects or tools`,
37
+ )
38
+ }
39
+ if (!schema.component) continue
40
+ if (!/^[A-Z][A-Za-z0-9]*$/.test(word)) {
41
+ throw new Error(`${word}: kit state must be a CamelCase component`)
42
+ }
43
+ if (schema.sync != 'none') {
44
+ throw new Error(`${word}: kit state must declare sync: none`)
45
+ }
46
+ if (!schema.durable || schema.durable == 'forever') {
47
+ throw new Error(`${word}: kit state must declare a page lifetime`)
48
+ }
49
+ }
50
+ let errors = storable(kit.vocab)
51
+ if (errors.length) throw new Error(errors.join('; '))
52
+ for (let [name, piece] of Object.entries(kit.components)) {
53
+ for (let word of piece.state) {
54
+ let schema = kit.vocab.$defs?.[word]
55
+ if (!/^[A-Z][A-Za-z0-9]*$/.test(word) || !schema?.component) {
56
+ throw new Error(`${name}: ${word} must declare a CamelCase component`)
57
+ }
58
+ if (schema.sync != 'none') {
59
+ throw new Error(`${name}: ${word} must declare sync: none`)
60
+ }
61
+ }
62
+ }
63
+ return kit
64
+ }
65
+
66
+ /** The documents a page loads beside its domain vocabulary. */
67
+ export let kitDocs = (
68
+ kits: Record<string, Kit>,
69
+ ): VocabDoc[] => [
70
+ ...new Set(Object.values(kits).map((kit) => defineKit(kit).vocab)),
71
+ ]