@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.
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/flat-list.native.d.ts +17 -0
- package/dist/flat-list.native.d.ts.map +1 -0
- package/dist/flat-list.native.js +89 -0
- package/dist/flat-list.native.js.map +1 -0
- package/dist/foundation.d.ts +104 -0
- package/dist/foundation.d.ts.map +1 -0
- package/dist/foundation.js +115 -0
- package/dist/foundation.js.map +1 -0
- package/dist/foundation.native.d.ts +59 -0
- package/dist/foundation.native.d.ts.map +1 -0
- package/dist/foundation.native.js +40 -0
- package/dist/foundation.native.js.map +1 -0
- package/dist/generated/animated.d.ts +2 -0
- package/dist/generated/animated.d.ts.map +1 -0
- package/dist/generated/animated.js +3 -0
- package/dist/generated/animated.js.map +1 -0
- package/dist/generated/backdrop-filter.d.ts +2 -0
- package/dist/generated/backdrop-filter.d.ts.map +1 -0
- package/dist/generated/backdrop-filter.js +3 -0
- package/dist/generated/backdrop-filter.js.map +1 -0
- package/dist/generated/container.d.ts +2 -0
- package/dist/generated/container.d.ts.map +1 -0
- package/dist/generated/container.js +3 -0
- package/dist/generated/container.js.map +1 -0
- package/dist/generated/flat-list.d.ts +2 -0
- package/dist/generated/flat-list.d.ts.map +1 -0
- package/dist/generated/flat-list.js +2 -0
- package/dist/generated/flat-list.js.map +1 -0
- package/dist/generated/flat-list.native.d.ts +3 -0
- package/dist/generated/flat-list.native.d.ts.map +1 -0
- package/dist/generated/flat-list.native.js +2 -0
- package/dist/generated/flat-list.native.js.map +1 -0
- package/dist/generated/grid.d.ts +3 -0
- package/dist/generated/grid.d.ts.map +1 -0
- package/dist/generated/grid.js +4 -0
- package/dist/generated/grid.js.map +1 -0
- package/dist/generated/link.d.ts +2 -0
- package/dist/generated/link.d.ts.map +1 -0
- package/dist/generated/link.js +2 -0
- package/dist/generated/link.js.map +1 -0
- package/dist/generated/link.native.d.ts +2 -0
- package/dist/generated/link.native.d.ts.map +1 -0
- package/dist/generated/link.native.js +2 -0
- package/dist/generated/link.native.js.map +1 -0
- package/dist/generated/pressable.d.ts +2 -0
- package/dist/generated/pressable.d.ts.map +1 -0
- package/dist/generated/pressable.js +2 -0
- package/dist/generated/pressable.js.map +1 -0
- package/dist/generated/pressable.native.d.ts +2 -0
- package/dist/generated/pressable.native.d.ts.map +1 -0
- package/dist/generated/pressable.native.js +2 -0
- package/dist/generated/pressable.native.js.map +1 -0
- package/dist/generated/refresh-control.d.ts +2 -0
- package/dist/generated/refresh-control.d.ts.map +1 -0
- package/dist/generated/refresh-control.js +2 -0
- package/dist/generated/refresh-control.js.map +1 -0
- package/dist/generated/refresh-control.native.d.ts +2 -0
- package/dist/generated/refresh-control.native.d.ts.map +1 -0
- package/dist/generated/refresh-control.native.js +2 -0
- package/dist/generated/refresh-control.native.js.map +1 -0
- package/dist/generated/scroll-view.d.ts +2 -0
- package/dist/generated/scroll-view.d.ts.map +1 -0
- package/dist/generated/scroll-view.js +2 -0
- package/dist/generated/scroll-view.js.map +1 -0
- package/dist/generated/scroll-view.native.d.ts +2 -0
- package/dist/generated/scroll-view.native.d.ts.map +1 -0
- package/dist/generated/scroll-view.native.js +2 -0
- package/dist/generated/scroll-view.native.js.map +1 -0
- package/dist/generated/spaced.d.ts +2 -0
- package/dist/generated/spaced.d.ts.map +1 -0
- package/dist/generated/spaced.js +3 -0
- package/dist/generated/spaced.js.map +1 -0
- package/dist/generated/text-input.d.ts +2 -0
- package/dist/generated/text-input.d.ts.map +1 -0
- package/dist/generated/text-input.js +2 -0
- package/dist/generated/text-input.js.map +1 -0
- package/dist/generated/text-input.native.d.ts +2 -0
- package/dist/generated/text-input.native.d.ts.map +1 -0
- package/dist/generated/text-input.native.js +2 -0
- package/dist/generated/text-input.native.js.map +1 -0
- package/dist/generated/text-size.d.ts +2 -0
- package/dist/generated/text-size.d.ts.map +1 -0
- package/dist/generated/text-size.js +3 -0
- package/dist/generated/text-size.js.map +1 -0
- package/dist/generated/text.d.ts +2 -0
- package/dist/generated/text.d.ts.map +1 -0
- package/dist/generated/text.js +5 -0
- package/dist/generated/text.js.map +1 -0
- package/dist/generated/view.d.ts +2 -0
- package/dist/generated/view.d.ts.map +1 -0
- package/dist/generated/view.js +2 -0
- package/dist/generated/view.js.map +1 -0
- package/dist/generated/view.native.d.ts +2 -0
- package/dist/generated/view.native.d.ts.map +1 -0
- package/dist/generated/view.native.js +2 -0
- package/dist/generated/view.native.js.map +1 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +718 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +22 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +8 -0
- package/dist/index.native.js.map +1 -0
- package/dist/link.d.ts +320 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +69 -0
- package/dist/link.js.map +1 -0
- package/dist/link.native.d.ts +33 -0
- package/dist/link.native.d.ts.map +1 -0
- package/dist/link.native.js +44 -0
- package/dist/link.native.js.map +1 -0
- package/dist/pressable.d.ts +65 -0
- package/dist/pressable.d.ts.map +1 -0
- package/dist/pressable.js +202 -0
- package/dist/pressable.js.map +1 -0
- package/dist/pressable.native.d.ts +55 -0
- package/dist/pressable.native.d.ts.map +1 -0
- package/dist/pressable.native.js +405 -0
- package/dist/pressable.native.js.map +1 -0
- package/dist/runtime-backdrop.native.d.ts +8 -0
- package/dist/runtime-backdrop.native.d.ts.map +1 -0
- package/dist/runtime-backdrop.native.js +12 -0
- package/dist/runtime-backdrop.native.js.map +1 -0
- package/dist/runtime-container.native.d.ts +41 -0
- package/dist/runtime-container.native.d.ts.map +1 -0
- package/dist/runtime-container.native.js +61 -0
- package/dist/runtime-container.native.js.map +1 -0
- package/dist/runtime-grid.d.ts +37 -0
- package/dist/runtime-grid.d.ts.map +1 -0
- package/dist/runtime-grid.js +139 -0
- package/dist/runtime-grid.js.map +1 -0
- package/dist/runtime-grid.native.d.ts +26 -0
- package/dist/runtime-grid.native.d.ts.map +1 -0
- package/dist/runtime-grid.native.js +76 -0
- package/dist/runtime-grid.native.js.map +1 -0
- package/dist/runtime-measured.d.ts +20 -0
- package/dist/runtime-measured.d.ts.map +1 -0
- package/dist/runtime-measured.js +42 -0
- package/dist/runtime-measured.js.map +1 -0
- package/dist/runtime-spacing.d.ts +9 -0
- package/dist/runtime-spacing.d.ts.map +1 -0
- package/dist/runtime-spacing.js +35 -0
- package/dist/runtime-spacing.js.map +1 -0
- package/dist/runtime-spacing.native.d.ts +12 -0
- package/dist/runtime-spacing.native.d.ts.map +1 -0
- package/dist/runtime-spacing.native.js +64 -0
- package/dist/runtime-spacing.native.js.map +1 -0
- package/dist/runtime-text-size.native.d.ts +29 -0
- package/dist/runtime-text-size.native.d.ts.map +1 -0
- package/dist/runtime-text-size.native.js +51 -0
- package/dist/runtime-text-size.native.js.map +1 -0
- package/dist/runtime-transition.native.d.ts +15 -0
- package/dist/runtime-transition.native.d.ts.map +1 -0
- package/dist/runtime-transition.native.js +156 -0
- package/dist/runtime-transition.native.js.map +1 -0
- package/dist/text-input.d.ts +40 -0
- package/dist/text-input.d.ts.map +1 -0
- package/dist/text-input.js +54 -0
- package/dist/text-input.js.map +1 -0
- package/dist/view.d.ts +42 -0
- package/dist/view.d.ts.map +1 -0
- package/dist/view.js +84 -0
- package/dist/view.js.map +1 -0
- package/dist/view.native.d.ts +8 -0
- package/dist/view.native.d.ts.map +1 -0
- package/dist/view.native.js +5 -0
- package/dist/view.native.js.map +1 -0
- package/dist/windowing.d.ts +149 -0
- package/dist/windowing.d.ts.map +1 -0
- package/dist/windowing.js +336 -0
- package/dist/windowing.js.map +1 -0
- package/package.json +160 -0
- package/src/flat-list.native.tsx +158 -0
- package/src/foundation.native.tsx +179 -0
- package/src/foundation.tsx +323 -0
- package/src/generated/animated.ts +6 -0
- package/src/generated/backdrop-filter.ts +5 -0
- package/src/generated/container.ts +7 -0
- package/src/generated/flat-list.native.ts +6 -0
- package/src/generated/flat-list.ts +7 -0
- package/src/generated/grid.ts +4 -0
- package/src/generated/link.native.ts +1 -0
- package/src/generated/link.ts +1 -0
- package/src/generated/pressable.native.ts +5 -0
- package/src/generated/pressable.ts +5 -0
- package/src/generated/refresh-control.native.ts +4 -0
- package/src/generated/refresh-control.ts +1 -0
- package/src/generated/scroll-view.native.ts +4 -0
- package/src/generated/scroll-view.ts +1 -0
- package/src/generated/spaced.ts +2 -0
- package/src/generated/text-input.native.ts +4 -0
- package/src/generated/text-input.ts +1 -0
- package/src/generated/text-size.ts +7 -0
- package/src/generated/text.ts +8 -0
- package/src/generated/view.native.ts +1 -0
- package/src/generated/view.ts +1 -0
- package/src/index.native.ts +24 -0
- package/src/index.ts +1111 -0
- package/src/link.native.tsx +77 -0
- package/src/link.ts +125 -0
- package/src/pressable.native.tsx +538 -0
- package/src/pressable.ts +358 -0
- package/src/runtime-backdrop.native.tsx +22 -0
- package/src/runtime-container.native.tsx +105 -0
- package/src/runtime-grid.native.tsx +143 -0
- package/src/runtime-grid.ts +198 -0
- package/src/runtime-measured.ts +42 -0
- package/src/runtime-spacing.native.tsx +75 -0
- package/src/runtime-spacing.ts +37 -0
- package/src/runtime-text-size.native.tsx +79 -0
- package/src/runtime-transition.native.tsx +183 -0
- package/src/text-input.ts +129 -0
- package/src/view.native.ts +7 -0
- package/src/view.ts +165 -0
- package/src/windowing.ts +415 -0
package/src/windowing.ts
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
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
|
+
/** What one row's geometry is, in the scrolling axis. */
|
|
46
|
+
export interface RowMetrics {
|
|
47
|
+
offset: number
|
|
48
|
+
length: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Row lengths, measured where they have been and estimated where they have
|
|
53
|
+
* not.
|
|
54
|
+
*
|
|
55
|
+
* Keyed by the caller's own key rather than by index, which is the whole
|
|
56
|
+
* point: a feed that prepends ten rows moves every index by ten, and a
|
|
57
|
+
* store keyed by index would report ten rows of wrong heights and jump the
|
|
58
|
+
* scroll position. Compose makes the same argument for `key` on
|
|
59
|
+
* `LazyColumn`, and it is the one thing SwiftUI and Compose *do* ask of a
|
|
60
|
+
* caller.
|
|
61
|
+
*/
|
|
62
|
+
export class ListMetrics {
|
|
63
|
+
/** Measured lengths, by key, surviving reorder and window movement. */
|
|
64
|
+
private readonly measured = new Map<string, number>()
|
|
65
|
+
/** Prefix sums for the current key order; rebuilt when something changes. */
|
|
66
|
+
private offsets: number[] = []
|
|
67
|
+
private keys: readonly string[] = []
|
|
68
|
+
private dirty = true
|
|
69
|
+
// A plain field rather than a parameter property: Node strips types
|
|
70
|
+
// rather than compiling them, and a parameter property is the one piece
|
|
71
|
+
// of TypeScript that emits code.
|
|
72
|
+
private estimate: number
|
|
73
|
+
|
|
74
|
+
constructor(estimate: number) {
|
|
75
|
+
this.estimate = estimate
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The key order to measure against. Cheap to call every render. */
|
|
79
|
+
setKeys(keys: readonly string[]): void {
|
|
80
|
+
if (keys.length === this.keys.length && keys.every((key, i) => key === this.keys[i])) return
|
|
81
|
+
this.keys = keys
|
|
82
|
+
this.dirty = true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Records what a row actually measured.
|
|
87
|
+
*
|
|
88
|
+
* Returns whether this changed anything, so a caller can avoid a re-render
|
|
89
|
+
* for a `ResizeObserver` callback that reports the size it already knew --
|
|
90
|
+
* which is most of them, since the observer fires on mount for every row.
|
|
91
|
+
*/
|
|
92
|
+
measure(key: string, length: number): boolean {
|
|
93
|
+
if (this.measured.get(key) === length) return false
|
|
94
|
+
this.measured.set(key, length)
|
|
95
|
+
this.dirty = true
|
|
96
|
+
return true
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Forgets a measurement. For a row whose content changed identity under one key. */
|
|
100
|
+
forget(key: string): void {
|
|
101
|
+
if (this.measured.delete(key)) this.dirty = true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The estimate used for rows nothing has measured.
|
|
106
|
+
*
|
|
107
|
+
* The mean of what *has* been measured, once anything has, rather than
|
|
108
|
+
* the constructor's number. A list of 60px rows told to estimate 100
|
|
109
|
+
* would otherwise keep a scrollbar half again too long for as long as it
|
|
110
|
+
* was scrolled, and every `scrollToIndex` past the window would land in
|
|
111
|
+
* the wrong place. React Native keeps the same running average for the
|
|
112
|
+
* same reason.
|
|
113
|
+
*/
|
|
114
|
+
get averageLength(): number {
|
|
115
|
+
if (this.measured.size === 0) return this.estimate
|
|
116
|
+
let total = 0
|
|
117
|
+
for (const length of this.measured.values()) total += length
|
|
118
|
+
return total / this.measured.size
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Replaces the starting estimate. For a caller that learns a better one. */
|
|
122
|
+
setEstimate(estimate: number): void {
|
|
123
|
+
if (estimate === this.estimate) return
|
|
124
|
+
this.estimate = estimate
|
|
125
|
+
this.dirty = true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
lengthAt(index: number): number {
|
|
129
|
+
const key = this.keys[index]
|
|
130
|
+
if (key === undefined) return this.averageLength
|
|
131
|
+
return this.measured.get(key) ?? this.averageLength
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
offsetAt(index: number): number {
|
|
135
|
+
this.rebuild()
|
|
136
|
+
if (index <= 0) return 0
|
|
137
|
+
if (index >= this.offsets.length) return this.totalLength
|
|
138
|
+
return this.offsets[index] as number
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
metricsAt(index: number): RowMetrics {
|
|
142
|
+
return { offset: this.offsetAt(index), length: this.lengthAt(index) }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get totalLength(): number {
|
|
146
|
+
this.rebuild()
|
|
147
|
+
return this.offsets.length === 0 ? 0 : (this.offsets[this.offsets.length - 1] as number)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** How many of the current rows have a real measurement. */
|
|
151
|
+
get measuredCount(): number {
|
|
152
|
+
let count = 0
|
|
153
|
+
for (const key of this.keys) if (this.measured.has(key)) count += 1
|
|
154
|
+
return count
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private rebuild(): void {
|
|
158
|
+
if (!this.dirty) return
|
|
159
|
+
this.dirty = false
|
|
160
|
+
const average = this.averageLength
|
|
161
|
+
// One more entry than there are rows: the last is the total, which is
|
|
162
|
+
// also the offset a row appended next would take.
|
|
163
|
+
this.offsets = new Array(this.keys.length + 1)
|
|
164
|
+
let running = 0
|
|
165
|
+
for (let index = 0; index < this.keys.length; index++) {
|
|
166
|
+
this.offsets[index] = running
|
|
167
|
+
running += this.measured.get(this.keys[index] as string) ?? average
|
|
168
|
+
}
|
|
169
|
+
this.offsets[this.keys.length] = running
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The rows to mount, inclusive at both ends. `last < first` means none. */
|
|
174
|
+
export interface WindowRange {
|
|
175
|
+
first: number
|
|
176
|
+
last: number
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface WindowRangeInput {
|
|
180
|
+
count: number
|
|
181
|
+
/** Scroll offset along the axis, in list space. */
|
|
182
|
+
offset: number
|
|
183
|
+
/** The viewport's length along the axis. */
|
|
184
|
+
visibleLength: number
|
|
185
|
+
/** React Native's units: `(windowSize - 1)` viewports of overscan. */
|
|
186
|
+
windowSize: number
|
|
187
|
+
maxToRenderPerBatch: number
|
|
188
|
+
/** Signed, for the fill bias. Zero is the honest answer when unknown. */
|
|
189
|
+
velocity?: number
|
|
190
|
+
previous: WindowRange
|
|
191
|
+
metrics: Pick<ListMetrics, 'offsetAt' | 'lengthAt'>
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The first and last row indices to mount.
|
|
196
|
+
*
|
|
197
|
+
* A port of `computeWindowedRenderLimits`; see this file's header for why
|
|
198
|
+
* it is a port rather than an invention.
|
|
199
|
+
*/
|
|
200
|
+
export function windowRange(input: WindowRangeInput): WindowRange {
|
|
201
|
+
const { count, visibleLength, windowSize, maxToRenderPerBatch, previous, metrics } = input
|
|
202
|
+
if (count === 0) return { first: 0, last: -1 }
|
|
203
|
+
|
|
204
|
+
const velocity = input.velocity ?? 0
|
|
205
|
+
const visibleBegin = Math.max(0, input.offset)
|
|
206
|
+
const visibleEnd = visibleBegin + visibleLength
|
|
207
|
+
const overscanLength = (windowSize - 1) * visibleLength
|
|
208
|
+
|
|
209
|
+
// React Native's own comment says considering velocity here introduced
|
|
210
|
+
// more churn than it was worth, and its `leadFactor` is the constant that
|
|
211
|
+
// remains. The bias survives only in `fillPreference`.
|
|
212
|
+
const leadFactor = 0.5
|
|
213
|
+
const fillPreference = velocity > 1 ? 'after' : velocity < -1 ? 'before' : 'none'
|
|
214
|
+
|
|
215
|
+
const overscanBegin = Math.max(0, visibleBegin - (1 - leadFactor) * overscanLength)
|
|
216
|
+
const overscanEnd = Math.max(0, visibleEnd + leadFactor * overscanLength)
|
|
217
|
+
|
|
218
|
+
if (metrics.offsetAt(count - 1) < overscanBegin) {
|
|
219
|
+
// The whole list is above the window: scrolled past the end, which
|
|
220
|
+
// happens while data shrinks under a stationary scroll position.
|
|
221
|
+
return { first: Math.max(0, count - 1 - maxToRenderPerBatch), last: count - 1 }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const found = rowsAtOffsets(
|
|
225
|
+
[overscanBegin, visibleBegin, visibleEnd, overscanEnd],
|
|
226
|
+
count,
|
|
227
|
+
metrics,
|
|
228
|
+
)
|
|
229
|
+
const overscanFirst = found[0] ?? 0
|
|
230
|
+
let first = found[1] ?? Math.max(0, overscanFirst)
|
|
231
|
+
const overscanLast = found[3] ?? count - 1
|
|
232
|
+
let last = found[2] ?? Math.min(overscanLast, first + maxToRenderPerBatch - 1)
|
|
233
|
+
const visible = { first, last }
|
|
234
|
+
|
|
235
|
+
// Grown a batch at a time rather than filled at once: the overscan window
|
|
236
|
+
// is up to twenty viewports of content, and rendering all of it in one
|
|
237
|
+
// commit is a long pause staring at whatever is already on screen.
|
|
238
|
+
let newCells = newRangeCount(previous, visible)
|
|
239
|
+
for (;;) {
|
|
240
|
+
if (first <= overscanFirst && last >= overscanLast) break
|
|
241
|
+
const atMax = newCells >= maxToRenderPerBatch
|
|
242
|
+
const firstWillAddMore = first <= previous.first
|
|
243
|
+
const lastWillAddMore = last >= previous.last
|
|
244
|
+
const firstShouldIncrement = first > overscanFirst && (!atMax || !firstWillAddMore)
|
|
245
|
+
const lastShouldIncrement = last < overscanLast && (!atMax || !lastWillAddMore)
|
|
246
|
+
// Stop only when the batch is full *and* neither end can grow without
|
|
247
|
+
// adding a new cell -- so rows already mounted stay mounted and the
|
|
248
|
+
// window keeps as much of itself as it can.
|
|
249
|
+
if (atMax && !firstShouldIncrement && !lastShouldIncrement) break
|
|
250
|
+
if (
|
|
251
|
+
firstShouldIncrement &&
|
|
252
|
+
!(fillPreference === 'after' && lastShouldIncrement && lastWillAddMore)
|
|
253
|
+
) {
|
|
254
|
+
if (firstWillAddMore) newCells += 1
|
|
255
|
+
first -= 1
|
|
256
|
+
}
|
|
257
|
+
if (
|
|
258
|
+
lastShouldIncrement &&
|
|
259
|
+
!(fillPreference === 'before' && firstShouldIncrement && firstWillAddMore)
|
|
260
|
+
) {
|
|
261
|
+
if (lastWillAddMore) newCells += 1
|
|
262
|
+
last += 1
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return { first, last }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** How many rows `next` mounts that `previous` did not. */
|
|
270
|
+
function newRangeCount(previous: WindowRange, next: WindowRange): number {
|
|
271
|
+
return (
|
|
272
|
+
next.last -
|
|
273
|
+
next.first +
|
|
274
|
+
1 -
|
|
275
|
+
Math.max(0, 1 + Math.min(next.last, previous.last) - Math.max(next.first, previous.first))
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The row containing each offset, or `undefined` past the ends.
|
|
281
|
+
*
|
|
282
|
+
* Binary search rather than a scan, because it runs on every scroll event
|
|
283
|
+
* and the list it searches is the reason this file exists.
|
|
284
|
+
*
|
|
285
|
+
* Bounds are React Native's, and they are asymmetric: a row owns its end
|
|
286
|
+
* offset but not its start, except for the first row, which owns both. So
|
|
287
|
+
* an offset exactly on a boundary resolves to the *earlier* of the two
|
|
288
|
+
* rows -- which is why an overscan edge that lands on a boundary pulls in
|
|
289
|
+
* the row above it.
|
|
290
|
+
*/
|
|
291
|
+
function rowsAtOffsets(
|
|
292
|
+
offsets: readonly number[],
|
|
293
|
+
count: number,
|
|
294
|
+
metrics: Pick<ListMetrics, 'offsetAt' | 'lengthAt'>,
|
|
295
|
+
): (number | undefined)[] {
|
|
296
|
+
return offsets.map((offset) => {
|
|
297
|
+
let left = 0
|
|
298
|
+
let right = count - 1
|
|
299
|
+
while (left <= right) {
|
|
300
|
+
const mid = left + Math.floor((right - left) / 2)
|
|
301
|
+
const start = metrics.offsetAt(mid)
|
|
302
|
+
const end = start + metrics.lengthAt(mid)
|
|
303
|
+
if ((mid === 0 && offset < start) || (mid !== 0 && offset <= start)) right = mid - 1
|
|
304
|
+
else if (offset > end) left = mid + 1
|
|
305
|
+
else return mid
|
|
306
|
+
}
|
|
307
|
+
return undefined
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* The first `count` rows, for the render before anything has been measured.
|
|
313
|
+
*
|
|
314
|
+
* `initialNumToRender` is not an overscan window and is not derived from
|
|
315
|
+
* one: it is what to put on the screen when the viewport's own size is not
|
|
316
|
+
* known yet, which on the Web is every server render and the first client
|
|
317
|
+
* commit. Rendering nothing there would hand a crawler and a
|
|
318
|
+
* `renderToStaticMarkup` caller an empty list.
|
|
319
|
+
*/
|
|
320
|
+
export function initialRange(count: number, initialNumToRender: number): WindowRange {
|
|
321
|
+
if (count === 0) return { first: 0, last: -1 }
|
|
322
|
+
return { first: 0, last: Math.min(count, Math.max(1, initialNumToRender)) - 1 }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** A row to hold still across a data change, and where it was. */
|
|
326
|
+
export interface Anchor {
|
|
327
|
+
key: string
|
|
328
|
+
offset: number
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export interface MaintainVisibleContentPosition {
|
|
332
|
+
/**
|
|
333
|
+
* The first row allowed to be the anchor.
|
|
334
|
+
*
|
|
335
|
+
* `1` is the usual value for a feed with a header row: the header is
|
|
336
|
+
* index 0 and is always at offset 0, so anchoring on it would hold
|
|
337
|
+
* nothing still.
|
|
338
|
+
*/
|
|
339
|
+
minIndexForVisible: number
|
|
340
|
+
/**
|
|
341
|
+
* Scroll to the top instead of holding position, when already within this
|
|
342
|
+
* many pixels of it. Absent means never.
|
|
343
|
+
*/
|
|
344
|
+
autoscrollToTopThreshold?: number
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* The row to hold still when the data changes under a scrolled list.
|
|
349
|
+
*
|
|
350
|
+
* The first row at or after `minIndexForVisible` whose end is below the
|
|
351
|
+
* current offset -- that is, the first one the reader can see. Holding a
|
|
352
|
+
* row by *key* rather than by index is what makes this work at all: a feed
|
|
353
|
+
* that prepends ten rows renumbers every index, and there is no arithmetic
|
|
354
|
+
* on indices that survives it.
|
|
355
|
+
*/
|
|
356
|
+
export function anchorRow(
|
|
357
|
+
keys: readonly string[],
|
|
358
|
+
metrics: Pick<ListMetrics, 'offsetAt' | 'lengthAt'>,
|
|
359
|
+
scrollOffset: number,
|
|
360
|
+
minIndexForVisible: number,
|
|
361
|
+
): Anchor | undefined {
|
|
362
|
+
for (let index = Math.max(0, minIndexForVisible); index < keys.length; index++) {
|
|
363
|
+
const offset = metrics.offsetAt(index)
|
|
364
|
+
if (offset + metrics.lengthAt(index) > scrollOffset) {
|
|
365
|
+
return { key: keys[index] as string, offset }
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return undefined
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* How far the scroll offset has to move for `anchor` to stay where it was.
|
|
373
|
+
*
|
|
374
|
+
* Zero when the anchor is gone -- deleted, or scrolled out of a list that
|
|
375
|
+
* was replaced wholesale -- because the honest answer there is that there
|
|
376
|
+
* is nothing left to hold still, and inventing a correction would move a
|
|
377
|
+
* list the reader was looking at.
|
|
378
|
+
*/
|
|
379
|
+
export function anchorCorrection(
|
|
380
|
+
anchor: Anchor,
|
|
381
|
+
keys: readonly string[],
|
|
382
|
+
metrics: Pick<ListMetrics, 'offsetAt'>,
|
|
383
|
+
): number {
|
|
384
|
+
const index = keys.indexOf(anchor.key)
|
|
385
|
+
if (index === -1) return 0
|
|
386
|
+
return metrics.offsetAt(index) - anchor.offset
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Rows kept mounted around the focused one, however the scroll window moved.
|
|
391
|
+
*
|
|
392
|
+
* Focus is not the viewport. A row can hold focus and be scrolled well out
|
|
393
|
+
* of sight, and unmounting it drops focus to `<body>` -- which is not a
|
|
394
|
+
* degraded experience but a lost place. Tabbing forward has the mirror
|
|
395
|
+
* problem: the last mounted row is the last row in the document, so there
|
|
396
|
+
* is nothing after it to move to.
|
|
397
|
+
*
|
|
398
|
+
* `FOCUS_MARGIN` is small on purpose. It is a landing strip either side of
|
|
399
|
+
* focus so a step past the edge has somewhere to go before the next
|
|
400
|
+
* recompute catches up -- not a second overscan window.
|
|
401
|
+
*/
|
|
402
|
+
export const FOCUS_MARGIN = 3
|
|
403
|
+
|
|
404
|
+
export function withFocus(
|
|
405
|
+
window: WindowRange,
|
|
406
|
+
focusRow: number | null,
|
|
407
|
+
count: number,
|
|
408
|
+
): WindowRange {
|
|
409
|
+
if (focusRow === null || count === 0) return window
|
|
410
|
+
if (focusRow < 0 || focusRow >= count) return window
|
|
411
|
+
return {
|
|
412
|
+
first: Math.max(0, Math.min(window.first, focusRow - FOCUS_MARGIN)),
|
|
413
|
+
last: Math.min(count - 1, Math.max(window.last, focusRow + FOCUS_MARGIN)),
|
|
414
|
+
}
|
|
415
|
+
}
|