@oxog/scrollex 1.1.0 → 1.1.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.
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkND5PV5MU_cjs = require('./chunk-ND5PV5MU.cjs');
3
+ var chunkMEEFAXSA_cjs = require('./chunk-MEEFAXSA.cjs');
4
4
  var react = require('react');
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
6
 
@@ -299,7 +299,7 @@ var ScrollEngine = class {
299
299
  this.scrollEndTimer = null;
300
300
  }
301
301
  if (this.animationId) {
302
- chunkND5PV5MU_cjs.cancelRaf(this.animationId);
302
+ chunkMEEFAXSA_cjs.cancelRaf(this.animationId);
303
303
  this.animationId = null;
304
304
  }
305
305
  this.boundHandleScroll = null;
@@ -370,7 +370,7 @@ var ScrollEngine = class {
370
370
  setScrollPosition(position) {
371
371
  if (!this.container) return;
372
372
  if (this.animationId) {
373
- chunkND5PV5MU_cjs.cancelRaf(this.animationId);
373
+ chunkMEEFAXSA_cjs.cancelRaf(this.animationId);
374
374
  this.animationId = null;
375
375
  }
376
376
  setScrollPosition(this.container, position);
@@ -402,7 +402,7 @@ var ScrollEngine = class {
402
402
  */
403
403
  animateScroll(targetOffset, duration, easing) {
404
404
  if (this.animationId) {
405
- chunkND5PV5MU_cjs.cancelRaf(this.animationId);
405
+ chunkMEEFAXSA_cjs.cancelRaf(this.animationId);
406
406
  }
407
407
  this.animationStartTime = performance.now();
408
408
  this.animationStartOffset = this.horizontal ? this.scrollPosition.scrollLeft : this.scrollPosition.scrollTop;
@@ -416,12 +416,12 @@ var ScrollEngine = class {
416
416
  const currentOffset = this.animationStartOffset + (this.animationTargetOffset - this.animationStartOffset) * easedProgress;
417
417
  this.setScrollPosition(this.horizontal ? { scrollLeft: currentOffset } : { scrollTop: currentOffset });
418
418
  if (progress < 1) {
419
- this.animationId = chunkND5PV5MU_cjs.raf(animate);
419
+ this.animationId = chunkMEEFAXSA_cjs.raf(animate);
420
420
  } else {
421
421
  this.animationId = null;
422
422
  }
423
423
  };
424
- this.animationId = chunkND5PV5MU_cjs.raf(animate);
424
+ this.animationId = chunkMEEFAXSA_cjs.raf(animate);
425
425
  }
426
426
  /**
427
427
  * Check if currently scrolling.
@@ -508,7 +508,7 @@ var ScrollEngine = class {
508
508
  */
509
509
  stopAnimation() {
510
510
  if (this.animationId) {
511
- chunkND5PV5MU_cjs.cancelRaf(this.animationId);
511
+ chunkMEEFAXSA_cjs.cancelRaf(this.animationId);
512
512
  this.animationId = null;
513
513
  }
514
514
  }
@@ -895,9 +895,59 @@ var RangeCalculator = class {
895
895
  this.countTree = new Float64Array(1);
896
896
  this.sumTree = new Float64Array(1);
897
897
  this.treesValid = false;
898
+ // Items whose size came from sizeGetter (not from a measurement): a new getter sizes them again
899
+ this.sized = /* @__PURE__ */ new Set();
898
900
  this.itemCount = options.itemCount;
899
901
  this.overscan = options.overscan;
900
902
  this.measurementCache = new MeasurementCache(options.estimatedItemHeight);
903
+ this.sizeGetter = options.getItemSize;
904
+ }
905
+ /**
906
+ * Set (or clear) the function that gives the known size of an item. Unmeasured items take that size
907
+ * up front; measurements already taken are kept, and the items sized by the previous function are sized
908
+ * again by the new one.
909
+ *
910
+ * @param getItemSize - Size of an item, or undefined to size unmeasured items with the estimate
911
+ * @returns Whether any item size (or the total size) changed
912
+ */
913
+ setSizeGetter(getItemSize) {
914
+ if (getItemSize === this.sizeGetter) return false;
915
+ const hadGetter = this.sizeGetter !== void 0;
916
+ this.sizeGetter = getItemSize;
917
+ let changed = hadGetter !== (getItemSize !== void 0);
918
+ for (let index = 0; index < this.itemCount; index++) {
919
+ if (this.measurementCache.has(index) && !this.sized.has(index)) continue;
920
+ const size = getItemSize ? getItemSize(index) : NaN;
921
+ const previous = this.measurementCache.get(index);
922
+ if (Number.isFinite(size) && size >= 0) {
923
+ if (size !== previous) {
924
+ this.measurementCache.set(index, size);
925
+ this.sized.add(index);
926
+ changed = true;
927
+ }
928
+ } else if (previous !== void 0) {
929
+ this.measurementCache.delete(index);
930
+ this.sized.delete(index);
931
+ changed = true;
932
+ }
933
+ }
934
+ if (changed) this.treesValid = false;
935
+ return changed;
936
+ }
937
+ /**
938
+ * Size the unmeasured items with the known-size function.
939
+ */
940
+ applyKnownSizes() {
941
+ const getItemSize = this.sizeGetter;
942
+ if (!getItemSize) return;
943
+ for (let index = 0; index < this.itemCount; index++) {
944
+ if (this.measurementCache.has(index)) continue;
945
+ const size = getItemSize(index);
946
+ if (Number.isFinite(size) && size >= 0) {
947
+ this.measurementCache.set(index, size);
948
+ this.sized.add(index);
949
+ }
950
+ }
901
951
  }
902
952
  /**
903
953
  * Get the visible range of items.
@@ -936,7 +986,7 @@ var RangeCalculator = class {
936
986
  const itemEnd = this.getItemEnd(mid);
937
987
  return itemEnd > scrollOffset;
938
988
  });
939
- return chunkND5PV5MU_cjs.clamp(index, 0, this.itemCount - 1);
989
+ return chunkMEEFAXSA_cjs.clamp(index, 0, this.itemCount - 1);
940
990
  }
941
991
  /**
942
992
  * Find the end index for a given scroll end position.
@@ -947,7 +997,7 @@ var RangeCalculator = class {
947
997
  while (index < this.itemCount - 1 && this.getItemOffset(index + 1) < scrollEnd) {
948
998
  index++;
949
999
  }
950
- return chunkND5PV5MU_cjs.clamp(index, startIndex, this.itemCount - 1);
1000
+ return chunkMEEFAXSA_cjs.clamp(index, startIndex, this.itemCount - 1);
951
1001
  }
952
1002
  /**
953
1003
  * Get the offset (start position) of an item.
@@ -976,6 +1026,7 @@ var RangeCalculator = class {
976
1026
  * @returns Item size
977
1027
  */
978
1028
  getItemSize(index) {
1029
+ if (this.sizeGetter) this.ensureTrees();
979
1030
  return this.measurementCache.getOrEstimate(index);
980
1031
  }
981
1032
  /**
@@ -994,6 +1045,7 @@ var RangeCalculator = class {
994
1045
  * @returns Previous height, or undefined if not measured
995
1046
  */
996
1047
  setItemHeight(index, height) {
1048
+ this.sized.delete(index);
997
1049
  const previousHeight = this.measurementCache.set(index, height);
998
1050
  this.track(index, previousHeight === void 0 ? 1 : 0, height - (previousHeight ?? 0));
999
1051
  return previousHeight;
@@ -1005,6 +1057,7 @@ var RangeCalculator = class {
1005
1057
  * @returns Cached height, or undefined if not measured
1006
1058
  */
1007
1059
  getCachedHeight(index) {
1060
+ if (this.sizeGetter) this.ensureTrees();
1008
1061
  return this.measurementCache.get(index);
1009
1062
  }
1010
1063
  /**
@@ -1014,15 +1067,24 @@ var RangeCalculator = class {
1014
1067
  */
1015
1068
  invalidate(index) {
1016
1069
  const previousHeight = this.measurementCache.get(index);
1070
+ this.sized.delete(index);
1017
1071
  if (this.measurementCache.delete(index)) {
1018
1072
  this.track(index, -1, -(previousHeight ?? 0));
1019
1073
  }
1074
+ if (this.sizeGetter && Number.isInteger(index) && index >= 0 && index < this.itemCount) {
1075
+ const size = this.sizeGetter(index);
1076
+ if (Number.isFinite(size) && size >= 0) {
1077
+ this.setItemHeight(index, size);
1078
+ this.sized.add(index);
1079
+ }
1080
+ }
1020
1081
  }
1021
1082
  /**
1022
1083
  * Invalidate all measurements.
1023
1084
  */
1024
1085
  invalidateAll() {
1025
1086
  this.measurementCache.clear();
1087
+ this.sized.clear();
1026
1088
  this.treesValid = false;
1027
1089
  }
1028
1090
  /**
@@ -1038,6 +1100,9 @@ var RangeCalculator = class {
1038
1100
  for (const measured of this.measurementCache.getMeasuredIndices()) {
1039
1101
  if (measured >= count) this.measurementCache.delete(measured);
1040
1102
  }
1103
+ for (const index of this.sized) {
1104
+ if (index >= count) this.sized.delete(index);
1105
+ }
1041
1106
  }
1042
1107
  this.treesValid = false;
1043
1108
  }
@@ -1063,6 +1128,7 @@ var RangeCalculator = class {
1063
1128
  * @returns Estimated height
1064
1129
  */
1065
1130
  getEstimatedHeight() {
1131
+ if (this.sizeGetter) this.ensureTrees();
1066
1132
  return this.measurementCache.getEstimatedHeight();
1067
1133
  }
1068
1134
  /**
@@ -1124,6 +1190,7 @@ var RangeCalculator = class {
1124
1190
  ensureTrees() {
1125
1191
  if (this.treesValid) return;
1126
1192
  const size = this.itemCount;
1193
+ this.applyKnownSizes();
1127
1194
  this.countTree = new Float64Array(size + 1);
1128
1195
  this.sumTree = new Float64Array(size + 1);
1129
1196
  for (const index of this.measurementCache.getMeasuredIndices()) {
@@ -1411,7 +1478,7 @@ var DEFAULT_OPTIONS = {
1411
1478
  };
1412
1479
  function createKernel(options = {}) {
1413
1480
  const config = { ...DEFAULT_OPTIONS, ...options };
1414
- config.overscan = chunkND5PV5MU_cjs.normalizeOverscan(config.overscan);
1481
+ config.overscan = chunkMEEFAXSA_cjs.normalizeOverscan(config.overscan);
1415
1482
  const eventBus = new EventBus();
1416
1483
  const scrollEngine = new ScrollEngine();
1417
1484
  scrollEngine.setDirection(config.direction);
@@ -1419,10 +1486,12 @@ function createKernel(options = {}) {
1419
1486
  const rangeCalculator = new RangeCalculator({
1420
1487
  itemCount: config.itemCount,
1421
1488
  estimatedItemHeight: config.estimatedItemHeight,
1422
- overscan: config.overscan
1489
+ overscan: config.overscan,
1490
+ getItemSize: config.getItemSize
1423
1491
  });
1424
1492
  const pluginRegistry = new PluginRegistry();
1425
1493
  const startPadding = () => config.paddingStart ?? 0;
1494
+ const listStart = () => (config.scrollMargin ?? 0) + startPadding();
1426
1495
  let rangeAnnounced = false;
1427
1496
  let currentRange = {
1428
1497
  startIndex: 0,
@@ -1451,7 +1520,7 @@ function createKernel(options = {}) {
1451
1520
  const horizontal = config.direction === "horizontal";
1452
1521
  const offset = horizontal ? position.scrollLeft : position.scrollTop;
1453
1522
  const viewport = viewportManager.getViewport();
1454
- const maxScroll = rangeCalculator.getTotalSize() + startPadding() - (horizontal ? viewport.width : viewport.height);
1523
+ const maxScroll = rangeCalculator.getTotalSize() + listStart() - (horizontal ? viewport.width : viewport.height);
1455
1524
  const percentage = maxScroll > 0 ? Math.min(1, Math.max(0, offset / maxScroll)) : 0;
1456
1525
  return { ...position, offset, percentage };
1457
1526
  },
@@ -1463,7 +1532,7 @@ function createKernel(options = {}) {
1463
1532
  },
1464
1533
  scrollToIndex(index, options2 = {}) {
1465
1534
  const { align = "auto", offset: additionalOffset = 0, ...scrollOptions } = options2;
1466
- const itemOffset = rangeCalculator.getItemOffset(index) + startPadding();
1535
+ const itemOffset = rangeCalculator.getItemOffset(index) + listStart();
1467
1536
  const itemSize = rangeCalculator.getItemSize(index);
1468
1537
  const viewport = viewportManager.getViewport();
1469
1538
  const horizontal = config.direction === "horizontal";
@@ -1495,7 +1564,7 @@ function createKernel(options = {}) {
1495
1564
  break;
1496
1565
  }
1497
1566
  }
1498
- const maxScroll = Math.max(0, rangeCalculator.getTotalSize() + startPadding() - viewportSize);
1567
+ const maxScroll = Math.max(0, rangeCalculator.getTotalSize() + listStart() - viewportSize);
1499
1568
  targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
1500
1569
  scrollEngine.scrollTo(targetOffset, scrollOptions);
1501
1570
  },
@@ -1545,7 +1614,7 @@ function createKernel(options = {}) {
1545
1614
  return rangeCalculator.getEstimatedHeight();
1546
1615
  },
1547
1616
  getItemOffset(index) {
1548
- return rangeCalculator.getItemOffset(index) + startPadding();
1617
+ return rangeCalculator.getItemOffset(index) + listStart();
1549
1618
  },
1550
1619
  // Events
1551
1620
  emit(event) {
@@ -1576,13 +1645,21 @@ function createKernel(options = {}) {
1576
1645
  layoutChanged = newOptions.paddingStart !== startPadding();
1577
1646
  config.paddingStart = newOptions.paddingStart;
1578
1647
  }
1648
+ if (newOptions.scrollMargin !== void 0) {
1649
+ layoutChanged = layoutChanged || newOptions.scrollMargin !== (config.scrollMargin ?? 0);
1650
+ config.scrollMargin = newOptions.scrollMargin;
1651
+ }
1652
+ if ("getItemSize" in newOptions && newOptions.getItemSize !== config.getItemSize) {
1653
+ config.getItemSize = newOptions.getItemSize;
1654
+ layoutChanged = rangeCalculator.setSizeGetter(newOptions.getItemSize) || layoutChanged;
1655
+ }
1579
1656
  if (newOptions.estimatedItemHeight !== void 0) {
1580
1657
  layoutChanged = layoutChanged || newOptions.estimatedItemHeight !== config.estimatedItemHeight;
1581
1658
  config.estimatedItemHeight = newOptions.estimatedItemHeight;
1582
1659
  rangeCalculator.setEstimatedHeight(newOptions.estimatedItemHeight);
1583
1660
  }
1584
1661
  if (newOptions.overscan !== void 0) {
1585
- config.overscan = chunkND5PV5MU_cjs.normalizeOverscan(newOptions.overscan);
1662
+ config.overscan = chunkMEEFAXSA_cjs.normalizeOverscan(newOptions.overscan);
1586
1663
  rangeCalculator.setOverscan(config.overscan);
1587
1664
  }
1588
1665
  if (newOptions.direction !== void 0) {
@@ -1652,7 +1729,7 @@ function createKernel(options = {}) {
1652
1729
  if (!attached) return;
1653
1730
  const viewport = viewportManager.getViewport();
1654
1731
  const scrollPosition = scrollEngine.getScrollPosition();
1655
- const scrollOffset = (config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft) - startPadding();
1732
+ const scrollOffset = (config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft) - listStart();
1656
1733
  const viewportSize = config.direction === "vertical" ? viewport.height : viewport.width;
1657
1734
  const previousRange = { ...currentRange };
1658
1735
  currentRange = rangeCalculator.getVisibleRange(scrollOffset, viewportSize);
@@ -1741,6 +1818,7 @@ function useVirtualList(options) {
1741
1818
  overscan = 5,
1742
1819
  paddingStart = 0,
1743
1820
  paddingEnd = 0,
1821
+ scrollMargin = 0,
1744
1822
  initialOffset = 0,
1745
1823
  getItemKey = (index) => index,
1746
1824
  horizontal = false,
@@ -1755,7 +1833,10 @@ function useVirtualList(options) {
1755
1833
  overscan,
1756
1834
  direction: horizontal ? "horizontal" : "vertical",
1757
1835
  getItemKey,
1758
- paddingStart
1836
+ paddingStart,
1837
+ scrollMargin,
1838
+ // Known heights lay out every item up front, not only the rows attached with measureElement
1839
+ getItemSize: getItemHeight
1759
1840
  })
1760
1841
  );
1761
1842
  const [virtualItems, setVirtualItems] = react.useState([]);
@@ -1773,7 +1854,7 @@ function useVirtualList(options) {
1773
1854
  const resizeObserverRef = react.useRef(null);
1774
1855
  const kernel = kernelRef.current;
1775
1856
  react.useEffect(() => {
1776
- const plugin = chunkND5PV5MU_cjs.listRendererPlugin({ getItemKey: (index) => getItemKeyRef.current(index) });
1857
+ const plugin = chunkMEEFAXSA_cjs.listRendererPlugin({ getItemKey: (index) => getItemKeyRef.current(index) });
1777
1858
  kernel.register(plugin);
1778
1859
  listRendererRef.current = plugin.api;
1779
1860
  const unsubScroll = kernel.on("scroll", (event) => {
@@ -1837,6 +1918,12 @@ function useVirtualList(options) {
1837
1918
  react.useEffect(() => {
1838
1919
  kernel.configure({ paddingStart });
1839
1920
  }, [paddingStart, kernel]);
1921
+ react.useEffect(() => {
1922
+ kernel.configure({ scrollMargin });
1923
+ }, [scrollMargin, kernel]);
1924
+ react.useEffect(() => {
1925
+ kernel.configure({ getItemSize: getItemHeight });
1926
+ }, [getItemHeight, kernel]);
1840
1927
  react.useEffect(() => {
1841
1928
  kernel.configure({ direction: horizontal ? "horizontal" : "vertical" });
1842
1929
  }, [horizontal, kernel]);
@@ -1880,7 +1967,7 @@ function useVirtualList(options) {
1880
1967
  observer?.observe(element);
1881
1968
  if (getItemHeight) {
1882
1969
  const size = getItemHeight(index);
1883
- kernel.measureItem(index, size);
1970
+ if (kernel.getCachedHeight(index) !== size) kernel.measureItem(index, size);
1884
1971
  } else {
1885
1972
  const rect = element.getBoundingClientRect();
1886
1973
  const size = horizontal ? rect.width : rect.height;
@@ -2209,7 +2296,7 @@ function useVirtualGrid(options) {
2209
2296
  overscanStartIndex: 0,
2210
2297
  overscanEndIndex: 0
2211
2298
  });
2212
- const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? chunkND5PV5MU_cjs.normalizeColumnCount(columns) : 1);
2299
+ const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? chunkMEEFAXSA_cjs.normalizeColumnCount(columns) : 1);
2213
2300
  const [columnWidth, setColumnWidth] = react.useState(0);
2214
2301
  const gridRendererRef = react.useRef(null);
2215
2302
  const kernel = kernelRef.current;
@@ -2221,7 +2308,7 @@ function useVirtualGrid(options) {
2221
2308
  if (api && itemCount > 0) kernel.configure({ estimatedItemHeight: api.getTotalHeight() / itemCount });
2222
2309
  }, [kernel]);
2223
2310
  react.useEffect(() => {
2224
- const plugin = chunkND5PV5MU_cjs.gridRendererPlugin({
2311
+ const plugin = chunkMEEFAXSA_cjs.gridRendererPlugin({
2225
2312
  columns,
2226
2313
  minColumnWidth,
2227
2314
  rowHeight,
@@ -2618,7 +2705,7 @@ function useVirtualMasonry(options) {
2618
2705
  overscanStartIndex: 0,
2619
2706
  overscanEndIndex: 0
2620
2707
  });
2621
- const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? chunkND5PV5MU_cjs.normalizeColumnCount(columns) : 1);
2708
+ const [columnCount, setColumnCount] = react.useState(typeof columns === "number" ? chunkMEEFAXSA_cjs.normalizeColumnCount(columns) : 1);
2622
2709
  const [columnWidth, setColumnWidth] = react.useState(0);
2623
2710
  const [columnHeights, setColumnHeights] = react.useState([]);
2624
2711
  const [totalSize, setTotalSize] = react.useState(0);
@@ -2637,7 +2724,7 @@ function useVirtualMasonry(options) {
2637
2724
  setTotalSize(0);
2638
2725
  return;
2639
2726
  }
2640
- const columnTotal = columns === "auto" ? chunkND5PV5MU_cjs.autoColumnCount(containerWidth, minColumnWidth, gapX) : chunkND5PV5MU_cjs.normalizeColumnCount(columns);
2727
+ const columnTotal = columns === "auto" ? chunkMEEFAXSA_cjs.autoColumnCount(containerWidth, minColumnWidth, gapX) : chunkMEEFAXSA_cjs.normalizeColumnCount(columns);
2641
2728
  const newColumnWidth = (containerWidth - (columnTotal - 1) * gapX) / columnTotal;
2642
2729
  setColumnCount(columnTotal);
2643
2730
  setColumnWidth(newColumnWidth);
@@ -2668,7 +2755,7 @@ function useVirtualMasonry(options) {
2668
2755
  const getVisibleItems = react.useCallback((scrollTop, viewportHeight) => {
2669
2756
  const cache = layoutCacheRef.current;
2670
2757
  const items = [];
2671
- const overscanAmount = chunkND5PV5MU_cjs.normalizeOverscan(overscan) * 100;
2758
+ const overscanAmount = chunkMEEFAXSA_cjs.normalizeOverscan(overscan) * 100;
2672
2759
  const viewStart = Math.max(0, scrollTop - overscanAmount);
2673
2760
  const viewEnd = scrollTop + viewportHeight + overscanAmount;
2674
2761
  for (let i = 0; i < count; i++) {
@@ -3175,23 +3262,23 @@ function useMeasureMany() {
3175
3262
 
3176
3263
  Object.defineProperty(exports, "autoMeasurerPlugin", {
3177
3264
  enumerable: true,
3178
- get: function () { return chunkND5PV5MU_cjs.autoMeasurerPlugin; }
3265
+ get: function () { return chunkMEEFAXSA_cjs.autoMeasurerPlugin; }
3179
3266
  });
3180
3267
  Object.defineProperty(exports, "gridRendererPlugin", {
3181
3268
  enumerable: true,
3182
- get: function () { return chunkND5PV5MU_cjs.gridRendererPlugin; }
3269
+ get: function () { return chunkMEEFAXSA_cjs.gridRendererPlugin; }
3183
3270
  });
3184
3271
  Object.defineProperty(exports, "infiniteLoaderPlugin", {
3185
3272
  enumerable: true,
3186
- get: function () { return chunkND5PV5MU_cjs.infiniteLoaderPlugin; }
3273
+ get: function () { return chunkMEEFAXSA_cjs.infiniteLoaderPlugin; }
3187
3274
  });
3188
3275
  Object.defineProperty(exports, "listRendererPlugin", {
3189
3276
  enumerable: true,
3190
- get: function () { return chunkND5PV5MU_cjs.listRendererPlugin; }
3277
+ get: function () { return chunkMEEFAXSA_cjs.listRendererPlugin; }
3191
3278
  });
3192
3279
  Object.defineProperty(exports, "scrollControllerPlugin", {
3193
3280
  enumerable: true,
3194
- get: function () { return chunkND5PV5MU_cjs.scrollControllerPlugin; }
3281
+ get: function () { return chunkMEEFAXSA_cjs.scrollControllerPlugin; }
3195
3282
  });
3196
3283
  exports.VirtualGrid = VirtualGrid;
3197
3284
  exports.VirtualList = VirtualList;