basalt-ui 2.27.2 → 2.29.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 (343) hide show
  1. package/CHANGELOG.md +466 -307
  2. package/MIGRATING.md +358 -12
  3. package/agent/rules/basalt-batteries.md +1 -1
  4. package/agent/rules/basalt-mantine.md +1 -1
  5. package/agent/rules/basalt-tokens.md +2 -2
  6. package/agent/skills/basalt-app/SKILL.md +3 -3
  7. package/agent/skills/basalt-charts/SKILL.md +1 -1
  8. package/agent/skills/basalt-design/SKILL.md +12 -6
  9. package/agent/skills/basalt-design/references/catalogue.md +42 -36
  10. package/agent/skills/basalt-design/references/recipes.md +47 -0
  11. package/agent/skills/basalt-design/references/states.md +1 -1
  12. package/configs/oxlint-basalt.json +2 -1
  13. package/configs/oxlint-plugin.js +112 -3
  14. package/configs/oxlint-plugin.test.ts +99 -4
  15. package/configs/oxlint.json +1 -0
  16. package/dist/agent/chat/thread-message.d.ts +1 -160
  17. package/dist/agent/chat/thread-message.d.ts.map +1 -1
  18. package/dist/agent/chat/thread-message.js +7 -162
  19. package/dist/agent/chat/virtualize.d.ts +5 -5
  20. package/dist/charts/ChartCard.d.ts +14 -7
  21. package/dist/charts/ChartCard.d.ts.map +1 -1
  22. package/dist/charts/ChartCard.js +112 -23
  23. package/dist/charts/Lines.d.ts.map +1 -1
  24. package/dist/charts/Lines.js +11 -4
  25. package/dist/charts/Sparkline.d.ts +14 -2
  26. package/dist/charts/Sparkline.d.ts.map +1 -1
  27. package/dist/charts/Sparkline.js +155 -44
  28. package/dist/charts/chart-box.module.css +59 -2
  29. package/dist/charts/chart-box.module.css.d.ts +5 -0
  30. package/dist/charts/chart-card.module.css +28 -17
  31. package/dist/charts/chart-card.module.css.d.ts +3 -0
  32. package/dist/charts/compile.d.ts +1 -1
  33. package/dist/charts/compile.d.ts.map +1 -1
  34. package/dist/charts/compile.js +38 -27
  35. package/dist/charts/labels.d.ts +13 -3
  36. package/dist/charts/labels.d.ts.map +1 -1
  37. package/dist/charts/labels.js +131 -20
  38. package/dist/charts/legend.d.ts.map +1 -1
  39. package/dist/charts/legend.js +1 -1
  40. package/dist/charts/ref-label.d.ts +37 -8
  41. package/dist/charts/ref-label.d.ts.map +1 -1
  42. package/dist/charts/ref-label.js +55 -10
  43. package/dist/charts/sparkline.module.css +50 -0
  44. package/dist/charts/sparkline.module.css.d.ts +3 -0
  45. package/dist/charts/state.d.ts +4 -4
  46. package/dist/charts/state.d.ts.map +1 -1
  47. package/dist/charts/state.js +23 -5
  48. package/dist/cli/doctor-upgrade.d.ts +11 -0
  49. package/dist/cli/doctor-upgrade.d.ts.map +1 -1
  50. package/dist/cli/doctor-upgrade.js +54 -0
  51. package/dist/cli/doctor.d.ts.map +1 -1
  52. package/dist/cli/doctor.js +32 -0
  53. package/dist/cli/help.d.ts.map +1 -1
  54. package/dist/cli/help.js +8 -3
  55. package/dist/cli/index.d.ts +13 -13
  56. package/dist/cli/index.d.ts.map +1 -1
  57. package/dist/cli/index.js +36 -2
  58. package/dist/cli/init.d.ts +6 -0
  59. package/dist/cli/init.d.ts.map +1 -1
  60. package/dist/cli/init.js +118 -27
  61. package/dist/cli/router-wiring.d.ts +3 -0
  62. package/dist/cli/router-wiring.d.ts.map +1 -1
  63. package/dist/cli/router-wiring.js +2 -0
  64. package/dist/cli/shot/capture.d.ts +34 -2
  65. package/dist/cli/shot/capture.d.ts.map +1 -1
  66. package/dist/cli/shot/capture.js +95 -12
  67. package/dist/cli/shot/detect.d.ts +4 -1
  68. package/dist/cli/shot/detect.d.ts.map +1 -1
  69. package/dist/cli/shot/detect.js +38 -16
  70. package/dist/cli/shot/index.d.ts.map +1 -1
  71. package/dist/cli/shot/index.js +9 -2
  72. package/dist/cli/shot/measure.d.ts.map +1 -1
  73. package/dist/cli/shot/measure.js +11 -2
  74. package/dist/cli/shot/types.d.ts +4 -1
  75. package/dist/cli/shot/types.d.ts.map +1 -1
  76. package/dist/cli/sync.d.ts.map +1 -1
  77. package/dist/cli/sync.js +10 -2
  78. package/dist/common/could-not-load.d.ts +13 -0
  79. package/dist/common/could-not-load.d.ts.map +1 -0
  80. package/dist/common/could-not-load.js +14 -0
  81. package/dist/common/empty-copy.d.ts +16 -0
  82. package/dist/common/empty-copy.d.ts.map +1 -0
  83. package/dist/common/empty-copy.js +9 -0
  84. package/dist/common/index.d.ts +1 -0
  85. package/dist/common/index.d.ts.map +1 -1
  86. package/dist/common/index.js +3 -0
  87. package/dist/common/props.d.ts +20 -0
  88. package/dist/common/props.d.ts.map +1 -1
  89. package/dist/common/props.js +13 -1
  90. package/dist/common/retry-button.module.css +7 -3
  91. package/dist/common/virtual-rows.d.ts +196 -0
  92. package/dist/common/virtual-rows.d.ts.map +1 -0
  93. package/dist/common/virtual-rows.js +290 -0
  94. package/dist/controls/controls.module.css +33 -1
  95. package/dist/controls/controls.module.css.d.ts +2 -0
  96. package/dist/controls/enum-filter.d.ts.map +1 -1
  97. package/dist/controls/enum-filter.js +8 -2
  98. package/dist/controls/filter-pill.d.ts +27 -3
  99. package/dist/controls/filter-pill.d.ts.map +1 -1
  100. package/dist/controls/filter-pill.js +15 -4
  101. package/dist/controls/form-layout.d.ts +9 -3
  102. package/dist/controls/form-layout.d.ts.map +1 -1
  103. package/dist/controls/form-layout.js +74 -12
  104. package/dist/controls/form-layout.module.css +112 -7
  105. package/dist/controls/form-layout.module.css.d.ts +1 -0
  106. package/dist/controls/multi-select-filter.d.ts +2 -2
  107. package/dist/controls/multi-select-filter.d.ts.map +1 -1
  108. package/dist/controls/multi-select-filter.js +12 -6
  109. package/dist/controls/range-filter.d.ts +1 -1
  110. package/dist/controls/range-filter.d.ts.map +1 -1
  111. package/dist/controls/range-filter.js +8 -2
  112. package/dist/dashboard/bar-list.d.ts +5 -5
  113. package/dist/dashboard/bar-list.d.ts.map +1 -1
  114. package/dist/dashboard/bar-list.js +15 -8
  115. package/dist/dashboard/bar-list.module.css +21 -5
  116. package/dist/dashboard/board-nav.d.ts +22 -0
  117. package/dist/dashboard/board-nav.d.ts.map +1 -0
  118. package/dist/dashboard/board-nav.js +49 -0
  119. package/dist/dashboard/board.d.ts +52 -0
  120. package/dist/dashboard/board.d.ts.map +1 -0
  121. package/dist/dashboard/board.js +247 -0
  122. package/dist/dashboard/board.module.css +179 -0
  123. package/dist/dashboard/board.module.css.d.ts +28 -0
  124. package/dist/dashboard/empty-state.d.ts +6 -6
  125. package/dist/dashboard/empty-state.d.ts.map +1 -1
  126. package/dist/dashboard/empty-state.js +17 -8
  127. package/dist/dashboard/event-list-model.d.ts +54 -0
  128. package/dist/dashboard/event-list-model.d.ts.map +1 -0
  129. package/dist/dashboard/event-list-model.js +76 -0
  130. package/dist/dashboard/event-list-rows.d.ts +23 -0
  131. package/dist/dashboard/event-list-rows.d.ts.map +1 -0
  132. package/dist/dashboard/event-list-rows.js +89 -0
  133. package/dist/dashboard/event-list-scroll.d.ts +55 -0
  134. package/dist/dashboard/event-list-scroll.d.ts.map +1 -0
  135. package/dist/dashboard/event-list-scroll.js +216 -0
  136. package/dist/dashboard/event-list-skeleton.d.ts +10 -0
  137. package/dist/dashboard/event-list-skeleton.d.ts.map +1 -0
  138. package/dist/dashboard/event-list-skeleton.js +34 -0
  139. package/dist/dashboard/event-list.d.ts +65 -0
  140. package/dist/dashboard/event-list.d.ts.map +1 -0
  141. package/dist/dashboard/event-list.js +208 -0
  142. package/dist/dashboard/event-list.module.css +336 -0
  143. package/dist/dashboard/event-list.module.css.d.ts +34 -0
  144. package/dist/dashboard/index.d.ts +2 -0
  145. package/dist/dashboard/index.d.ts.map +1 -1
  146. package/dist/dashboard/index.js +4 -0
  147. package/dist/dashboard/meter-model.d.ts +6 -3
  148. package/dist/dashboard/meter-model.d.ts.map +1 -1
  149. package/dist/dashboard/meter-model.js +9 -4
  150. package/dist/dashboard/meter-parts.d.ts +3 -0
  151. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  152. package/dist/dashboard/meter-parts.js +36 -6
  153. package/dist/dashboard/meter.d.ts +3 -3
  154. package/dist/dashboard/meter.d.ts.map +1 -1
  155. package/dist/dashboard/meter.js +10 -2
  156. package/dist/dashboard/meter.module.css +20 -12
  157. package/dist/dashboard/meter.module.css.d.ts +0 -1
  158. package/dist/dashboard/page-summary.d.ts +3 -3
  159. package/dist/dashboard/page-summary.d.ts.map +1 -1
  160. package/dist/dashboard/page-summary.js +7 -2
  161. package/dist/dashboard/page-summary.module.css +5 -2
  162. package/dist/dashboard/query-state.d.ts +9 -8
  163. package/dist/dashboard/query-state.d.ts.map +1 -1
  164. package/dist/dashboard/query-state.js +29 -11
  165. package/dist/dashboard/section.d.ts +11 -3
  166. package/dist/dashboard/section.d.ts.map +1 -1
  167. package/dist/dashboard/section.js +34 -7
  168. package/dist/dashboard/settings-section.d.ts +8 -5
  169. package/dist/dashboard/settings-section.d.ts.map +1 -1
  170. package/dist/dashboard/settings-section.js +12 -8
  171. package/dist/dashboard/settings-section.module.css +25 -4
  172. package/dist/dashboard/settings-section.module.css.d.ts +1 -0
  173. package/dist/dashboard/stat-card-parts.d.ts +5 -1
  174. package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
  175. package/dist/dashboard/stat-card-parts.js +8 -3
  176. package/dist/dashboard/stat-card.d.ts +3 -3
  177. package/dist/dashboard/stat-card.d.ts.map +1 -1
  178. package/dist/dashboard/stat-card.js +47 -12
  179. package/dist/dashboard/stat-card.module.css +42 -7
  180. package/dist/dashboard/stat-card.module.css.d.ts +3 -0
  181. package/dist/dashboard/stat-group.d.ts +2 -2
  182. package/dist/dashboard/stat-group.d.ts.map +1 -1
  183. package/dist/dashboard/stat-group.js +10 -3
  184. package/dist/dashboard/stat-strip.d.ts +5 -4
  185. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  186. package/dist/dashboard/stat-strip.js +41 -31
  187. package/dist/dashboard/stat-strip.module.css +8 -6
  188. package/dist/dashboard/visual-slot.d.ts +4 -0
  189. package/dist/dashboard/visual-slot.d.ts.map +1 -1
  190. package/dist/dashboard/visual-slot.js +2 -0
  191. package/dist/dashboard/widget-header.d.ts +6 -6
  192. package/dist/dashboard/widget-header.d.ts.map +1 -1
  193. package/dist/dashboard/widget-header.js +4 -2
  194. package/dist/dashboard/widget-header.module.css +12 -15
  195. package/dist/format/shared.d.ts.map +1 -1
  196. package/dist/format/shared.js +24 -1
  197. package/dist/guard/index.d.ts +1 -1
  198. package/dist/guard/index.d.ts.map +1 -1
  199. package/dist/guard/index.js +4 -1
  200. package/dist/index.d.ts +1 -1
  201. package/dist/index.d.ts.map +1 -1
  202. package/dist/index.js +4 -0
  203. package/dist/shell/app-main.module.css +18 -0
  204. package/dist/styles.css +69 -0
  205. package/dist/surfaces.d.ts +2 -2
  206. package/dist/surfaces.d.ts.map +1 -1
  207. package/dist/surfaces.js +8 -2
  208. package/dist/table/cell-kinds.d.ts +47 -0
  209. package/dist/table/cell-kinds.d.ts.map +1 -0
  210. package/dist/table/cell-kinds.js +180 -0
  211. package/dist/table/cell-kinds.module.css +55 -0
  212. package/dist/table/cell-kinds.module.css.d.ts +15 -0
  213. package/dist/table/data-table-fold.d.ts +155 -0
  214. package/dist/table/data-table-fold.d.ts.map +1 -0
  215. package/dist/table/data-table-fold.js +320 -0
  216. package/dist/table/data-table-state.d.ts +10 -3
  217. package/dist/table/data-table-state.d.ts.map +1 -1
  218. package/dist/table/data-table-state.js +11 -6
  219. package/dist/table/data-table.d.ts +13 -28
  220. package/dist/table/data-table.d.ts.map +1 -1
  221. package/dist/table/data-table.js +362 -255
  222. package/dist/table/data-table.module.css +282 -3
  223. package/dist/table/data-table.module.css.d.ts +16 -0
  224. package/dist/table/table-features.d.ts +34 -4
  225. package/dist/table/table-features.d.ts.map +1 -1
  226. package/dist/table/table-features.js +10 -0
  227. package/dist/theme/card-inset.module.css +35 -0
  228. package/dist/theme/controls.module.css +55 -2
  229. package/dist/theme/ctl-theme.d.ts.map +1 -1
  230. package/dist/theme/ctl-theme.js +5 -0
  231. package/dist/theme/index.d.ts.map +1 -1
  232. package/dist/theme/index.js +14 -10
  233. package/dist/tokens/core.d.ts +1 -0
  234. package/dist/tokens/core.d.ts.map +1 -1
  235. package/dist/tokens/core.js +1 -0
  236. package/dist/tokens/derive.js +1 -1
  237. package/dist/tokens/palette.d.ts +2 -1
  238. package/dist/tokens/palette.d.ts.map +1 -1
  239. package/llms.txt +295 -5
  240. package/package.json +1 -1
  241. package/src/agent/chat/thread-message.tsx +10 -403
  242. package/src/agent/chat/virtualize.ts +5 -5
  243. package/src/charts/ChartCard.tsx +175 -31
  244. package/src/charts/Lines.tsx +13 -4
  245. package/src/charts/Sparkline.tsx +213 -48
  246. package/src/charts/chart-box.module.css +59 -2
  247. package/src/charts/chart-box.module.css.d.ts +5 -0
  248. package/src/charts/chart-card.module.css +28 -17
  249. package/src/charts/chart-card.module.css.d.ts +3 -0
  250. package/src/charts/compile.ts +59 -33
  251. package/src/charts/labels.ts +175 -24
  252. package/src/charts/legend.tsx +3 -2
  253. package/src/charts/ref-label.ts +114 -18
  254. package/src/charts/sparkline.module.css +50 -0
  255. package/src/charts/sparkline.module.css.d.ts +3 -0
  256. package/src/charts/state.tsx +40 -10
  257. package/src/cli/doctor-upgrade.ts +75 -0
  258. package/src/cli/doctor.ts +36 -0
  259. package/src/cli/help.ts +16 -6
  260. package/src/cli/index.ts +73 -67
  261. package/src/cli/init.ts +157 -36
  262. package/src/cli/router-wiring.ts +2 -2
  263. package/src/cli/shot/capture.ts +138 -16
  264. package/src/cli/shot/detect.ts +46 -22
  265. package/src/cli/shot/index.ts +8 -2
  266. package/src/cli/shot/measure.ts +11 -2
  267. package/src/cli/shot/types.ts +4 -1
  268. package/src/cli/sync.ts +12 -2
  269. package/src/common/could-not-load.ts +22 -0
  270. package/src/common/empty-copy.ts +17 -0
  271. package/src/common/index.ts +1 -0
  272. package/src/common/props.ts +30 -0
  273. package/src/common/retry-button.module.css +7 -3
  274. package/src/common/virtual-rows.tsx +682 -0
  275. package/src/controls/controls.module.css +33 -1
  276. package/src/controls/controls.module.css.d.ts +2 -0
  277. package/src/controls/enum-filter.tsx +8 -2
  278. package/src/controls/filter-pill.tsx +80 -27
  279. package/src/controls/form-layout.module.css +112 -7
  280. package/src/controls/form-layout.module.css.d.ts +1 -0
  281. package/src/controls/form-layout.tsx +115 -22
  282. package/src/controls/multi-select-filter.tsx +22 -11
  283. package/src/controls/range-filter.tsx +9 -3
  284. package/src/dashboard/bar-list.module.css +21 -5
  285. package/src/dashboard/bar-list.tsx +53 -45
  286. package/src/dashboard/board-nav.ts +67 -0
  287. package/src/dashboard/board.module.css +179 -0
  288. package/src/dashboard/board.module.css.d.ts +28 -0
  289. package/src/dashboard/board.tsx +415 -0
  290. package/src/dashboard/empty-state.tsx +21 -11
  291. package/src/dashboard/event-list-model.ts +125 -0
  292. package/src/dashboard/event-list-rows.tsx +117 -0
  293. package/src/dashboard/event-list-scroll.ts +291 -0
  294. package/src/dashboard/event-list-skeleton.tsx +62 -0
  295. package/src/dashboard/event-list.module.css +336 -0
  296. package/src/dashboard/event-list.module.css.d.ts +34 -0
  297. package/src/dashboard/event-list.tsx +336 -0
  298. package/src/dashboard/index.ts +2 -0
  299. package/src/dashboard/meter-model.ts +16 -7
  300. package/src/dashboard/meter-parts.tsx +53 -14
  301. package/src/dashboard/meter.module.css +20 -12
  302. package/src/dashboard/meter.module.css.d.ts +0 -1
  303. package/src/dashboard/meter.tsx +36 -18
  304. package/src/dashboard/page-summary.module.css +5 -2
  305. package/src/dashboard/page-summary.tsx +13 -6
  306. package/src/dashboard/query-state.tsx +55 -30
  307. package/src/dashboard/section.tsx +83 -41
  308. package/src/dashboard/settings-section.module.css +25 -4
  309. package/src/dashboard/settings-section.module.css.d.ts +1 -0
  310. package/src/dashboard/settings-section.tsx +18 -8
  311. package/src/dashboard/stat-card-parts.tsx +20 -3
  312. package/src/dashboard/stat-card.module.css +42 -7
  313. package/src/dashboard/stat-card.module.css.d.ts +3 -0
  314. package/src/dashboard/stat-card.tsx +55 -13
  315. package/src/dashboard/stat-group.tsx +22 -14
  316. package/src/dashboard/stat-strip.module.css +8 -6
  317. package/src/dashboard/stat-strip.tsx +29 -20
  318. package/src/dashboard/visual-slot.ts +5 -0
  319. package/src/dashboard/widget-header.module.css +12 -15
  320. package/src/dashboard/widget-header.tsx +8 -5
  321. package/src/format/shared.ts +30 -1
  322. package/src/guard/index.ts +7 -1
  323. package/src/index.ts +4 -0
  324. package/src/shell/app-main.module.css +18 -0
  325. package/src/styles.css +69 -0
  326. package/src/surfaces.ts +7 -1
  327. package/src/table/cell-kinds.module.css +55 -0
  328. package/src/table/cell-kinds.module.css.d.ts +15 -0
  329. package/src/table/cell-kinds.tsx +263 -0
  330. package/src/table/data-table-fold.tsx +567 -0
  331. package/src/table/data-table-state.ts +24 -13
  332. package/src/table/data-table.module.css +282 -3
  333. package/src/table/data-table.module.css.d.ts +16 -0
  334. package/src/table/data-table.tsx +430 -362
  335. package/src/table/table-features.ts +44 -4
  336. package/src/theme/__snapshots__/plain-theme.test.ts.snap +2 -66
  337. package/src/theme/card-inset.module.css +35 -0
  338. package/src/theme/controls.module.css +55 -2
  339. package/src/theme/ctl-theme.tsx +8 -0
  340. package/src/theme/index.ts +12 -8
  341. package/src/tokens/core.ts +1 -0
  342. package/src/tokens/derive.ts +1 -1
  343. package/src/tokens/palette.ts +4 -1
@@ -0,0 +1,682 @@
1
+ /**
2
+ * `VirtualRows` — the package's ONE lazy virtual-list seam (SPEC D11 / § 9.3).
3
+ *
4
+ * `@tanstack/react-virtual` is an OPTIONAL peer, so this is the only module that may name it at
5
+ * runtime, and only through a dynamic `import()`: `lazy` + `.catch()` to the plain rows keeps the
6
+ * root entry and `./agent` resolving with the peer absent (a static import here would make every
7
+ * consumer of either require it, even one that never virtualises). Same shape as
8
+ * `../agent/stick-to-bottom.tsx`'s `LazyStickToBottom`. Internal: no barrel exports it.
9
+ *
10
+ * Two scroll owners, one prop (a union, so both cannot be passed):
11
+ * - `height` — the seam renders its own scroll node (the chat transcript's mode).
12
+ * - `scroll: 'parent'` — the rows virtualise against the nearest scrolling ancestor
13
+ * ({@link scrollParentOf}; `null` → the window) with `scrollMargin` = the list's offset inside it.
14
+ * That is an event list on a page inside `BasaltShell`, whose `AppShell.Main` is the scrollport.
15
+ *
16
+ * Verified against the INSTALLED virtual-core 3.18.0 (react-virtual 3.14.14), not memory:
17
+ * `rangeExtractor`, `scrollMargin` (item `start` includes it; the `translateY` below subtracts it,
18
+ * as react-virtual's own `applyItemPosition` does), `gap`, `anchorTo`, `followOnAppend`,
19
+ * `scrollEndThreshold`, `getItemKey`, `measureElement` and `range` are all `VirtualizerOptions`
20
+ * members / instance fields; `useWindowVirtualizer` supplies the window scroll/rect observers.
21
+ * `anchorTo: 'end'` re-resolves the first visible item BY KEY after a count/edge-key change
22
+ * (`setOptions`), which is why `getItemKey` is always `row.key`: a prepend then keeps the previously
23
+ * first-visible row at the same on-screen offset.
24
+ */
25
+ // theme-allow-file raw-html-layout, hidden-inline-style — src/common is Mantine-free by contract (common/boundary.test.ts), so the seam owns plain elements.
26
+ import {
27
+ Fragment,
28
+ lazy,
29
+ Suspense,
30
+ useEffect,
31
+ useLayoutEffect,
32
+ useMemo,
33
+ useRef,
34
+ useState,
35
+ } from 'react'
36
+ import type { ComponentType, CSSProperties, JSX, ReactNode } from 'react'
37
+ import type { Range, Virtualizer } from '@tanstack/react-virtual'
38
+ import { cx } from './props'
39
+ import type { BasaltProps } from './props'
40
+ import { scrollParentOf } from './scroll-parent'
41
+
42
+ export type VirtualRow = {
43
+ readonly key: string
44
+ readonly node: ReactNode
45
+ /** A group header: the active one stays rendered while scrolled and is drawn `position: sticky`. */
46
+ readonly sticky?: boolean
47
+ }
48
+
49
+ type InitialScroll = 'start' | 'end'
50
+
51
+ type ScrollOwner =
52
+ | { readonly height: number | string; readonly scroll?: never }
53
+ | {
54
+ readonly scroll: 'parent'
55
+ readonly height?: never
56
+ /**
57
+ * The caller's own scroll node for the parent mode, instead of the detected scrolling ancestor:
58
+ * lets one stable element own the scroll whether or not its rows are windowed. `null` = not
59
+ * mounted yet (the rows wait for it); omitted = detect the ancestor.
60
+ */
61
+ readonly scrollRoot?: HTMLElement | null
62
+ }
63
+
64
+ export type VirtualRowsProps = BasaltProps &
65
+ ScrollOwner & {
66
+ readonly rows: readonly VirtualRow[]
67
+ /** Extra rows rendered beyond the viewport, each side. */
68
+ readonly overscan: number
69
+ /** Estimated row height in px before a row is measured; estimate toward the LARGER end. */
70
+ readonly estimateSize: number
71
+ /** px between rows. Default 0. */
72
+ readonly gap?: number
73
+ /** Row gap of the peer-absent degrade when it should differ from the windowed `gap`. */
74
+ readonly fallbackGap?: number | string
75
+ /** Where the list sits on first mount. Default 'start'. */
76
+ readonly initialScroll?: InitialScroll
77
+ /** Which edge a count/edge-key change keeps fixed. Default 'start'. */
78
+ readonly anchorTo?: 'start' | 'end'
79
+ /** Follow an append while at the end. Default false. */
80
+ readonly followOnAppend?: boolean
81
+ }
82
+
83
+ /** `scroll: 'parent'` resolved: the scrolling ancestor (`null` → window) and the list's offset in it. */
84
+ type ParentScroll = { readonly element: HTMLElement | null; readonly margin: number }
85
+
86
+ type InnerProps = VirtualRowsProps & { readonly parent?: ParentScroll }
87
+
88
+ /** Opaque sticky-header backdrop; a caller overrides `--virtual-rows-sticky-bg` via `className`. */
89
+ const STICKY_STYLE: CSSProperties = {
90
+ position: 'sticky',
91
+ top: 0,
92
+ zIndex: 1,
93
+ background: 'var(--virtual-rows-sticky-bg, var(--vx-surface-bg))',
94
+ }
95
+
96
+ /** How close (px) to the end still counts as "at the end" for `followOnAppend`/`anchorTo: 'end'`. */
97
+ const SCROLL_END_THRESHOLD = 64
98
+
99
+ /** Caps `applyInitialScroll`'s clobber-recovery retries (see `agent/chat/virtualize.ts`'s fourth composition
100
+ * rule for the full mechanism this guards). A small fixed budget, not a time/frame-based deadline:
101
+ * every observed case of virtual-core's `_willUpdate` clobbering this effect's jump resolves within
102
+ * one extra commit, so this exists purely as a backstop against a pathological DOM that never
103
+ * reports settled — not as the expected path. Once exhausted, the effect gives up and marks the
104
+ * jump permanently done wherever the last attempt landed, rather than fighting the DOM (or a user
105
+ * who has since scrolled away) forever. Exported (like `messageBlockRenderCounter`) purely so its
106
+ * test can assert the exact bound without duplicating the number; not part of the public surface. */
107
+ export const MAX_INITIAL_SCROLL_ATTEMPTS = 5
108
+
109
+ /**
110
+ * Minimal structural slice of `Virtualizer` (virtual-core 3.18.0) that
111
+ * {@link resolveGuardedMeasurement} needs — `indexFromElement`/`itemSizeCache`/`options` are all
112
+ * public instance members, but typing against the real (generic, peer-only) `Virtualizer` class
113
+ * would force this test-only-exported helper to import the peer's runtime types at module scope,
114
+ * the exact static-import-forces-the-peer problem the surrounding `lazy(() => import(...))` exists
115
+ * to avoid. A structural type sidesteps that without loosening to `any`. Generic over the item
116
+ * element type (rather than fixed at `Element`) so it stays exactly assignable FROM a real
117
+ * `Virtualizer<TScrollElement, TItemElement>` at the call site below — `indexFromElement` is
118
+ * declared with arrow-property (not method) syntax upstream, so it is checked contravariantly, and
119
+ * a fixed `Element` parameter there does not structurally match a real instance's narrower
120
+ * `TItemElement` parameter.
121
+ */
122
+ type MeasurementCacheHost<TItemElement extends Element> = {
123
+ readonly indexFromElement: (node: TItemElement) => number
124
+ readonly itemSizeCache: Map<unknown, number>
125
+ readonly options: {
126
+ readonly getItemKey: (index: number) => unknown
127
+ readonly estimateSize: (index: number) => number
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Resolves the size a `measureElement` implementation should report, given what the underlying
133
+ * (default or custom) measurement already produced: that value unchanged when it is a genuine
134
+ * measurement, or the row's own last-known size when it is `0` — never a real reading for a mounted
135
+ * row, so a `0` only ever means the ResizeObserver measured through a `display: none` (or otherwise
136
+ * unlaid-out) ancestor, e.g. a virtualized transcript sitting inside a collapsed `ThreadFeedRow`.
137
+ * Falling through to it unguarded is what corrupted `itemSizeCache`/`measurementsCache`: virtual-
138
+ * core's default `resizeItem` writes whatever `measureElement` returns with no floor, so every
139
+ * mounted row remeasures at 0px the instant an ancestor hides, `getTotalSize()` collapses within
140
+ * the same tick, and the collapse is never undone — a later real remeasurement only OVERWRITES
141
+ * entries the ResizeObserver happens to re-fire for, it does not restore ones that silently kept
142
+ * their poisoned 0 (see `virtualize.ts`'s composition-rule doc for the full mechanism and the
143
+ * measured numbers).
144
+ *
145
+ * Takes the already-computed `size` plus the {@link MeasurementCacheHost} slice, rather than
146
+ * wrapping the `measureElement` function itself, so this stays a plain, non-generic-over-a-
147
+ * generic-function call at its one call site below (`instance` there is `Virtualizer<TScrollElement,
148
+ * TItemElement>` for THAT call's concrete type arguments, not the peer's own generic
149
+ * `measureElement` export — wrapping the latter directly hits TS's higher-rank inference limits on
150
+ * a doubly-generic HOF). That shape is also what makes this unit-testable with a fake host object
151
+ * and no ResizeObserver/layout engine at all (happy-dom has neither). Not exported from
152
+ * `agent-chat/index.ts`: a test-only escape hatch, matching
153
+ * `messageBlockRenderCounter`/`nonVirtualizedRowsFallback` above.
154
+ */
155
+ export function resolveGuardedMeasurement<TItemElement extends Element>(
156
+ host: MeasurementCacheHost<TItemElement>,
157
+ element: TItemElement,
158
+ size: number,
159
+ ): number {
160
+ if (size > 0) return size
161
+ // Same key derivation `resizeItem` itself uses (index → getItemKey), read BEFORE `resizeItem`
162
+ // writes the new (bad) size — `itemSizeCache` still holds the last-good measurement here.
163
+ const index = host.indexFromElement(element)
164
+ const key = host.options.getItemKey(index)
165
+ return host.itemSizeCache.get(key) ?? host.options.estimateSize(index)
166
+ }
167
+
168
+ /**
169
+ * Decides what `VirtualRowsInner`'s one-shot initial-scroll effect should do this render,
170
+ * given the inputs the effect closes over. Pure and peer/DOM-free — pulled out of the lazy-loaded
171
+ * closure for the same reason `resolveGuardedMeasurement` is: it stays unit-testable without a
172
+ * real `Virtualizer` instance or `@tanstack/react-virtual` itself.
173
+ *
174
+ * `'start'` and an empty transcript resolve to `'skip-done'` — there is nothing to scroll to, and
175
+ * DONE is permanent (see `virtualize.ts`'s fourth composition rule: the real scroll must fire at
176
+ * most once, ever, so a later append never yanks a user who has scrolled up). A non-positive
177
+ * `containerHeight` — the transcript's own scroll node measuring `0`, e.g. mounted behind a
178
+ * `display: none` ancestor per `virtualize.ts`'s third composition rule — resolves to
179
+ * `'skip-retry'`: NOT done, so the caller's effect (which re-runs after EVERY commit until the jump
180
+ * lands — see {@link applyInitialScroll}) gets another chance the next time this subtree renders,
181
+ * instead of permanently stranding the transcript at the top the moment it becomes visible. Only a
182
+ * genuinely measured, non-empty, `'end'`-targeted mount resolves to `'scroll'`.
183
+ */
184
+ export function resolveInitialScrollAction(
185
+ initialScroll: InitialScroll,
186
+ rowCount: number,
187
+ containerHeight: number,
188
+ ): 'scroll' | 'skip-done' | 'skip-retry' {
189
+ if (initialScroll === 'start') return 'skip-done'
190
+ if (rowCount === 0) return 'skip-done'
191
+ if (containerHeight <= 0) return 'skip-retry'
192
+ return 'scroll'
193
+ }
194
+
195
+ /** The mutable half of the one-shot initial-scroll contract. `hasApplied` is whether it has
196
+ * PERMANENTLY finished — either there was nothing to do (`'start'`/empty transcript), or
197
+ * `scrollToEnd` fired and was subsequently CONFIRMED (via a real DOM read — see
198
+ * {@link applyInitialScroll}'s doc for why virtual-core's own tracked offset can't be trusted for
199
+ * this) to have survived whatever else committed since. `attempts` counts how many times
200
+ * `scrollToEnd` has been (re-)fired this mount; 0 means never attempted, and also gates whether a
201
+ * call re-derives {@link resolveInitialScrollAction} at all. One instance lives for the lifetime of
202
+ * a `VirtualRowsInner` mount, held in a `useRef` so it survives re-renders without itself
203
+ * triggering one. */
204
+ export type InitialScrollState = { hasApplied: boolean; attempts: number }
205
+
206
+ /**
207
+ * The stateful half of "fires once, on first mount, and never again" (`virtualize.ts`'s fourth
208
+ * composition rule): wraps {@link resolveInitialScrollAction}'s per-call decision with the
209
+ * ACROSS-CALLS memory that decision alone can't carry. Exported (like `resolveGuardedMeasurement`)
210
+ * so `VirtualRowsInner`'s effect below calls this SAME function rather than a hand-mirrored
211
+ * copy for tests — a fake `scrollToEnd` is the only DOM/peer-free way to prove the guard, since a
212
+ * real `Virtualizer`'s own `followOnAppend` also calls the real DOM `scrollTo` on a later append,
213
+ * making that specific call indistinguishable from this one at the DOM boundary (see this
214
+ * function's test suite for the measurement that ruled that boundary out).
215
+ *
216
+ * Firing `scrollToEnd()` once is NOT the same as landing there: virtual-core 3.18.0's own
217
+ * `_willUpdate` (verified against the installed dist) writes to the SAME scroll container from
218
+ * `this.scrollOffset`, which updates only from the ASYNCHRONOUS native `scroll` event — never from
219
+ * the synchronous call that requested a scroll. A commit that runs `_willUpdate` before this
220
+ * effect gets a turn (element-attach) or that re-derives an anchor while `scrollOffset` still holds
221
+ * the pre-jump value writes that STALE offset straight back to `scrollTop`, undoing the jump within
222
+ * about one extra commit of it landing (see `agent/chat/virtualize.ts`'s fourth composition rule for the full
223
+ * trace this was diagnosed from). So `resolveInitialScrollAction`'s `'scroll'` outcome does not, by
224
+ * itself, set `hasApplied`: `isAtEnd` — a real `scrollTop`/`scrollHeight`/`clientHeight` read, the
225
+ * one piece of ground truth that is NEVER stale, unlike virtual-core's own tracked offset — is
226
+ * consulted on the NEXT call (i.e. the next commit, exactly the cadence this dependency-free effect
227
+ * already runs on) to confirm the jump survived. If it didn't, `scrollToEnd` fires again; if it did,
228
+ * `hasApplied` is set and the effect is inert for the rest of the mount. `MAX_INITIAL_SCROLL_ATTEMPTS`
229
+ * bounds this so a DOM that pathologically never reports settled — or a user who scrolls away mid-
230
+ * measurement — can never be fought forever; once exhausted, `hasApplied` is set regardless, wherever
231
+ * the last attempt left the container.
232
+ *
233
+ * `state.hasApplied` is the ONLY thing that can permanently stop this from calling `scrollToEnd`
234
+ * again: `'start'` and an empty transcript set it immediately (nothing to verify), and `'skip-retry'`
235
+ * deliberately leaves it `false` so a transcript that mounted behind a hidden ancestor gets another
236
+ * attempt on the caller's next commit, rather than being silently abandoned the moment it becomes
237
+ * visible. That retry — and the clobber-recovery above — is why the caller's effect carries NO
238
+ * dependency array: the commit that matters (a collapsed `ThreadFeedRow` flipping its body back to
239
+ * `display: block`, or virtual-core's own follow-up commit after this effect's own jump) changes
240
+ * neither the row count nor the virtualizer identity, so a dependency-keyed effect would never
241
+ * re-run for it.
242
+ *
243
+ * `getContainerHeight` is a THUNK, not a number, for the same reason: reading `offsetHeight` forces
244
+ * a synchronous layout, and it is read at most ONCE per mount — only on the very first call
245
+ * (`attempts === 0`) — never again while verifying or retrying, so a landed transcript still pays
246
+ * nothing for the effect it keeps re-running (a streaming transcript commits on every chunk).
247
+ * `isAtEnd` is cheaper (a plain property read, no forced reflow beyond what the browser already
248
+ * tracks) and is exactly what the verification/retry loop above consults instead.
249
+ */
250
+ export function applyInitialScroll(
251
+ state: InitialScrollState,
252
+ initialScroll: InitialScroll,
253
+ rowCount: number,
254
+ getContainerHeight: () => number,
255
+ scrollToEnd: () => void,
256
+ isAtEnd: () => boolean,
257
+ ): void {
258
+ if (state.hasApplied) return
259
+
260
+ // Already fired at least once this mount — verify it survived rather than re-deriving
261
+ // `resolveInitialScrollAction` (row count / container height are irrelevant to "did the DOM
262
+ // keep the scroll we already decided on").
263
+ if (state.attempts > 0) {
264
+ if (isAtEnd() || state.attempts >= MAX_INITIAL_SCROLL_ATTEMPTS) {
265
+ state.hasApplied = true
266
+ return
267
+ }
268
+ scrollToEnd()
269
+ state.attempts += 1
270
+ return
271
+ }
272
+
273
+ const action = resolveInitialScrollAction(initialScroll, rowCount, getContainerHeight())
274
+ if (action === 'skip-retry') return
275
+ if (action === 'skip-done') {
276
+ state.hasApplied = true
277
+ return
278
+ }
279
+ scrollToEnd()
280
+ state.attempts += 1
281
+ }
282
+
283
+ /** Reads the transcript's own scroll container to answer "is it currently at (or within
284
+ * `threshold`px of) the end" — the ground-truth check {@link applyInitialScroll} verifies its jump
285
+ * against. Deliberately NOT `virtualizer.isAtEnd()`/`getScrollOffset()`: both are backed by
286
+ * `this.scrollOffset`, the exact value that lags a real scroll until the next native `scroll`
287
+ * event fires — using it here would make the verification vulnerable to the identical staleness
288
+ * that causes the clobber in the first place. `scrollTop`/`scrollHeight`/`clientHeight` are plain
289
+ * DOM properties with no such lag: `Element.scrollTo` with `'auto'` (this module's own behavior)
290
+ * updates `scrollTop` synchronously, so a read immediately after — or, as here, on the next
291
+ * commit — reflects reality. `null` (container not yet attached) reads as "not at end" rather than
292
+ * throwing, matching every other guard in this file's tolerance for a not-yet-mounted ref. */
293
+ function isContainerAtEnd(container: Element | null, threshold: number): boolean {
294
+ if (container === null) return false
295
+ const distanceFromEnd = container.scrollHeight - container.clientHeight - container.scrollTop
296
+ return distanceFromEnd <= threshold
297
+ }
298
+
299
+ /** `scroll: 'parent'`'s ground-truth "at the end": the LIST's bottom edge against the scrollport's
300
+ * visible bottom (`null` scroller → the window). Not the whole scrollport — content after the list
301
+ * keeps that distance positive forever, which would make {@link applyInitialScroll} re-fire
302
+ * `scrollToEnd` up to its attempt cap and yank a user who has scrolled away. */
303
+ export function isListAtEnd(
304
+ list: Element | null,
305
+ scroller: HTMLElement | null,
306
+ threshold: number,
307
+ ): boolean {
308
+ if (list === null) return false
309
+ const visibleBottom =
310
+ scroller === null
311
+ ? window.innerHeight
312
+ : scroller.getBoundingClientRect().top + scroller.clientTop + scroller.clientHeight
313
+ return list.getBoundingClientRect().bottom - visibleBottom <= threshold
314
+ }
315
+
316
+ // ── Sticky headers ───────────────────────────────────────────────────────────────
317
+
318
+ /** The active sticky header: the last sticky index at or before the first visible index. */
319
+ export function activeStickyIndex(
320
+ stickyIndexes: readonly number[],
321
+ firstVisible: number | undefined,
322
+ ): number | undefined {
323
+ if (firstVisible === undefined) return undefined
324
+ let active: number | undefined
325
+ for (const index of stickyIndexes) {
326
+ if (index > firstVisible) break
327
+ active = index
328
+ }
329
+ return active
330
+ }
331
+
332
+ /** `base` (virtual-core's `defaultRangeExtractor`) plus the active sticky index, ascending. */
333
+ export function extractRangeWithSticky(
334
+ range: Range,
335
+ stickyIndexes: readonly number[],
336
+ base: (range: Range) => number[],
337
+ ): number[] {
338
+ const indexes = base(range)
339
+ const active = activeStickyIndex(stickyIndexes, range.startIndex)
340
+ if (active === undefined || indexes.includes(active)) return indexes
341
+ return [active, ...indexes]
342
+ }
343
+
344
+ // ── Degrade target ───────────────────────────────────────────────────────────────
345
+
346
+ /**
347
+ * Every row rendered unwindowed in the same scroll owner. This is what a list falls back to when
348
+ * `@tanstack/react-virtual` fails to resolve (peer absent). Sticky headers stay `position: sticky`
349
+ * through CSS alone.
350
+ *
351
+ * NOT used as the `Suspense` fallback (see `SuspensePane`): mounting every real row for the one
352
+ * tick the lazy import takes would make that settle a genuine unmount/remount of every row,
353
+ * re-firing each row's effects and risking a message mid-stream during the resolve window.
354
+ */
355
+ export function NonVirtualizedRows({
356
+ rows,
357
+ height,
358
+ gap = 0,
359
+ fallbackGap = gap,
360
+ className,
361
+ style,
362
+ }: VirtualRowsProps): JSX.Element {
363
+ const list = (
364
+ // theme-allow inline-display — the degrade target is a plain column of rows.
365
+ <div style={{ display: 'flex', flexDirection: 'column', gap: fallbackGap }}>
366
+ {rows.map((row) =>
367
+ row.sticky === true ? (
368
+ <div key={row.key} style={STICKY_STYLE}>
369
+ {row.node}
370
+ </div>
371
+ ) : (
372
+ <Fragment key={row.key}>{row.node}</Fragment>
373
+ ),
374
+ )}
375
+ </div>
376
+ )
377
+ // theme-allow raw-scroll-container — degrade target owns its own scroll node, same as the real virtualizer below.
378
+ if (height === undefined)
379
+ return (
380
+ <div className={cx(className)} style={style}>
381
+ {list}
382
+ </div>
383
+ )
384
+ return (
385
+ // theme-allow raw-scroll-container — as above.
386
+ <div className={cx(className)} style={{ height, overflow: 'auto', ...style }}>
387
+ {list}
388
+ </div>
389
+ )
390
+ }
391
+
392
+ /** Test-only escape hatch: proves the peer-absent degrade renders every row without having to fight
393
+ * `React.lazy`'s permanent once-per-module-instance resolution cache. */
394
+ export const nonVirtualizedRowsFallback = NonVirtualizedRows
395
+
396
+ // ── Lazy virtualizer ─────────────────────────────────────────────────────────────
397
+
398
+ type ReactVirtual = typeof import('@tanstack/react-virtual')
399
+
400
+ /** The slice of the virtualizer the rows component reads — it never leaves that component. */
401
+ type RowsVirtualizer = {
402
+ readonly getVirtualItems: () => ReadonlyArray<{ readonly index: number; readonly start: number }>
403
+ readonly getTotalSize: () => number
404
+ readonly measureElement: (node: Element | null) => void
405
+ readonly scrollToEnd: (options?: { behavior?: ScrollBehavior }) => void
406
+ readonly range: { readonly startIndex: number } | null
407
+ }
408
+
409
+ type RowsOptions = Pick<
410
+ Parameters<ReactVirtual['useVirtualizer']>[0],
411
+ | 'count'
412
+ | 'estimateSize'
413
+ | 'overscan'
414
+ | 'getItemKey'
415
+ | 'anchorTo'
416
+ | 'followOnAppend'
417
+ | 'scrollEndThreshold'
418
+ | 'useFlushSync'
419
+ | 'gap'
420
+ | 'scrollMargin'
421
+ | 'rangeExtractor'
422
+ | 'initialOffset'
423
+ > & { readonly getScrollElement: () => Element | null }
424
+
425
+ type UseRowsVirtualizer = (options: RowsOptions) => RowsVirtualizer
426
+
427
+ /** `measureElement` with the hidden-ancestor 0px guard (see {@link resolveGuardedMeasurement}).
428
+ * NOT `enabled: false`, which virtual-core wipes `itemSizeCache`/`measurementsCache` on. */
429
+ function guardedMeasure<TScroll extends Element | Window>(base: ReactVirtual['measureElement']) {
430
+ return (
431
+ element: Element,
432
+ entry: ResizeObserverEntry | undefined,
433
+ instance: Virtualizer<TScroll, Element>,
434
+ ): number => resolveGuardedMeasurement(instance, element, base(element, entry, instance))
435
+ }
436
+
437
+ /** One rows component per scroll owner (element or window), since the hook differs. Window height
438
+ * for the `null` scroll parent, else the element's own `offsetHeight` — the measurement
439
+ * virtual-core's `getRect` takes, so a hidden ancestor reads as unmeasured. */
440
+ function createRows(rv: ReactVirtual, useRowsVirtualizer: UseRowsVirtualizer, onWindow: boolean) {
441
+ return function VirtualRowsInner({
442
+ rows,
443
+ height,
444
+ overscan,
445
+ estimateSize,
446
+ gap = 0,
447
+ initialScroll = 'start',
448
+ anchorTo = 'start',
449
+ followOnAppend = false,
450
+ parent,
451
+ className,
452
+ style,
453
+ }: InnerProps): JSX.Element {
454
+ 'use no memo'
455
+ const ownRef = useRef<HTMLDivElement>(null)
456
+ const listRef = useRef<HTMLDivElement>(null)
457
+ const initialScrollStateRef = useRef<InitialScrollState>({ hasApplied: false, attempts: 0 })
458
+ const stickyIndexes = useMemo(
459
+ () => rows.flatMap((row, index) => (row.sticky === true ? [index] : [])),
460
+ [rows],
461
+ )
462
+ const rangeExtractor = useMemo(
463
+ () => (range: Range) =>
464
+ extractRangeWithSticky(range, stickyIndexes, rv.defaultRangeExtractor),
465
+ [stickyIndexes],
466
+ )
467
+ const elementNode = (): HTMLElement | null => parent?.element ?? ownRef.current
468
+ const scrollNode = (): Element | null => (onWindow ? document.scrollingElement : elementNode())
469
+
470
+ // `getItemKey` returns `row.key` (NOT the default index) so a prepend/stream doesn't scramble
471
+ // anchoring; `useFlushSync: false` is the React-19 opt-out `BasaltVirtualList` documents.
472
+ const virtualizer = useRowsVirtualizer({
473
+ count: rows.length,
474
+ getScrollElement: scrollNode,
475
+ estimateSize: () => estimateSize,
476
+ overscan,
477
+ gap,
478
+ scrollMargin: parent?.margin ?? 0,
479
+ // `scroll: 'parent'` mounts on a scroller that may already be scrolled (a list that was plain
480
+ // DOM a frame ago): start at that offset, not 0 — virtual-core writes its tracked offset back
481
+ // on attach, which would jump the scroller to the top.
482
+ ...(parent !== undefined && { initialOffset: () => scrollNode()?.scrollTop ?? 0 }),
483
+ rangeExtractor,
484
+ getItemKey: (index) => rows[index]?.key ?? index,
485
+ anchorTo,
486
+ followOnAppend,
487
+ scrollEndThreshold: SCROLL_END_THRESHOLD,
488
+ useFlushSync: false,
489
+ })
490
+
491
+ // The one-shot "open at the end" jump — see `applyInitialScroll` for the guard mechanics,
492
+ // including why this effect deliberately carries NO dependency array.
493
+ useLayoutEffect(() => {
494
+ applyInitialScroll(
495
+ initialScrollStateRef.current,
496
+ initialScroll,
497
+ rows.length,
498
+ () => (onWindow ? window.innerHeight : (elementNode()?.offsetHeight ?? 0)),
499
+ () => virtualizer.scrollToEnd({ behavior: 'auto' }),
500
+ () =>
501
+ parent === undefined
502
+ ? isContainerAtEnd(scrollNode(), SCROLL_END_THRESHOLD)
503
+ : isListAtEnd(listRef.current, parent.element, SCROLL_END_THRESHOLD),
504
+ )
505
+ })
506
+
507
+ const margin = parent?.margin ?? 0
508
+ const virtualItems = virtualizer.getVirtualItems()
509
+ // `range` is assigned inside `calculateRange` (virtual-core index.js:795), which
510
+ // `getVirtualItems()` drives through `getVirtualIndexes` — before this call it is the PREVIOUS
511
+ // render's (null on the first), so the active header is derived after it.
512
+ const activeSticky = activeStickyIndex(stickyIndexes, virtualizer.range?.startIndex)
513
+ const items = (
514
+ <div ref={listRef} style={{ height: virtualizer.getTotalSize(), position: 'relative' }}>
515
+ {virtualItems.map((virtualItem) => {
516
+ const row = rows[virtualItem.index]
517
+ if (row === undefined) return null
518
+ return (
519
+ <div
520
+ key={row.key}
521
+ // Dynamic measurement of variable-height rows — `ref` and `data-index` are both
522
+ // required together.
523
+ ref={virtualizer.measureElement}
524
+ data-index={virtualItem.index}
525
+ style={
526
+ virtualItem.index === activeSticky
527
+ ? STICKY_STYLE
528
+ : {
529
+ position: 'absolute',
530
+ top: 0,
531
+ left: 0,
532
+ width: '100%',
533
+ transform: `translateY(${virtualItem.start - margin}px)`,
534
+ }
535
+ }
536
+ >
537
+ {row.node}
538
+ </div>
539
+ )
540
+ })}
541
+ </div>
542
+ )
543
+ // `scroll: 'parent'`: the ancestor scrolls; the outer wrapper already carries className/style.
544
+ if (height === undefined) return items
545
+ return (
546
+ <div
547
+ ref={ownRef}
548
+ className={cx(className)}
549
+ // theme-allow raw-scroll-container — TanStack Virtual measures/scrolls this element (never nest in BasaltStickToBottom, see agent/chat/virtualize).
550
+ style={{ height, overflow: 'auto', ...style }}
551
+ >
552
+ {items}
553
+ </div>
554
+ )
555
+ }
556
+ }
557
+
558
+ /** Resolved once, permanently, per module instance — `LazyStickToBottom`'s caching behaviour. */
559
+ const LazyVirtualRows = lazy<ComponentType<InnerProps>>(() =>
560
+ import('@tanstack/react-virtual')
561
+ .then((rv) => {
562
+ const { useVirtualizer, useWindowVirtualizer, measureElement } = rv
563
+ const Rows = createRows(
564
+ rv,
565
+ (o) =>
566
+ useVirtualizer({
567
+ ...o,
568
+ measureElement: guardedMeasure(measureElement),
569
+ }),
570
+ false,
571
+ )
572
+ const WindowRows = createRows(
573
+ rv,
574
+ ({ getScrollElement: _unused, ...o }) =>
575
+ useWindowVirtualizer({ ...o, measureElement: guardedMeasure(measureElement) }),
576
+ true,
577
+ )
578
+ return {
579
+ default: function Dispatch(props: InnerProps): JSX.Element {
580
+ return props.parent?.element === null ? <WindowRows {...props} /> : <Rows {...props} />
581
+ },
582
+ }
583
+ })
584
+ .catch(() => ({ default: NonVirtualizedRows })),
585
+ )
586
+ Object.assign(LazyVirtualRows, { displayName: 'LazyVirtualRows' })
587
+
588
+ /** `Suspense` fallback: an empty, correctly sized scroll pane — shown only for the one tick the
589
+ * dynamic import takes. Deliberately not `NonVirtualizedRows` (see its doc). */
590
+ function SuspensePane({
591
+ height,
592
+ className,
593
+ style,
594
+ }: BasaltProps & { readonly height: number | string }): JSX.Element {
595
+ // theme-allow raw-scroll-container — placeholder owns its own scroll node, matching the real virtualizer's shape.
596
+ return <div className={cx(className)} style={{ height, overflow: 'auto', ...style }} />
597
+ }
598
+
599
+ /** Measures which box scrolls the list and where the list starts inside it. */
600
+ function measureParent(
601
+ list: HTMLElement | null,
602
+ scrollRoot: HTMLElement | null | undefined,
603
+ ): ParentScroll | null {
604
+ if (list === null || scrollRoot === null) return null
605
+ const element = scrollRoot ?? scrollParentOf(list)
606
+ const top = list.getBoundingClientRect().top
607
+ const margin =
608
+ element === null
609
+ ? top + window.scrollY
610
+ : top - element.getBoundingClientRect().top - element.clientTop + element.scrollTop
611
+ return { element, margin: Math.round(margin) }
612
+ }
613
+
614
+ function sameParent(a: ParentScroll | null, b: ParentScroll): boolean {
615
+ return a !== null && a.element === b.element && a.margin === b.margin
616
+ }
617
+
618
+ function ParentRows(props: Extract<VirtualRowsProps, { scroll: 'parent' }>): JSX.Element {
619
+ const { className, style, scrollRoot, ...rest } = props
620
+ const listRef = useRef<HTMLDivElement>(null)
621
+ const [parent, setParent] = useState<ParentScroll | null>(null)
622
+
623
+ const remeasure = (): void => {
624
+ const next = measureParent(listRef.current, scrollRoot)
625
+ if (next !== null) setParent((prev) => (sameParent(prev, next) ? prev : next))
626
+ }
627
+
628
+ // After every commit (a layout read, no dependency array); an unchanged value bails the state
629
+ // out. Changes that arrive WITHOUT a commit — a font or image settling above the list — are the
630
+ // observer's job below.
631
+ // oxlint-disable-next-line react-hooks/exhaustive-deps -- every commit, by design (see above)
632
+ useLayoutEffect(remeasure)
633
+
634
+ // The offset moves when anything above the list resizes, and that resizes every ancestor between
635
+ // the list and the scrollport (the list's own box and the scrollport's do not change). Observe
636
+ // that chain, plus the window for the window case.
637
+ const scroller = parent?.element
638
+ // oxlint-disable react-hooks/exhaustive-deps -- `remeasure` reads refs, a stable setter and `scrollRoot` (in the deps)
639
+ useEffect(() => {
640
+ const list = listRef.current
641
+ if (list === null) return
642
+ window.addEventListener('resize', remeasure)
643
+ const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(remeasure)
644
+ for (let node: HTMLElement | null = list; node !== null; node = node.parentElement) {
645
+ observer?.observe(node)
646
+ if (node === scroller) break
647
+ }
648
+ return () => {
649
+ window.removeEventListener('resize', remeasure)
650
+ observer?.disconnect()
651
+ }
652
+ }, [scroller, scrollRoot])
653
+ // oxlint-enable react-hooks/exhaustive-deps
654
+
655
+ return (
656
+ <div ref={listRef} className={cx(className)} style={style}>
657
+ {parent !== null && (
658
+ <Suspense fallback={null}>
659
+ <LazyVirtualRows {...rest} parent={parent} />
660
+ </Suspense>
661
+ )}
662
+ </div>
663
+ )
664
+ }
665
+
666
+ /** The seam. `height` → own scroll node; `scroll: 'parent'` → the nearest scrolling ancestor. */
667
+ export function VirtualRows(props: VirtualRowsProps): JSX.Element {
668
+ if (props.scroll === 'parent') return <ParentRows {...props} />
669
+ return (
670
+ <Suspense
671
+ fallback={
672
+ <SuspensePane
673
+ height={props.height}
674
+ {...(props.className !== undefined && { className: props.className })}
675
+ {...(props.style !== undefined && { style: props.style })}
676
+ />
677
+ }
678
+ >
679
+ <LazyVirtualRows {...props} />
680
+ </Suspense>
681
+ )
682
+ }