@hozo/primitives 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/dist/flat-list.native.d.ts +17 -0
  4. package/dist/flat-list.native.d.ts.map +1 -0
  5. package/dist/flat-list.native.js +89 -0
  6. package/dist/flat-list.native.js.map +1 -0
  7. package/dist/foundation.d.ts +104 -0
  8. package/dist/foundation.d.ts.map +1 -0
  9. package/dist/foundation.js +115 -0
  10. package/dist/foundation.js.map +1 -0
  11. package/dist/foundation.native.d.ts +59 -0
  12. package/dist/foundation.native.d.ts.map +1 -0
  13. package/dist/foundation.native.js +40 -0
  14. package/dist/foundation.native.js.map +1 -0
  15. package/dist/generated/animated.d.ts +2 -0
  16. package/dist/generated/animated.d.ts.map +1 -0
  17. package/dist/generated/animated.js +3 -0
  18. package/dist/generated/animated.js.map +1 -0
  19. package/dist/generated/backdrop-filter.d.ts +2 -0
  20. package/dist/generated/backdrop-filter.d.ts.map +1 -0
  21. package/dist/generated/backdrop-filter.js +3 -0
  22. package/dist/generated/backdrop-filter.js.map +1 -0
  23. package/dist/generated/container.d.ts +2 -0
  24. package/dist/generated/container.d.ts.map +1 -0
  25. package/dist/generated/container.js +3 -0
  26. package/dist/generated/container.js.map +1 -0
  27. package/dist/generated/flat-list.d.ts +2 -0
  28. package/dist/generated/flat-list.d.ts.map +1 -0
  29. package/dist/generated/flat-list.js +2 -0
  30. package/dist/generated/flat-list.js.map +1 -0
  31. package/dist/generated/flat-list.native.d.ts +3 -0
  32. package/dist/generated/flat-list.native.d.ts.map +1 -0
  33. package/dist/generated/flat-list.native.js +2 -0
  34. package/dist/generated/flat-list.native.js.map +1 -0
  35. package/dist/generated/grid.d.ts +3 -0
  36. package/dist/generated/grid.d.ts.map +1 -0
  37. package/dist/generated/grid.js +4 -0
  38. package/dist/generated/grid.js.map +1 -0
  39. package/dist/generated/link.d.ts +2 -0
  40. package/dist/generated/link.d.ts.map +1 -0
  41. package/dist/generated/link.js +2 -0
  42. package/dist/generated/link.js.map +1 -0
  43. package/dist/generated/link.native.d.ts +2 -0
  44. package/dist/generated/link.native.d.ts.map +1 -0
  45. package/dist/generated/link.native.js +2 -0
  46. package/dist/generated/link.native.js.map +1 -0
  47. package/dist/generated/pressable.d.ts +2 -0
  48. package/dist/generated/pressable.d.ts.map +1 -0
  49. package/dist/generated/pressable.js +2 -0
  50. package/dist/generated/pressable.js.map +1 -0
  51. package/dist/generated/pressable.native.d.ts +2 -0
  52. package/dist/generated/pressable.native.d.ts.map +1 -0
  53. package/dist/generated/pressable.native.js +2 -0
  54. package/dist/generated/pressable.native.js.map +1 -0
  55. package/dist/generated/refresh-control.d.ts +2 -0
  56. package/dist/generated/refresh-control.d.ts.map +1 -0
  57. package/dist/generated/refresh-control.js +2 -0
  58. package/dist/generated/refresh-control.js.map +1 -0
  59. package/dist/generated/refresh-control.native.d.ts +2 -0
  60. package/dist/generated/refresh-control.native.d.ts.map +1 -0
  61. package/dist/generated/refresh-control.native.js +2 -0
  62. package/dist/generated/refresh-control.native.js.map +1 -0
  63. package/dist/generated/scroll-view.d.ts +2 -0
  64. package/dist/generated/scroll-view.d.ts.map +1 -0
  65. package/dist/generated/scroll-view.js +2 -0
  66. package/dist/generated/scroll-view.js.map +1 -0
  67. package/dist/generated/scroll-view.native.d.ts +2 -0
  68. package/dist/generated/scroll-view.native.d.ts.map +1 -0
  69. package/dist/generated/scroll-view.native.js +2 -0
  70. package/dist/generated/scroll-view.native.js.map +1 -0
  71. package/dist/generated/spaced.d.ts +2 -0
  72. package/dist/generated/spaced.d.ts.map +1 -0
  73. package/dist/generated/spaced.js +3 -0
  74. package/dist/generated/spaced.js.map +1 -0
  75. package/dist/generated/text-input.d.ts +2 -0
  76. package/dist/generated/text-input.d.ts.map +1 -0
  77. package/dist/generated/text-input.js +2 -0
  78. package/dist/generated/text-input.js.map +1 -0
  79. package/dist/generated/text-input.native.d.ts +2 -0
  80. package/dist/generated/text-input.native.d.ts.map +1 -0
  81. package/dist/generated/text-input.native.js +2 -0
  82. package/dist/generated/text-input.native.js.map +1 -0
  83. package/dist/generated/text-size.d.ts +2 -0
  84. package/dist/generated/text-size.d.ts.map +1 -0
  85. package/dist/generated/text-size.js +3 -0
  86. package/dist/generated/text-size.js.map +1 -0
  87. package/dist/generated/text.d.ts +2 -0
  88. package/dist/generated/text.d.ts.map +1 -0
  89. package/dist/generated/text.js +5 -0
  90. package/dist/generated/text.js.map +1 -0
  91. package/dist/generated/view.d.ts +2 -0
  92. package/dist/generated/view.d.ts.map +1 -0
  93. package/dist/generated/view.js +2 -0
  94. package/dist/generated/view.js.map +1 -0
  95. package/dist/generated/view.native.d.ts +2 -0
  96. package/dist/generated/view.native.d.ts.map +1 -0
  97. package/dist/generated/view.native.js +2 -0
  98. package/dist/generated/view.native.js.map +1 -0
  99. package/dist/index.d.ts +188 -0
  100. package/dist/index.d.ts.map +1 -0
  101. package/dist/index.js +718 -0
  102. package/dist/index.js.map +1 -0
  103. package/dist/index.native.d.ts +22 -0
  104. package/dist/index.native.d.ts.map +1 -0
  105. package/dist/index.native.js +8 -0
  106. package/dist/index.native.js.map +1 -0
  107. package/dist/link.d.ts +320 -0
  108. package/dist/link.d.ts.map +1 -0
  109. package/dist/link.js +69 -0
  110. package/dist/link.js.map +1 -0
  111. package/dist/link.native.d.ts +33 -0
  112. package/dist/link.native.d.ts.map +1 -0
  113. package/dist/link.native.js +44 -0
  114. package/dist/link.native.js.map +1 -0
  115. package/dist/pressable.d.ts +65 -0
  116. package/dist/pressable.d.ts.map +1 -0
  117. package/dist/pressable.js +202 -0
  118. package/dist/pressable.js.map +1 -0
  119. package/dist/pressable.native.d.ts +55 -0
  120. package/dist/pressable.native.d.ts.map +1 -0
  121. package/dist/pressable.native.js +405 -0
  122. package/dist/pressable.native.js.map +1 -0
  123. package/dist/runtime-backdrop.native.d.ts +8 -0
  124. package/dist/runtime-backdrop.native.d.ts.map +1 -0
  125. package/dist/runtime-backdrop.native.js +12 -0
  126. package/dist/runtime-backdrop.native.js.map +1 -0
  127. package/dist/runtime-container.native.d.ts +41 -0
  128. package/dist/runtime-container.native.d.ts.map +1 -0
  129. package/dist/runtime-container.native.js +61 -0
  130. package/dist/runtime-container.native.js.map +1 -0
  131. package/dist/runtime-grid.d.ts +37 -0
  132. package/dist/runtime-grid.d.ts.map +1 -0
  133. package/dist/runtime-grid.js +139 -0
  134. package/dist/runtime-grid.js.map +1 -0
  135. package/dist/runtime-grid.native.d.ts +26 -0
  136. package/dist/runtime-grid.native.d.ts.map +1 -0
  137. package/dist/runtime-grid.native.js +76 -0
  138. package/dist/runtime-grid.native.js.map +1 -0
  139. package/dist/runtime-measured.d.ts +20 -0
  140. package/dist/runtime-measured.d.ts.map +1 -0
  141. package/dist/runtime-measured.js +42 -0
  142. package/dist/runtime-measured.js.map +1 -0
  143. package/dist/runtime-spacing.d.ts +9 -0
  144. package/dist/runtime-spacing.d.ts.map +1 -0
  145. package/dist/runtime-spacing.js +35 -0
  146. package/dist/runtime-spacing.js.map +1 -0
  147. package/dist/runtime-spacing.native.d.ts +12 -0
  148. package/dist/runtime-spacing.native.d.ts.map +1 -0
  149. package/dist/runtime-spacing.native.js +64 -0
  150. package/dist/runtime-spacing.native.js.map +1 -0
  151. package/dist/runtime-text-size.native.d.ts +29 -0
  152. package/dist/runtime-text-size.native.d.ts.map +1 -0
  153. package/dist/runtime-text-size.native.js +51 -0
  154. package/dist/runtime-text-size.native.js.map +1 -0
  155. package/dist/runtime-transition.native.d.ts +15 -0
  156. package/dist/runtime-transition.native.d.ts.map +1 -0
  157. package/dist/runtime-transition.native.js +156 -0
  158. package/dist/runtime-transition.native.js.map +1 -0
  159. package/dist/text-input.d.ts +40 -0
  160. package/dist/text-input.d.ts.map +1 -0
  161. package/dist/text-input.js +54 -0
  162. package/dist/text-input.js.map +1 -0
  163. package/dist/view.d.ts +42 -0
  164. package/dist/view.d.ts.map +1 -0
  165. package/dist/view.js +84 -0
  166. package/dist/view.js.map +1 -0
  167. package/dist/view.native.d.ts +8 -0
  168. package/dist/view.native.d.ts.map +1 -0
  169. package/dist/view.native.js +5 -0
  170. package/dist/view.native.js.map +1 -0
  171. package/dist/windowing.d.ts +149 -0
  172. package/dist/windowing.d.ts.map +1 -0
  173. package/dist/windowing.js +336 -0
  174. package/dist/windowing.js.map +1 -0
  175. package/package.json +160 -0
  176. package/src/flat-list.native.tsx +158 -0
  177. package/src/foundation.native.tsx +179 -0
  178. package/src/foundation.tsx +323 -0
  179. package/src/generated/animated.ts +6 -0
  180. package/src/generated/backdrop-filter.ts +5 -0
  181. package/src/generated/container.ts +7 -0
  182. package/src/generated/flat-list.native.ts +6 -0
  183. package/src/generated/flat-list.ts +7 -0
  184. package/src/generated/grid.ts +4 -0
  185. package/src/generated/link.native.ts +1 -0
  186. package/src/generated/link.ts +1 -0
  187. package/src/generated/pressable.native.ts +5 -0
  188. package/src/generated/pressable.ts +5 -0
  189. package/src/generated/refresh-control.native.ts +4 -0
  190. package/src/generated/refresh-control.ts +1 -0
  191. package/src/generated/scroll-view.native.ts +4 -0
  192. package/src/generated/scroll-view.ts +1 -0
  193. package/src/generated/spaced.ts +2 -0
  194. package/src/generated/text-input.native.ts +4 -0
  195. package/src/generated/text-input.ts +1 -0
  196. package/src/generated/text-size.ts +7 -0
  197. package/src/generated/text.ts +8 -0
  198. package/src/generated/view.native.ts +1 -0
  199. package/src/generated/view.ts +1 -0
  200. package/src/index.native.ts +24 -0
  201. package/src/index.ts +1111 -0
  202. package/src/link.native.tsx +77 -0
  203. package/src/link.ts +125 -0
  204. package/src/pressable.native.tsx +538 -0
  205. package/src/pressable.ts +358 -0
  206. package/src/runtime-backdrop.native.tsx +22 -0
  207. package/src/runtime-container.native.tsx +105 -0
  208. package/src/runtime-grid.native.tsx +143 -0
  209. package/src/runtime-grid.ts +198 -0
  210. package/src/runtime-measured.ts +42 -0
  211. package/src/runtime-spacing.native.tsx +75 -0
  212. package/src/runtime-spacing.ts +37 -0
  213. package/src/runtime-text-size.native.tsx +79 -0
  214. package/src/runtime-transition.native.tsx +183 -0
  215. package/src/text-input.ts +129 -0
  216. package/src/view.native.ts +7 -0
  217. package/src/view.ts +165 -0
  218. package/src/windowing.ts +415 -0
@@ -0,0 +1,336 @@
1
+ // Which rows a long list actually mounts, and where they sit.
2
+ //
3
+ // The Web `FlatList` bridge accepted `initialNumToRender`, `windowSize` and
4
+ // `removeClippedSubviews` and rendered every row anyway, so a thousand-row
5
+ // feed was a thousand mounted subtrees (#385). This is the part that
6
+ // decides otherwise, and it is here rather than in `./list.ts` because it
7
+ // is arithmetic: no DOM, no React, no browser, and therefore testable
8
+ // directly rather than through a rendered page.
9
+ //
10
+ // ## Where the design comes from
11
+ //
12
+ // Four other frameworks solve this, and they do not agree about how much
13
+ // to ask of the caller:
14
+ //
15
+ // | | sizing | window unit |
16
+ // |---|---|---|
17
+ // | React Native `VirtualizedList` | measured, `getItemLayout` overrides | `windowSize`, in viewports |
18
+ // | SwiftUI `LazyVStack`, Compose `LazyColumn` | intrinsic, no knobs at all | none exposed |
19
+ // | `react-window` | `itemSize` required from the caller | `overscanCount`, in items |
20
+ // | `react-virtuoso`, TanStack Virtual | measured automatically | overscan in px / in items |
21
+ //
22
+ // The two platform frameworks with the best reputation for this expose no
23
+ // tuning whatsoever -- the framework measures, and the only thing the
24
+ // caller supplies is a stable key. The React libraries that measure
25
+ // automatically are also the ones that handle variable heights well, and
26
+ // the one that demands sizes up front is the oldest design of the four.
27
+ //
28
+ // So: measure by default, treat React Native's knobs as bounds rather than
29
+ // as requirements, and let `getItemLayout` be the shortcut for a caller who
30
+ // already knows rather than a precondition for working at all.
31
+ //
32
+ // ## Why the arithmetic is React Native's rather than a new one
33
+ //
34
+ // `windowSize` has a meaning -- `(windowSize - 1)` viewports of overscan,
35
+ // split half before and half after -- and `react-native-web` ships React
36
+ // Native's own implementation of it, so a Web project using RNW today
37
+ // already gets exactly this. A prop that quietly meant something narrower
38
+ // here would be the same rule with two implementations, which is the shape
39
+ // of bug this repository keeps finding. `windowRange` is therefore a port
40
+ // of `computeWindowedRenderLimits` from
41
+ // `@react-native/virtualized-lists` 0.87, read from the installed package
42
+ // rather than from memory, minus the zoom scale (which the Web has no
43
+ // equivalent of on a scroll container).
44
+ /**
45
+ * Row lengths, measured where they have been and estimated where they have
46
+ * not.
47
+ *
48
+ * Keyed by the caller's own key rather than by index, which is the whole
49
+ * point: a feed that prepends ten rows moves every index by ten, and a
50
+ * store keyed by index would report ten rows of wrong heights and jump the
51
+ * scroll position. Compose makes the same argument for `key` on
52
+ * `LazyColumn`, and it is the one thing SwiftUI and Compose *do* ask of a
53
+ * caller.
54
+ */
55
+ export class ListMetrics {
56
+ /** Measured lengths, by key, surviving reorder and window movement. */
57
+ measured = new Map();
58
+ /** Prefix sums for the current key order; rebuilt when something changes. */
59
+ offsets = [];
60
+ keys = [];
61
+ dirty = true;
62
+ // A plain field rather than a parameter property: Node strips types
63
+ // rather than compiling them, and a parameter property is the one piece
64
+ // of TypeScript that emits code.
65
+ estimate;
66
+ constructor(estimate) {
67
+ this.estimate = estimate;
68
+ }
69
+ /** The key order to measure against. Cheap to call every render. */
70
+ setKeys(keys) {
71
+ if (keys.length === this.keys.length && keys.every((key, i) => key === this.keys[i]))
72
+ return;
73
+ this.keys = keys;
74
+ this.dirty = true;
75
+ }
76
+ /**
77
+ * Records what a row actually measured.
78
+ *
79
+ * Returns whether this changed anything, so a caller can avoid a re-render
80
+ * for a `ResizeObserver` callback that reports the size it already knew --
81
+ * which is most of them, since the observer fires on mount for every row.
82
+ */
83
+ measure(key, length) {
84
+ if (this.measured.get(key) === length)
85
+ return false;
86
+ this.measured.set(key, length);
87
+ this.dirty = true;
88
+ return true;
89
+ }
90
+ /** Forgets a measurement. For a row whose content changed identity under one key. */
91
+ forget(key) {
92
+ if (this.measured.delete(key))
93
+ this.dirty = true;
94
+ }
95
+ /**
96
+ * The estimate used for rows nothing has measured.
97
+ *
98
+ * The mean of what *has* been measured, once anything has, rather than
99
+ * the constructor's number. A list of 60px rows told to estimate 100
100
+ * would otherwise keep a scrollbar half again too long for as long as it
101
+ * was scrolled, and every `scrollToIndex` past the window would land in
102
+ * the wrong place. React Native keeps the same running average for the
103
+ * same reason.
104
+ */
105
+ get averageLength() {
106
+ if (this.measured.size === 0)
107
+ return this.estimate;
108
+ let total = 0;
109
+ for (const length of this.measured.values())
110
+ total += length;
111
+ return total / this.measured.size;
112
+ }
113
+ /** Replaces the starting estimate. For a caller that learns a better one. */
114
+ setEstimate(estimate) {
115
+ if (estimate === this.estimate)
116
+ return;
117
+ this.estimate = estimate;
118
+ this.dirty = true;
119
+ }
120
+ lengthAt(index) {
121
+ const key = this.keys[index];
122
+ if (key === undefined)
123
+ return this.averageLength;
124
+ return this.measured.get(key) ?? this.averageLength;
125
+ }
126
+ offsetAt(index) {
127
+ this.rebuild();
128
+ if (index <= 0)
129
+ return 0;
130
+ if (index >= this.offsets.length)
131
+ return this.totalLength;
132
+ return this.offsets[index];
133
+ }
134
+ metricsAt(index) {
135
+ return { offset: this.offsetAt(index), length: this.lengthAt(index) };
136
+ }
137
+ get totalLength() {
138
+ this.rebuild();
139
+ return this.offsets.length === 0 ? 0 : this.offsets[this.offsets.length - 1];
140
+ }
141
+ /** How many of the current rows have a real measurement. */
142
+ get measuredCount() {
143
+ let count = 0;
144
+ for (const key of this.keys)
145
+ if (this.measured.has(key))
146
+ count += 1;
147
+ return count;
148
+ }
149
+ rebuild() {
150
+ if (!this.dirty)
151
+ return;
152
+ this.dirty = false;
153
+ const average = this.averageLength;
154
+ // One more entry than there are rows: the last is the total, which is
155
+ // also the offset a row appended next would take.
156
+ this.offsets = new Array(this.keys.length + 1);
157
+ let running = 0;
158
+ for (let index = 0; index < this.keys.length; index++) {
159
+ this.offsets[index] = running;
160
+ running += this.measured.get(this.keys[index]) ?? average;
161
+ }
162
+ this.offsets[this.keys.length] = running;
163
+ }
164
+ }
165
+ /**
166
+ * The first and last row indices to mount.
167
+ *
168
+ * A port of `computeWindowedRenderLimits`; see this file's header for why
169
+ * it is a port rather than an invention.
170
+ */
171
+ export function windowRange(input) {
172
+ const { count, visibleLength, windowSize, maxToRenderPerBatch, previous, metrics } = input;
173
+ if (count === 0)
174
+ return { first: 0, last: -1 };
175
+ const velocity = input.velocity ?? 0;
176
+ const visibleBegin = Math.max(0, input.offset);
177
+ const visibleEnd = visibleBegin + visibleLength;
178
+ const overscanLength = (windowSize - 1) * visibleLength;
179
+ // React Native's own comment says considering velocity here introduced
180
+ // more churn than it was worth, and its `leadFactor` is the constant that
181
+ // remains. The bias survives only in `fillPreference`.
182
+ const leadFactor = 0.5;
183
+ const fillPreference = velocity > 1 ? 'after' : velocity < -1 ? 'before' : 'none';
184
+ const overscanBegin = Math.max(0, visibleBegin - (1 - leadFactor) * overscanLength);
185
+ const overscanEnd = Math.max(0, visibleEnd + leadFactor * overscanLength);
186
+ if (metrics.offsetAt(count - 1) < overscanBegin) {
187
+ // The whole list is above the window: scrolled past the end, which
188
+ // happens while data shrinks under a stationary scroll position.
189
+ return { first: Math.max(0, count - 1 - maxToRenderPerBatch), last: count - 1 };
190
+ }
191
+ const found = rowsAtOffsets([overscanBegin, visibleBegin, visibleEnd, overscanEnd], count, metrics);
192
+ const overscanFirst = found[0] ?? 0;
193
+ let first = found[1] ?? Math.max(0, overscanFirst);
194
+ const overscanLast = found[3] ?? count - 1;
195
+ let last = found[2] ?? Math.min(overscanLast, first + maxToRenderPerBatch - 1);
196
+ const visible = { first, last };
197
+ // Grown a batch at a time rather than filled at once: the overscan window
198
+ // is up to twenty viewports of content, and rendering all of it in one
199
+ // commit is a long pause staring at whatever is already on screen.
200
+ let newCells = newRangeCount(previous, visible);
201
+ for (;;) {
202
+ if (first <= overscanFirst && last >= overscanLast)
203
+ break;
204
+ const atMax = newCells >= maxToRenderPerBatch;
205
+ const firstWillAddMore = first <= previous.first;
206
+ const lastWillAddMore = last >= previous.last;
207
+ const firstShouldIncrement = first > overscanFirst && (!atMax || !firstWillAddMore);
208
+ const lastShouldIncrement = last < overscanLast && (!atMax || !lastWillAddMore);
209
+ // Stop only when the batch is full *and* neither end can grow without
210
+ // adding a new cell -- so rows already mounted stay mounted and the
211
+ // window keeps as much of itself as it can.
212
+ if (atMax && !firstShouldIncrement && !lastShouldIncrement)
213
+ break;
214
+ if (firstShouldIncrement &&
215
+ !(fillPreference === 'after' && lastShouldIncrement && lastWillAddMore)) {
216
+ if (firstWillAddMore)
217
+ newCells += 1;
218
+ first -= 1;
219
+ }
220
+ if (lastShouldIncrement &&
221
+ !(fillPreference === 'before' && firstShouldIncrement && firstWillAddMore)) {
222
+ if (lastWillAddMore)
223
+ newCells += 1;
224
+ last += 1;
225
+ }
226
+ }
227
+ return { first, last };
228
+ }
229
+ /** How many rows `next` mounts that `previous` did not. */
230
+ function newRangeCount(previous, next) {
231
+ return (next.last -
232
+ next.first +
233
+ 1 -
234
+ Math.max(0, 1 + Math.min(next.last, previous.last) - Math.max(next.first, previous.first)));
235
+ }
236
+ /**
237
+ * The row containing each offset, or `undefined` past the ends.
238
+ *
239
+ * Binary search rather than a scan, because it runs on every scroll event
240
+ * and the list it searches is the reason this file exists.
241
+ *
242
+ * Bounds are React Native's, and they are asymmetric: a row owns its end
243
+ * offset but not its start, except for the first row, which owns both. So
244
+ * an offset exactly on a boundary resolves to the *earlier* of the two
245
+ * rows -- which is why an overscan edge that lands on a boundary pulls in
246
+ * the row above it.
247
+ */
248
+ function rowsAtOffsets(offsets, count, metrics) {
249
+ return offsets.map((offset) => {
250
+ let left = 0;
251
+ let right = count - 1;
252
+ while (left <= right) {
253
+ const mid = left + Math.floor((right - left) / 2);
254
+ const start = metrics.offsetAt(mid);
255
+ const end = start + metrics.lengthAt(mid);
256
+ if ((mid === 0 && offset < start) || (mid !== 0 && offset <= start))
257
+ right = mid - 1;
258
+ else if (offset > end)
259
+ left = mid + 1;
260
+ else
261
+ return mid;
262
+ }
263
+ return undefined;
264
+ });
265
+ }
266
+ /**
267
+ * The first `count` rows, for the render before anything has been measured.
268
+ *
269
+ * `initialNumToRender` is not an overscan window and is not derived from
270
+ * one: it is what to put on the screen when the viewport's own size is not
271
+ * known yet, which on the Web is every server render and the first client
272
+ * commit. Rendering nothing there would hand a crawler and a
273
+ * `renderToStaticMarkup` caller an empty list.
274
+ */
275
+ export function initialRange(count, initialNumToRender) {
276
+ if (count === 0)
277
+ return { first: 0, last: -1 };
278
+ return { first: 0, last: Math.min(count, Math.max(1, initialNumToRender)) - 1 };
279
+ }
280
+ /**
281
+ * The row to hold still when the data changes under a scrolled list.
282
+ *
283
+ * The first row at or after `minIndexForVisible` whose end is below the
284
+ * current offset -- that is, the first one the reader can see. Holding a
285
+ * row by *key* rather than by index is what makes this work at all: a feed
286
+ * that prepends ten rows renumbers every index, and there is no arithmetic
287
+ * on indices that survives it.
288
+ */
289
+ export function anchorRow(keys, metrics, scrollOffset, minIndexForVisible) {
290
+ for (let index = Math.max(0, minIndexForVisible); index < keys.length; index++) {
291
+ const offset = metrics.offsetAt(index);
292
+ if (offset + metrics.lengthAt(index) > scrollOffset) {
293
+ return { key: keys[index], offset };
294
+ }
295
+ }
296
+ return undefined;
297
+ }
298
+ /**
299
+ * How far the scroll offset has to move for `anchor` to stay where it was.
300
+ *
301
+ * Zero when the anchor is gone -- deleted, or scrolled out of a list that
302
+ * was replaced wholesale -- because the honest answer there is that there
303
+ * is nothing left to hold still, and inventing a correction would move a
304
+ * list the reader was looking at.
305
+ */
306
+ export function anchorCorrection(anchor, keys, metrics) {
307
+ const index = keys.indexOf(anchor.key);
308
+ if (index === -1)
309
+ return 0;
310
+ return metrics.offsetAt(index) - anchor.offset;
311
+ }
312
+ /**
313
+ * Rows kept mounted around the focused one, however the scroll window moved.
314
+ *
315
+ * Focus is not the viewport. A row can hold focus and be scrolled well out
316
+ * of sight, and unmounting it drops focus to `<body>` -- which is not a
317
+ * degraded experience but a lost place. Tabbing forward has the mirror
318
+ * problem: the last mounted row is the last row in the document, so there
319
+ * is nothing after it to move to.
320
+ *
321
+ * `FOCUS_MARGIN` is small on purpose. It is a landing strip either side of
322
+ * focus so a step past the edge has somewhere to go before the next
323
+ * recompute catches up -- not a second overscan window.
324
+ */
325
+ export const FOCUS_MARGIN = 3;
326
+ export function withFocus(window, focusRow, count) {
327
+ if (focusRow === null || count === 0)
328
+ return window;
329
+ if (focusRow < 0 || focusRow >= count)
330
+ return window;
331
+ return {
332
+ first: Math.max(0, Math.min(window.first, focusRow - FOCUS_MARGIN)),
333
+ last: Math.min(count - 1, Math.max(window.last, focusRow + FOCUS_MARGIN)),
334
+ };
335
+ }
336
+ //# sourceMappingURL=windowing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"windowing.js","sourceRoot":"","sources":["../src/windowing.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,qEAAqE;AACrE,0EAA0E;AAC1E,sEAAsE;AACtE,gDAAgD;AAChD,EAAE;AACF,iCAAiC;AACjC,EAAE;AACF,yEAAyE;AACzE,wBAAwB;AACxB,EAAE;AACF,6BAA6B;AAC7B,gBAAgB;AAChB,wGAAwG;AACxG,6FAA6F;AAC7F,uFAAuF;AACvF,8FAA8F;AAC9F,EAAE;AACF,0EAA0E;AAC1E,sEAAsE;AACtE,oEAAoE;AACpE,yEAAyE;AACzE,wEAAwE;AACxE,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,+DAA+D;AAC/D,EAAE;AACF,gEAAgE;AAChE,EAAE;AACF,0EAA0E;AAC1E,yEAAyE;AACzE,sEAAsE;AACtE,0EAA0E;AAC1E,2EAA2E;AAC3E,0EAA0E;AAC1E,wCAAwC;AACxC,0EAA0E;AAC1E,sEAAsE;AACtE,wCAAwC;AAQxC;;;;;;;;;;GAUG;AACH,MAAM,OAAO,WAAW;IACtB,uEAAuE;IACtD,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAA;IACrD,6EAA6E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CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,MAAc,EACd,IAAuB,EACvB,OAAsC;IAEtC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IACtC,IAAI,KAAK,KAAK,CAAC,CAAC;QAAE,OAAO,CAAC,CAAA;IAC1B,OAAO,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,CAAA;AAChD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAA;AAE7B,MAAM,UAAU,SAAS,CACvB,MAAmB,EACnB,QAAuB,EACvB,KAAa;IAEb,IAAI,QAAQ,KAAK,IAAI,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,MAAM,CAAA;IACnD,IAAI,QAAQ,GAAG,CAAC,IAAI,QAAQ,IAAI,KAAK;QAAE,OAAO,MAAM,CAAA;IACpD,OAAO;QACL,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,YAAY,CAAC,CAAC;QACnE,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,GAAG,YAAY,CAAC,CAAC;KAC1E,CAAA;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,160 @@
1
+ {
2
+ "name": "@hozo/primitives",
3
+ "version": "0.1.0",
4
+ "description": "Canonical universal UI primitives for Hozo applications.",
5
+ "keywords": [
6
+ "react-native",
7
+ "react",
8
+ "primitives",
9
+ "components",
10
+ "universal"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/iray-tno/hozo.git",
16
+ "directory": "packages/primitives"
17
+ },
18
+ "homepage": "https://iray-tno.github.io/hozo/",
19
+ "bugs": {
20
+ "url": "https://github.com/iray-tno/hozo/issues"
21
+ },
22
+ "type": "module",
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "sideEffects": false,
26
+ "exports": {
27
+ ".": {
28
+ "react-native": {
29
+ "types": "./dist/index.native.d.ts",
30
+ "default": "./dist/index.native.js"
31
+ },
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./generated/animated": {
36
+ "types": "./dist/generated/animated.d.ts",
37
+ "default": "./dist/generated/animated.js"
38
+ },
39
+ "./generated/backdrop-filter": {
40
+ "types": "./dist/generated/backdrop-filter.d.ts",
41
+ "default": "./dist/generated/backdrop-filter.js"
42
+ },
43
+ "./generated/container": {
44
+ "types": "./dist/generated/container.d.ts",
45
+ "default": "./dist/generated/container.js"
46
+ },
47
+ "./generated/flat-list": {
48
+ "react-native": {
49
+ "types": "./dist/generated/flat-list.native.d.ts",
50
+ "default": "./dist/generated/flat-list.native.js"
51
+ },
52
+ "types": "./dist/generated/flat-list.d.ts",
53
+ "default": "./dist/generated/flat-list.js"
54
+ },
55
+ "./generated/grid": {
56
+ "types": "./dist/generated/grid.d.ts",
57
+ "default": "./dist/generated/grid.js"
58
+ },
59
+ "./generated/link": {
60
+ "react-native": {
61
+ "types": "./dist/generated/link.native.d.ts",
62
+ "default": "./dist/generated/link.native.js"
63
+ },
64
+ "types": "./dist/generated/link.d.ts",
65
+ "default": "./dist/generated/link.js"
66
+ },
67
+ "./generated/pressable": {
68
+ "react-native": {
69
+ "types": "./dist/generated/pressable.native.d.ts",
70
+ "default": "./dist/generated/pressable.native.js"
71
+ },
72
+ "types": "./dist/generated/pressable.d.ts",
73
+ "default": "./dist/generated/pressable.js"
74
+ },
75
+ "./generated/refresh-control": {
76
+ "react-native": {
77
+ "types": "./dist/generated/refresh-control.native.d.ts",
78
+ "default": "./dist/generated/refresh-control.native.js"
79
+ },
80
+ "types": "./dist/generated/refresh-control.d.ts",
81
+ "default": "./dist/generated/refresh-control.js"
82
+ },
83
+ "./generated/scroll-view": {
84
+ "react-native": {
85
+ "types": "./dist/generated/scroll-view.native.d.ts",
86
+ "default": "./dist/generated/scroll-view.native.js"
87
+ },
88
+ "types": "./dist/generated/scroll-view.d.ts",
89
+ "default": "./dist/generated/scroll-view.js"
90
+ },
91
+ "./generated/spaced": {
92
+ "types": "./dist/generated/spaced.d.ts",
93
+ "default": "./dist/generated/spaced.js"
94
+ },
95
+ "./generated/text": {
96
+ "types": "./dist/generated/text.d.ts",
97
+ "default": "./dist/generated/text.js"
98
+ },
99
+ "./generated/text-input": {
100
+ "react-native": {
101
+ "types": "./dist/generated/text-input.native.d.ts",
102
+ "default": "./dist/generated/text-input.native.js"
103
+ },
104
+ "types": "./dist/generated/text-input.d.ts",
105
+ "default": "./dist/generated/text-input.js"
106
+ },
107
+ "./generated/text-size": {
108
+ "types": "./dist/generated/text-size.d.ts",
109
+ "default": "./dist/generated/text-size.js"
110
+ },
111
+ "./generated/view": {
112
+ "react-native": {
113
+ "types": "./dist/generated/view.native.d.ts",
114
+ "default": "./dist/generated/view.native.js"
115
+ },
116
+ "types": "./dist/generated/view.d.ts",
117
+ "default": "./dist/generated/view.js"
118
+ },
119
+ "./package.json": "./package.json"
120
+ },
121
+ "files": [
122
+ "dist",
123
+ "src",
124
+ "!src/**/*.test.ts",
125
+ "!src/**/*.test.tsx",
126
+ "!src/**/*.bench.ts"
127
+ ],
128
+ "publishConfig": {
129
+ "access": "public",
130
+ "provenance": true
131
+ },
132
+ "dependencies": {
133
+ "@hozo/behaviors": "^0.1.0",
134
+ "@hozo/engine": "^0.1.0"
135
+ },
136
+ "peerDependencies": {
137
+ "react": "^19.0.0",
138
+ "react-native": ">=0.86"
139
+ },
140
+ "peerDependenciesMeta": {
141
+ "react-native": {
142
+ "optional": true
143
+ }
144
+ },
145
+ "devDependencies": {
146
+ "@types/react": "^19.2.18",
147
+ "@types/react-dom": "^19.2.4",
148
+ "axe-core": "4.13.0",
149
+ "esbuild": "0.28.2",
150
+ "react": "^19.2.8",
151
+ "react-dom": "^19.2.8",
152
+ "react-native": "^0.87.1"
153
+ },
154
+ "scripts": {
155
+ "build": "node ../../scripts/clean-package-dist.mjs && tsc -p .",
156
+ "typecheck": "tsc -p . --noEmit",
157
+ "test": "node -e \"require('node:fs').rmSync('.test-build', { recursive: true, force: true })\" && tsc -p tsconfig.test.json && node --test .test-build/*.test.js",
158
+ "test:browser": "node scripts/check-list.mjs"
159
+ }
160
+ }
@@ -0,0 +1,158 @@
1
+ import { createElement, forwardRef, type ReactElement, type Ref, useMemo } from 'react'
2
+ import * as ReactNative from 'react-native'
3
+
4
+ // Its own module rather than part of the index, because the index starts
5
+ // with `export * from ./foundation.native.tsx`, and Metro does not
6
+ // tree-shake: a compiled screen that renders one list used to carry every
7
+ // primitive with it. `@hozo/primitives/generated/flat-list` imports this.
8
+
9
+ export type HozoFlatListProps<T> = ReactNative.FlatListProps<T>
10
+ export type HozoFlatListHandle<T = unknown> = ReactNative.FlatList<T>
11
+
12
+ /**
13
+ * What Android's accessibility framework calls a collection.
14
+ *
15
+ * `AccessibilityNodeInfo.setCollectionInfo`, which is how TalkBack knows a
16
+ * scrolling view is a list of a certain length rather than a box with
17
+ * things in it.
18
+ *
19
+ * Typed here because React Native does not type it. The props are real --
20
+ * `BaseViewConfig.android.js` registers `accessibilityCollection` and
21
+ * `accessibilityCollectionItem` as native props, `BaseViewManager` stores
22
+ * them as tags, and `ReactScrollViewAccessibilityDelegate` reads them back
23
+ * and even works out which children are on screen -- but they appear in no
24
+ * `.d.ts` and in no documentation, so nothing that does not go looking for
25
+ * them will find them.
26
+ */
27
+ interface AndroidCollection {
28
+ itemCount: number
29
+ rowCount: number
30
+ columnCount: number
31
+ hierarchical: boolean
32
+ }
33
+
34
+ interface AndroidCollectionItem {
35
+ rowIndex: number
36
+ columnIndex: number
37
+ rowSpan: number
38
+ columnSpan: number
39
+ heading: boolean
40
+ }
41
+
42
+ /**
43
+ * Android only, and that is not a preference.
44
+ *
45
+ * `BaseViewConfig.ios.js` registers neither prop and `React/Views` has no
46
+ * implementation of either, so there is nothing on iOS to send them to.
47
+ * VoiceOver is told how long a React Native list is by nothing at all --
48
+ * which needs either a `UIAccessibilityContainer` shim in native code
49
+ * (#353) or React Native to grow the prop.
50
+ *
51
+ * Read where it is used rather than once at module scope. A module-scope
52
+ * read runs on import, so a `react-native` that does not expose `Platform`
53
+ * -- the conformance stub, until this needed it -- fails to load the file
54
+ * at all rather than answering one question wrongly.
55
+ */
56
+ function isAndroid(): boolean {
57
+ return ReactNative.Platform?.OS === 'android'
58
+ }
59
+
60
+ /**
61
+ * A cell that says where it sits in the collection.
62
+ *
63
+ * `CellRendererComponent` is React Native's own extension point for this,
64
+ * so the information goes on the view the list already wraps each item in
65
+ * rather than on a second one nested inside it -- no extra level, and no
66
+ * change to the layout.
67
+ *
68
+ * The handlers are forwarded because `VirtualizedList` needs them: it
69
+ * learns a cell's size from `onLayout` and follows focus with
70
+ * `onFocusCapture`, and a cell renderer that swallowed either would break
71
+ * the list to describe it.
72
+ */
73
+ function collectionCell(columns: number) {
74
+ return function HozoCollectionCell({
75
+ children,
76
+ index,
77
+ onFocusCapture,
78
+ onLayout,
79
+ style,
80
+ }: {
81
+ children?: unknown
82
+ index: number
83
+ onFocusCapture?: (event: unknown) => void
84
+ onLayout?: (event: unknown) => void
85
+ style?: unknown
86
+ }) {
87
+ const item: AndroidCollectionItem = {
88
+ rowIndex: Math.floor(index / columns),
89
+ columnIndex: index % columns,
90
+ rowSpan: 1,
91
+ columnSpan: 1,
92
+ heading: false,
93
+ }
94
+ return createElement(
95
+ ReactNative.View as unknown as never,
96
+ {
97
+ style,
98
+ onLayout,
99
+ onFocusCapture,
100
+ accessibilityCollectionItem: item,
101
+ } as never,
102
+ children as never,
103
+ )
104
+ }
105
+ }
106
+
107
+ function HozoFlatListInner<T>(
108
+ props: HozoFlatListProps<T>,
109
+ forwardedRef: Ref<ReactNative.FlatList<T>>,
110
+ ): ReactElement {
111
+ const { data, numColumns, CellRendererComponent, ...rest } = props
112
+ const android = isAndroid()
113
+ const columns = Math.max(1, Math.floor(numColumns ?? 1))
114
+ const count = data?.length ?? 0
115
+
116
+ // A component identity that only changes when the column count does. A
117
+ // fresh one every render would remount every cell in the list.
118
+ const cell = useMemo(() => collectionCell(columns), [columns])
119
+
120
+ const collection: AndroidCollection | undefined = android
121
+ ? {
122
+ itemCount: count,
123
+ rowCount: Math.ceil(count / columns),
124
+ columnCount: columns,
125
+ hierarchical: false,
126
+ }
127
+ : undefined
128
+
129
+ return createElement(
130
+ ReactNative.FlatList as unknown as never,
131
+ {
132
+ ...rest,
133
+ data,
134
+ numColumns,
135
+ ref: forwardedRef,
136
+ accessibilityCollection: collection,
137
+ // The caller's own cell renderer wins. Composing would put one view
138
+ // inside another and hand the outer one the handlers the inner one
139
+ // needs; deferring means the list still reports its length, and only
140
+ // the per-item position is left unsaid.
141
+ CellRendererComponent:
142
+ CellRendererComponent ?? (android ? (cell as unknown as never) : undefined),
143
+ } as never,
144
+ )
145
+ }
146
+
147
+ /**
148
+ * React Native's `FlatList`, told how long it is.
149
+ *
150
+ * A windowed list has a length its accessibility tree does not: only the
151
+ * rows near the viewport exist as views, so TalkBack counts those and
152
+ * announces them. On the Web the answer is `aria-setsize`; here it is
153
+ * `accessibilityCollection`, and neither is something an app should have to
154
+ * write itself when the list already knows.
155
+ */
156
+ export const HozoFlatList = forwardRef(HozoFlatListInner) as <T>(
157
+ props: HozoFlatListProps<T> & { ref?: Ref<ReactNative.FlatList<T>> },
158
+ ) => ReactElement