@knitui/components 0.3.0 → 0.5.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 (97) hide show
  1. package/README.md +2 -2
  2. package/lib/commonjs/ActionIcon/ActionIcon.js.map +1 -1
  3. package/lib/commonjs/Alert/Alert.js.map +1 -1
  4. package/lib/commonjs/Avatar/Avatar.js.map +1 -1
  5. package/lib/commonjs/Badge/Badge.js.map +1 -1
  6. package/lib/commonjs/Button/Button.js.map +1 -1
  7. package/lib/commonjs/ColorPicker/ColorPicker.js.map +1 -1
  8. package/lib/commonjs/Image/createImage.js +48 -2
  9. package/lib/commonjs/Image/createImage.js.map +1 -1
  10. package/lib/commonjs/PinInput/PinInput.js.map +1 -1
  11. package/lib/commonjs/ScrollArea/ScrollArea.native.js +18 -0
  12. package/lib/commonjs/ScrollArea/ScrollArea.native.js.map +1 -1
  13. package/lib/commonjs/ScrollArea/ScrollArea.shared.js.map +1 -1
  14. package/lib/commonjs/ThemeIcon/ThemeIcon.js.map +1 -1
  15. package/lib/commonjs/Title/Title.js.map +1 -1
  16. package/lib/commonjs/Transition/use-transition.js.map +1 -1
  17. package/lib/commonjs/UnstyledButton/UnstyledButton.js +12 -0
  18. package/lib/commonjs/UnstyledButton/UnstyledButton.js.map +1 -1
  19. package/lib/commonjs/VirtualList/VirtualList.js +311 -0
  20. package/lib/commonjs/VirtualList/VirtualList.js.map +1 -0
  21. package/lib/commonjs/VirtualList/VirtualList.shared.js +233 -0
  22. package/lib/commonjs/VirtualList/VirtualList.shared.js.map +1 -0
  23. package/lib/commonjs/VirtualList/index.js +17 -0
  24. package/lib/commonjs/VirtualList/index.js.map +1 -0
  25. package/lib/commonjs/index.js +7 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/module/ActionIcon/ActionIcon.js.map +1 -1
  28. package/lib/module/Alert/Alert.js.map +1 -1
  29. package/lib/module/Avatar/Avatar.js.map +1 -1
  30. package/lib/module/Badge/Badge.js.map +1 -1
  31. package/lib/module/Button/Button.js.map +1 -1
  32. package/lib/module/ColorPicker/ColorPicker.js.map +1 -1
  33. package/lib/module/Image/createImage.js +48 -2
  34. package/lib/module/Image/createImage.js.map +1 -1
  35. package/lib/module/PinInput/PinInput.js.map +1 -1
  36. package/lib/module/ScrollArea/ScrollArea.native.js +18 -0
  37. package/lib/module/ScrollArea/ScrollArea.native.js.map +1 -1
  38. package/lib/module/ScrollArea/ScrollArea.shared.js.map +1 -1
  39. package/lib/module/ThemeIcon/ThemeIcon.js.map +1 -1
  40. package/lib/module/Title/Title.js.map +1 -1
  41. package/lib/module/Transition/use-transition.js.map +1 -1
  42. package/lib/module/UnstyledButton/UnstyledButton.js +13 -1
  43. package/lib/module/UnstyledButton/UnstyledButton.js.map +1 -1
  44. package/lib/module/VirtualList/VirtualList.js +306 -0
  45. package/lib/module/VirtualList/VirtualList.js.map +1 -0
  46. package/lib/module/VirtualList/VirtualList.shared.js +222 -0
  47. package/lib/module/VirtualList/VirtualList.shared.js.map +1 -0
  48. package/lib/module/VirtualList/index.js +4 -0
  49. package/lib/module/VirtualList/index.js.map +1 -0
  50. package/lib/module/index.js +1 -0
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/typescript/ActionIcon/ActionIcon.d.ts.map +1 -1
  53. package/lib/typescript/Alert/Alert.d.ts.map +1 -1
  54. package/lib/typescript/Avatar/Avatar.d.ts.map +1 -1
  55. package/lib/typescript/Badge/Badge.d.ts.map +1 -1
  56. package/lib/typescript/Button/Button.d.ts.map +1 -1
  57. package/lib/typescript/ColorPicker/ColorPicker.d.ts.map +1 -1
  58. package/lib/typescript/FileButton/FileButton.types.d.ts.map +1 -1
  59. package/lib/typescript/Image/createImage.d.ts.map +1 -1
  60. package/lib/typescript/PinInput/PinInput.d.ts.map +1 -1
  61. package/lib/typescript/ScrollArea/ScrollArea.native.d.ts.map +1 -1
  62. package/lib/typescript/ScrollArea/ScrollArea.shared.d.ts +14 -0
  63. package/lib/typescript/ScrollArea/ScrollArea.shared.d.ts.map +1 -1
  64. package/lib/typescript/ThemeIcon/ThemeIcon.d.ts.map +1 -1
  65. package/lib/typescript/Title/Title.d.ts.map +1 -1
  66. package/lib/typescript/Transition/use-transition.d.ts.map +1 -1
  67. package/lib/typescript/Tree/Tree.d.ts +1 -1
  68. package/lib/typescript/Tree/Tree.d.ts.map +1 -1
  69. package/lib/typescript/UnstyledButton/UnstyledButton.d.ts.map +1 -1
  70. package/lib/typescript/VirtualList/VirtualList.d.ts +15 -0
  71. package/lib/typescript/VirtualList/VirtualList.d.ts.map +1 -0
  72. package/lib/typescript/VirtualList/VirtualList.shared.d.ts +188 -0
  73. package/lib/typescript/VirtualList/VirtualList.shared.d.ts.map +1 -0
  74. package/lib/typescript/VirtualList/index.d.ts +3 -0
  75. package/lib/typescript/VirtualList/index.d.ts.map +1 -0
  76. package/lib/typescript/index.d.ts +1 -0
  77. package/lib/typescript/index.d.ts.map +1 -1
  78. package/package.json +13 -13
  79. package/src/ActionIcon/ActionIcon.tsx +1 -8
  80. package/src/Alert/Alert.tsx +1 -7
  81. package/src/Avatar/Avatar.tsx +1 -7
  82. package/src/Badge/Badge.tsx +1 -8
  83. package/src/Button/Button.tsx +1 -8
  84. package/src/ColorPicker/ColorPicker.tsx +1 -2
  85. package/src/FileButton/FileButton.types.ts +2 -11
  86. package/src/Image/createImage.tsx +45 -2
  87. package/src/PinInput/PinInput.tsx +1 -8
  88. package/src/ScrollArea/ScrollArea.native.tsx +17 -0
  89. package/src/ScrollArea/ScrollArea.shared.ts +16 -0
  90. package/src/ThemeIcon/ThemeIcon.tsx +1 -8
  91. package/src/Title/Title.tsx +1 -13
  92. package/src/Transition/use-transition.ts +1 -6
  93. package/src/UnstyledButton/UnstyledButton.tsx +19 -1
  94. package/src/VirtualList/VirtualList.shared.ts +359 -0
  95. package/src/VirtualList/VirtualList.tsx +379 -0
  96. package/src/VirtualList/index.ts +7 -0
  97. package/src/index.ts +8 -0
@@ -12,12 +12,7 @@ import { useDidUpdate, useReducedMotion } from "@knitui/hooks";
12
12
  * union), so the hook stays free of any platform-specific style imports.
13
13
  */
14
14
  export type TransitionStatus =
15
- | "entering"
16
- | "entered"
17
- | "exiting"
18
- | "exited"
19
- | "pre-exiting"
20
- | "pre-entering";
15
+ "entering" | "entered" | "exiting" | "exited" | "pre-exiting" | "pre-entering";
21
16
 
22
17
  export interface UseTransitionOptions {
23
18
  duration: number;
@@ -1,8 +1,15 @@
1
1
  import { type GetProps, styled, withStaticProperties } from "@knitui/core";
2
2
 
3
3
  import { Box } from "../Box";
4
+ import { usePressScale } from "../internal/motion";
4
5
  import { renderTextChild } from "../internal/render-text-child";
5
- import { focusRingStyle, webButton, webButtonTextReset, webCursor } from "../internal/style-props";
6
+ import {
7
+ focusRingStyle,
8
+ pressScaleStyle,
9
+ webButton,
10
+ webButtonTextReset,
11
+ webCursor,
12
+ } from "../internal/style-props";
6
13
  import { Text } from "../Text";
7
14
 
8
15
  /**
@@ -19,6 +26,14 @@ export const UnstyledButtonFrame = styled(Box, {
19
26
  borderWidth: 0,
20
27
  userSelect: "none",
21
28
  ...focusRingStyle,
29
+ // Tactile press-dip — the same base affordance `Button`/`ActionIcon`/`Chip`
30
+ // carry, eased via the `usePressScale()` props applied on render
31
+ // (reduced-motion aware). Without it an `UnstyledButton` is the only pressable
32
+ // in the kit that gives NO feedback on press, so custom controls built on it
33
+ // read as dead next to their built-in siblings. Descendants that add their own
34
+ // `pressStyle` (e.g. a background shift) merge with this rather than replace
35
+ // it, exactly as `Chip`'s per-state colours do.
36
+ ...pressScaleStyle,
22
37
 
23
38
  variants: {
24
39
  disabled: {
@@ -43,6 +58,8 @@ export interface UnstyledButtonProps extends UnstyledButtonFrameProps {
43
58
  const UnstyledButtonComponent = UnstyledButtonFrame.styleable<UnstyledButtonProps>(
44
59
  function UnstyledButton(props, ref) {
45
60
  const { children, disabled, style, ...rest } = props;
61
+ // Reduced-motion-aware press easing (neutralises the base dip when reduced).
62
+ const press = usePressScale();
46
63
  // On web the semantic `<button>` host carries a UA `text-align: center` that
47
64
  // would centre text content — diverging from native, where a pressable's text
48
65
  // starts at the inline edge. `webButtonTextReset()` neutralises that (web-only)
@@ -54,6 +71,7 @@ const UnstyledButtonComponent = UnstyledButtonFrame.styleable<UnstyledButtonProp
54
71
  <UnstyledButtonFrame
55
72
  ref={ref}
56
73
  disabled={disabled}
74
+ {...press}
57
75
  {...rest}
58
76
  aria-disabled={disabled || undefined}
59
77
  {...webButton()}
@@ -0,0 +1,359 @@
1
+ import type * as React from "react";
2
+
3
+ import type { BoxProps } from "../Box";
4
+ import type { SlotStyles } from "../internal/styles";
5
+
6
+ /* -------------------------------------------------------------------------- */
7
+ /* Public types */
8
+ /* -------------------------------------------------------------------------- */
9
+
10
+ /** Argument passed to {@link VirtualListProps.renderItem} for one row. */
11
+ export interface VirtualListRenderItemInfo<T> {
12
+ /** The data item for this row. */
13
+ item: T;
14
+ /** Its index into `data`. */
15
+ index: number;
16
+ }
17
+
18
+ /**
19
+ * Uniform per-slot style passthrough for `VirtualList` (Pillar B). Each key
20
+ * spreads onto the matching part, layered after the discrete props.
21
+ * Slots: `scrollArea` (the underlying scroller frame) / `content` (the sized
22
+ * spacer that holds the absolutely-positioned rows) / `item` (each row wrapper).
23
+ */
24
+ export interface VirtualListStyles {
25
+ /** Props spread onto the underlying `ScrollArea` frame. */
26
+ scrollArea?: Partial<BoxProps>;
27
+ /** Props spread onto the total-size content spacer. */
28
+ content?: Partial<BoxProps>;
29
+ /** Props spread onto each absolutely-positioned row wrapper. */
30
+ item?: Partial<BoxProps>;
31
+ }
32
+
33
+ /** Slot names of {@link VirtualListStyles}, for `slotStyles`. */
34
+ export const VIRTUAL_LIST_SLOTS = [
35
+ "scrollArea",
36
+ "content",
37
+ "item",
38
+ ] as const satisfies readonly (keyof VirtualListStyles)[];
39
+
40
+ /**
41
+ * Imperative handle exposed on the `VirtualList` ref (and the optional
42
+ * `handleRef`). Drives the list programmatically — scroll to an index, an
43
+ * absolute offset, or an end — on both web and native.
44
+ */
45
+ export interface VirtualListHandle {
46
+ /**
47
+ * Reveal a row by index. `viewPosition` aligns it within the viewport
48
+ * (`0` top / `0.5` centre / `1` bottom; default `0`); `viewOffset` nudges the
49
+ * resting position by px.
50
+ */
51
+ scrollToIndex(options: {
52
+ index: number;
53
+ animated?: boolean;
54
+ viewPosition?: number;
55
+ viewOffset?: number;
56
+ }): void;
57
+ /** Scroll to an absolute vertical offset (px). */
58
+ scrollToOffset(options: { offset: number; animated?: boolean }): void;
59
+ /** Scroll to the very top. */
60
+ scrollToTop(animated?: boolean): void;
61
+ /** Scroll to the very end (bottom). */
62
+ scrollToEnd(animated?: boolean): void;
63
+ /** The current vertical scroll offset (px). */
64
+ getScrollOffset(): number;
65
+ }
66
+
67
+ /**
68
+ * Props shared across the (single, cross-platform) `VirtualList`
69
+ * implementation. Mirrors the familiar `FlatList` / `FlashList` surface for the
70
+ * subset v1 covers: a vertical, single-column, variable-height, windowed list.
71
+ */
72
+ export interface VirtualListOwnProps<T> {
73
+ /** The data to render, one row per element. */
74
+ data: ReadonlyArray<T>;
75
+
76
+ /** Render one row. Kept referentially stable (or memoize your rows). */
77
+ renderItem: (info: VirtualListRenderItemInfo<T>) => React.ReactNode;
78
+
79
+ /**
80
+ * Stable identity per row. Defaults to the index, which is only safe for
81
+ * append-only data — supply this whenever rows can be inserted/removed/reordered
82
+ * so measurements and React state track the right item.
83
+ */
84
+ keyExtractor?: (item: T, index: number) => string;
85
+
86
+ /**
87
+ * Group rows into recycle/estimation buckets. Rows of the same type share a
88
+ * running average size, so heterogeneous lists estimate each kind separately.
89
+ * @default a single bucket
90
+ */
91
+ getItemType?: (item: T, index: number) => string | number;
92
+
93
+ /**
94
+ * Seed size (px) for a not-yet-measured row, per axis (here: height). Only an
95
+ * initial guess — measured rows immediately override it and feed the per-type
96
+ * running average. A closer value means less first-scroll correction.
97
+ * @default 200
98
+ */
99
+ estimatedItemSize?: number;
100
+
101
+ /**
102
+ * Extra distance (px) rendered beyond each edge of the viewport, so rows exist
103
+ * slightly before they scroll in. @default 250
104
+ */
105
+ drawDistance?: number;
106
+
107
+ /** Re-render trigger for `renderItem` when external state changes (PureComponent-style). */
108
+ extraData?: unknown;
109
+
110
+ /* ---- edge callbacks ---------------------------------------------------- */
111
+
112
+ /** Fired once as the end (bottom) comes within `onEndReachedThreshold` viewports. */
113
+ onEndReached?: () => void;
114
+ /**
115
+ * Distance from the end at which `onEndReached` fires, in units of visible
116
+ * length (a viewport). @default 0.5
117
+ */
118
+ onEndReachedThreshold?: number;
119
+
120
+ /**
121
+ * The set of currently-rendered row indices whenever it changes. Useful for
122
+ * prefetch/telemetry; not a viewability report (that is phase 2).
123
+ */
124
+ onRenderedRangeChange?: (range: { start: number; end: number }) => void;
125
+
126
+ /* ---- chrome slots ------------------------------------------------------ */
127
+
128
+ /** Rendered once above the first row (scrolls with content). */
129
+ ListHeaderComponent?: React.ReactNode;
130
+ /** Rendered once below the last row (scrolls with content). */
131
+ ListFooterComponent?: React.ReactNode;
132
+ /** Rendered instead of rows when `data` is empty. */
133
+ ListEmptyComponent?: React.ReactNode;
134
+ /** Rendered between rows (not after the last). Its height is folded into each row. */
135
+ ItemSeparatorComponent?: React.ReactNode;
136
+
137
+ /* ---- imperative + style ------------------------------------------------ */
138
+
139
+ /** Imperative handle; an alternative to the forwarded `ref`. */
140
+ handleRef?: React.Ref<VirtualListHandle>;
141
+
142
+ /** Uniform per-slot style passthrough. See {@link VirtualListStyles}. */
143
+ styles?: SlotStyles<VirtualListStyles>;
144
+ }
145
+
146
+ /* -------------------------------------------------------------------------- */
147
+ /* Constants */
148
+ /* -------------------------------------------------------------------------- */
149
+
150
+ /** Default seed size for an unmeasured row (px). */
151
+ export const DEFAULT_ESTIMATED_ITEM_SIZE = 200;
152
+ /** Default render-ahead buffer beyond each viewport edge (px). */
153
+ export const DEFAULT_DRAW_DISTANCE = 250;
154
+ /** Default `onEndReached` threshold, in viewports. */
155
+ export const DEFAULT_END_REACHED_THRESHOLD = 0.5;
156
+ /** Height change (px) below which a measurement is treated as unchanged. */
157
+ const MEASURE_EPSILON = 0.5;
158
+
159
+ /* -------------------------------------------------------------------------- */
160
+ /* Layout store (pure, unit-tested) */
161
+ /* -------------------------------------------------------------------------- */
162
+
163
+ /**
164
+ * Cumulative running mean of every measured size for a type. Kept as a
165
+ * converging mean (running sum + count) rather than a short rolling window so
166
+ * the estimate *stabilises* as more rows are measured: each new sample moves it
167
+ * by only `1 / count`, so its ripple into the not-yet-measured rows' total
168
+ * height (and therefore the scrollbar thumb) decays toward zero instead of
169
+ * oscillating up and down on every measurement.
170
+ */
171
+ interface AverageBucket {
172
+ sum: number;
173
+ count: number;
174
+ }
175
+
176
+ /**
177
+ * Mutable layout model for a variable-height, single-axis list. Holds each row
178
+ * wrapper's measured height (or a per-type estimate until measured) and lazily
179
+ * maintains the cumulative offset array used for windowing. Kept framework-free
180
+ * so it is fully unit-testable; the component owns one instance in a ref.
181
+ */
182
+ export interface LayoutState {
183
+ count: number;
184
+ estimate: number;
185
+ /** Measured wrapper height per index (includes a folded separator). */
186
+ heights: number[];
187
+ measured: boolean[];
188
+ types: Array<string | number>;
189
+ /** `offsets[i]` = top of row `i` within the content; `offsets[count]` = total. */
190
+ offsets: number[];
191
+ averages: Map<string | number, AverageBucket>;
192
+ /** Lowest index whose offset may be stale; `count + 1` means clean. */
193
+ dirtyFrom: number;
194
+ }
195
+
196
+ const DEFAULT_TYPE = 0;
197
+
198
+ /** Create a layout store for `count` rows, seeding every row with `estimate`. */
199
+ export const createLayoutState = (count: number, estimate: number): LayoutState => ({
200
+ count,
201
+ estimate,
202
+ heights: new Array(count).fill(estimate),
203
+ measured: new Array(count).fill(false),
204
+ types: new Array(count).fill(DEFAULT_TYPE),
205
+ offsets: new Array(count + 1).fill(0),
206
+ averages: new Map(),
207
+ dirtyFrom: 0,
208
+ });
209
+
210
+ const bucketAverage = (bucket: AverageBucket | undefined, fallback: number): number => {
211
+ if (!bucket || bucket.count === 0) return fallback;
212
+ return bucket.sum / bucket.count;
213
+ };
214
+
215
+ /** Estimated height for an unmeasured row of `type`, off its running average. */
216
+ const estimateFor = (state: LayoutState, type: string | number): number =>
217
+ bucketAverage(state.averages.get(type), state.estimate);
218
+
219
+ /** Height of row `i` — its measurement if present, else the per-type estimate. */
220
+ export const heightAt = (state: LayoutState, i: number): number =>
221
+ state.measured[i] ? state.heights[i] : estimateFor(state, state.types[i]);
222
+
223
+ /**
224
+ * Grow/shrink the store to `count` rows, preserving existing measurements by
225
+ * index (correct for append/pop; a prepend/reorder without a stable
226
+ * `keyExtractor` would misattribute — a known v1 limitation). Returns the new
227
+ * state (same instance, mutated) so callers can keep their ref.
228
+ */
229
+ export const resizeLayoutState = (state: LayoutState, count: number): LayoutState => {
230
+ if (count === state.count) return state;
231
+ const keep = Math.min(count, state.count);
232
+ if (count > state.count) {
233
+ for (let i = state.count; i < count; i++) {
234
+ state.heights.push(state.estimate);
235
+ state.measured.push(false);
236
+ state.types.push(DEFAULT_TYPE);
237
+ }
238
+ } else {
239
+ state.heights.length = count;
240
+ state.measured.length = count;
241
+ state.types.length = count;
242
+ }
243
+ state.offsets.length = count + 1;
244
+ state.count = count;
245
+ state.dirtyFrom = Math.min(state.dirtyFrom, keep);
246
+ return state;
247
+ };
248
+
249
+ /**
250
+ * Record a measured wrapper height for row `i` of `type`. Updates the per-type
251
+ * running average and marks offsets stale from `i`. Returns `true` when the
252
+ * height changed beyond {@link MEASURE_EPSILON} (i.e. a relayout is warranted).
253
+ */
254
+ export const setMeasured = (
255
+ state: LayoutState,
256
+ i: number,
257
+ type: string | number,
258
+ height: number,
259
+ ): boolean => {
260
+ if (i < 0 || i >= state.count) return false;
261
+ // A non-positive height is not a usable measurement (hidden/teardown layouts,
262
+ // and jsdom, report 0) — keep the estimate until a real height arrives.
263
+ if (!(height > 0)) return false;
264
+ const typeChanged = state.types[i] !== type;
265
+ const changed = !state.measured[i] || Math.abs(state.heights[i] - height) > MEASURE_EPSILON;
266
+ state.types[i] = type;
267
+
268
+ if (!changed && !typeChanged) return false;
269
+
270
+ // Feed the type's cumulative running mean (only real measurements contribute).
271
+ let bucket = state.averages.get(type);
272
+ if (!bucket) {
273
+ bucket = { sum: 0, count: 0 };
274
+ state.averages.set(type, bucket);
275
+ }
276
+ bucket.sum += height;
277
+ bucket.count += 1;
278
+
279
+ state.heights[i] = height;
280
+ state.measured[i] = true;
281
+ state.dirtyFrom = Math.min(state.dirtyFrom, i);
282
+ return true;
283
+ };
284
+
285
+ /** Recompute cumulative offsets forward from `dirtyFrom` (lazy; idempotent). */
286
+ const ensureOffsets = (state: LayoutState): void => {
287
+ if (state.dirtyFrom > state.count) return;
288
+ const from = Math.max(0, state.dirtyFrom);
289
+ for (let i = from; i < state.count; i++) {
290
+ state.offsets[i + 1] = state.offsets[i] + heightAt(state, i);
291
+ }
292
+ state.dirtyFrom = state.count + 1;
293
+ };
294
+
295
+ /** Total content height (px) across all rows. */
296
+ export const getContentSize = (state: LayoutState): number => {
297
+ ensureOffsets(state);
298
+ return state.count === 0 ? 0 : state.offsets[state.count];
299
+ };
300
+
301
+ /** Top offset (px) of row `i` within the content. */
302
+ export const getItemOffset = (state: LayoutState, i: number): number => {
303
+ ensureOffsets(state);
304
+ return state.offsets[Math.max(0, Math.min(i, state.count))];
305
+ };
306
+
307
+ /**
308
+ * Smallest row index whose bottom edge (`offsets[i + 1]`) is past `target`, i.e.
309
+ * the first row at least partially below `target`. Returns the last index if
310
+ * every row ends at/before `target` (scrolled past all content).
311
+ */
312
+ const firstRowEndingAfter = (state: LayoutState, target: number): number => {
313
+ let lo = 0;
314
+ let hi = state.count - 1;
315
+ while (lo < hi) {
316
+ const mid = (lo + hi) >> 1;
317
+ if (state.offsets[mid + 1] > target) hi = mid;
318
+ else lo = mid + 1;
319
+ }
320
+ return lo;
321
+ };
322
+
323
+ /**
324
+ * Largest row index whose top edge (`offsets[i]`) is before `target`, i.e. the
325
+ * last row at least partially above `target`. Returns `0` if no row starts before.
326
+ */
327
+ const lastRowStartingBefore = (state: LayoutState, target: number): number => {
328
+ let lo = 0;
329
+ let hi = state.count - 1;
330
+ while (lo < hi) {
331
+ const mid = (lo + hi + 1) >> 1;
332
+ if (state.offsets[mid] < target) lo = mid;
333
+ else hi = mid - 1;
334
+ }
335
+ return lo;
336
+ };
337
+
338
+ /**
339
+ * Index range `[start, end]` to mount for a scroll position, padded by
340
+ * `overscan` px on each side. `end` is `-1` (empty range) when there are no rows;
341
+ * when all content fits within the viewport every row is mounted.
342
+ */
343
+ export const findVisibleRange = (
344
+ state: LayoutState,
345
+ scrollStart: number,
346
+ viewportSize: number,
347
+ overscan: number,
348
+ ): { start: number; end: number } => {
349
+ if (state.count === 0) return { start: 0, end: -1 };
350
+ ensureOffsets(state);
351
+ const total = state.offsets[state.count];
352
+ if (total <= viewportSize) return { start: 0, end: state.count - 1 };
353
+
354
+ const top = Math.max(0, scrollStart - overscan);
355
+ const bottom = scrollStart + viewportSize + overscan;
356
+ const start = firstRowEndingAfter(state, top);
357
+ const end = Math.max(start, lastRowStartingBefore(state, bottom));
358
+ return { start, end };
359
+ };