@prestack/thread-ui 0.0.1 → 0.0.2

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 (269) hide show
  1. package/dist/ui/animations/anm_appear.ts +13 -0
  2. package/dist/ui/animations/anm_blur_in.ts +19 -0
  3. package/dist/ui/animations/anm_blur_out.ts +18 -0
  4. package/dist/ui/animations/anm_dismiss.ts +18 -0
  5. package/dist/ui/animations/anm_drop.ts +19 -0
  6. package/dist/ui/animations/anm_fall.ts +18 -0
  7. package/dist/ui/animations/anm_flash.ts +18 -0
  8. package/dist/ui/animations/anm_glow.ts +16 -0
  9. package/dist/ui/animations/anm_lift.ts +16 -0
  10. package/dist/ui/animations/anm_nudge.ts +16 -0
  11. package/dist/ui/animations/anm_pop_in.ts +23 -0
  12. package/dist/ui/animations/anm_pop_out.ts +22 -0
  13. package/dist/ui/animations/anm_press.ts +12 -0
  14. package/dist/ui/animations/anm_pulse.ts +13 -0
  15. package/dist/ui/animations/anm_rise.ts +19 -0
  16. package/dist/ui/animations/anm_shake.ts +13 -0
  17. package/dist/ui/animations/anm_slide_in_left.ts +19 -0
  18. package/dist/ui/animations/anm_slide_in_right.ts +19 -0
  19. package/dist/ui/animations/anm_slide_out_left.ts +18 -0
  20. package/dist/ui/animations/anm_slide_out_right.ts +18 -0
  21. package/dist/ui/animations/anm_spin.ts +12 -0
  22. package/dist/ui/animations/anm_swell.ts +16 -0
  23. package/dist/ui/animations/anm_wobble.ts +24 -0
  24. package/dist/ui/animations/anm_zoom_in.ts +19 -0
  25. package/dist/ui/animations/anm_zoom_out.ts +18 -0
  26. package/dist/ui/animations.ts +317 -0
  27. package/dist/ui/attach.js +13 -4
  28. package/dist/ui/attach.ts +613 -0
  29. package/dist/ui/charts.tsx +74 -0
  30. package/dist/ui/checked.d.ts +2 -2
  31. package/dist/ui/checked.ts +32 -0
  32. package/dist/ui/collector.ts +140 -0
  33. package/dist/ui/compositions.d.ts +10 -1
  34. package/dist/ui/compositions.js +262 -23
  35. package/dist/ui/compositions.ts +640 -0
  36. package/dist/ui/consent.ts +55 -0
  37. package/dist/ui/content.tsx +39 -0
  38. package/dist/ui/contrast.ts +27 -0
  39. package/dist/ui/controls.tsx +30 -0
  40. package/dist/ui/disclosure.tsx +22 -0
  41. package/dist/ui/dismiss-layer.ts +87 -0
  42. package/dist/ui/door.ts +140 -0
  43. package/dist/ui/drag.ts +316 -0
  44. package/dist/ui/elements/accordion.js +2 -2
  45. package/dist/ui/elements/accordion.tsx +47 -0
  46. package/dist/ui/elements/accordion.works.ts +66 -0
  47. package/dist/ui/elements/aspect-ratio.js +1 -1
  48. package/dist/ui/elements/aspect-ratio.tsx +30 -0
  49. package/dist/ui/elements/aspect-ratio.works.ts +5 -0
  50. package/dist/ui/elements/avatar.js +1 -1
  51. package/dist/ui/elements/avatar.tsx +33 -0
  52. package/dist/ui/elements/avatar.works.ts +16 -0
  53. package/dist/ui/elements/badge.js +1 -1
  54. package/dist/ui/elements/badge.tsx +32 -0
  55. package/dist/ui/elements/badge.works.ts +9 -0
  56. package/dist/ui/elements/bars.js +2 -2
  57. package/dist/ui/elements/bars.tsx +49 -0
  58. package/dist/ui/elements/bars.works.ts +29 -0
  59. package/dist/ui/elements/box.js +3 -2
  60. package/dist/ui/elements/box.tsx +35 -0
  61. package/dist/ui/elements/breadcrumb.js +2 -2
  62. package/dist/ui/elements/breadcrumb.tsx +37 -0
  63. package/dist/ui/elements/breadcrumb.works.ts +24 -0
  64. package/dist/ui/elements/button.js +1 -1
  65. package/dist/ui/elements/button.tsx +45 -0
  66. package/dist/ui/elements/button.works.tsx +52 -0
  67. package/dist/ui/elements/column.js +1 -1
  68. package/dist/ui/elements/column.tsx +28 -0
  69. package/dist/ui/elements/consent.js +2 -2
  70. package/dist/ui/elements/consent.tsx +70 -0
  71. package/dist/ui/elements/consent.works.tsx +132 -0
  72. package/dist/ui/elements/donut.js +2 -2
  73. package/dist/ui/elements/donut.tsx +44 -0
  74. package/dist/ui/elements/donut.works.ts +42 -0
  75. package/dist/ui/elements/each.js +1 -1
  76. package/dist/ui/elements/each.tsx +33 -0
  77. package/dist/ui/elements/each.works.tsx +82 -0
  78. package/dist/ui/elements/empty.js +1 -1
  79. package/dist/ui/elements/empty.tsx +32 -0
  80. package/dist/ui/elements/field.js +1 -1
  81. package/dist/ui/elements/field.tsx +44 -0
  82. package/dist/ui/elements/field.works.ts +65 -0
  83. package/dist/ui/elements/funnel.js +2 -2
  84. package/dist/ui/elements/funnel.tsx +49 -0
  85. package/dist/ui/elements/funnel.works.ts +37 -0
  86. package/dist/ui/elements/gauge.js +1 -1
  87. package/dist/ui/elements/gauge.tsx +39 -0
  88. package/dist/ui/elements/gauge.works.ts +42 -0
  89. package/dist/ui/elements/glyph.js +1 -1
  90. package/dist/ui/elements/glyph.tsx +29 -0
  91. package/dist/ui/elements/glyph.works.tsx +42 -0
  92. package/dist/ui/elements/grid.js +1 -1
  93. package/dist/ui/elements/grid.tsx +28 -0
  94. package/dist/ui/elements/heatmap.js +2 -2
  95. package/dist/ui/elements/heatmap.tsx +47 -0
  96. package/dist/ui/elements/heatmap.works.ts +24 -0
  97. package/dist/ui/elements/hit-box.js +1 -1
  98. package/dist/ui/elements/hit-box.tsx +46 -0
  99. package/dist/ui/elements/hit-box.works.tsx +576 -0
  100. package/dist/ui/elements/image.js +1 -1
  101. package/dist/ui/elements/image.tsx +28 -0
  102. package/dist/ui/elements/line-chart.js +2 -2
  103. package/dist/ui/elements/line-chart.tsx +44 -0
  104. package/dist/ui/elements/line-chart.works.ts +41 -0
  105. package/dist/ui/elements/link.js +1 -1
  106. package/dist/ui/elements/link.tsx +37 -0
  107. package/dist/ui/elements/link.works.ts +25 -0
  108. package/dist/ui/elements/media.js +1 -1
  109. package/dist/ui/elements/media.tsx +56 -0
  110. package/dist/ui/elements/menu.js +2 -2
  111. package/dist/ui/elements/menu.tsx +50 -0
  112. package/dist/ui/elements/menu.works.tsx +153 -0
  113. package/dist/ui/elements/money.js +1 -1
  114. package/dist/ui/elements/money.tsx +28 -0
  115. package/dist/ui/elements/money.works.ts +9 -0
  116. package/dist/ui/elements/pagination.js +2 -2
  117. package/dist/ui/elements/pagination.tsx +38 -0
  118. package/dist/ui/elements/pagination.works.ts +20 -0
  119. package/dist/ui/elements/progress.js +1 -1
  120. package/dist/ui/elements/progress.tsx +33 -0
  121. package/dist/ui/elements/progress.works.ts +8 -0
  122. package/dist/ui/elements/radio-group.js +2 -2
  123. package/dist/ui/elements/radio-group.tsx +49 -0
  124. package/dist/ui/elements/radio-group.works.ts +33 -0
  125. package/dist/ui/elements/row.js +1 -1
  126. package/dist/ui/elements/row.tsx +28 -0
  127. package/dist/ui/elements/scatter.js +1 -1
  128. package/dist/ui/elements/scatter.tsx +41 -0
  129. package/dist/ui/elements/scatter.works.ts +38 -0
  130. package/dist/ui/elements/select.js +2 -2
  131. package/dist/ui/elements/select.tsx +42 -0
  132. package/dist/ui/elements/select.works.ts +5 -0
  133. package/dist/ui/elements/separator.js +1 -1
  134. package/dist/ui/elements/separator.tsx +30 -0
  135. package/dist/ui/elements/skeleton.js +1 -1
  136. package/dist/ui/elements/skeleton.tsx +30 -0
  137. package/dist/ui/elements/slider.js +1 -1
  138. package/dist/ui/elements/slider.tsx +33 -0
  139. package/dist/ui/elements/slider.works.ts +5 -0
  140. package/dist/ui/elements/sparkline.js +1 -1
  141. package/dist/ui/elements/sparkline.tsx +36 -0
  142. package/dist/ui/elements/sparkline.works.ts +32 -0
  143. package/dist/ui/elements/spinner.js +1 -1
  144. package/dist/ui/elements/spinner.tsx +32 -0
  145. package/dist/ui/elements/stack.js +1 -1
  146. package/dist/ui/elements/stack.tsx +28 -0
  147. package/dist/ui/elements/stat.js +1 -1
  148. package/dist/ui/elements/stat.tsx +35 -0
  149. package/dist/ui/elements/stat.works.ts +14 -0
  150. package/dist/ui/elements/surface.js +1 -1
  151. package/dist/ui/elements/surface.tsx +35 -0
  152. package/dist/ui/elements/surface.works.ts +9 -0
  153. package/dist/ui/elements/table.js +2 -2
  154. package/dist/ui/elements/table.tsx +61 -0
  155. package/dist/ui/elements/table.works.ts +30 -0
  156. package/dist/ui/elements/tabs.js +2 -2
  157. package/dist/ui/elements/tabs.tsx +47 -0
  158. package/dist/ui/elements/tabs.works.ts +84 -0
  159. package/dist/ui/elements/text.js +1 -1
  160. package/dist/ui/elements/text.tsx +32 -0
  161. package/dist/ui/elements/textarea.js +1 -1
  162. package/dist/ui/elements/textarea.tsx +37 -0
  163. package/dist/ui/elements/textarea.works.ts +5 -0
  164. package/dist/ui/elements/time.js +1 -1
  165. package/dist/ui/elements/time.tsx +34 -0
  166. package/dist/ui/elements/time.works.ts +25 -0
  167. package/dist/ui/elements/toggle.js +1 -1
  168. package/dist/ui/elements/toggle.tsx +33 -0
  169. package/dist/ui/elements/toggle.works.ts +5 -0
  170. package/dist/ui/elements/tooltip.js +1 -1
  171. package/dist/ui/elements/tooltip.tsx +39 -0
  172. package/dist/ui/elements/tooltip.works.ts +77 -0
  173. package/dist/ui/elements/value.js +1 -1
  174. package/dist/ui/elements/value.tsx +30 -0
  175. package/dist/ui/elements/value.works.ts +9 -0
  176. package/dist/ui/elements.ts +127 -0
  177. package/dist/ui/focus-scope.ts +121 -0
  178. package/dist/ui/fonts.ts +355 -0
  179. package/dist/ui/generators/dev.ts +79 -0
  180. package/dist/ui/generators/elements.d.ts +1 -11
  181. package/dist/ui/generators/elements.js +43 -13
  182. package/dist/ui/generators/elements.ts +794 -0
  183. package/dist/ui/generators/i18n.ts +301 -0
  184. package/dist/ui/generators/icons.ts +214 -0
  185. package/dist/ui/generators/react.js +10 -3
  186. package/dist/ui/generators/react.ts +268 -0
  187. package/dist/ui/generators/routes.d.ts +1 -0
  188. package/dist/ui/generators/routes.js +89 -69
  189. package/dist/ui/generators/routes.ts +739 -0
  190. package/dist/ui/generators/show.ts +259 -0
  191. package/dist/ui/generators/styles.js +1 -1
  192. package/dist/ui/generators/styles.ts +670 -0
  193. package/dist/ui/generators/variants.ts +205 -0
  194. package/dist/ui/i18n/catalogs/en.ts +191 -0
  195. package/dist/ui/i18n/format.ts +221 -0
  196. package/dist/ui/i18n/locales.ts +33 -0
  197. package/dist/ui/i18n/parse.ts +315 -0
  198. package/dist/ui/i18n/translator.ts +31 -0
  199. package/dist/ui/i18n/types.ts +96 -0
  200. package/dist/ui/icons.ts +331 -0
  201. package/dist/ui/islands/map.ts +58 -0
  202. package/dist/ui/localizations.ts +75 -0
  203. package/dist/ui/makers/message.ts +292 -0
  204. package/dist/ui/material.ts +236 -0
  205. package/dist/ui/materials/mat_bordered.ts +13 -0
  206. package/dist/ui/materials/mat_ceramic.ts +13 -0
  207. package/dist/ui/materials/mat_clay.ts +13 -0
  208. package/dist/ui/materials/mat_elevated.ts +24 -0
  209. package/dist/ui/materials/mat_flat.ts +34 -0
  210. package/dist/ui/materials/mat_glass.ts +20 -0
  211. package/dist/ui/materials/mat_lit.ts +13 -0
  212. package/dist/ui/materials/mat_moulded.ts +11 -0
  213. package/dist/ui/materials.ts +206 -0
  214. package/dist/ui/modes.ts +44 -0
  215. package/dist/ui/navigation.tsx +32 -0
  216. package/dist/ui/positioning.ts +90 -0
  217. package/dist/ui/presented.tsx +119 -0
  218. package/dist/ui/provider.d.ts +2 -0
  219. package/dist/ui/provider.js +7 -1
  220. package/dist/ui/provider.tsx +182 -0
  221. package/dist/ui/render-loop.ts +133 -0
  222. package/dist/ui/roving-focus.ts +161 -0
  223. package/dist/ui/screens.js +17 -8
  224. package/dist/ui/screens.ts +142 -0
  225. package/dist/ui/scroll-lock.ts +28 -0
  226. package/dist/ui/series.ts +97 -0
  227. package/dist/ui/signals.ts +386 -0
  228. package/dist/ui/styleguide.ts +231 -0
  229. package/dist/ui/styleguides.ts +374 -0
  230. package/dist/ui/themes.ts +22 -0
  231. package/dist/ui/token.js +1 -1
  232. package/dist/ui/token.ts +78 -0
  233. package/dist/ui/tokens/shadow.ts +25 -0
  234. package/dist/ui/tokens.ts +235 -0
  235. package/dist/ui/validators.ts +232 -0
  236. package/dist/ui/variant.d.ts +1 -0
  237. package/dist/ui/variant.js +3 -0
  238. package/dist/ui/variant.ts +352 -0
  239. package/dist/ui/variants/var_avatar-round.ts +14 -0
  240. package/dist/ui/variants/var_bubble-me.ts +14 -0
  241. package/dist/ui/variants/var_bubble-them.ts +13 -0
  242. package/dist/ui/variants/var_button-border.ts +76 -0
  243. package/dist/ui/variants/var_button-cta.ts +76 -0
  244. package/dist/ui/variants/var_button-destructive-ghost.ts +72 -0
  245. package/dist/ui/variants/var_button-destructive.ts +76 -0
  246. package/dist/ui/variants/var_button-fill.ts +76 -0
  247. package/dist/ui/variants/var_button-ghost.ts +72 -0
  248. package/dist/ui/variants/var_button-glass.ts +55 -0
  249. package/dist/ui/variants/var_button-normal.ts +76 -0
  250. package/dist/ui/variants/var_button-quiet.ts +74 -0
  251. package/dist/ui/variants/var_card.ts +17 -0
  252. package/dist/ui/variants/var_chip.ts +13 -0
  253. package/dist/ui/variants/var_choice.ts +15 -0
  254. package/dist/ui/variants/var_framed.ts +10 -0
  255. package/dist/ui/variants/var_glyph-plain.ts +11 -0
  256. package/dist/ui/variants/var_input.ts +43 -0
  257. package/dist/ui/variants/var_link-inline.ts +29 -0
  258. package/dist/ui/variants/var_list-row.ts +34 -0
  259. package/dist/ui/variants/var_rule.ts +10 -0
  260. package/dist/ui/variants/var_skeleton-block.ts +12 -0
  261. package/dist/ui/variants/var_spinner-turning.ts +12 -0
  262. package/dist/ui/variants/var_stat-tile.ts +13 -0
  263. package/dist/ui/variants/var_tab-bar.ts +15 -0
  264. package/dist/ui/variants/var_tap-target.ts +25 -0
  265. package/dist/ui/variants.ts +64 -0
  266. package/dist/ui/wrappers.d.ts +4 -1
  267. package/dist/ui/wrappers.js +60 -20
  268. package/dist/ui/wrappers.tsx +426 -0
  269. package/package.json +8 -2
@@ -0,0 +1,182 @@
1
+ 'use client'
2
+
3
+ import { useCallback, useContext, useEffect, useMemo, useSyncExternalStore, type ReactNode } from 'react'
4
+
5
+ import { en } from './i18n/catalogs/en.ts'
6
+ import { createTranslator } from './i18n/translator.ts'
7
+ import { EDGE_SCROLL_DEFAULTS } from './drag.ts'
8
+
9
+ import { createCollector, INERT_COLLECTOR } from './collector.ts'
10
+ import { Clock, Ticker } from './door.ts'
11
+ import { UiContext, UiProvider, type Appearance, type UiConfig } from './wrappers.tsx'
12
+
13
+ export interface ThreadUiProps {
14
+ readonly children: ReactNode
15
+
16
+ readonly palette?: { readonly accent: string; readonly surface: string }
17
+
18
+ readonly mode?: 'light' | 'dark'
19
+ readonly locale?: string
20
+
21
+ readonly wording?: Readonly<Record<string, Readonly<Record<string, string>>>>
22
+
23
+ readonly actor?: { readonly id: string | null; readonly roles: readonly string[]; readonly entitlements: readonly string[] }
24
+
25
+ readonly asking?: readonly { readonly id: string; readonly selector: string; readonly when?: string }[]
26
+
27
+ readonly permits?: (action: string) => boolean
28
+
29
+ readonly platform?: string
30
+
31
+ readonly telemetry?: {
32
+
33
+ readonly endpoint?: string
34
+ readonly allowed: () => boolean
35
+
36
+ readonly sample?: number
37
+ }
38
+
39
+ readonly dispatch?: (name: string, input?: Readonly<Record<string, unknown>>) => Promise<unknown>
40
+
41
+ readonly navigateTo?: (screen: string) => void
42
+
43
+ readonly config?: UiConfig
44
+ }
45
+
46
+ function useMedia(query: string, fallback: boolean): boolean {
47
+
48
+ const subscribe = useCallback(
49
+ (onChange: () => void) => {
50
+ if (typeof globalThis.matchMedia !== 'function') return () => {}
51
+ const list = globalThis.matchMedia(query)
52
+ list.addEventListener('change', onChange)
53
+ return () => list.removeEventListener('change', onChange)
54
+ },
55
+ [query],
56
+ )
57
+
58
+ const read = useCallback(
59
+ () => (typeof globalThis.matchMedia === 'function' ? globalThis.matchMedia(query).matches : fallback),
60
+ [query, fallback],
61
+ )
62
+
63
+ return useSyncExternalStore(subscribe, read, () => fallback)
64
+ }
65
+
66
+ function readPalette(): { accent: string; surface: string } {
67
+ if (typeof globalThis.getComputedStyle !== 'function' || typeof globalThis.document !== 'object') {
68
+ return { accent: '', surface: '' }
69
+ }
70
+ const style = globalThis.getComputedStyle(globalThis.document.documentElement)
71
+ const read = (name: string): string => style.getPropertyValue(name).trim()
72
+ return {
73
+ accent: read('--thread-palette-accent'),
74
+ surface: read('--thread-palette-surface'),
75
+ }
76
+ }
77
+
78
+ function readEdgeScroll(): { band: number; speed: number } {
79
+ if (typeof globalThis.getComputedStyle !== 'function' || typeof globalThis.document !== 'object') return { ...EDGE_SCROLL_DEFAULTS }
80
+ const style = globalThis.getComputedStyle(globalThis.document.documentElement)
81
+ const read = (name: string, fallback: number): number => {
82
+ const value = Number.parseFloat(style.getPropertyValue(name))
83
+ return Number.isFinite(value) ? value : fallback
84
+ }
85
+ return {
86
+ band: read('--thread-edge-scroll-band', EDGE_SCROLL_DEFAULTS.band),
87
+ speed: read('--thread-edge-scroll-speed', EDGE_SCROLL_DEFAULTS.speed),
88
+ }
89
+ }
90
+
91
+ export function ThreadUi(props: ThreadUiProps): ReactNode {
92
+
93
+ const existing = useContext(UiContext)
94
+ const opinionated =
95
+ props.mode !== undefined ||
96
+ props.locale !== undefined ||
97
+ props.palette !== undefined ||
98
+ props.platform !== undefined ||
99
+ props.telemetry !== undefined ||
100
+ props.wording !== undefined ||
101
+ props.config !== undefined
102
+
103
+ const prefersDark = useMedia('(prefers-color-scheme: dark)', false)
104
+ const reduceMotion = useMedia('(prefers-reduced-motion: reduce)', false)
105
+
106
+ const appearance: Appearance = {
107
+ mode: props.mode ?? (prefersDark ? 'dark' : 'light'),
108
+ reduceMotion,
109
+ }
110
+
111
+ const language = props.locale ?? 'en'
112
+ const translate = useMemo(() => {
113
+ const held = props.wording?.[language]
114
+ if (props.wording !== undefined && held === undefined) {
115
+ throw new Error(
116
+ `This app was handed wording and none of it is in "${language}". It has ` +
117
+ `${Object.keys(props.wording).sort().join(', ')} — declare the language in ` +
118
+ '`_threads/localizations.ts` and run `thread i18n`, or ask for one it is in.',
119
+ )
120
+ }
121
+ return createTranslator(held ?? en, { locale: language })
122
+ }, [language, props.wording])
123
+
124
+ const clock = useMemo(() => new Clock(), [])
125
+
126
+ const ticker = useMemo(() => new Ticker(), [])
127
+
128
+ const telemetryOff = props.telemetry === undefined
129
+
130
+ const collector = useMemo(() => {
131
+ if (props.telemetry === undefined) return INERT_COLLECTOR
132
+ return createCollector({
133
+ endpoint: props.telemetry.endpoint ?? '/obs/client',
134
+ allowed: props.telemetry.allowed,
135
+ ...(props.telemetry.sample === undefined ? {} : { sample: props.telemetry.sample }),
136
+ ...(typeof navigator === 'object' && typeof navigator.sendBeacon === 'function'
137
+ ? { beacon: (url: string, body: string) => navigator.sendBeacon(url, new Blob([body], { type: 'application/json' })) }
138
+ : {}),
139
+ })
140
+
141
+ }, [telemetryOff, props.telemetry?.endpoint, props.telemetry?.sample, props.telemetry?.allowed])
142
+
143
+ useEffect(() => () => collector.stop(), [collector])
144
+
145
+ const gate = props.permits
146
+ const asked = props.asking
147
+
148
+ const holding = useMemo(
149
+ () =>
150
+ gate === undefined
151
+ ? []
152
+ : (asked ?? []).filter((one) => one.when !== undefined && !gate(one.when)).map((one) => one.id),
153
+ [gate, asked],
154
+ )
155
+
156
+ const derived: UiConfig = useMemo(
157
+ () => ({
158
+ palette: props.palette ?? readPalette(),
159
+ appearance,
160
+ clock,
161
+ ticker,
162
+
163
+ now: () => Date.now(),
164
+ edgeScroll: readEdgeScroll(),
165
+
166
+ ...(props.actor === undefined ? {} : { actor: props.actor }),
167
+ ...(props.asking === undefined ? {} : { asking: props.asking }),
168
+ ...(holding.length === 0 ? {} : { holding }),
169
+ ...(props.platform === undefined ? {} : { platform: props.platform }),
170
+ ...(props.dispatch === undefined ? {} : { dispatch: props.dispatch }),
171
+ ...(props.navigateTo === undefined ? {} : { navigateTo: props.navigateTo }),
172
+ collector,
173
+ translate: (key, params) => translate(key as never, params as never),
174
+ }),
175
+
176
+ [props.actor, props.asking, props.palette, props.platform, props.dispatch, props.navigateTo, holding, appearance.mode, appearance.reduceMotion, clock, ticker, collector, translate],
177
+ )
178
+
179
+ if (existing !== null && !opinionated) return props.children
180
+
181
+ return <UiProvider config={props.config ?? derived}>{props.children}</UiProvider>
182
+ }
@@ -0,0 +1,133 @@
1
+ 'use client'
2
+
3
+ import { useRef } from 'react'
4
+
5
+ const RENDER_LIMIT = 40
6
+
7
+ const WINDOW_MS = 1000
8
+
9
+ const COMPARE_DEPTH = 3
10
+
11
+ function armed(): boolean {
12
+
13
+ return process.env.NODE_ENV !== 'production'
14
+ }
15
+
16
+ export interface RenderLoop {
17
+
18
+ readonly component: string
19
+ readonly renders: number
20
+ readonly withinMs: number
21
+
22
+ readonly unstable: readonly string[]
23
+
24
+ readonly differing: readonly string[]
25
+ }
26
+
27
+ export class RenderLoopError extends Error {
28
+ readonly loop: RenderLoop
29
+
30
+ constructor(loop: RenderLoop) {
31
+ super(describe(loop))
32
+ this.name = 'RenderLoopError'
33
+ this.loop = loop
34
+ }
35
+ }
36
+
37
+ function describe(loop: RenderLoop): string {
38
+ const head = `${loop.component} rendered ${loop.renders} times in ${loop.withinMs}ms, which is a render loop.`
39
+
40
+ if (loop.unstable.length > 0) {
41
+ const props = loop.unstable.join(', ')
42
+ return (
43
+ `${head} ${props} arrived with a new identity every render while its value stayed equal, so ` +
44
+ `every render caused the next one. Return the same object for the same answer — that is the ` +
45
+ `read layer's job (ADR-180 rule 10), not the component's — or compute the value inside render ` +
46
+ `instead of syncing it into state from an effect (rule 11).`
47
+ )
48
+ }
49
+
50
+ if (loop.differing.length > 0) {
51
+ return (
52
+ `${head} ${loop.differing.join(', ')} changed VALUE on every render, so this is not an identity ` +
53
+ `problem: something is writing during render. Move the write to an event or an action.`
54
+ )
55
+ }
56
+
57
+ return (
58
+ `${head} No prop changed at all, so the re-render comes from state or context inside the ` +
59
+ `component — look for a setter called from an effect body (ADR-180 rule 11).`
60
+ )
61
+ }
62
+
63
+ function equalValue(a: unknown, b: unknown, depth = 0): boolean {
64
+ if (Object.is(a, b)) return true
65
+ if (depth >= COMPARE_DEPTH) return false
66
+ if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false
67
+
68
+ if (Array.isArray(a) || Array.isArray(b)) {
69
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false
70
+ return a.every((item, index) => equalValue(item, b[index], depth + 1))
71
+ }
72
+
73
+ const left = a as Record<string, unknown>
74
+ const right = b as Record<string, unknown>
75
+ const keys = Object.keys(left)
76
+ if (keys.length !== Object.keys(right).length) return false
77
+ return keys.every((key) => key in right && equalValue(left[key], right[key], depth + 1))
78
+ }
79
+
80
+ function compare(
81
+ previous: Record<string, unknown>,
82
+ next: Record<string, unknown>,
83
+ ): { unstable: string[]; differing: string[] } {
84
+ const unstable: string[] = []
85
+ const differing: string[] = []
86
+
87
+ for (const key of [...new Set([...Object.keys(previous), ...Object.keys(next)])].sort()) {
88
+ if (Object.is(previous[key], next[key])) continue
89
+ if (equalValue(previous[key], next[key])) unstable.push(key)
90
+ else differing.push(key)
91
+ }
92
+
93
+ return { unstable, differing }
94
+ }
95
+
96
+ export function useRenderLoopDetector(component: string, props: Record<string, unknown>): void {
97
+ const renders = useRef(0)
98
+ const openedAt = useRef(0)
99
+ const previous = useRef<Record<string, unknown> | undefined>(undefined)
100
+ const tripped = useRef<RenderLoop | undefined>(undefined)
101
+
102
+ if (armed()) {
103
+
104
+ if (tripped.current !== undefined) throw new RenderLoopError(tripped.current)
105
+
106
+ const now = Date.now()
107
+ const elapsed = now - openedAt.current
108
+
109
+ if (openedAt.current === 0 || elapsed > WINDOW_MS) {
110
+ openedAt.current = now
111
+ renders.current = 0
112
+ }
113
+
114
+ renders.current += 1
115
+
116
+ if (renders.current > RENDER_LIMIT) {
117
+ const loop: RenderLoop = {
118
+ component,
119
+ renders: renders.current,
120
+
121
+ withinMs: now - openedAt.current,
122
+ ...compare(previous.current ?? {}, props),
123
+ }
124
+
125
+ tripped.current = loop
126
+ renders.current = 0
127
+ openedAt.current = now
128
+ throw new RenderLoopError(loop)
129
+ }
130
+ }
131
+
132
+ previous.current = props
133
+ }
@@ -0,0 +1,161 @@
1
+ 'use client'
2
+
3
+ import { useCallback, useEffect, useRef, type RefObject } from 'react'
4
+
5
+ export const ITEM_ATTR = 'data-roving-item'
6
+
7
+ export interface RovingOptions {
8
+ readonly orientation: 'vertical' | 'horizontal'
9
+
10
+ readonly wrap?: boolean
11
+ readonly active?: boolean
12
+ }
13
+
14
+ interface Item {
15
+ readonly element: HTMLElement
16
+ readonly disabled: boolean
17
+ readonly text: string
18
+ }
19
+
20
+ function items(root: HTMLElement | null): readonly Item[] {
21
+ if (root === null) return []
22
+ return [...root.querySelectorAll<HTMLElement>(`[${ITEM_ATTR}]`)].map((element) => ({
23
+ element,
24
+
25
+ disabled: element.hasAttribute('disabled') || element.getAttribute('aria-disabled') === 'true',
26
+ text: (element.textContent ?? '').trim().toLowerCase(),
27
+ }))
28
+ }
29
+
30
+ function step(list: readonly Item[], from: number, direction: 1 | -1, wrap: boolean): number {
31
+ const count = list.length
32
+ if (count === 0) return -1
33
+ let index = from
34
+ for (let moved = 0; moved < count; moved += 1) {
35
+ const next = index + direction
36
+ if (next < 0 || next >= count) {
37
+ if (!wrap) return from
38
+ index = direction === 1 ? 0 : count - 1
39
+ } else {
40
+ index = next
41
+ }
42
+ if (!(list[index] as Item).disabled) return index
43
+ }
44
+ return from
45
+ }
46
+
47
+ function firstEnabled(list: readonly Item[], from = 0): number {
48
+ for (let i = from; i < list.length; i += 1) if (!(list[i] as Item).disabled) return i
49
+ return -1
50
+ }
51
+
52
+ function lastEnabled(list: readonly Item[]): number {
53
+ for (let i = list.length - 1; i >= 0; i -= 1) if (!(list[i] as Item).disabled) return i
54
+ return -1
55
+ }
56
+
57
+ function isRtl(element: HTMLElement): boolean {
58
+ const declared = element.closest('[dir]')?.getAttribute('dir')?.toLowerCase()
59
+ if (declared === 'rtl') return true
60
+ if (declared === 'ltr') return false
61
+ return getComputedStyle(element).direction === 'rtl'
62
+ }
63
+
64
+ const TYPEAHEAD_MS = 1000
65
+
66
+ export function typeaheadMatch(list: readonly Item[], buffer: string, current: number): number {
67
+ const enabled = list.map((item, index) => ({ item, index })).filter(({ item }) => !item.disabled)
68
+ if (enabled.length === 0 || buffer.length === 0) return -1
69
+
70
+ const repeated = buffer.length > 1 && [...buffer].every((c) => c === buffer[0])
71
+ const prefix = repeated ? (buffer[0] as string) : buffer
72
+
73
+ const startAt = enabled.findIndex(({ index }) => index === current)
74
+ for (let i = 0; i < enabled.length; i += 1) {
75
+ const candidate = enabled[(startAt + 1 + i) % enabled.length] as { item: Item; index: number }
76
+ if (candidate.item.text.startsWith(prefix)) return candidate.index
77
+ }
78
+ return -1
79
+ }
80
+
81
+ export function useRovingFocus(ref: RefObject<HTMLElement | null>, options: RovingOptions): void {
82
+ const { orientation, wrap = false, active = true } = options
83
+ const indexRef = useRef(0)
84
+ const bufferRef = useRef('')
85
+ const timerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
86
+
87
+ const applyTabIndex = useCallback(
88
+ (list: readonly Item[], index: number): void => {
89
+ list.forEach((item, i) => {
90
+ item.element.setAttribute('tabindex', i === index ? '0' : '-1')
91
+ })
92
+ },
93
+ [],
94
+ )
95
+
96
+ useEffect(() => {
97
+ if (!active) return
98
+ const root = ref.current
99
+ if (root === null) return
100
+
101
+ const list = items(root)
102
+ const start = firstEnabled(list)
103
+ indexRef.current = start === -1 ? 0 : start
104
+ applyTabIndex(list, indexRef.current)
105
+
106
+ const move = (to: number): void => {
107
+ const current = items(root)
108
+ if (to < 0 || to >= current.length) return
109
+ indexRef.current = to
110
+ applyTabIndex(current, to)
111
+ ;(current[to] as Item).element.focus()
112
+ }
113
+
114
+ const onKeyDown = (event: KeyboardEvent): void => {
115
+ const current = items(root)
116
+ if (current.length === 0) return
117
+
118
+ const rtl = isRtl(root)
119
+ const forward = orientation === 'horizontal' ? (rtl ? 'ArrowLeft' : 'ArrowRight') : 'ArrowDown'
120
+ const backward = orientation === 'horizontal' ? (rtl ? 'ArrowRight' : 'ArrowLeft') : 'ArrowUp'
121
+
122
+ if (event.key === forward) {
123
+ event.preventDefault()
124
+ move(step(current, indexRef.current, 1, wrap))
125
+ return
126
+ }
127
+ if (event.key === backward) {
128
+ event.preventDefault()
129
+ move(step(current, indexRef.current, -1, wrap))
130
+ return
131
+ }
132
+ if (event.key === 'Home') {
133
+ event.preventDefault()
134
+ move(firstEnabled(current))
135
+ return
136
+ }
137
+ if (event.key === 'End') {
138
+ event.preventDefault()
139
+ move(lastEnabled(current))
140
+ return
141
+ }
142
+
143
+ if (event.key.length !== 1 || event.ctrlKey || event.metaKey || event.altKey) return
144
+ event.preventDefault()
145
+ bufferRef.current += event.key.toLowerCase()
146
+ if (timerRef.current !== undefined) clearTimeout(timerRef.current)
147
+ timerRef.current = setTimeout(() => {
148
+ bufferRef.current = ''
149
+ }, TYPEAHEAD_MS)
150
+
151
+ const found = typeaheadMatch(current, bufferRef.current, indexRef.current)
152
+ if (found !== -1) move(found)
153
+ }
154
+
155
+ root.addEventListener('keydown', onKeyDown)
156
+ return () => {
157
+ root.removeEventListener('keydown', onKeyDown)
158
+ if (timerRef.current !== undefined) clearTimeout(timerRef.current)
159
+ }
160
+ }, [active, orientation, wrap, ref, applyTabIndex])
161
+ }
@@ -8,11 +8,11 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
8
8
  };
9
9
  import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
10
10
  import { basename, dirname, join } from 'node:path';
11
- import { stdout } from 'node:process';
11
+ import { argv, stdout } from 'node:process';
12
12
  import { pathToFileURL } from 'node:url';
13
13
  import { exit, invoked } from "@prestack/thread/core/invoked";
14
14
  import { findTree, inTree } from "@prestack/thread/core/tree";
15
- import { undeclared } from "./compositions.js";
15
+ import { HOME, settled, undeclared } from "./compositions.js";
16
16
  import { PRESENTATIONS } from "./door.js";
17
17
  const here = 'thread/ui/screens.ts';
18
18
  const said = [
@@ -70,7 +70,7 @@ function starter(row) {
70
70
  const name = nameOf(row.at);
71
71
  return `'use client'
72
72
 
73
- import { screen } from './wrappers.tsx'
73
+ import { screen } from '${HOME}/wrappers'
74
74
 
75
75
  export const ${name} = screen('${row.id}', {}, () => (
76
76
  <>
@@ -94,13 +94,22 @@ async function run() {
94
94
  return 1;
95
95
  }
96
96
  const rows = held.SCREEN.declares ?? [];
97
+ const checking = argv.includes('--check');
97
98
  const made = seeds(rows, (where) => existsSync(join(tree.at, where)));
98
- for (const one of made) {
99
- mkdirSync(dirname(join(tree.at, one.path)), { recursive: true });
100
- writeFileSync(join(tree.at, one.path), one.content, 'utf8');
101
- stdout.write(`thread: seeded ${one.path}\n`);
99
+ const unseeded = made[0];
100
+ if (checking && unseeded !== undefined) {
101
+ stdout.write(`thread: ${unseeded.path} is declared and the file is not there yet. A row names a file, and ` +
102
+ '`--check` never writes one — run `thread screens` and commit what it seeds.\n');
103
+ return 1;
104
+ }
105
+ if (!checking) {
106
+ for (const one of made) {
107
+ mkdirSync(dirname(join(tree.at, one.path)), { recursive: true });
108
+ writeFileSync(join(tree.at, one.path), one.content, 'utf8');
109
+ stdout.write(`thread: seeded ${one.path}\n`);
110
+ }
102
111
  }
103
- const loose = await undeclared(tree, rows);
112
+ const loose = [...(await undeclared(tree, rows)), ...settled(tree.at, rows, checking, 'thread screens', new Set())];
104
113
  for (const one of loose)
105
114
  stdout.write(`thread: ${one}\n`);
106
115
  stdout.write(`thread: ${String(rows.length)} screen(s) declared, ${String(made.length)} seeded, ` +
@@ -0,0 +1,142 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
2
+ import { basename, dirname, join } from 'node:path'
3
+ import { argv, stdout } from 'node:process'
4
+ import { pathToFileURL } from 'node:url'
5
+
6
+ import { exit, invoked } from '@prestack/thread/core/invoked'
7
+ import type { Threaded } from '@prestack/thread/core/thread'
8
+ import { findTree, inTree } from '@prestack/thread/core/tree'
9
+
10
+ import { HOME, settled, undeclared } from './compositions.ts'
11
+ import { PRESENTATIONS } from './door.ts'
12
+
13
+ const here = 'thread/ui/screens.ts'
14
+
15
+ const said = [
16
+ {
17
+ on: '2026-08-25',
18
+ said: 'Screens are basically compositions arranged in a screen. And this is where we have breakpoint, etc. Compositions maybe also have breakpoints.',
19
+ },
20
+ {
21
+ on: '2026-08-25',
22
+ said: 'Arrangement is a real fucking thing. IT IS HOW THE STUFF LOOKS IN A SCREEN',
23
+ },
24
+ {
25
+ on: '2026-08-25',
26
+ said: 'Both',
27
+ },
28
+ {
29
+ on: '2026-09-09',
30
+ said: 'We have one screen or more. A button navigates to other screen. From THAT the routes are generated.',
31
+ },
32
+ ]
33
+
34
+ export const declares: readonly Threaded[] = []
35
+
36
+ export const SCREENS: Threaded & { readonly files: string } = {
37
+ id: 'screen',
38
+ at: here,
39
+ files: 'screens/scr_*.tsx',
40
+ describes:
41
+ 'Compositions arranged, and the way it is presented: page, sheet, popover, overlay or toast. Where it lives is never typed. The first page an app declares answers `/`, every other page answers its own name, and a sheet, popover, overlay or toast has no route, because a press opens it where somebody already is. The arrangement is the whole of what a screen adds, because how things sit on a screen is declared rather than markup written twice. Each row names a file prefixed `scr_`, sets the properties that lay its compositions out, and may set them differently under a mode, which is where a breakpoint lives: a width is a media query the same way dark is. A composition may set its own arrangement too. The framework ships none, because what an app is made of is the app’s, and this row says what a screen is, never what is inside it.',
42
+ version: 1,
43
+ cites: said,
44
+ declares,
45
+ }
46
+
47
+ const PASCAL = (id: string): string =>
48
+ id
49
+ .split(/[-_]/)
50
+ .map((part) => `${part.charAt(0).toUpperCase()}${part.slice(1)}`)
51
+ .join('')
52
+
53
+ export function nameOf(at: string): string {
54
+ const file = at.split('\\').join('/').split('/').at(-1) ?? at
55
+ return PASCAL(file.replace(/^scr_/, '').replace(/\.[^.]+$/, ''))
56
+ }
57
+
58
+ export const PAGE = PRESENTATIONS[0]
59
+
60
+ const PRESENTED = /screen\(\s*'[^']*'\s*,\s*\{[^}]*as:\s*'([^']*)'/
61
+
62
+ export function presentedIn(source: string): string {
63
+ return PRESENTED.exec(source)?.[1] ?? PAGE
64
+ }
65
+
66
+ export function routeFor(id: string, entry: boolean): string {
67
+ return entry ? '/' : `/${id}`
68
+ }
69
+
70
+ export function pageAt(route: string): string {
71
+ const trimmed = route.replace(/^\/+|\/+$/g, '')
72
+ return trimmed === '' ? `app/page.${'tsx'}` : `app/${trimmed}/page.${'tsx'}`
73
+ }
74
+
75
+ export function entryAmong(rows: readonly { readonly id: string }[], shownAs: (id: string) => string): string | undefined {
76
+ return rows.find((row) => shownAs(row.id) === PAGE)?.id
77
+ }
78
+
79
+ function starter(row: Threaded): string {
80
+ const name = nameOf(row.at)
81
+ return `'use client'
82
+
83
+ import { screen } from '${HOME}/wrappers'
84
+
85
+ export const ${name} = screen('${row.id}', {}, () => (
86
+ <>
87
+ </>
88
+ ))
89
+ `
90
+ }
91
+
92
+ export function seeds(rows: readonly Threaded[], held: (at: string) => boolean): readonly { readonly path: string; readonly content: string }[] {
93
+ return rows.filter((row) => !held(row.at)).map((row) => ({ path: row.at, content: starter(row) }))
94
+ }
95
+
96
+ async function run(): Promise<number> {
97
+ const tree = findTree()
98
+ const at = inTree(tree, 'screens.ts')
99
+ if (!existsSync(at)) {
100
+ stdout.write(`thread: this tree has no ${tree.name}/${basename(at)}, so it declares no screen and there is nothing to seed or check.\n`)
101
+ return 0
102
+ }
103
+ const held = (await import(pathToFileURL(at).href)) as { SCREEN?: { readonly declares?: readonly Threaded[] } }
104
+
105
+ if (held.SCREEN === undefined) {
106
+ stdout.write(`thread: ${tree.name}/screens.ts exports no \`SCREEN\`, so nothing declares a screen here.\n`)
107
+ return 1
108
+ }
109
+
110
+ const rows = held.SCREEN.declares ?? []
111
+ const checking = argv.includes('--check')
112
+ const made = seeds(rows, (where) => existsSync(join(tree.at, where)))
113
+
114
+ const unseeded = made[0]
115
+ if (checking && unseeded !== undefined) {
116
+ stdout.write(
117
+ `thread: ${unseeded.path} is declared and the file is not there yet. A row names a file, and ` +
118
+ '`--check` never writes one — run `thread screens` and commit what it seeds.\n',
119
+ )
120
+ return 1
121
+ }
122
+
123
+ if (!checking) {
124
+ for (const one of made) {
125
+ mkdirSync(dirname(join(tree.at, one.path)), { recursive: true })
126
+ writeFileSync(join(tree.at, one.path), one.content, 'utf8')
127
+ stdout.write(`thread: seeded ${one.path}\n`)
128
+ }
129
+ }
130
+
131
+ const loose = [...(await undeclared(tree, rows)), ...settled(tree.at, rows, checking, 'thread screens', new Set<string>())]
132
+ for (const one of loose) stdout.write(`thread: ${one}\n`)
133
+
134
+ stdout.write(
135
+ `thread: ${String(rows.length)} screen(s) declared, ${String(made.length)} seeded, ` +
136
+ `${String(rows.length - made.length)} already written, ` +
137
+ `${loose.length === 0 ? 'none reaching anything undeclared' : `${String(loose.length)} reaching something undeclared`}\n`,
138
+ )
139
+ return loose.length === 0 ? 0 : 1
140
+ }
141
+
142
+ if (invoked(import.meta.url)) exit(await run())
@@ -0,0 +1,28 @@
1
+ import { nameOf } from './token.ts'
2
+
3
+
4
+
5
+ let held = 0
6
+ let previous: string | null = null
7
+
8
+ export function lockScroll(): () => void {
9
+ if (held === 0) {
10
+ previous = document.body.style.overflow
11
+ document.body.style.overflow = `var(${nameOf('overflow-hidden')})`
12
+ }
13
+ held += 1
14
+
15
+ let released = false
16
+ return () => {
17
+ if (released) return
18
+ released = true
19
+ held -= 1
20
+ if (held > 0) return
21
+ document.body.style.overflow = previous ?? ''
22
+ previous = null
23
+ }
24
+ }
25
+
26
+ export function scrollLocks(): number {
27
+ return held
28
+ }