virtual-scroller 1.10.0 → 1.10.2

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 (45) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +65 -13
  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/DOM/ScrollableContainer.js +13 -1
  10. package/commonjs/DOM/ScrollableContainer.js.map +1 -1
  11. package/commonjs/Layout.js +65 -10
  12. package/commonjs/Layout.js.map +1 -1
  13. package/commonjs/ScrollableContainerNotReadyError.js +51 -0
  14. package/commonjs/ScrollableContainerNotReadyError.js.map +1 -0
  15. package/commonjs/VirtualScroller.constructor.js +20 -6
  16. package/commonjs/VirtualScroller.constructor.js.map +1 -1
  17. package/commonjs/VirtualScroller.state.js +20 -6
  18. package/commonjs/VirtualScroller.state.js.map +1 -1
  19. package/commonjs/react/VirtualScroller.js +13 -2
  20. package/commonjs/react/VirtualScroller.js.map +1 -1
  21. package/commonjs/react/useVirtualScroller.js +5 -0
  22. package/commonjs/react/useVirtualScroller.js.map +1 -1
  23. package/index.d.ts +2 -1
  24. package/modules/DOM/ScrollableContainer.js +11 -1
  25. package/modules/DOM/ScrollableContainer.js.map +1 -1
  26. package/modules/Layout.js +62 -10
  27. package/modules/Layout.js.map +1 -1
  28. package/modules/ScrollableContainerNotReadyError.js +44 -0
  29. package/modules/ScrollableContainerNotReadyError.js.map +1 -0
  30. package/modules/VirtualScroller.constructor.js +20 -6
  31. package/modules/VirtualScroller.constructor.js.map +1 -1
  32. package/modules/VirtualScroller.state.js +20 -6
  33. package/modules/VirtualScroller.state.js.map +1 -1
  34. package/modules/react/VirtualScroller.js +13 -2
  35. package/modules/react/VirtualScroller.js.map +1 -1
  36. package/modules/react/useVirtualScroller.js +5 -0
  37. package/modules/react/useVirtualScroller.js.map +1 -1
  38. package/package.json +1 -1
  39. package/source/DOM/ScrollableContainer.js +8 -0
  40. package/source/Layout.js +57 -9
  41. package/source/ScrollableContainerNotReadyError.js +5 -0
  42. package/source/VirtualScroller.constructor.js +17 -5
  43. package/source/VirtualScroller.state.js +19 -4
  44. package/source/react/VirtualScroller.js +14 -1
  45. package/source/react/useVirtualScroller.js +6 -0
@@ -46,7 +46,9 @@ export default function VirtualScrollerConstructor(
46
46
  getColumnsCount,
47
47
  getItemId,
48
48
  tbody,
49
+ // `estimatedItemHeight` is deprecated, use `getEstimatedItemHeight()` instead.
49
50
  estimatedItemHeight,
51
+ getEstimatedVisibleItemRowsCount,
50
52
  onItemInitialRender,
51
53
  // `onItemFirstRender(i)` is deprecated, use `onItemInitialRender(item)` instead.
52
54
  onItemFirstRender,
@@ -57,6 +59,7 @@ export default function VirtualScrollerConstructor(
57
59
 
58
60
  let {
59
61
  bypass,
62
+ getEstimatedItemHeight,
60
63
  getScrollableContainer
61
64
  } = options
62
65
 
@@ -66,6 +69,10 @@ export default function VirtualScrollerConstructor(
66
69
  // For example, React Native, `<canvas/>`, etc.
67
70
  this.engine = engine || DOMEngine
68
71
 
72
+ if (!getEstimatedItemHeight && typeof estimatedItemHeight === 'number') {
73
+ getEstimatedItemHeight = () => estimatedItemHeight
74
+ }
75
+
69
76
  // `scrollableContainer` option is deprecated.
70
77
  // Use `getScrollableContainer()` option instead.
71
78
  if (!getScrollableContainer && scrollableContainer) {
@@ -174,8 +181,8 @@ export default function VirtualScrollerConstructor(
174
181
  }
175
182
 
176
183
  log('Items count', items.length)
177
- if (estimatedItemHeight) {
178
- log('Estimated item height', estimatedItemHeight)
184
+ if (getEstimatedItemHeight) {
185
+ log('Estimated item height', getEstimatedItemHeight())
179
186
  }
180
187
 
181
188
  createStateHelpers.call(this, { state, onStateChange, render, items })
@@ -190,7 +197,8 @@ export default function VirtualScrollerConstructor(
190
197
 
191
198
  createHelpers.call(this, {
192
199
  getScrollableContainer,
193
- estimatedItemHeight,
200
+ getEstimatedItemHeight,
201
+ getEstimatedVisibleItemRowsCount,
194
202
  measureItemsBatchSize,
195
203
  initialScrollPosition,
196
204
  onScrollPositionChange,
@@ -208,7 +216,8 @@ export default function VirtualScrollerConstructor(
208
216
 
209
217
  function createHelpers({
210
218
  getScrollableContainer,
211
- estimatedItemHeight,
219
+ getEstimatedItemHeight,
220
+ getEstimatedVisibleItemRowsCount,
212
221
  measureItemsBatchSize,
213
222
  initialScrollPosition,
214
223
  onScrollPositionChange,
@@ -242,7 +251,8 @@ function createHelpers({
242
251
 
243
252
  this.layout = new Layout({
244
253
  bypass: this.bypass,
245
- estimatedItemHeight,
254
+ getInitialEstimatedItemHeight: getEstimatedItemHeight,
255
+ getInitialEstimatedVisibleItemRowsCount: getEstimatedVisibleItemRowsCount,
246
256
  measureItemsBatchSize,
247
257
  getPrerenderMargin: () => this.getPrerenderMargin(),
248
258
  getVerticalSpacing: () => this.getVerticalSpacing(),
@@ -253,6 +263,8 @@ function createHelpers({
253
263
  getItemHeightBeforeResize: (i) => this.getState().beforeResize && this.getState().beforeResize.itemHeights[i],
254
264
  getBeforeResizeItemsCount: () => this.getState().beforeResize ? this.getState().beforeResize.itemHeights.length : 0,
255
265
  getAverageItemHeight: () => this.itemHeights.getAverage(),
266
+ // `this.scrollableContainer` is gonna be `undefined` during server-side rendering.
267
+ // https://gitlab.com/catamphetamine/virtual-scroller/-/issues/30
256
268
  getMaxVisibleAreaHeight: () => this.scrollableContainer && this.scrollableContainer.getHeight(),
257
269
  //
258
270
  // The "previously calculated layout" feature is not currently used.
@@ -161,7 +161,7 @@ export default function createStateHelpers({
161
161
  function getInitialStateFromScratch() {
162
162
  const items = initialItems
163
163
  const state = {
164
- ...getInitialLayoutState.call(this, items),
164
+ ...getInitialLayoutState.call(this, items, { beforeStart: true }),
165
165
  items,
166
166
  itemStates: new Array(items.length)
167
167
  }
@@ -221,15 +221,26 @@ export default function createStateHelpers({
221
221
  warn('Reset Layout')
222
222
  state = {
223
223
  ...state,
224
- ...getInitialLayoutState.call(this, state.items)
224
+ ...getInitialLayoutState.call(this, state.items, { beforeStart: false })
225
225
  }
226
226
  }
227
227
 
228
228
  return state
229
229
  }
230
230
 
231
- function getInitialLayoutState(items) {
231
+ function getInitialLayoutState(items, { beforeStart }) {
232
232
  const itemsCount = items.length
233
+
234
+ const getColumnsCount = () => this.getActualColumnsCount()
235
+
236
+ const columnsCount = beforeStart
237
+ ? this.layout.getInitialLayoutValueWithFallback(
238
+ 'columnsCount',
239
+ getColumnsCount,
240
+ 1
241
+ )
242
+ : getColumnsCount()
243
+
233
244
  const {
234
245
  firstShownItemIndex,
235
246
  lastShownItemIndex,
@@ -237,9 +248,12 @@ export default function createStateHelpers({
237
248
  afterItemsHeight
238
249
  } = this.layout.getInitialLayoutValues({
239
250
  itemsCount,
240
- columnsCount: this.getActualColumnsCount()
251
+ columnsCount: this.getActualColumnsCount(),
252
+ beforeStart
241
253
  })
254
+
242
255
  const itemHeights = new Array(itemsCount)
256
+
243
257
  // Optionally preload items to be rendered.
244
258
  this.onBeforeShowItems(
245
259
  items,
@@ -247,6 +261,7 @@ export default function createStateHelpers({
247
261
  firstShownItemIndex,
248
262
  lastShownItemIndex
249
263
  )
264
+
250
265
  return {
251
266
  itemHeights,
252
267
  columnsCount: this.getActualColumnsCountForState(),
@@ -17,7 +17,11 @@ function VirtualScroller({
17
17
  items,
18
18
  itemComponent: Component,
19
19
  itemComponentProps,
20
+ // `estimatedItemHeight` property name is deprecated,
21
+ // use `getEstimatedItemHeight` property instead.
20
22
  estimatedItemHeight,
23
+ getEstimatedItemHeight,
24
+ getEstimatedVisibleItemRowsCount,
21
25
  bypass,
22
26
  tbody,
23
27
  // `preserveScrollPosition` property name is deprecated,
@@ -48,7 +52,11 @@ function VirtualScroller({
48
52
  // Create a `VirtualScroller` instance.
49
53
  const virtualScroller = useVirtualScroller({
50
54
  items,
55
+ // `estimatedItemHeight` property name is deprecated,
56
+ // use `getEstimatedItemHeight` property instead.
51
57
  estimatedItemHeight,
58
+ getEstimatedItemHeight,
59
+ getEstimatedVisibleItemRowsCount,
52
60
  bypass,
53
61
  // bypassBatchSize,
54
62
  tbody,
@@ -76,7 +84,8 @@ function VirtualScroller({
76
84
  return virtualScroller.getInitialState()
77
85
  }, [])
78
86
 
79
- // Create state management functions.
87
+ // Use React's `useState()` hook for managing `VirtualScroller`'s state lifecycle.
88
+ // This way, React will re-render the component on every state update.
80
89
  const {
81
90
  getState,
82
91
  updateState
@@ -224,7 +233,11 @@ VirtualScroller.propTypes = {
224
233
  items: PropTypes.arrayOf(PropTypes.any).isRequired,
225
234
  itemComponent: elementType.isRequired,
226
235
  itemComponentProps: PropTypes.object,
236
+ // `estimatedItemHeight` property name is deprecated,
237
+ // use `getEstimatedItemHeight` property instead.
227
238
  estimatedItemHeight: PropTypes.number,
239
+ getEstimatedItemHeight: PropTypes.func,
240
+ getEstimatedVisibleItemRowsCount: PropTypes.func,
228
241
  bypass: PropTypes.bool,
229
242
  // bypassBatchSize: PropTypes.number,
230
243
  tbody: PropTypes.bool,
@@ -5,7 +5,10 @@ import VirtualScroller from '../VirtualScroller.js'
5
5
  // Creates a `VirtualScroller` instance.
6
6
  export default function useVirtualScroller({
7
7
  items,
8
+ // `estimatedItemHeight` is deprecated, use `getEstimatedItemHeight()` instead.
8
9
  estimatedItemHeight,
10
+ getEstimatedItemHeight,
11
+ getEstimatedVisibleItemRowsCount,
9
12
  bypass,
10
13
  // bypassBatchSize,
11
14
  tbody,
@@ -34,7 +37,10 @@ export default function useVirtualScroller({
34
37
  items,
35
38
  {
36
39
  _useTimeoutInRenderLoop: true,
40
+ // `estimatedItemHeight` is deprecated, use `getEstimatedItemHeight()` instead.
37
41
  estimatedItemHeight,
42
+ getEstimatedItemHeight,
43
+ getEstimatedVisibleItemRowsCount,
38
44
  bypass,
39
45
  // bypassBatchSize,
40
46
  tbody,