virtual-scroller 1.14.0 → 1.15.1

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 (235) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +403 -254
  3. package/bundle/index-dom-bypass.html +197 -0
  4. package/bundle/index-dom-grid.html +203 -0
  5. package/bundle/index-dom-scrollableContainer.html +214 -0
  6. package/bundle/index-dom-tbody-scrollableContainer.html +81 -0
  7. package/bundle/index-dom-tbody.html +65 -0
  8. package/bundle/index-dom.html +114 -84
  9. package/bundle/index-react-bypass.html +194 -0
  10. package/bundle/{index-bypass.html → index-react-grid.html} +122 -120
  11. package/bundle/index-react-hook.html +209 -0
  12. package/bundle/index-react-scrollableContainer.html +207 -0
  13. package/bundle/index-react-strictMode.html +193 -0
  14. package/bundle/index-react-tbody-scrollableContainer.html +94 -0
  15. package/bundle/index-react-tbody.html +78 -0
  16. package/bundle/{messages.js → items.js} +1 -1
  17. package/bundle/lib/babel.min.js +25 -0
  18. package/bundle/lib/prop-types.min.js +1 -0
  19. package/bundle/lib/react-dom.development.js +29924 -0
  20. package/bundle/lib/react-dom.production.min.js +267 -0
  21. package/bundle/lib/react.development.js +3343 -0
  22. package/bundle/lib/react.production.min.js +31 -0
  23. package/bundle/style.base.css +33 -0
  24. package/{website/style.css → bundle/style.list.css} +10 -43
  25. package/bundle/style.list.grid.css +23 -0
  26. package/bundle/virtual-scroller-dom.js +1 -1
  27. package/bundle/virtual-scroller-dom.js.map +1 -1
  28. package/bundle/virtual-scroller-react.js +1 -1
  29. package/bundle/virtual-scroller-react.js.map +1 -1
  30. package/bundle/virtual-scroller.js +1 -1
  31. package/bundle/virtual-scroller.js.map +1 -1
  32. package/commonjs/BeforeResize.js +1 -2
  33. package/commonjs/BeforeResize.js.map +1 -1
  34. package/commonjs/DOM/VirtualScroller.js +7 -13
  35. package/commonjs/DOM/VirtualScroller.js.map +1 -1
  36. package/commonjs/DOM/tbody.js +6 -6
  37. package/commonjs/DOM/tbody.js.map +1 -1
  38. package/commonjs/ItemHeights.js +10 -13
  39. package/commonjs/ItemHeights.js.map +1 -1
  40. package/commonjs/Layout.defaults.js +21 -0
  41. package/commonjs/Layout.defaults.js.map +1 -0
  42. package/commonjs/Layout.js +75 -64
  43. package/commonjs/Layout.js.map +1 -1
  44. package/commonjs/Layout.test.js +8 -4
  45. package/commonjs/Layout.test.js.map +1 -1
  46. package/commonjs/VirtualScroller.constructor.js +38 -4
  47. package/commonjs/VirtualScroller.constructor.js.map +1 -1
  48. package/commonjs/VirtualScroller.items.js +50 -4
  49. package/commonjs/VirtualScroller.items.js.map +1 -1
  50. package/commonjs/VirtualScroller.js +23 -14
  51. package/commonjs/VirtualScroller.js.map +1 -1
  52. package/commonjs/VirtualScroller.layout.js +40 -29
  53. package/commonjs/VirtualScroller.layout.js.map +1 -1
  54. package/commonjs/VirtualScroller.onContainerResize.js +1 -2
  55. package/commonjs/VirtualScroller.onContainerResize.js.map +1 -1
  56. package/commonjs/VirtualScroller.state.js +10 -9
  57. package/commonjs/VirtualScroller.state.js.map +1 -1
  58. package/commonjs/VirtualScroller.verticalSpacing.js +39 -6
  59. package/commonjs/VirtualScroller.verticalSpacing.js.map +1 -1
  60. package/commonjs/react/VirtualScroller.js +124 -131
  61. package/commonjs/react/VirtualScroller.js.map +1 -1
  62. package/commonjs/react/useClassName.js +2 -2
  63. package/commonjs/react/useClassName.js.map +1 -1
  64. package/commonjs/react/useCreateVirtualScroller.js +64 -0
  65. package/commonjs/react/useCreateVirtualScroller.js.map +1 -0
  66. package/commonjs/react/useInstanceMethods.js +4 -4
  67. package/commonjs/react/useInstanceMethods.js.map +1 -1
  68. package/commonjs/react/useItemKeys.js +28 -5
  69. package/commonjs/react/useItemKeys.js.map +1 -1
  70. package/commonjs/react/useMergeRefs.js +52 -0
  71. package/commonjs/react/useMergeRefs.js.map +1 -0
  72. package/commonjs/react/useOnItemHeightDidChange.js +28 -12
  73. package/commonjs/react/useOnItemHeightDidChange.js.map +1 -1
  74. package/commonjs/react/useSetItemState.js +31 -12
  75. package/commonjs/react/useSetItemState.js.map +1 -1
  76. package/commonjs/react/{useVirtualScrollerStartStop.js → useStartStopVirtualScroller.js} +1 -1
  77. package/commonjs/react/{useVirtualScrollerStartStop.js.map → useStartStopVirtualScroller.js.map} +1 -1
  78. package/commonjs/react/useState.js +9 -9
  79. package/commonjs/react/useState.js.map +1 -1
  80. package/commonjs/react/{useStateNoStaleBug.js → useStateWithRepeatableRead.js} +3 -3
  81. package/commonjs/react/useStateWithRepeatableRead.js.map +1 -0
  82. package/commonjs/react/useStyle.js +28 -4
  83. package/commonjs/react/useStyle.js.map +1 -1
  84. package/commonjs/react/useValidateTableBodyItemsContainer.js +24 -0
  85. package/commonjs/react/useValidateTableBodyItemsContainer.js.map +1 -0
  86. package/commonjs/react/useVirtualScroller.js +142 -42
  87. package/commonjs/react/useVirtualScroller.js.map +1 -1
  88. package/commonjs/test/ItemsContainer.js +10 -10
  89. package/commonjs/test/ItemsContainer.js.map +1 -1
  90. package/commonjs/test/VirtualScroller.js +25 -10
  91. package/commonjs/test/VirtualScroller.js.map +1 -1
  92. package/dom/index.d.ts +6 -5
  93. package/index.d.ts +19 -8
  94. package/modules/BeforeResize.js +1 -2
  95. package/modules/BeforeResize.js.map +1 -1
  96. package/modules/DOM/VirtualScroller.js +7 -13
  97. package/modules/DOM/VirtualScroller.js.map +1 -1
  98. package/modules/DOM/tbody.js +4 -4
  99. package/modules/DOM/tbody.js.map +1 -1
  100. package/modules/ItemHeights.js +11 -14
  101. package/modules/ItemHeights.js.map +1 -1
  102. package/modules/Layout.defaults.js +11 -0
  103. package/modules/Layout.defaults.js.map +1 -0
  104. package/modules/Layout.js +74 -64
  105. package/modules/Layout.js.map +1 -1
  106. package/modules/Layout.test.js +8 -4
  107. package/modules/Layout.test.js.map +1 -1
  108. package/modules/VirtualScroller.constructor.js +37 -4
  109. package/modules/VirtualScroller.constructor.js.map +1 -1
  110. package/modules/VirtualScroller.items.js +51 -5
  111. package/modules/VirtualScroller.items.js.map +1 -1
  112. package/modules/VirtualScroller.js +23 -14
  113. package/modules/VirtualScroller.js.map +1 -1
  114. package/modules/VirtualScroller.layout.js +40 -29
  115. package/modules/VirtualScroller.layout.js.map +1 -1
  116. package/modules/VirtualScroller.onContainerResize.js +1 -2
  117. package/modules/VirtualScroller.onContainerResize.js.map +1 -1
  118. package/modules/VirtualScroller.state.js +10 -9
  119. package/modules/VirtualScroller.state.js.map +1 -1
  120. package/modules/VirtualScroller.verticalSpacing.js +38 -6
  121. package/modules/VirtualScroller.verticalSpacing.js.map +1 -1
  122. package/modules/react/VirtualScroller.js +122 -124
  123. package/modules/react/VirtualScroller.js.map +1 -1
  124. package/modules/react/useClassName.js +3 -3
  125. package/modules/react/useClassName.js.map +1 -1
  126. package/modules/react/useCreateVirtualScroller.js +53 -0
  127. package/modules/react/useCreateVirtualScroller.js.map +1 -0
  128. package/modules/react/useInstanceMethods.js +4 -4
  129. package/modules/react/useInstanceMethods.js.map +1 -1
  130. package/modules/react/useItemKeys.js +28 -5
  131. package/modules/react/useItemKeys.js.map +1 -1
  132. package/modules/react/useMergeRefs.js +44 -0
  133. package/modules/react/useMergeRefs.js.map +1 -0
  134. package/modules/react/useOnItemHeightDidChange.js +28 -12
  135. package/modules/react/useOnItemHeightDidChange.js.map +1 -1
  136. package/modules/react/useSetItemState.js +31 -12
  137. package/modules/react/useSetItemState.js.map +1 -1
  138. package/modules/react/{useVirtualScrollerStartStop.js → useStartStopVirtualScroller.js} +1 -1
  139. package/modules/react/{useVirtualScrollerStartStop.js.map → useStartStopVirtualScroller.js.map} +1 -1
  140. package/modules/react/useState.js +9 -9
  141. package/modules/react/useState.js.map +1 -1
  142. package/modules/react/{useStateNoStaleBug.js → useStateWithRepeatableRead.js} +2 -2
  143. package/modules/react/useStateWithRepeatableRead.js.map +1 -0
  144. package/modules/react/useStyle.js +27 -4
  145. package/modules/react/useStyle.js.map +1 -1
  146. package/modules/react/useValidateTableBodyItemsContainer.js +16 -0
  147. package/modules/react/useValidateTableBodyItemsContainer.js.map +1 -0
  148. package/modules/react/useVirtualScroller.js +136 -42
  149. package/modules/react/useVirtualScroller.js.map +1 -1
  150. package/modules/test/ItemsContainer.js +10 -10
  151. package/modules/test/ItemsContainer.js.map +1 -1
  152. package/modules/test/VirtualScroller.js +25 -10
  153. package/modules/test/VirtualScroller.js.map +1 -1
  154. package/package.json +4 -1
  155. package/react/as.d.ts +42 -0
  156. package/react/index.cjs +2 -1
  157. package/react/index.d.ts +248 -63
  158. package/react/index.js +1 -0
  159. package/rollup.config.mjs +15 -1
  160. package/source/BeforeResize.js +1 -2
  161. package/source/DOM/VirtualScroller.js +7 -13
  162. package/source/DOM/tbody.js +5 -5
  163. package/source/ItemHeights.js +11 -12
  164. package/source/Layout.defaults.js +8 -0
  165. package/source/Layout.js +66 -53
  166. package/source/Layout.test.js +7 -2
  167. package/source/VirtualScroller.constructor.js +27 -4
  168. package/source/VirtualScroller.items.js +47 -2
  169. package/source/VirtualScroller.js +23 -14
  170. package/source/VirtualScroller.layout.js +41 -28
  171. package/source/VirtualScroller.onContainerResize.js +1 -2
  172. package/source/VirtualScroller.state.js +10 -11
  173. package/source/VirtualScroller.verticalSpacing.js +32 -6
  174. package/source/react/VirtualScroller.js +135 -133
  175. package/source/react/useClassName.js +3 -3
  176. package/source/react/useCreateVirtualScroller.js +65 -0
  177. package/source/react/useInstanceMethods.js +12 -4
  178. package/source/react/useItemKeys.js +22 -4
  179. package/source/react/useMergeRefs.js +45 -0
  180. package/source/react/useOnItemHeightDidChange.js +29 -10
  181. package/source/react/useSetItemState.js +32 -10
  182. package/source/react/useState.js +6 -6
  183. package/source/react/{useStateNoStaleBug.js → useStateWithRepeatableRead.js} +1 -1
  184. package/source/react/useStyle.js +18 -2
  185. package/source/react/useValidateTableBodyItemsContainer.js +16 -0
  186. package/source/react/useVirtualScroller.js +155 -47
  187. package/source/test/ItemsContainer.js +10 -10
  188. package/source/test/VirtualScroller.js +16 -10
  189. package/website/index-dom-bypass.html +197 -0
  190. package/website/index-dom-grid.html +203 -0
  191. package/website/index-dom-scrollableContainer.html +214 -0
  192. package/website/index-dom-tbody-scrollableContainer.html +81 -0
  193. package/website/index-dom-tbody.html +65 -0
  194. package/website/index-dom.html +116 -84
  195. package/website/index-react-bypass.html +194 -0
  196. package/website/index-react-grid.html +197 -0
  197. package/website/index-react-hook.html +209 -0
  198. package/website/index-react-scrollableContainer.html +207 -0
  199. package/website/index-react-strictMode.html +193 -0
  200. package/website/index-react-tbody-scrollableContainer.html +94 -0
  201. package/website/index-react-tbody.html +78 -0
  202. package/website/index-react.html +193 -0
  203. package/website/index.html +120 -111
  204. package/website/{messages.js → items.js} +1 -1
  205. package/website/lib/babel.min.js +25 -0
  206. package/website/lib/prop-types.min.js +1 -0
  207. package/website/lib/react-dom.development.js +29924 -0
  208. package/website/lib/react-dom.production.min.js +267 -0
  209. package/website/lib/react.development.js +3343 -0
  210. package/website/lib/react.production.min.js +31 -0
  211. package/website/style.base.css +33 -0
  212. package/website/style.list.css +92 -0
  213. package/website/style.list.grid.css +23 -0
  214. package/bundle/index-grid.html +0 -216
  215. package/bundle/index-scrollableContainer.html +0 -208
  216. package/bundle/index-tbody-scrollableContainer.html +0 -70
  217. package/bundle/index-tbody.html +0 -57
  218. package/bundle/on-scroll-to-dom.js +0 -2
  219. package/bundle/on-scroll-to-dom.js.map +0 -1
  220. package/bundle/on-scroll-to-react.js +0 -2
  221. package/bundle/on-scroll-to-react.js.map +0 -1
  222. package/bundle/on-scroll-to.js +0 -2
  223. package/bundle/on-scroll-to.js.map +0 -1
  224. package/commonjs/react/useStateNoStaleBug.js.map +0 -1
  225. package/modules/react/useStateNoStaleBug.js.map +0 -1
  226. package/website/index-bypass.html +0 -185
  227. package/website/index-grid.html +0 -216
  228. package/website/index-scrollableContainer.html +0 -208
  229. package/website/index-tbody-scrollableContainer.html +0 -70
  230. package/website/index-tbody.html +0 -57
  231. package/website/lib/on-scroll-to-dom.js +0 -2
  232. package/website/lib/on-scroll-to-dom.js.map +0 -1
  233. package/website/lib/on-scroll-to-react.js +0 -2
  234. package/website/lib/on-scroll-to-react.js.map +0 -1
  235. /package/source/react/{useVirtualScrollerStartStop.js → useStartStopVirtualScroller.js} +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"VirtualScroller.js","names":["VirtualScroller","Engine","DEBUG","TestVirtualScroller","scrollableContainerWidth","screenWidth","scrollableContainerHeight","screenHeight","columnsCount","verticalSpacing","items","initialState","state","expectedStateUpdates","scrollableContainerElement","width","height","containerElement","paddingTop","paddingBottom","children","setScrollableContainerWidth","setScrollableContainerHeight","getItemsContainerElement","__columnsCount","getColumnsCount","getItemWidth","hasPausedStateUpdates","undefined","pausedStateUpdate","pausedStateUpdateAction","onBeforeUpdateState","stateUpdate","length","expectedStateUpdate","shift","expect","to","deep","equal","callback","render","virtualScroller","getState","beforeItemsHeight","afterItemsHeight","firstShownItemIndex","lastShownItemIndex","console","log","slice","map","item","area","marginTop","scrollableContainer","engine","_waitForScrollingToStop","onStateChange","getInitialState","useState","updateState","stateUpdateAction","onRender","Error","expectedState","should","include","push","firstNonMeasuredItemIndex","getScrollY","scrollPosition","scrollToY","resize","_triggerResizeListener","JSON","stringify","stop","start","updateLayout","i","getItemScrollPosition","itemHeights","getAverage","newItems","options","setItems"],"sources":["../../source/test/VirtualScroller.js"],"sourcesContent":["import VirtualScroller from '../VirtualScroller.js'\r\nimport Engine from './Engine.js'\r\n\r\nconst DEBUG = false\r\n\r\nexport default class TestVirtualScroller {\r\n\tconstructor({\r\n\t\tscreenWidth: scrollableContainerWidth,\r\n\t\tscreenHeight: scrollableContainerHeight,\r\n\t\tcolumnsCount,\r\n\t\tverticalSpacing,\r\n\t\titems,\r\n\t\tstate: initialState\r\n\t}) {\r\n\t\tthis.expectedStateUpdates = []\r\n\r\n\t\tconst scrollableContainerElement = {\r\n\t\t\twidth: scrollableContainerWidth,\r\n\t\t\theight: scrollableContainerHeight\r\n\t\t}\r\n\r\n\t\tconst containerElement = {\r\n\t\t\tpaddingTop: 0,\r\n\t\t\tpaddingBottom: 0,\r\n\t\t\twidth: scrollableContainerElement.width,\r\n\t\t\tchildren: []\r\n\t\t}\r\n\r\n\t\tthis.setScrollableContainerWidth = (width) => {\r\n\t\t\tscrollableContainerElement.width = width\r\n\t\t\tcontainerElement.width = width\r\n\t\t}\r\n\r\n\t\tthis.setScrollableContainerHeight = (height) => {\r\n\t\t\tscrollableContainerElement.height = height\r\n\t\t}\r\n\r\n\t\tconst getItemsContainerElement = () => containerElement\r\n\r\n\t\tthis.__columnsCount = columnsCount\r\n\r\n\t\tconst getColumnsCount = () => this.__columnsCount\r\n\t\tconst getItemWidth = () => scrollableContainerElement.width / getColumnsCount()\r\n\r\n\t\tthis.hasPausedStateUpdates = undefined\r\n\t\tthis.pausedStateUpdate = undefined\r\n\t\tthis.pausedStateUpdateAction = undefined\r\n\r\n\t\tconst onBeforeUpdateState = (stateUpdate) => {\r\n\t\t\tif (this.expectedStateUpdates.length > 0) {\r\n\t\t\t\tconst expectedStateUpdate = this.expectedStateUpdates.shift()\r\n\t\t\t\texpect(stateUpdate).to.deep.equal(expectedStateUpdate.stateUpdate)\r\n\t\t\t\tif (expectedStateUpdate.callback) {\r\n\t\t\t\t\texpectedStateUpdate.callback()\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst render = () => {\r\n\t\t\tconst {\r\n\t\t\t\titems,\r\n\t\t\t\tbeforeItemsHeight,\r\n\t\t\t\tafterItemsHeight,\r\n\t\t\t\tfirstShownItemIndex,\r\n\t\t\t\tlastShownItemIndex\r\n\t\t\t} = this.virtualScroller.getState()\r\n\r\n\t\t\tconsole.log('~ Render List ~')\r\n\r\n\t\t\tcontainerElement.paddingTop = beforeItemsHeight\r\n\t\t\tcontainerElement.paddingBottom = afterItemsHeight\r\n\r\n\t\t\tcontainerElement.children = items\r\n\t\t\t\t.slice(firstShownItemIndex, lastShownItemIndex + 1)\r\n\t\t\t\t.map((item) => ({\r\n\t\t\t\t\twidth: getItemWidth(),\r\n\t\t\t\t\theight: item.area / getItemWidth(),\r\n\t\t\t\t\tmarginTop: verticalSpacing\r\n\t\t\t\t}))\r\n\t\t}\r\n\r\n\t\tthis.virtualScroller = new VirtualScroller(getItemsContainerElement, items, {\r\n\t\t\tscrollableContainer: scrollableContainerElement,\r\n\t\t\tengine: Engine,\r\n\t\t\t_waitForScrollingToStop: false,\r\n\t\t\tgetColumnsCount,\r\n\t\t\tstate: initialState,\r\n\t\t\tonStateChange(state) {\r\n\t\t\t\tif (DEBUG) {\r\n\t\t\t\t\tconsole.log('~ Updated State ~')\r\n\t\t\t\t\tconsole.log(state)\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t})\r\n\r\n\t\tlet state = this.virtualScroller.getInitialState()\r\n\r\n\t\tthis.virtualScroller.useState({\r\n\t\t\tgetState: () => state,\r\n\t\t\tupdateState: (stateUpdate) => {\r\n\t\t\t\tconst stateUpdateAction = (stateUpdate) => {\r\n\t\t\t\t\t// Is only used in tests.\r\n\t\t\t\t\tif (onBeforeUpdateState) {\r\n\t\t\t\t\t\tonBeforeUpdateState(stateUpdate)\r\n\t\t\t\t\t}\r\n\t\t\t\t\tstate = {\r\n\t\t\t\t\t\t...state,\r\n\t\t\t\t\t\t...stateUpdate\r\n\t\t\t\t\t}\r\n\t\t\t\t\trender()\r\n\t\t\t\t\tthis.virtualScroller.onRender()\r\n\t\t\t\t}\r\n\t\t\t\tif (this.hasPausedStateUpdates) {\r\n\t\t\t\t\tthis.pausedStateUpdate = {\r\n\t\t\t\t\t\t...this.pausedStateUpdate,\r\n\t\t\t\t\t\t...stateUpdate\r\n\t\t\t\t\t}\r\n\t\t\t\t\tthis.pausedStateUpdateAction = stateUpdateAction\r\n\t\t\t\t} else {\r\n\t\t\t\t\tstateUpdateAction(stateUpdate)\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t})\r\n\r\n\t\trender()\r\n\t}\r\n\r\n\tpauseStateUpdates() {\r\n\t\tif (this.hasPausedStateUpdates) {\r\n\t\t\tthrow new Error('[virtual-scroller] State updates have already been paused')\r\n\t\t}\r\n\t\tthis.hasPausedStateUpdates = true\r\n\t}\r\n\r\n\tresumeStateUpdates() {\r\n\t\tif (this.pausedStateUpdate) {\r\n\t\t\tthis.pausedStateUpdateAction(this.pausedStateUpdate)\r\n\t\t\tthis.pausedStateUpdate = undefined\r\n\t\t\tthis.pausedStateUpdateAction = undefined\r\n\t\t}\r\n\t\tthis.hasPausedStateUpdates = false\r\n\t}\r\n\r\n\tverifyState(expectedState) {\r\n\t\t// `mocha`/`chai`.\r\n\t\tthis.virtualScroller.getState().should.deep.include(expectedState)\r\n\t}\r\n\r\n\texpectStateUpdate(stateUpdate, callback) {\r\n\t\tthis.expectedStateUpdates.push({\r\n\t\t\tstateUpdate,\r\n\t\t\tcallback\r\n\t\t})\r\n\t}\r\n\r\n\tgetFirstNonMeasuredItemIndex() {\r\n\t\treturn this.virtualScroller.firstNonMeasuredItemIndex\r\n\t}\r\n\r\n\tgetScrollY() {\r\n\t\treturn this.virtualScroller.scrollableContainer.getScrollY()\r\n\t}\r\n\r\n\tscrollTo(scrollPosition) {\r\n\t\tthis.virtualScroller.scrollableContainer.scrollToY(scrollPosition)\r\n\t}\r\n\r\n\tresize({\r\n\t\tscreenWidth: scrollableContainerWidth,\r\n\t\tscreenHeight: scrollableContainerHeight,\r\n\t\tcolumnsCount\r\n\t}) {\r\n\t\t// Resize scrollable container.\r\n\t\tthis.setScrollableContainerWidth(scrollableContainerWidth)\r\n\t\tthis.setScrollableContainerHeight(scrollableContainerHeight)\r\n\r\n\t\t// Update columns count.\r\n\t\tthis.__columnsCount = columnsCount\r\n\t}\r\n\r\n\t// Returns a `Promise`.\r\n\ttriggerResize({\r\n\t\tscreenWidth,\r\n\t\tscreenHeight,\r\n\t\tcolumnsCount,\r\n\t\tverticalSpacing\r\n\t}) {\r\n\t\tthis.resize({\r\n\t\t\tscreenWidth,\r\n\t\t\tscreenHeight,\r\n\t\t\tcolumnsCount,\r\n\t\t\tverticalSpacing\r\n\t\t})\r\n\r\n\t\t// Call \"on resize\" listener.\r\n\t\treturn this.virtualScroller.scrollableContainer._triggerResizeListener()\r\n\t}\r\n\r\n\tstop() {\r\n\t\tif (this.expectedStateUpdates.length > 0) {\r\n\t\t\tthrow new Error(`[virtual-scroller] Expected ${this.expectedStateUpdates.length} state updates which didn't happen:\\n${JSON.stringify(this.expectedStateUpdates, null, 2)}`)\r\n\t\t}\r\n\t\tif (this.pausedStateUpdate) {\r\n\t\t\tthrow new Error('[virtual-scroller] State updates were paused and haven\\'t been resumed afterwards')\r\n\t\t}\r\n\t\tthis.virtualScroller.stop()\r\n\t}\r\n\r\n\tstart() {\r\n\t\tthis.virtualScroller.start()\r\n\t}\r\n\r\n\tgetState() {\r\n\t\treturn this.virtualScroller.getState()\r\n\t}\r\n\r\n\tupdateLayout() {\r\n\t\tthis.virtualScroller.updateLayout()\r\n\t}\r\n\r\n\tgetItemScrollPosition(i) {\r\n\t\treturn this.virtualScroller.getItemScrollPosition(i)\r\n\t}\r\n\r\n\tgetAverageItemHeight() {\r\n\t\treturn this.virtualScroller.itemHeights.getAverage()\r\n\t}\r\n\r\n\tsetItems(newItems, options) {\r\n\t\tthis.virtualScroller.setItems(newItems, options)\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;;;;;AAAA,OAAOA,eAAP,MAA4B,uBAA5B;AACA,OAAOC,MAAP,MAAmB,aAAnB;AAEA,IAAMC,KAAK,GAAG,KAAd;;IAEqBC,mB;EACpB,mCAOG;IAAA;;IAAA,IANWC,wBAMX,QANFC,WAME;IAAA,IALYC,yBAKZ,QALFC,YAKE;IAAA,IAJFC,YAIE,QAJFA,YAIE;IAAA,IAHFC,eAGE,QAHFA,eAGE;IAAA,IAFFC,KAEE,QAFFA,KAEE;IAAA,IADKC,YACL,QADFC,KACE;;IAAA;;IACF,KAAKC,oBAAL,GAA4B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ACA;;MACD,IAAI,KAAKgB,iBAAT,EAA4B;QAC3B,MAAM,IAAImC,KAAJ,CAAU,mFAAV,CAAN;MACA;;MACD,KAAKtB,eAAL,CAAqBmC,IAArB;IACA;;;WAED,iBAAQ;MACP,KAAKnC,eAAL,CAAqBoC,KAArB;IACA;;;WAED,oBAAW;MACV,OAAO,KAAKpC,eAAL,CAAqBC,QAArB,EAAP;IACA;;;WAED,wBAAe;MACd,KAAKD,eAAL,CAAqBqC,YAArB;IACA;;;WAED,+BAAsBC,CAAtB,EAAyB;MACxB,OAAO,KAAKtC,eAAL,CAAqBuC,qBAArB,CAA2CD,CAA3C,CAAP;IACA;;;WAED,gCAAuB;MACtB,OAAO,KAAKtC,eAAL,CAAqBwC,WAArB,CAAiCC,UAAjC,EAAP;IACA;;;WAED,kBAASC,QAAT,EAAmBC,OAAnB,EAA4B;MAC3B,KAAK3C,eAAL,CAAqB4C,QAArB,CAA8BF,QAA9B,EAAwCC,OAAxC;IACA;;;;;;SAjOmBlF,mB"}
1
+ {"version":3,"file":"VirtualScroller.js","names":["VirtualScroller","Engine","DEBUG","TestVirtualScroller","scrollableContainerWidth","screenWidth","scrollableContainerHeight","screenHeight","columnsCount","verticalSpacing","items","initialState","state","expectedStateUpdates","scrollableContainerElement","width","height","containerElement","paddingTop","paddingBottom","children","setScrollableContainerWidth","setScrollableContainerHeight","getItemsContainerElement","__columnsCount","getColumnsCount","getItemWidth","hasPausedStateUpdates","undefined","pausedStateUpdate","pausedStateUpdateAction","onBeforeUpdateState","stateUpdate","length","expectedStateUpdate","shift","expect","to","deep","equal","callback","render","virtualScroller","getState","beforeItemsHeight","afterItemsHeight","firstShownItemIndex","lastShownItemIndex","slice","map","item","getWidth","getHeight","getArea","area","getMarginTop","scrollableContainer","engine","_waitForScrollingToStop","onStateChange","console","log","getInitialState","useState","updateState","stateUpdateAction","onRender","Error","expectedState","should","include","push","firstNonMeasuredItemIndex","getScrollY","scrollPosition","scrollToY","updateScreenDimensions","_triggerResizeListener","JSON","stringify","stop","start","updateLayout","itemOrIndex","getItemScrollPosition","onItemHeightDidChange","newState","setItemState","getAverageItemHeight","newItems","options","setItems"],"sources":["../../source/test/VirtualScroller.js"],"sourcesContent":["import VirtualScroller from '../VirtualScroller.js'\r\nimport Engine from './Engine.js'\r\n\r\nconst DEBUG = false\r\n\r\nexport default class TestVirtualScroller {\r\n\tconstructor({\r\n\t\tscreenWidth: scrollableContainerWidth,\r\n\t\tscreenHeight: scrollableContainerHeight,\r\n\t\tcolumnsCount,\r\n\t\tverticalSpacing,\r\n\t\titems,\r\n\t\tstate: initialState\r\n\t}) {\r\n\t\tthis.expectedStateUpdates = []\r\n\r\n\t\tconst scrollableContainerElement = {\r\n\t\t\twidth: scrollableContainerWidth,\r\n\t\t\theight: scrollableContainerHeight\r\n\t\t}\r\n\r\n\t\tconst containerElement = {\r\n\t\t\tpaddingTop: 0,\r\n\t\t\tpaddingBottom: 0,\r\n\t\t\twidth: scrollableContainerElement.width,\r\n\t\t\tchildren: []\r\n\t\t}\r\n\r\n\t\tthis.setScrollableContainerWidth = (width) => {\r\n\t\t\tscrollableContainerElement.width = width\r\n\t\t\tcontainerElement.width = width\r\n\t\t}\r\n\r\n\t\tthis.setScrollableContainerHeight = (height) => {\r\n\t\t\tscrollableContainerElement.height = height\r\n\t\t}\r\n\r\n\t\tconst getItemsContainerElement = () => containerElement\r\n\r\n\t\tthis.__columnsCount = columnsCount\r\n\r\n\t\tconst getColumnsCount = () => this.__columnsCount\r\n\t\tconst getItemWidth = () => scrollableContainerElement.width / getColumnsCount()\r\n\r\n\t\tthis.hasPausedStateUpdates = undefined\r\n\t\tthis.pausedStateUpdate = undefined\r\n\t\tthis.pausedStateUpdateAction = undefined\r\n\r\n\t\tconst onBeforeUpdateState = (stateUpdate) => {\r\n\t\t\tif (this.expectedStateUpdates.length > 0) {\r\n\t\t\t\tconst expectedStateUpdate = this.expectedStateUpdates.shift()\r\n\t\t\t\texpect(stateUpdate).to.deep.equal(expectedStateUpdate.stateUpdate)\r\n\t\t\t\tif (expectedStateUpdate.callback) {\r\n\t\t\t\t\texpectedStateUpdate.callback()\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst render = () => {\r\n\t\t\tconst {\r\n\t\t\t\titems,\r\n\t\t\t\tbeforeItemsHeight,\r\n\t\t\t\tafterItemsHeight,\r\n\t\t\t\tfirstShownItemIndex,\r\n\t\t\t\tlastShownItemIndex\r\n\t\t\t} = this.virtualScroller.getState()\r\n\r\n\t\t\tcontainerElement.paddingTop = beforeItemsHeight\r\n\t\t\tcontainerElement.paddingBottom = afterItemsHeight\r\n\r\n\t\t\tcontainerElement.children = items\r\n\t\t\t\t.slice(firstShownItemIndex, lastShownItemIndex + 1)\r\n\t\t\t\t.map((item) => ({\r\n\t\t\t\t\tgetWidth: () => getItemWidth(),\r\n\t\t\t\t\tgetHeight: () => (item.getArea ? item.getArea() : item.area) / getItemWidth(),\r\n\t\t\t\t\tgetMarginTop: () => verticalSpacing\r\n\t\t\t\t}))\r\n\t\t}\r\n\r\n\t\tthis.virtualScroller = new VirtualScroller(getItemsContainerElement, items, {\r\n\t\t\tscrollableContainer: scrollableContainerElement,\r\n\t\t\tengine: Engine,\r\n\t\t\t_waitForScrollingToStop: false,\r\n\t\t\tgetColumnsCount,\r\n\t\t\tstate: initialState,\r\n\t\t\tonStateChange(state) {\r\n\t\t\t\tif (DEBUG) {\r\n\t\t\t\t\tconsole.log('~ Updated State ~')\r\n\t\t\t\t\tconsole.log(state)\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t})\r\n\r\n\t\tlet state = this.virtualScroller.getInitialState()\r\n\r\n\t\tthis.virtualScroller.useState({\r\n\t\t\tgetState: () => state,\r\n\t\t\tupdateState: (stateUpdate) => {\r\n\t\t\t\tconst stateUpdateAction = (stateUpdate) => {\r\n\t\t\t\t\t// Is only used in tests.\r\n\t\t\t\t\tif (onBeforeUpdateState) {\r\n\t\t\t\t\t\tonBeforeUpdateState(stateUpdate)\r\n\t\t\t\t\t}\r\n\t\t\t\t\tstate = {\r\n\t\t\t\t\t\t...state,\r\n\t\t\t\t\t\t...stateUpdate\r\n\t\t\t\t\t}\r\n\t\t\t\t\trender()\r\n\t\t\t\t\tthis.virtualScroller.onRender()\r\n\t\t\t\t}\r\n\t\t\t\tif (this.hasPausedStateUpdates) {\r\n\t\t\t\t\tthis.pausedStateUpdate = {\r\n\t\t\t\t\t\t...this.pausedStateUpdate,\r\n\t\t\t\t\t\t...stateUpdate\r\n\t\t\t\t\t}\r\n\t\t\t\t\tthis.pausedStateUpdateAction = stateUpdateAction\r\n\t\t\t\t} else {\r\n\t\t\t\t\tstateUpdateAction(stateUpdate)\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t})\r\n\r\n\t\trender()\r\n\t}\r\n\r\n\tpauseStateUpdates() {\r\n\t\tif (this.hasPausedStateUpdates) {\r\n\t\t\tthrow new Error('[virtual-scroller] State updates have already been paused')\r\n\t\t}\r\n\t\tthis.hasPausedStateUpdates = true\r\n\t}\r\n\r\n\tresumeStateUpdates() {\r\n\t\tif (this.pausedStateUpdate) {\r\n\t\t\tthis.pausedStateUpdateAction(this.pausedStateUpdate)\r\n\t\t\tthis.pausedStateUpdate = undefined\r\n\t\t\tthis.pausedStateUpdateAction = undefined\r\n\t\t}\r\n\t\tthis.hasPausedStateUpdates = false\r\n\t}\r\n\r\n\tverifyState(expectedState) {\r\n\t\t// `mocha`/`chai`.\r\n\t\tthis.virtualScroller.getState().should.deep.include(expectedState)\r\n\t}\r\n\r\n\texpectStateUpdate(stateUpdate, callback) {\r\n\t\tthis.expectedStateUpdates.push({\r\n\t\t\tstateUpdate,\r\n\t\t\tcallback\r\n\t\t})\r\n\t}\r\n\r\n\tgetFirstNonMeasuredItemIndex() {\r\n\t\treturn this.virtualScroller.firstNonMeasuredItemIndex\r\n\t}\r\n\r\n\tgetScrollY() {\r\n\t\treturn this.virtualScroller.scrollableContainer.getScrollY()\r\n\t}\r\n\r\n\tscrollTo(scrollPosition) {\r\n\t\tthis.virtualScroller.scrollableContainer.scrollToY(scrollPosition)\r\n\t}\r\n\r\n\tupdateScreenDimensions({\r\n\t\tscreenWidth: scrollableContainerWidth,\r\n\t\tscreenHeight: scrollableContainerHeight,\r\n\t\tcolumnsCount\r\n\t}) {\r\n\t\t// Resize scrollable container.\r\n\t\tthis.setScrollableContainerWidth(scrollableContainerWidth)\r\n\t\tthis.setScrollableContainerHeight(scrollableContainerHeight)\r\n\r\n\t\t// Update columns count.\r\n\t\tthis.__columnsCount = columnsCount\r\n\t}\r\n\r\n\t// Returns a `Promise`.\r\n\ttriggerResize({\r\n\t\tscreenWidth,\r\n\t\tscreenHeight,\r\n\t\tcolumnsCount,\r\n\t\tverticalSpacing\r\n\t}) {\r\n\t\tthis.updateScreenDimensions({\r\n\t\t\tscreenWidth,\r\n\t\t\tscreenHeight,\r\n\t\t\tcolumnsCount,\r\n\t\t\tverticalSpacing\r\n\t\t})\r\n\r\n\t\t// Call \"on resize\" listener.\r\n\t\treturn this.virtualScroller.scrollableContainer._triggerResizeListener()\r\n\t}\r\n\r\n\tstop() {\r\n\t\tif (this.expectedStateUpdates.length > 0) {\r\n\t\t\tthrow new Error(`[virtual-scroller] Expected ${this.expectedStateUpdates.length} state updates which didn't happen:\\n${JSON.stringify(this.expectedStateUpdates, null, 2)}`)\r\n\t\t}\r\n\t\tif (this.pausedStateUpdate) {\r\n\t\t\tthrow new Error('[virtual-scroller] State updates were paused and haven\\'t been resumed afterwards')\r\n\t\t}\r\n\t\tthis.virtualScroller.stop()\r\n\t}\r\n\r\n\tstart() {\r\n\t\tthis.virtualScroller.start()\r\n\t}\r\n\r\n\tgetState() {\r\n\t\treturn this.virtualScroller.getState()\r\n\t}\r\n\r\n\tupdateLayout() {\r\n\t\tthis.virtualScroller.updateLayout()\r\n\t}\r\n\r\n\tgetItemScrollPosition(itemOrIndex) {\r\n\t\treturn this.virtualScroller.getItemScrollPosition(itemOrIndex)\r\n\t}\r\n\r\n\tonItemHeightDidChange(itemOrIndex) {\r\n\t\tthis.virtualScroller.onItemHeightDidChange(itemOrIndex)\r\n\t}\r\n\r\n\tsetItemState(itemOrIndex, newState) {\r\n\t\tthis.virtualScroller.setItemState(itemOrIndex, newState)\r\n\t}\r\n\r\n\tgetAverageItemHeight() {\r\n\t\treturn this.virtualScroller.getAverageItemHeight()\r\n\t}\r\n\r\n\tsetItems(newItems, options) {\r\n\t\tthis.virtualScroller.setItems(newItems, options)\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;;;;;AAAA,OAAOA,eAAP,MAA4B,uBAA5B;AACA,OAAOC,MAAP,MAAmB,aAAnB;AAEA,IAAMC,KAAK,GAAG,KAAd;;IAEqBC,mB;EACpB,mCAOG;IAAA;;IAAA,IANWC,wBAMX,QANFC,WAME;IAAA,IALYC,yBAKZ,QALFC,YAKE;IAAA,IAJFC,YAIE,QAJFA,YAIE;IAAA,IAHFC,eAGE,QAHFA,eAGE;IAAA,IAFFC,KAEE,QAFFA,KAEE;IAAA,IADKC,YACL,QADFC,KACE;;IAAA;;IACF,KAAKC,oBAAL,GAA4B,EAA5B;IAEA,IAAMC,0BAA0B,GAAG;MAClCC,KAAK,EAAEX,wBAD2B;MAElCY,MAAM,EAAEV;IAF0B,CAAnC;IAKA,IAAMW,gBAAgB,GAAG;MACxBC,UAAU,EAAE,CADY;MAExBC,aAAa,EAAE,CAFS;MAGxBJ,KAAK,EAAED,0BAA0B,CAACC,KAHV;MAIxBK,QAAQ,EAAE;IAJc,CAAzB;;IAOA,KAAKC,2BAAL,GAAmC,UAACN,KAAD,EAAW;MAC7CD,0BAA0B,CAACC,KAA3B,GAAmCA,KAAnC;MACAE,gBAAgB,CAACF,KAAjB,GAAyBA,KAAzB;IACA,CAHD;;IAKA,KAAKO,4BAAL,GAAoC,UAACN,MAAD,EAAY;MAC/CF,0BAA0B,CAACE,MAA3B,GAAoCA,MAApC;IACA,CAFD;;IAIA,IAAMO,wBAAwB,GAAG,SAA3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AtB,EAAmC;MAClC,KAAKzC,eAAL,CAAqB2C,qBAArB,CAA2CF,WAA3C;IACA;;;WAED,sBAAaA,WAAb,EAA0BG,QAA1B,EAAoC;MACnC,KAAK5C,eAAL,CAAqB6C,YAArB,CAAkCJ,WAAlC,EAA+CG,QAA/C;IACA;;;WAED,gCAAuB;MACtB,OAAO,KAAK5C,eAAL,CAAqB8C,oBAArB,EAAP;IACA;;;WAED,kBAASC,QAAT,EAAmBC,OAAnB,EAA4B;MAC3B,KAAKhD,eAAL,CAAqBiD,QAArB,CAA8BF,QAA9B,EAAwCC,OAAxC;IACA;;;;;;SAvOmBvF,mB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "virtual-scroller",
3
- "version": "1.14.0",
3
+ "version": "1.15.1",
4
4
  "description": "A component for efficiently rendering large lists of variable height items",
5
5
  "main": "index.cjs",
6
6
  "module": "index.js",
@@ -46,6 +46,9 @@
46
46
  "mocha": "^10.0.0",
47
47
  "npm-run-all": "^4.1.5",
48
48
  "nyc": "^15.1.0",
49
+ "prop-types": "^15.8.1",
50
+ "react": "^19.2.0",
51
+ "react-dom": "^19.2.0",
49
52
  "regenerator-runtime": "^0.13.9",
50
53
  "rimraf": "^3.0.2",
51
54
  "rollup": "^2.73.0",
package/react/as.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ // The following is an `as` property implementation
2
+ // that was copy-pasted from https://www.benmvp.com/blog/polymorphic-react-components-typescript/
3
+ //
4
+ // P.S. `as` property is now deprecated, so this file is a legacy one.
5
+ // Use `itemsContainerComponent` property instead.
6
+ //
7
+ // "A more precise version of `React.ComponentPropsWithoutRef`".
8
+ // It's not clear what exactly they meant by that.
9
+ type PropsOf<
10
+ Component extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
11
+ > = JSX.LibraryManagedAttributes<Component, React.ComponentPropsWithoutRef<Component>>
12
+ //
13
+ // Combines props with any additional props.
14
+ type CombineProps<
15
+ BaseProps = {},
16
+ AdditionalProps = {}
17
+ > = Omit<BaseProps, keyof AdditionalProps> & AdditionalProps
18
+ //
19
+ // Combines component props with any additional props.
20
+ type CombineComponentPropsWith<
21
+ Component extends React.ElementType,
22
+ Props = {}
23
+ > = CombineProps<PropsOf<Component>, Props>
24
+ //
25
+ // Adds an `as: AsComponent` property.
26
+ export type WithAsProperty<
27
+ AsComponent extends React.ElementType,
28
+ Props = {}
29
+ > = CombineComponentPropsWith<AsComponent, Props & { as?: AsComponent }>
30
+ // //
31
+ // // This is a type of a `ref` that will be passed to a given `Component`.
32
+ // export type ComponentRef<
33
+ // Component extends React.ElementType
34
+ // > = React.ComponentPropsWithRef<Component>['ref']
35
+ // //
36
+ // // Adds an `as: AsComponent` property along with a `ref` property.
37
+ // export type WithAsPropertyAndRef<
38
+ // AsComponent extends React.ElementType,
39
+ // Props = {}
40
+ // > = WithAsProperty<AsComponent, Props> & {
41
+ // ref?: ComponentRef<AsComponent>
42
+ // }
package/react/index.cjs CHANGED
@@ -1,4 +1,5 @@
1
1
  'use strict'
2
2
 
3
3
  exports = module.exports = require('../commonjs/react/VirtualScroller.js').default
4
- exports['default'] = require('../commonjs/react/VirtualScroller.js').default
4
+ exports['default'] = require('../commonjs/react/VirtualScroller.js').default
5
+ exports.useVirtualScroller = require('../commonjs/react/useVirtualScroller.js').default
package/react/index.d.ts CHANGED
@@ -1,85 +1,270 @@
1
- import { State, NoItemState, VirtualScrollerCommonOptions } from '../index.d.js';
1
+ import { default as VirtualScrollerCore, State, VirtualScrollerCommonOptions } from '../index.d.js';
2
2
 
3
- export { State } from '../index.d.js';
3
+ // `as` property is now deprecated, so `WithAsProperty` type is a legacy one.
4
+ // Use `itemsContainerComponent` property instead.
5
+ import { WithAsProperty } from './as.d.js';
4
6
 
5
7
  import * as React from 'react';
6
8
 
7
- // Taken from https://www.benmvp.com/blog/polymorphic-react-components-typescript/
9
+ export { State, NoItemState } from '../index.d.js';
8
10
 
9
- // A more precise version of just React.ComponentPropsWithoutRef on its own
10
- export type PropsOf<
11
- C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
12
- > = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithoutRef<C>>
11
+ // If `<VirtualScroller/>` simply forwarded the `ref` to the `itemsContainerComponent`
12
+ // then the type of the `ref` would be this:
13
+ //
14
+ // export type VirtualScrollerRef<Component> = Ref<Component>
15
+ //
16
+ // But since `<VirtualScroller/>` exposes some instance methods through the `ref`,
17
+ // the type of the `ref` is:
18
+ //
19
+ export interface VirtualScrollerRefValue {
20
+ // // Returns the items container `Element`.
21
+ // getElement(): HTMLElement;
22
+ //
23
+ // Below is a more sophisticated variant of `getElement()` that returns exactly
24
+ // the tag specified as the `ItemsContainerComponent`.
25
+ // There seems to be no requirement for such "more sophisticated" type declaration.
26
+ // Hence, it is commented out and a simpler type of just `HTMLElement` is used.
27
+ //
28
+ // getElement(): ItemsContainerComponent extends keyof JSX.IntrinsicElements
29
+ // ? (
30
+ // JSX.IntrinsicElements[ItemsContainerComponent] extends React.DetailedHTMLProps<React.AnchorHTMLAttributes<infer T>, T>
31
+ // ? T
32
+ // : HTMLElement
33
+ // ) : HTMLElement;
13
34
 
14
- type AsProp<C extends React.ElementType> = {
15
- /**
16
- * An override of the default HTML tag.
17
- * Can also be another React component.
18
- */
19
- as?: C
35
+ // Forces a re-calculation and re-render of the list.
36
+ updateLayout(): void;
20
37
  }
21
38
 
22
- /**
23
- * Allows for extending a set of props (`ExtendedProps`) by an overriding set of props
24
- * (`OverrideProps`), ensuring that any duplicates are overridden by the overriding
25
- * set of props.
26
- */
27
- export type ExtendableProps<
28
- ExtendedProps = {},
29
- OverrideProps = {}
30
- > = OverrideProps & Omit<ExtendedProps, keyof OverrideProps>
31
-
32
- /**
33
- * Allows for inheriting the props from the specified element type so that
34
- * props like children, className & style work, as well as element-specific
35
- * attributes like aria roles. The component (`C`) must be passed in.
36
- */
37
- export type InheritableElementProps<
38
- C extends React.ElementType,
39
- Props = {}
40
- > = ExtendableProps<PropsOf<C>, Props>
41
-
42
- /**
43
- * A more sophisticated version of `InheritableElementProps` where
44
- * the passed in `as` prop will determine which props can be included
45
- */
46
- export type PolymorphicComponentProps<
47
- C extends React.ElementType,
48
- Props = {}
49
- > = InheritableElementProps<C, Props & AsProp<C>>
50
-
51
- export interface ItemComponentVirtualScrollerProps<Item, ItemState> {
52
- item: Item;
53
- state: ItemState;
54
- setState: (newState: ItemState) => void;
55
- onHeightDidChange: () => void;
56
- }
39
+ // // This is a type of a `ref` that will be passed to a given `Component`.
40
+ // type ComponentRef<Component extends React.ElementType> = React.ComponentPropsWithRef<Component>['ref']
57
41
 
58
- interface PropsBase<ItemComponentProps extends object, Item, ItemState> extends VirtualScrollerCommonOptions<Item, ItemState> {
42
+ // These props are shared between `useVirtualScroller()` hook and `<VirtualScroller/>` component.
43
+ interface PropsBase<
44
+ // `Item` is the type of an element of the `items` array.
45
+ Item,
46
+
47
+ // `ItemComponentState` is the type of an item component state.
48
+ // Example: `interface ItemComponentState { expanded?: boolean }`.
49
+ //
50
+ // In most cases, a developer won't even have to bother about item component state
51
+ // because `ItemComponent` won't have any state.
52
+ // In such cases, type `unknown` is used as a "dummy" placeholder
53
+ // just so that TypeScript doesn't complain about this "generic" being unspecified.
54
+ //
55
+ ItemComponentState = unknown
56
+ > extends VirtualScrollerCommonOptions<Item, ItemComponentState> {
59
57
  items: Item[];
60
- itemComponent: React.ElementType<ItemComponentProps & ItemComponentVirtualScrollerProps<Item, ItemState>>;
61
- itemComponentProps?: ItemComponentProps;
62
- initialState?: State<Item, ItemState>;
58
+ initialState?: State<Item, ItemComponentState>;
63
59
  preserveScrollPositionOnPrependItems?: boolean;
64
60
  readyToStart?: boolean;
65
-
61
+ tbody?: boolean;
66
62
  getScrollableContainer?(): HTMLElement;
67
63
  }
68
64
 
65
+ // These are `<VirtualScroller/>` props except the deprecated `as` property.
66
+ // When `as` property is removed in some future, `PropsWithoutAs` could be renamed to just `export Props`.
67
+ interface PropsWithoutAs<
68
+ // `Item` is the type of an element of the `items` array.
69
+ Item,
70
+
71
+ // `ItemComponent` is a React component that renders an `item` element.
72
+ ItemComponent extends React.ElementType,
73
+
74
+ // `ItemComponentState` is the type of an item component state.
75
+ // Example: `interface ItemComponentState { expanded?: boolean }`.
76
+ //
77
+ // In most cases, a developer won't even have to bother about item component state
78
+ // because `ItemComponent` won't have any state.
79
+ // In such cases, type `unknown` is used as a "dummy" placeholder
80
+ // just so that TypeScript doesn't complain about this "generic" being unspecified.
81
+ //
82
+ ItemComponentState = unknown,
83
+
84
+ // `ItemsContainerComponent` is a React component that renders an element
85
+ // that will be used as a container for all item elements.
86
+ //
87
+ // The default value of `"div"` is legacy behavior.
88
+ // New applications should always specify `itemsContainerComponent` property value.
89
+ //
90
+ ItemsContainerComponent extends React.ElementType = 'div'
91
+ > extends PropsBase<Item, ItemComponentState> {
92
+ ref: React.Ref<VirtualScrollerRefValue>;
93
+ // If `ref` was just "forwarded" to the `ItemsContainerComponent`, its type would have been this:
94
+ // ref: ComponentRef<ItemsContainerComponent>;
95
+ itemComponent: ItemComponent;
96
+ itemComponentProps?: Partial<
97
+ Omit<
98
+ React.ComponentPropsWithoutRef<ItemComponent>,
99
+ keyof VirtualScrollerItemComponentProps<Item, ItemComponentState>
100
+ >
101
+ >;
102
+ // `itemsContainerComponent` property is optional just to avoid legacy compatibility issues.
103
+ // Any new application should explicitly specify it and consider it required.
104
+ itemsContainerComponent?: ItemsContainerComponent;
105
+ itemsContainerComponentProps?: Partial<
106
+ React.ComponentPropsWithoutRef<ItemsContainerComponent>
107
+ >;
108
+ itemsContainerRef?: React.ComponentPropsWithRef<ItemsContainerComponent>['ref'];
109
+ }
110
+
111
+ // These are `<VirtualScroller/>` props including the deprecated `as` property.
112
+ // When `as` property is removed in some future, `PropsWithoutAs` will simply replace this type.
69
113
  export type Props<
70
- ItemComponentProps extends object = {},
71
- Item = any,
72
- ItemState = NoItemState,
73
- AsElement extends React.ElementType = 'div'
74
- > = PolymorphicComponentProps<AsElement, PropsBase<ItemComponentProps, Item, ItemState>>
114
+ // `Item` is the type of an element of the `items` array.
115
+ Item,
116
+
117
+ // `ItemComponent` is a React component that renders an `item` element.
118
+ ItemComponent extends React.ElementType,
119
+
120
+ // `ItemComponentState` is the type of an item component state.
121
+ // Example: `interface ItemComponentState { expanded?: boolean }`.
122
+ //
123
+ // In most cases, a developer won't even have to bother about item component state
124
+ // because `ItemComponent` won't have any state.
125
+ // In such cases, type `unknown` is used as a "dummy" placeholder
126
+ // just so that TypeScript doesn't complain about this "generic" being unspecified.
127
+ //
128
+ ItemComponentState = unknown,
129
+
130
+ // `ItemsContainerComponent` is a React component that renders an element
131
+ // that will be used as a container for all item elements.
132
+ //
133
+ // The default value of `"div"` is legacy behavior.
134
+ // New applications should always specify `itemsContainerComponent` property value.
135
+ //
136
+ ItemsContainerComponent extends React.ElementType = 'div',
137
+
138
+ // `as` property is deprecated; use `itemsContainerComponent` property instead.
139
+ // In some future, `AsComponent` "generic" will be removed from here
140
+ // and it won't be considered a "breaking change" because it doesn't affect the javascript code.
141
+ AsComponent extends React.ElementType = 'div'
142
+ > = WithAsProperty<
143
+ AsComponent,
144
+ PropsWithoutAs<
145
+ Item,
146
+ ItemComponent,
147
+ ItemComponentState,
148
+ ItemsContainerComponent
149
+ >
150
+ >
75
151
 
76
152
  declare function VirtualScroller<
77
- ItemComponentProps extends object = {},
78
- Item = any,
79
- ItemState = NoItemState,
80
- AsElement extends React.ElementType = 'div'
153
+ // `Item` is the type of an element of the `items` array.
154
+ Item,
155
+
156
+ // `ItemComponent` is a React component that renders an `item` element.
157
+ ItemComponent extends React.ElementType,
158
+
159
+ // `ItemComponentState` is the type of an item component state.
160
+ // Example: `interface ItemComponentState { expanded?: boolean }`.
161
+ //
162
+ // In most cases, a developer won't even have to bother about item component state
163
+ // because `ItemComponent` won't have any state.
164
+ // In such cases, type `unknown` is used as a "dummy" placeholder
165
+ // just so that TypeScript doesn't complain about this "generic" being unspecified.
166
+ //
167
+ ItemComponentState = unknown,
168
+
169
+ // `ItemsContainerComponent` is a React component that renders an element
170
+ // that will be used as a container for all item elements.
171
+ //
172
+ // The default value of `"div"` is legacy behavior.
173
+ // New applications should always specify `itemsContainerComponent` property value.
174
+ //
175
+ ItemsContainerComponent extends React.ElementType = 'div',
176
+
177
+ // `as` property is deprecated, use `itemsContainerComponent` property instead.
178
+ // In some future, `AsComponent` "generic" will be removed from here
179
+ // and it won't be considered a "breaking change" because it doesn't affect the javascript code.
180
+ AsComponent extends React.ElementType = 'div'
81
181
  >(
82
- props: Props<ItemComponentProps, Item, ItemState, AsElement>
182
+ props: Props<
183
+ Item,
184
+ ItemComponent,
185
+ ItemComponentState,
186
+ ItemsContainerComponent,
187
+ // `as` property is deprecated, use `itemsContainerComponent` property instead.
188
+ // In some future, `AsComponent` "generic" will be removed from here
189
+ // and it won't be considered a "breaking change" because it doesn't affect the javascript code.
190
+ AsComponent
191
+ >
83
192
  ): JSX.Element;
84
193
 
85
194
  export default VirtualScroller;
195
+
196
+ export function useVirtualScroller<
197
+ // `Item` is the type of an element of the `items` array.
198
+ Item,
199
+
200
+ // `ItemComponentState` is the type of an item component state.
201
+ // Example: `interface ItemComponentState { expanded?: boolean }`.
202
+ //
203
+ // In most cases, a developer won't even have to bother about item component state
204
+ // because `ItemComponent` won't have any state.
205
+ // In such cases, type `unknown` is used as a "dummy" placeholder
206
+ // just so that TypeScript doesn't complain about this "generic" being unspecified.
207
+ //
208
+ ItemComponentState = unknown
209
+ >(
210
+ props: PropsBase<Item, ItemComponentState>
211
+ ): UseVirtualScrollerResult<Item, ItemComponentState>;
212
+
213
+ interface UseVirtualScrollerResult<Item, ItemComponentState> {
214
+ // Use this `state` instead of `virtualScroller.getState()`.
215
+ state: State<Item, ItemComponentState>;
216
+ style?: React.CSSProperties;
217
+ className?: string;
218
+ itemsContainerRef: React.Ref<HTMLElement>;
219
+ virtualScroller: VirtualScrollerCore<HTMLElement, Item, ItemComponentState>;
220
+ }
221
+
222
+ // The props that're passed by `<VirtualScroller/>` to `ItemComponent`.
223
+ export interface VirtualScrollerItemComponentProps<Item, ItemComponentState> {
224
+ item: Item;
225
+ state?: ItemComponentState;
226
+ setState: (newState: ItemComponentState) => void;
227
+ onHeightDidChange: () => void;
228
+ }
229
+
230
+ // This properties are passed by `<VirtualScroller/>` to the `itemsContainerComponent`.
231
+ export interface VirtualScrollerItemsContainerComponentProps<
232
+ ItemsContainerComponent extends React.ElementType
233
+ > {
234
+ ref: React.ComponentPropsWithRef<ItemsContainerComponent>['ref'];
235
+ className?: string;
236
+ style?: React.CSSProperties;
237
+ }
238
+
239
+ // // Utility Type: Combines props with any additional props.
240
+ // //
241
+ // // The rationale for using it is that the standard `&` operator
242
+ // // doesn't work in "merge-and-replace" mode but rather in "merge-and-combine" mode.
243
+ // //
244
+ // // "When properties with the same name but different types are intersected,
245
+ // // TypeScript tries to find a common type. If no common type exists,
246
+ // // the resulting type for that property becomes `never`."
247
+ // //
248
+ // type CombineProps<
249
+ // BaseProps = {},
250
+ // AdditionalProps = {}
251
+ // > = Omit<BaseProps, keyof AdditionalProps> & AdditionalProps
252
+
253
+ // // `ItemComponentProps`.
254
+ // type ItemComponentProps<Item, ItemComponent extends React.ElementType, ItemComponentState> =
255
+ // CombineProps<
256
+ // React.ComponentPropsWithRef<ItemComponent>,
257
+ // VirtualScrollerItemComponentProps<Item, ItemComponentState>
258
+ // >
259
+
260
+ // // `ItemsContainerComponentProps`.
261
+ // type ItemsContainerComponentProps<ItemsContainerComponent extends React.ElementType> =
262
+ // CombineProps<
263
+ // React.ComponentPropsWithRef<ItemsContainerComponent>,
264
+ // VirtualScrollerItemsContainerComponentProps
265
+ // >
266
+
267
+ // // Utility Type: Fits any object.
268
+ // interface AnyObject {
269
+ // [key: string]: any;
270
+ // }
package/react/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export { default } from '../modules/react/VirtualScroller.js'
2
+ export { default as useVirtualScroller } from '../modules/react/useVirtualScroller.js'
package/rollup.config.mjs CHANGED
@@ -37,7 +37,21 @@ export default [
37
37
  }
38
38
  },
39
39
  {
40
- input: 'react/index.js',
40
+ // Legacy compatibility:
41
+ //
42
+ // Originally, the default export of the `virtual-scroller/react` subpackage
43
+ // was only the `VirtualScroller` component, and there were no other exports.
44
+ //
45
+ // Later, `useVirtualScroller()` hook export was added.
46
+ // In order to maintain legacy compatibility, the new exports shouldn't "break"
47
+ // the existing environments that were using the old versions of the package.
48
+ // This means that in non-ES6-import environments, any additional exports
49
+ // should be added directly to the default `VirtualScroller` export.
50
+ //
51
+ // That's the reason why it uses "modules/react/VirtualScroller.js" file
52
+ // as `input` here instead of "react/index.js".
53
+ //
54
+ input: 'modules/react/VirtualScroller.js',
41
55
  plugins: [
42
56
  resolveModules,
43
57
  commonjs(),
@@ -171,8 +171,7 @@ export default class BeforeResize {
171
171
  //
172
172
  snapshotBeforeResizeItemHeights({
173
173
  firstShownItemIndex,
174
- newFirstShownItemIndex,
175
- newColumnsCount
174
+ newFirstShownItemIndex
176
175
  }) {
177
176
  const columnsCount = this.getColumnsCount()
178
177
  const verticalSpacing = this.getVerticalSpacing()
@@ -176,17 +176,17 @@ export default class VirtualScroller {
176
176
  * @deprecated
177
177
  * `.onItemHeightChange()` has been renamed to `.onItemHeightDidChange()`.
178
178
  */
179
- onItemHeightChange(i) {
180
- warn('`.onItemHeightChange(i)` method was renamed to `.onItemHeightDidChange(i)`')
181
- this.onItemHeightDidChange(i)
179
+ onItemHeightChange(itemOrIndex) {
180
+ warn('`.onItemHeightChange(item)` method was renamed to `.onItemHeightDidChange(item)`')
181
+ this.onItemHeightDidChange(itemOrIndex)
182
182
  }
183
183
 
184
- onItemHeightDidChange(i) {
185
- this.virtualScroller.onItemHeightDidChange(i)
184
+ onItemHeightDidChange(itemOrIndex) {
185
+ this.virtualScroller.onItemHeightDidChange(itemOrIndex)
186
186
  }
187
187
 
188
- setItemState(i, newState) {
189
- this.virtualScroller.setItemState(i, newState)
188
+ setItemState(itemOrIndex, newState) {
189
+ this.virtualScroller.setItemState(itemOrIndex, newState)
190
190
  }
191
191
 
192
192
  /**
@@ -201,10 +201,4 @@ export default class VirtualScroller {
201
201
  setItems(newItems, options) {
202
202
  this.virtualScroller.setItems(newItems, options)
203
203
  }
204
-
205
- /*
206
- getItemCoordinates(i) {
207
- return this.virtualScroller.getItemCoordinates(i)
208
- }
209
- */
210
204
  }
@@ -19,17 +19,17 @@ export function supportsTbody() {
19
19
  return !isInternetExplorer()
20
20
  }
21
21
 
22
- export const TBODY_CLASS_NAME = 'VirtualScroller'
22
+ export const CLASS_NAME_FOR_TBODY_WORKAROUND = 'VirtualScroller'
23
23
  const STYLE_ELEMENT_ID = 'VirtualScrollerStyle'
24
24
 
25
25
  export function hasTbodyStyles(tbody) {
26
- return tbody.classList.contains(TBODY_CLASS_NAME) &&
26
+ return tbody.classList.contains(CLASS_NAME_FOR_TBODY_WORKAROUND) &&
27
27
  Boolean(document.getElementById(STYLE_ELEMENT_ID))
28
28
  }
29
29
 
30
30
  export function addTbodyStyles(tbody) {
31
31
  // `classList.add` is supported in Internet Explorer 10+.
32
- tbody.classList.add(TBODY_CLASS_NAME)
32
+ tbody.classList.add(CLASS_NAME_FOR_TBODY_WORKAROUND)
33
33
 
34
34
  // Create a `<style/>` element.
35
35
  const style = document.createElement('style')
@@ -37,12 +37,12 @@ export function addTbodyStyles(tbody) {
37
37
 
38
38
  // CSS variables aren't supported in Internet Explorer.
39
39
  style.innerText = `
40
- tbody.${TBODY_CLASS_NAME}:before {
40
+ tbody.${CLASS_NAME_FOR_TBODY_WORKAROUND}:before {
41
41
  content: '';
42
42
  display: table-row;
43
43
  height: var(--VirtualScroller-paddingTop);
44
44
  }
45
- tbody.${TBODY_CLASS_NAME}:after {
45
+ tbody.${CLASS_NAME_FOR_TBODY_WORKAROUND}:after {
46
46
  content: '';
47
47
  display: table-row;
48
48
  height: var(--VirtualScroller-paddingBottom);