virtual-scroller 1.8.1 → 1.9.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 (247) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +337 -160
  3. package/bundle/virtual-scroller-dom.js +1 -1
  4. package/bundle/virtual-scroller-dom.js.map +1 -1
  5. package/bundle/virtual-scroller-react.js +1 -1
  6. package/bundle/virtual-scroller-react.js.map +1 -1
  7. package/bundle/virtual-scroller.js +1 -1
  8. package/bundle/virtual-scroller.js.map +1 -1
  9. package/commonjs/BeforeResize.js +23 -27
  10. package/commonjs/BeforeResize.js.map +1 -1
  11. package/commonjs/DOM/Engine.js +7 -7
  12. package/commonjs/DOM/Engine.js.map +1 -1
  13. package/commonjs/DOM/ItemsContainer.js +1 -1
  14. package/commonjs/DOM/ItemsContainer.js.map +1 -1
  15. package/commonjs/DOM/ListTopOffsetWatcher.js +15 -9
  16. package/commonjs/DOM/ListTopOffsetWatcher.js.map +1 -1
  17. package/commonjs/DOM/ScrollableContainer.js +28 -28
  18. package/commonjs/DOM/ScrollableContainer.js.map +1 -1
  19. package/commonjs/DOM/VirtualScroller.js +20 -17
  20. package/commonjs/DOM/VirtualScroller.js.map +1 -1
  21. package/commonjs/DOM/tbody.js +16 -10
  22. package/commonjs/DOM/tbody.js.map +1 -1
  23. package/commonjs/ItemHeights.js +13 -7
  24. package/commonjs/ItemHeights.js.map +1 -1
  25. package/commonjs/Layout.js +15 -13
  26. package/commonjs/Layout.js.map +1 -1
  27. package/commonjs/Layout.test.js +8 -3
  28. package/commonjs/Layout.test.js.map +1 -1
  29. package/commonjs/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +26 -28
  30. package/commonjs/ListHeightMeasurement.js.map +1 -0
  31. package/commonjs/Resize.js +38 -28
  32. package/commonjs/Resize.js.map +1 -1
  33. package/commonjs/Scroll.js +28 -44
  34. package/commonjs/Scroll.js.map +1 -1
  35. package/commonjs/VirtualScroller.columns.js +43 -0
  36. package/commonjs/VirtualScroller.columns.js.map +1 -0
  37. package/commonjs/VirtualScroller.constructor.js +408 -0
  38. package/commonjs/VirtualScroller.constructor.js.map +1 -0
  39. package/commonjs/VirtualScroller.items.js +305 -0
  40. package/commonjs/VirtualScroller.items.js.map +1 -0
  41. package/commonjs/VirtualScroller.js +132 -1872
  42. package/commonjs/VirtualScroller.js.map +1 -1
  43. package/commonjs/VirtualScroller.layout.js +562 -0
  44. package/commonjs/VirtualScroller.layout.js.map +1 -0
  45. package/commonjs/VirtualScroller.onRender.js +357 -0
  46. package/commonjs/VirtualScroller.onRender.js.map +1 -0
  47. package/commonjs/VirtualScroller.resize.js +186 -0
  48. package/commonjs/VirtualScroller.resize.js.map +1 -0
  49. package/commonjs/VirtualScroller.state.js +301 -0
  50. package/commonjs/VirtualScroller.state.js.map +1 -0
  51. package/commonjs/VirtualScroller.verticalSpacing.js +65 -0
  52. package/commonjs/VirtualScroller.verticalSpacing.js.map +1 -0
  53. package/commonjs/getItemCoordinates.js.map +1 -1
  54. package/commonjs/getItemsDiff.js.map +1 -1
  55. package/commonjs/getVerticalSpacing.js.map +1 -1
  56. package/commonjs/package.json +5 -0
  57. package/commonjs/react/VirtualScroller.js +180 -620
  58. package/commonjs/react/VirtualScroller.js.map +1 -1
  59. package/commonjs/react/useClassName.js +26 -0
  60. package/commonjs/react/useClassName.js.map +1 -0
  61. package/commonjs/react/useHandleItemsChange.js +116 -0
  62. package/commonjs/react/useHandleItemsChange.js.map +1 -0
  63. package/commonjs/react/useInstanceMethods.js +37 -0
  64. package/commonjs/react/useInstanceMethods.js.map +1 -0
  65. package/commonjs/react/useItemKeys.js +60 -0
  66. package/commonjs/react/useItemKeys.js.map +1 -0
  67. package/commonjs/react/useOnItemHeightChange.js +32 -0
  68. package/commonjs/react/useOnItemHeightChange.js.map +1 -0
  69. package/commonjs/react/useOnItemStateChange.js +32 -0
  70. package/commonjs/react/useOnItemStateChange.js.map +1 -0
  71. package/commonjs/react/useState.js +140 -0
  72. package/commonjs/react/useState.js.map +1 -0
  73. package/commonjs/react/useStyle.js +29 -0
  74. package/commonjs/react/useStyle.js.map +1 -0
  75. package/commonjs/react/useVirtualScroller.js +62 -0
  76. package/commonjs/react/useVirtualScroller.js.map +1 -0
  77. package/commonjs/react/useVirtualScrollerStartStop.js +20 -0
  78. package/commonjs/react/useVirtualScrollerStartStop.js.map +1 -0
  79. package/commonjs/test/Engine.js +23 -0
  80. package/commonjs/test/Engine.js.map +1 -0
  81. package/commonjs/test/ItemsContainer.js +127 -0
  82. package/commonjs/test/ItemsContainer.js.map +1 -0
  83. package/commonjs/test/ScrollableContainer.js +130 -0
  84. package/commonjs/test/ScrollableContainer.js.map +1 -0
  85. package/commonjs/test/VirtualScroller.js +281 -0
  86. package/commonjs/test/VirtualScroller.js.map +1 -0
  87. package/commonjs/utility/debounce.js +2 -2
  88. package/commonjs/utility/debounce.js.map +1 -1
  89. package/commonjs/utility/debug.js.map +1 -1
  90. package/commonjs/utility/getStateSnapshot.js +2 -2
  91. package/commonjs/utility/getStateSnapshot.js.map +1 -1
  92. package/commonjs/utility/px.js.map +1 -1
  93. package/commonjs/utility/px.test.js +1 -1
  94. package/commonjs/utility/px.test.js.map +1 -1
  95. package/commonjs/utility/shallowEqual.js +1 -1
  96. package/commonjs/utility/shallowEqual.js.map +1 -1
  97. package/commonjs/utility/throttle.js.map +1 -1
  98. package/dom/index.cjs +4 -0
  99. package/dom/index.cjs.js +9 -0
  100. package/dom/index.d.ts +6 -4
  101. package/dom/index.js +1 -1
  102. package/dom/package.json +10 -4
  103. package/index.cjs +4 -0
  104. package/index.cjs.js +9 -0
  105. package/index.d.ts +30 -15
  106. package/index.js +1 -1
  107. package/modules/BeforeResize.js +22 -27
  108. package/modules/BeforeResize.js.map +1 -1
  109. package/modules/DOM/Engine.js +6 -6
  110. package/modules/DOM/Engine.js.map +1 -1
  111. package/modules/DOM/ItemsContainer.js +1 -1
  112. package/modules/DOM/ItemsContainer.js.map +1 -1
  113. package/modules/DOM/ListTopOffsetWatcher.js +15 -9
  114. package/modules/DOM/ListTopOffsetWatcher.js.map +1 -1
  115. package/modules/DOM/ScrollableContainer.js +28 -28
  116. package/modules/DOM/ScrollableContainer.js.map +1 -1
  117. package/modules/DOM/VirtualScroller.js +19 -16
  118. package/modules/DOM/VirtualScroller.js.map +1 -1
  119. package/modules/DOM/tbody.js +11 -9
  120. package/modules/DOM/tbody.js.map +1 -1
  121. package/modules/ItemHeights.js +12 -6
  122. package/modules/ItemHeights.js.map +1 -1
  123. package/modules/Layout.js +14 -12
  124. package/modules/Layout.js.map +1 -1
  125. package/modules/Layout.test.js +8 -3
  126. package/modules/Layout.test.js.map +1 -1
  127. package/modules/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +25 -27
  128. package/modules/ListHeightMeasurement.js.map +1 -0
  129. package/modules/Resize.js +38 -28
  130. package/modules/Resize.js.map +1 -1
  131. package/modules/Scroll.js +28 -44
  132. package/modules/Scroll.js.map +1 -1
  133. package/modules/VirtualScroller.columns.js +36 -0
  134. package/modules/VirtualScroller.columns.js.map +1 -0
  135. package/modules/VirtualScroller.constructor.js +371 -0
  136. package/modules/VirtualScroller.constructor.js.map +1 -0
  137. package/modules/VirtualScroller.items.js +288 -0
  138. package/modules/VirtualScroller.items.js.map +1 -0
  139. package/modules/VirtualScroller.js +132 -1860
  140. package/modules/VirtualScroller.js.map +1 -1
  141. package/modules/VirtualScroller.layout.js +549 -0
  142. package/modules/VirtualScroller.layout.js.map +1 -0
  143. package/modules/VirtualScroller.onRender.js +337 -0
  144. package/modules/VirtualScroller.onRender.js.map +1 -0
  145. package/modules/VirtualScroller.resize.js +176 -0
  146. package/modules/VirtualScroller.resize.js.map +1 -0
  147. package/modules/VirtualScroller.state.js +283 -0
  148. package/modules/VirtualScroller.state.js.map +1 -0
  149. package/modules/VirtualScroller.verticalSpacing.js +54 -0
  150. package/modules/VirtualScroller.verticalSpacing.js.map +1 -0
  151. package/modules/getItemCoordinates.js.map +1 -1
  152. package/modules/getItemsDiff.js.map +1 -1
  153. package/modules/getVerticalSpacing.js.map +1 -1
  154. package/modules/react/VirtualScroller.js +176 -625
  155. package/modules/react/VirtualScroller.js.map +1 -1
  156. package/modules/react/useClassName.js +18 -0
  157. package/modules/react/useClassName.js.map +1 -0
  158. package/modules/react/useHandleItemsChange.js +108 -0
  159. package/modules/react/useHandleItemsChange.js.map +1 -0
  160. package/modules/react/useInstanceMethods.js +28 -0
  161. package/modules/react/useInstanceMethods.js.map +1 -0
  162. package/modules/react/useItemKeys.js +52 -0
  163. package/modules/react/useItemKeys.js.map +1 -0
  164. package/modules/react/useOnItemHeightChange.js +24 -0
  165. package/modules/react/useOnItemHeightChange.js.map +1 -0
  166. package/modules/react/useOnItemStateChange.js +24 -0
  167. package/modules/react/useOnItemStateChange.js.map +1 -0
  168. package/modules/react/useState.js +132 -0
  169. package/modules/react/useState.js.map +1 -0
  170. package/modules/react/useStyle.js +19 -0
  171. package/modules/react/useStyle.js.map +1 -0
  172. package/modules/react/useVirtualScroller.js +51 -0
  173. package/modules/react/useVirtualScroller.js.map +1 -0
  174. package/modules/react/useVirtualScrollerStartStop.js +12 -0
  175. package/modules/react/useVirtualScrollerStartStop.js.map +1 -0
  176. package/modules/test/Engine.js +11 -0
  177. package/modules/test/Engine.js.map +1 -0
  178. package/modules/test/ItemsContainer.js +120 -0
  179. package/modules/test/ItemsContainer.js.map +1 -0
  180. package/modules/test/ScrollableContainer.js +123 -0
  181. package/modules/test/ScrollableContainer.js.map +1 -0
  182. package/modules/test/VirtualScroller.js +270 -0
  183. package/modules/test/VirtualScroller.js.map +1 -0
  184. package/modules/utility/debounce.js +2 -2
  185. package/modules/utility/debounce.js.map +1 -1
  186. package/modules/utility/debug.js.map +1 -1
  187. package/modules/utility/getStateSnapshot.js +2 -2
  188. package/modules/utility/getStateSnapshot.js.map +1 -1
  189. package/modules/utility/px.js.map +1 -1
  190. package/modules/utility/px.test.js +1 -1
  191. package/modules/utility/px.test.js.map +1 -1
  192. package/modules/utility/shallowEqual.js +1 -1
  193. package/modules/utility/shallowEqual.js.map +1 -1
  194. package/modules/utility/throttle.js.map +1 -1
  195. package/package.json +46 -23
  196. package/react/index.cjs +4 -0
  197. package/react/index.cjs.js +9 -0
  198. package/react/index.d.ts +10 -9
  199. package/react/index.js +1 -1
  200. package/react/package.json +10 -4
  201. package/rollup.config.mjs +62 -0
  202. package/runnable/create-commonjs-package-json.js +11 -0
  203. package/source/BeforeResize.js +16 -21
  204. package/source/DOM/Engine.js +8 -10
  205. package/source/DOM/ListTopOffsetWatcher.js +13 -8
  206. package/source/DOM/ScrollableContainer.js +23 -21
  207. package/source/DOM/VirtualScroller.js +27 -11
  208. package/source/DOM/tbody.js +30 -21
  209. package/source/ItemHeights.js +9 -4
  210. package/source/Layout.js +12 -9
  211. package/source/Layout.test.js +8 -3
  212. package/source/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +21 -20
  213. package/source/Resize.js +41 -25
  214. package/source/Scroll.js +27 -35
  215. package/source/VirtualScroller.columns.js +26 -0
  216. package/source/VirtualScroller.constructor.js +336 -0
  217. package/source/VirtualScroller.items.js +302 -0
  218. package/source/VirtualScroller.js +144 -1875
  219. package/source/VirtualScroller.layout.js +539 -0
  220. package/source/VirtualScroller.onRender.js +345 -0
  221. package/source/VirtualScroller.resize.js +189 -0
  222. package/source/VirtualScroller.state.js +284 -0
  223. package/source/VirtualScroller.verticalSpacing.js +51 -0
  224. package/source/react/VirtualScroller.js +243 -587
  225. package/source/react/useClassName.js +14 -0
  226. package/source/react/useHandleItemsChange.js +115 -0
  227. package/source/react/useInstanceMethods.js +25 -0
  228. package/source/react/useItemKeys.js +59 -0
  229. package/source/react/useOnItemHeightChange.js +28 -0
  230. package/source/react/useOnItemStateChange.js +28 -0
  231. package/source/react/useState.js +114 -0
  232. package/source/react/useStyle.js +20 -0
  233. package/source/react/useVirtualScroller.js +59 -0
  234. package/source/react/useVirtualScrollerStartStop.js +12 -0
  235. package/source/test/Engine.js +11 -0
  236. package/source/test/ItemsContainer.js +87 -0
  237. package/source/test/ScrollableContainer.js +88 -0
  238. package/source/test/VirtualScroller.js +232 -0
  239. package/source/utility/debounce.js +2 -2
  240. package/source/utility/px.test.js +1 -1
  241. package/babel.config.js +0 -25
  242. package/babel.js +0 -5
  243. package/commonjs/ListHeightChangeWatcher.js.map +0 -1
  244. package/dom/index.commonjs.js +0 -4
  245. package/index.commonjs.js +0 -4
  246. package/modules/ListHeightChangeWatcher.js.map +0 -1
  247. package/react/index.commonjs.js +0 -4
package/source/Scroll.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // https://github.com/bvaughn/react-virtualized/issues/722
5
5
  import { setTimeout, clearTimeout } from 'request-animation-frame-timeout'
6
6
 
7
- import log from './utility/debug'
7
+ import log from './utility/debug.js'
8
8
 
9
9
  export default class Scroll {
10
10
  constructor({
@@ -39,9 +39,10 @@ export default class Scroll {
39
39
  this.waitForScrollingToStop = waitForScrollingToStop
40
40
  }
41
41
 
42
- listen() {
42
+ start() {
43
43
  if (this.initialScrollPosition !== undefined) {
44
44
  this.scrollToY(this.initialScrollPosition)
45
+ this.initialScrollPosition = undefined
45
46
  }
46
47
  if (this.onScrollPositionChange) {
47
48
  this.onScrollPositionChange(this.getScrollY())
@@ -50,20 +51,10 @@ export default class Scroll {
50
51
  }
51
52
 
52
53
  stop() {
53
- if (this.stopReportingScrollPositionChange) {
54
- this.stopReportingScrollPositionChange()
55
- this.stopReportingScrollPositionChange = undefined
56
- }
57
- if (this.stopListeningToScroll) {
58
- this.stopListeningToScroll()
59
- this.stopListeningToScroll = undefined
60
- }
61
- if (this.onStopScrollingListener) {
62
- this.onStopScrollingListener = undefined
63
- }
64
- if (this.onScrollOnStopScrolling) {
65
- this.onScrollOnStopScrolling = undefined
66
- }
54
+ this.stopListeningToScroll()
55
+ this.stopListeningToScroll = undefined
56
+ // this.onStopScrollingListener = undefined
57
+ this.shouldCallOnScrollListenerWhenStopsScrolling = undefined
67
58
  this.cancelOnStopScrollingTimer()
68
59
  }
69
60
 
@@ -175,7 +166,7 @@ export default class Scroll {
175
166
  return
176
167
  }
177
168
 
178
- this.onScrollOnStopScrolling = true
169
+ this.shouldCallOnScrollListenerWhenStopsScrolling = true
179
170
  this.watchOnStopScrolling()
180
171
  }
181
172
 
@@ -184,21 +175,22 @@ export default class Scroll {
184
175
  () => {
185
176
  this.onStopScrollingTimer = undefined
186
177
 
187
- if (this.onScrollOnStopScrolling) {
188
- this.onScrollOnStopScrolling = undefined
178
+ if (this.shouldCallOnScrollListenerWhenStopsScrolling) {
179
+ this.shouldCallOnScrollListenerWhenStopsScrolling = undefined
189
180
  this.onScroll({ delayed: true })
190
181
  }
191
182
 
192
- if (this.onStopScrollingListener) {
193
- const onStopScrollingListener = this.onStopScrollingListener
194
- this.onStopScrollingListener = undefined
195
- // `onStopScrollingListener()` may hypothetically schedule
196
- // another `onStopScrolling()` listener, so set
197
- // `this.onStopScrollingListener` to `undefined` before
198
- // calling it rather than after.
199
- log('~ The user has stopped scrolling ~')
200
- onStopScrollingListener()
201
- }
183
+ // `onStopScrolling()` feature is not currently used.
184
+ // if (this.onStopScrollingListener) {
185
+ // const onStopScrollingListener = this.onStopScrollingListener
186
+ // this.onStopScrollingListener = undefined
187
+ // // `onStopScrollingListener()` may hypothetically schedule
188
+ // // another `onStopScrolling()` listener, so set
189
+ // // `this.onStopScrollingListener` to `undefined` before
190
+ // // calling it rather than after.
191
+ // log('~ The user has stopped scrolling ~')
192
+ // onStopScrollingListener()
193
+ // }
202
194
  },
203
195
  // "scroll" events are usually dispatched every 16 milliseconds
204
196
  // for 60fps refresh rate, so waiting for 100 milliseconds feels
@@ -213,12 +205,12 @@ export default class Scroll {
213
205
  }
214
206
 
215
207
  // (this function isn't currently used)
216
- onStopScrolling(onStopScrollingListener) {
217
- this.onStopScrollingListener = onStopScrollingListener
218
- if (!this.onStopScrollingTimer) {
219
- this.watchOnStopScrolling()
220
- }
221
- }
208
+ // onStopScrolling(onStopScrollingListener) {
209
+ // this.onStopScrollingListener = onStopScrollingListener
210
+ // if (!this.onStopScrollingTimer) {
211
+ // this.watchOnStopScrolling()
212
+ // }
213
+ // }
222
214
 
223
215
  /**
224
216
  * Returns visible area coordinates relative to the scrollable container.
@@ -0,0 +1,26 @@
1
+ export default function createColumnsHelpers({ getColumnsCount }) {
2
+ if (getColumnsCount) {
3
+ const scrollableContainerArgument = {
4
+ getWidth: () => this.scrollableContainer.getWidth()
5
+ }
6
+ this.getActualColumnsCountForState = () => {
7
+ const columnsCount = getColumnsCount(scrollableContainerArgument)
8
+ // `columnsCount: 1` is effectively same as `columnsCount: undefined`
9
+ // from the code's point of view. This makes one less property in `state`
10
+ // which makes `state` a bit less cluttered (easier for inspection).
11
+ if (columnsCount !== 1) {
12
+ return columnsCount
13
+ }
14
+ }
15
+ } else {
16
+ this.getActualColumnsCountForState = () => undefined
17
+ }
18
+
19
+ this.getActualColumnsCount = () => {
20
+ return this.getActualColumnsCountForState() || 1
21
+ }
22
+
23
+ this.getColumnsCount = () => {
24
+ return this.getState() && this.getState().columnsCount || 1
25
+ }
26
+ }
@@ -0,0 +1,336 @@
1
+ import {
2
+ supportsTbody,
3
+ BROWSER_NOT_SUPPORTED_ERROR
4
+ } from './DOM/tbody.js'
5
+
6
+ import DOMEngine from './DOM/Engine.js'
7
+
8
+ import Layout, { LAYOUT_REASON } from './Layout.js'
9
+ import Resize from './Resize.js'
10
+ import BeforeResize from './BeforeResize.js'
11
+ import Scroll from './Scroll.js'
12
+ import ListHeightMeasurement from './ListHeightMeasurement.js'
13
+ import ItemHeights from './ItemHeights.js'
14
+
15
+ import log, { warn, isDebug, reportError } from './utility/debug.js'
16
+
17
+ import createStateHelpers from './VirtualScroller.state.js'
18
+ import createVerticalSpacingHelpers from './VirtualScroller.verticalSpacing.js'
19
+ import createColumnsHelpers from './VirtualScroller.columns.js'
20
+ import createLayoutHelpers from './VirtualScroller.layout.js'
21
+ import createOnRenderHelpers from './VirtualScroller.onRender.js'
22
+ import createResizeHelpers from './VirtualScroller.resize.js'
23
+ import createItemsHelpers from './VirtualScroller.items.js'
24
+
25
+ /**
26
+ * @param {function} getItemsContainerElement — Returns the container DOM `Element`.
27
+ * @param {any[]} items — The list of items.
28
+ * @param {Object} [options] — See README.md.
29
+ * @return {VirtualScroller}
30
+ */
31
+ export default function VirtualScrollerConstructor(
32
+ getItemsContainerElement,
33
+ items,
34
+ options = {}
35
+ ) {
36
+ const {
37
+ render,
38
+ state,
39
+ onStateChange,
40
+ initialScrollPosition,
41
+ onScrollPositionChange,
42
+ // `scrollableContainer` option is deprecated.
43
+ // Use `getScrollableContainer()` option instead.
44
+ scrollableContainer,
45
+ measureItemsBatchSize = 50,
46
+ getColumnsCount,
47
+ getItemId,
48
+ tbody,
49
+ estimatedItemHeight,
50
+ onItemInitialRender,
51
+ // `onItemFirstRender(i)` is deprecated, use `onItemInitialRender(item)` instead.
52
+ onItemFirstRender,
53
+ _useTimeoutInRenderLoop,
54
+ _waitForScrollingToStop,
55
+ engine
56
+ } = options
57
+
58
+ let {
59
+ bypass,
60
+ getScrollableContainer
61
+ } = options
62
+
63
+ log('~ Initialize ~')
64
+
65
+ // Could support non-DOM rendering engines.
66
+ // For example, React Native, `<canvas/>`, etc.
67
+ this.engine = engine || DOMEngine
68
+
69
+ // `scrollableContainer` option is deprecated.
70
+ // Use `getScrollableContainer()` option instead.
71
+ if (!getScrollableContainer && scrollableContainer) {
72
+ getScrollableContainer = () => scrollableContainer
73
+ }
74
+
75
+ // Sometimes, when `new VirtualScroller()` instance is created,
76
+ // `getItemsContainerElement()` might not be ready to return the "container" DOM Element yet
77
+ // (for example, because it's not rendered yet). That's the reason why it's a getter function.
78
+ // For example, in React `<VirtualScroller/>` component, a `VirtualScroller`
79
+ // instance is created in the React component's `constructor()`, and at that time
80
+ // the container Element is not yet available. The container Element is available
81
+ // in `componentDidMount()`, but `componentDidMount()` is not executed on server,
82
+ // which would mean that React `<VirtualScroller/>` wouldn't render at all
83
+ // on server side, while with the `getItemsContainerElement()` approach, on server side,
84
+ // it still "renders" a list with a predefined amount of items in it by default.
85
+ // (`initiallyRenderedItemsCount`, or `1`).
86
+ this.getItemsContainerElement = getItemsContainerElement
87
+
88
+ // if (prerenderMargin === undefined) {
89
+ // // Renders items which are outside of the screen by this "prerender margin".
90
+ // // Is the screen height by default: seems to be the optimal value
91
+ // // for "Page Up" / "Page Down" navigation and optimized mouse wheel scrolling.
92
+ // prerenderMargin = this.scrollableContainer ? this.scrollableContainer.getHeight() : 0
93
+ // }
94
+
95
+ if (options.getState || options.setState) {
96
+ throw new Error('[virtual-scroller] `getState`/`setState` options usage has changed in the new version. See the readme for more details.')
97
+ }
98
+
99
+ // Work around `<tbody/>` not being able to have `padding`.
100
+ // https://gitlab.com/catamphetamine/virtual-scroller/-/issues/1
101
+ if (tbody) {
102
+ if (this.engine !== DOMEngine) {
103
+ throw new Error('[virtual-scroller] `tbody` option is only supported for DOM rendering engine')
104
+ }
105
+ log('~ <tbody/> detected ~')
106
+ this.tbody = true
107
+ if (!supportsTbody()) {
108
+ log('~ <tbody/> not supported ~')
109
+ reportError(BROWSER_NOT_SUPPORTED_ERROR)
110
+ bypass = true
111
+ }
112
+ }
113
+
114
+ if (bypass) {
115
+ log('~ "bypass" mode ~')
116
+ }
117
+
118
+ // In `bypass` mode, `VirtualScroller` doesn't wait
119
+ // for the user to scroll down to render all items:
120
+ // instead, it renders all items right away, as if
121
+ // the list is rendered without using `VirtualScroller`.
122
+ // It was added just to measure how much is the
123
+ // performance difference between using a `VirtualScroller`
124
+ // and not using a `VirtualScroller`.
125
+ // It turned out that unmounting large React component trees
126
+ // is a very long process, so `VirtualScroller` does seem to
127
+ // make sense when used in a React application.
128
+ this.bypass = bypass
129
+ // this.bypassBatchSize = bypassBatchSize || 10
130
+
131
+ // Using `setTimeout()` in render loop is a workaround
132
+ // for avoiding a React error message:
133
+ // "Maximum update depth exceeded.
134
+ // This can happen when a component repeatedly calls
135
+ // `.setState()` inside `componentWillUpdate()` or `componentDidUpdate()`.
136
+ // React limits the number of nested updates to prevent infinite loops."
137
+ this._useTimeoutInRenderLoop = _useTimeoutInRenderLoop
138
+
139
+ if (getItemId) {
140
+ this.isItemEqual = (a, b) => getItemId(a) === getItemId(b)
141
+ } else {
142
+ this.isItemEqual = (a, b) => a === b
143
+ }
144
+
145
+ if (onItemInitialRender) {
146
+ this.onItemInitialRender = onItemInitialRender
147
+ }
148
+ // `onItemFirstRender(i)` is deprecated, use `onItemInitialRender(item)` instead.
149
+ else if (onItemFirstRender) {
150
+ this.onItemInitialRender = (item) => {
151
+ warn('`onItemFirstRender(i)` is deprecated, use `onItemInitialRender(item)` instead.')
152
+ const { items } = this.getState()
153
+ const i = items.indexOf(item)
154
+ // The `item` could also be non-found due to the inconsistency bug:
155
+ // The reason is that `i` can be non-consistent with the `items`
156
+ // passed to `<VirtualScroller/>` in React due to `updateState()` not being
157
+ // instanteneous: when new `items` are passed to `<VirtualScroller/>`,
158
+ // `VirtualScroller.updateState({ items })` is called, and if `onItemFirstRender(i)`
159
+ // is called after the aforementioned `updateState()` is called but before it finishes,
160
+ // `i` would point to an index in "previous" `items` while the application
161
+ // would assume that `i` points to an index in the "new" `items`,
162
+ // resulting in an incorrect item being assumed by the application
163
+ // or even in an "array index out of bounds" error.
164
+ if (i >= 0) {
165
+ onItemFirstRender(i)
166
+ }
167
+ }
168
+ }
169
+
170
+ // If initial `state` is passed then use `items` from `state`
171
+ // instead of the `items` argument.
172
+ if (state) {
173
+ items = state.items
174
+ }
175
+
176
+ log('Items count', items.length)
177
+ if (estimatedItemHeight) {
178
+ log('Estimated item height', estimatedItemHeight)
179
+ }
180
+
181
+ createStateHelpers.call(this, { state, onStateChange, render, items })
182
+
183
+ createVerticalSpacingHelpers.call(this)
184
+ createColumnsHelpers.call(this, { getColumnsCount })
185
+
186
+ createLayoutHelpers.call(this)
187
+ createOnRenderHelpers.call(this)
188
+ createResizeHelpers.call(this)
189
+ createItemsHelpers.call(this)
190
+
191
+ createHelpers.call(this, {
192
+ getScrollableContainer,
193
+ estimatedItemHeight,
194
+ measureItemsBatchSize,
195
+ initialScrollPosition,
196
+ onScrollPositionChange,
197
+ waitForScrollingToStop: _waitForScrollingToStop
198
+ })
199
+
200
+ if (state) {
201
+ // Initialize `ItemHeights` from previously measured `state.itemHeights`.
202
+ this.itemHeights.readItemHeightsFromState(state)
203
+
204
+ // Initialize some `BeforeResize` internal flags from a previously saved state.
205
+ this.beforeResize.initializeFromState(state)
206
+ }
207
+ }
208
+
209
+ function createHelpers({
210
+ getScrollableContainer,
211
+ estimatedItemHeight,
212
+ measureItemsBatchSize,
213
+ initialScrollPosition,
214
+ onScrollPositionChange,
215
+ waitForScrollingToStop
216
+ }) {
217
+ this.itemsContainer = this.engine.createItemsContainer(
218
+ this.getItemsContainerElement
219
+ )
220
+
221
+ // If the items "container" element is mounted at this stage,
222
+ // remove any accidental text nodes from it (like whitespace).
223
+ //
224
+ // Also, this guards against cases when someone accidentally tries
225
+ // using `VirtualScroller` on a non-empty element.
226
+ //
227
+ if (this.getItemsContainerElement()) {
228
+ this.itemsContainer.clear()
229
+ }
230
+
231
+ this.scrollableContainer = this.engine.createScrollableContainer(
232
+ getScrollableContainer,
233
+ this.getItemsContainerElement
234
+ )
235
+
236
+ // Create `ItemHeights` instance.
237
+ this.itemHeights = new ItemHeights({
238
+ container: this.itemsContainer,
239
+ getItemHeight: (i) => this.getState().itemHeights[i],
240
+ setItemHeight: (i, height) => this.getState().itemHeights[i] = height
241
+ })
242
+
243
+ this.layout = new Layout({
244
+ bypass: this.bypass,
245
+ estimatedItemHeight,
246
+ measureItemsBatchSize,
247
+ getPrerenderMargin: () => this.getPrerenderMargin(),
248
+ getVerticalSpacing: () => this.getVerticalSpacing(),
249
+ getVerticalSpacingBeforeResize: () => this.getVerticalSpacingBeforeResize(),
250
+ getColumnsCount: () => this.getColumnsCount(),
251
+ getColumnsCountBeforeResize: () => this.getState().beforeResize && this.getState().beforeResize.columnsCount,
252
+ getItemHeight: (i) => this.getState().itemHeights[i],
253
+ getItemHeightBeforeResize: (i) => this.getState().beforeResize && this.getState().beforeResize.itemHeights[i],
254
+ getBeforeResizeItemsCount: () => this.getState().beforeResize ? this.getState().beforeResize.itemHeights.length : 0,
255
+ getAverageItemHeight: () => this.itemHeights.getAverage(),
256
+ getMaxVisibleAreaHeight: () => this.scrollableContainer && this.scrollableContainer.getHeight(),
257
+ //
258
+ // The "previously calculated layout" feature is not currently used.
259
+ //
260
+ // The current layout snapshot could be stored as a "previously calculated layout" variable
261
+ // so that it could theoretically be used when calculating new layout incrementally
262
+ // rather than from scratch, which would be an optimization.
263
+ //
264
+ getPreviouslyCalculatedLayout: () => this.previouslyCalculatedLayout
265
+ })
266
+
267
+ this.resize = new Resize({
268
+ bypass: this.bypass,
269
+ getWidth: () => this.scrollableContainer.getWidth(),
270
+ getHeight: () => this.scrollableContainer.getHeight(),
271
+ listenForResize: (listener) => this.scrollableContainer.onResize(listener),
272
+ onResizeStart: () => {
273
+ log('~ Scrollable container resize started ~')
274
+ this._isResizing = true
275
+ },
276
+ onResizeStop: () => {
277
+ log('~ Scrollable container resize finished ~')
278
+ this._isResizing = undefined
279
+ },
280
+ onNoChange: () => {
281
+ // There might have been some missed `this.onUpdateShownItemIndexes()` calls
282
+ // due to setting `this._isResizing` flag to `true` during the resize.
283
+ // So, update shown item indexes just in case.
284
+ this.onUpdateShownItemIndexes({
285
+ reason: LAYOUT_REASON.VIEWPORT_SIZE_UNCHANGED
286
+ })
287
+ },
288
+ onHeightChange: () => this.onUpdateShownItemIndexes({
289
+ reason: LAYOUT_REASON.VIEWPORT_HEIGHT_CHANGED
290
+ }),
291
+ onWidthChange: (prevWidth, newWidth) => {
292
+ log('~ Scrollable container width changed from', prevWidth, 'to', newWidth, '~')
293
+ this.onResize()
294
+ }
295
+ })
296
+
297
+ this.scroll = new Scroll({
298
+ bypass: this.bypass,
299
+ scrollableContainer: this.scrollableContainer,
300
+ itemsContainer: this.itemsContainer,
301
+ waitForScrollingToStop,
302
+ onScroll: ({ delayed } = {}) => {
303
+ this.onUpdateShownItemIndexes({
304
+ reason: delayed ? LAYOUT_REASON.STOPPED_SCROLLING : LAYOUT_REASON.SCROLL
305
+ })
306
+ },
307
+ initialScrollPosition,
308
+ onScrollPositionChange,
309
+ isImmediateLayoutScheduled: () => Boolean(this.layoutTimer),
310
+ hasNonRenderedItemsAtTheTop: () => this.getState().firstShownItemIndex > 0,
311
+ hasNonRenderedItemsAtTheBottom: () => this.getState().lastShownItemIndex < this.getItemsCount() - 1,
312
+ getLatestLayoutVisibleArea: () => this.latestLayoutVisibleArea,
313
+ getListTopOffset: this.getListTopOffsetInsideScrollableContainer,
314
+ getPrerenderMargin: () => this.getPrerenderMargin()
315
+ })
316
+
317
+ this.listHeightMeasurement = new ListHeightMeasurement({
318
+ itemsContainer: this.itemsContainer,
319
+ getListTopOffset: this.getListTopOffsetInsideScrollableContainer
320
+ })
321
+
322
+ if (this.engine.watchListTopOffset) {
323
+ this.listTopOffsetWatcher = this.engine.watchListTopOffset({
324
+ getListTopOffset: this.getListTopOffsetInsideScrollableContainer,
325
+ onListTopOffsetChange: ({ reason }) => this.onUpdateShownItemIndexes({
326
+ reason: LAYOUT_REASON.TOP_OFFSET_CHANGED
327
+ })
328
+ })
329
+ }
330
+
331
+ this.beforeResize = new BeforeResize({
332
+ getState: this.getState,
333
+ getVerticalSpacing: this.getVerticalSpacing,
334
+ getColumnsCount: this.getColumnsCount
335
+ })
336
+ }