@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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React$1 from 'react';
2
- import { V as VirtualListProps, a as VirtualListHandle, b as VirtualGridProps, c as VirtualMasonryProps, U as UseVirtualListOptions, d as UseVirtualListReturn, e as UseVirtualGridOptions, f as UseVirtualGridReturn, g as UseVirtualMasonryOptions, h as UseVirtualMasonryReturn, K as Kernel, i as KernelOptions, P as Plugin } from './scroll-controller-D5AV1E2p.cjs';
3
- export { A as AutoMeasurerAPI, j as AutoMeasurerOptions, E as EasingFunction, k as EventHandler, l as EventType, G as GridGap, m as GridRenderItemProps, n as GridRendererAPI, o as GridRendererOptions, I as InfiniteLoaderAPI, p as InfiniteLoaderOptions, q as ItemMeasuredEvent, r as ItemsChangeEvent, s as ItemsRenderedInfo, t as KernelEvent, L as ListRendererAPI, u as ListRendererOptions, v as LoadDirection, w as LoadMoreEvent, M as MasonryRenderItemProps, x as PluginConfig, y as PluginHooks, z as PluginInfo, B as PluginType, R as Range, C as RenderItemProps, D as ResizeEvent, S as ScrollAlignment, F as ScrollBehavior, H as ScrollControllerAPI, J as ScrollControllerOptions, N as ScrollDirection, O as ScrollEndEvent, Q as ScrollEvent, T as ScrollOptions, W as ScrollPosition, X as ScrollStartEvent, Y as ScrollToIndexOptions, Z as Unsubscribe, _ as Viewport, $ as VirtualItem, a0 as VisibleRangeChangeEvent, a1 as autoMeasurerPlugin, a2 as gridRendererPlugin, a3 as infiniteLoaderPlugin, a4 as listRendererPlugin, a5 as scrollControllerPlugin } from './scroll-controller-D5AV1E2p.cjs';
2
+ import { V as VirtualListProps, a as VirtualListHandle, b as VirtualGridProps, c as VirtualMasonryProps, U as UseVirtualListOptions, d as UseVirtualListReturn, e as UseVirtualGridOptions, f as UseVirtualGridReturn, g as UseVirtualMasonryOptions, h as UseVirtualMasonryReturn, K as Kernel, i as KernelOptions, P as Plugin } from './scroll-controller-CBna5Yim.cjs';
3
+ export { A as AutoMeasurerAPI, j as AutoMeasurerOptions, E as EasingFunction, k as EventHandler, l as EventType, G as GridGap, m as GridRenderItemProps, n as GridRendererAPI, o as GridRendererOptions, I as InfiniteLoaderAPI, p as InfiniteLoaderOptions, q as ItemMeasuredEvent, r as ItemsChangeEvent, s as ItemsRenderedInfo, t as KernelEvent, L as ListRendererAPI, u as ListRendererOptions, v as LoadDirection, w as LoadMoreEvent, M as MasonryRenderItemProps, x as PluginConfig, y as PluginHooks, z as PluginInfo, B as PluginType, R as Range, C as RenderItemProps, D as ResizeEvent, S as ScrollAlignment, F as ScrollBehavior, H as ScrollControllerAPI, J as ScrollControllerOptions, N as ScrollDirection, O as ScrollEndEvent, Q as ScrollEvent, T as ScrollOptions, W as ScrollPosition, X as ScrollStartEvent, Y as ScrollToIndexOptions, Z as Unsubscribe, _ as Viewport, $ as VirtualItem, a0 as VisibleRangeChangeEvent, a1 as autoMeasurerPlugin, a2 as gridRendererPlugin, a3 as infiniteLoaderPlugin, a4 as listRendererPlugin, a5 as scrollControllerPlugin } from './scroll-controller-CBna5Yim.cjs';
4
4
 
5
5
  /**
6
6
  * VirtualList component with forwardRef.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React$1 from 'react';
2
- import { V as VirtualListProps, a as VirtualListHandle, b as VirtualGridProps, c as VirtualMasonryProps, U as UseVirtualListOptions, d as UseVirtualListReturn, e as UseVirtualGridOptions, f as UseVirtualGridReturn, g as UseVirtualMasonryOptions, h as UseVirtualMasonryReturn, K as Kernel, i as KernelOptions, P as Plugin } from './scroll-controller-D5AV1E2p.js';
3
- export { A as AutoMeasurerAPI, j as AutoMeasurerOptions, E as EasingFunction, k as EventHandler, l as EventType, G as GridGap, m as GridRenderItemProps, n as GridRendererAPI, o as GridRendererOptions, I as InfiniteLoaderAPI, p as InfiniteLoaderOptions, q as ItemMeasuredEvent, r as ItemsChangeEvent, s as ItemsRenderedInfo, t as KernelEvent, L as ListRendererAPI, u as ListRendererOptions, v as LoadDirection, w as LoadMoreEvent, M as MasonryRenderItemProps, x as PluginConfig, y as PluginHooks, z as PluginInfo, B as PluginType, R as Range, C as RenderItemProps, D as ResizeEvent, S as ScrollAlignment, F as ScrollBehavior, H as ScrollControllerAPI, J as ScrollControllerOptions, N as ScrollDirection, O as ScrollEndEvent, Q as ScrollEvent, T as ScrollOptions, W as ScrollPosition, X as ScrollStartEvent, Y as ScrollToIndexOptions, Z as Unsubscribe, _ as Viewport, $ as VirtualItem, a0 as VisibleRangeChangeEvent, a1 as autoMeasurerPlugin, a2 as gridRendererPlugin, a3 as infiniteLoaderPlugin, a4 as listRendererPlugin, a5 as scrollControllerPlugin } from './scroll-controller-D5AV1E2p.js';
2
+ import { V as VirtualListProps, a as VirtualListHandle, b as VirtualGridProps, c as VirtualMasonryProps, U as UseVirtualListOptions, d as UseVirtualListReturn, e as UseVirtualGridOptions, f as UseVirtualGridReturn, g as UseVirtualMasonryOptions, h as UseVirtualMasonryReturn, K as Kernel, i as KernelOptions, P as Plugin } from './scroll-controller-CBna5Yim.js';
3
+ export { A as AutoMeasurerAPI, j as AutoMeasurerOptions, E as EasingFunction, k as EventHandler, l as EventType, G as GridGap, m as GridRenderItemProps, n as GridRendererAPI, o as GridRendererOptions, I as InfiniteLoaderAPI, p as InfiniteLoaderOptions, q as ItemMeasuredEvent, r as ItemsChangeEvent, s as ItemsRenderedInfo, t as KernelEvent, L as ListRendererAPI, u as ListRendererOptions, v as LoadDirection, w as LoadMoreEvent, M as MasonryRenderItemProps, x as PluginConfig, y as PluginHooks, z as PluginInfo, B as PluginType, R as Range, C as RenderItemProps, D as ResizeEvent, S as ScrollAlignment, F as ScrollBehavior, H as ScrollControllerAPI, J as ScrollControllerOptions, N as ScrollDirection, O as ScrollEndEvent, Q as ScrollEvent, T as ScrollOptions, W as ScrollPosition, X as ScrollStartEvent, Y as ScrollToIndexOptions, Z as Unsubscribe, _ as Viewport, $ as VirtualItem, a0 as VisibleRangeChangeEvent, a1 as autoMeasurerPlugin, a2 as gridRendererPlugin, a3 as infiniteLoaderPlugin, a4 as listRendererPlugin, a5 as scrollControllerPlugin } from './scroll-controller-CBna5Yim.js';
4
4
 
5
5
  /**
6
6
  * VirtualList component with forwardRef.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { listRendererPlugin, normalizeColumnCount, gridRendererPlugin, autoColumnCount, normalizeOverscan, cancelRaf, raf, clamp } from './chunk-CIB3EBBJ.js';
2
- export { autoMeasurerPlugin, gridRendererPlugin, infiniteLoaderPlugin, listRendererPlugin, scrollControllerPlugin } from './chunk-CIB3EBBJ.js';
1
+ import { listRendererPlugin, normalizeColumnCount, gridRendererPlugin, autoColumnCount, normalizeOverscan, cancelRaf, raf, clamp } from './chunk-VLA5K5HH.js';
2
+ export { autoMeasurerPlugin, gridRendererPlugin, infiniteLoaderPlugin, listRendererPlugin, scrollControllerPlugin } from './chunk-VLA5K5HH.js';
3
3
  import { createContext, forwardRef, useRef, useEffect, useImperativeHandle, useCallback, useMemo, useState, useContext } from 'react';
4
4
  import { jsx, jsxs } from 'react/jsx-runtime';
5
5
 
@@ -894,9 +894,59 @@ var RangeCalculator = class {
894
894
  this.countTree = new Float64Array(1);
895
895
  this.sumTree = new Float64Array(1);
896
896
  this.treesValid = false;
897
+ // Items whose size came from sizeGetter (not from a measurement): a new getter sizes them again
898
+ this.sized = /* @__PURE__ */ new Set();
897
899
  this.itemCount = options.itemCount;
898
900
  this.overscan = options.overscan;
899
901
  this.measurementCache = new MeasurementCache(options.estimatedItemHeight);
902
+ this.sizeGetter = options.getItemSize;
903
+ }
904
+ /**
905
+ * Set (or clear) the function that gives the known size of an item. Unmeasured items take that size
906
+ * up front; measurements already taken are kept, and the items sized by the previous function are sized
907
+ * again by the new one.
908
+ *
909
+ * @param getItemSize - Size of an item, or undefined to size unmeasured items with the estimate
910
+ * @returns Whether any item size (or the total size) changed
911
+ */
912
+ setSizeGetter(getItemSize) {
913
+ if (getItemSize === this.sizeGetter) return false;
914
+ const hadGetter = this.sizeGetter !== void 0;
915
+ this.sizeGetter = getItemSize;
916
+ let changed = hadGetter !== (getItemSize !== void 0);
917
+ for (let index = 0; index < this.itemCount; index++) {
918
+ if (this.measurementCache.has(index) && !this.sized.has(index)) continue;
919
+ const size = getItemSize ? getItemSize(index) : NaN;
920
+ const previous = this.measurementCache.get(index);
921
+ if (Number.isFinite(size) && size >= 0) {
922
+ if (size !== previous) {
923
+ this.measurementCache.set(index, size);
924
+ this.sized.add(index);
925
+ changed = true;
926
+ }
927
+ } else if (previous !== void 0) {
928
+ this.measurementCache.delete(index);
929
+ this.sized.delete(index);
930
+ changed = true;
931
+ }
932
+ }
933
+ if (changed) this.treesValid = false;
934
+ return changed;
935
+ }
936
+ /**
937
+ * Size the unmeasured items with the known-size function.
938
+ */
939
+ applyKnownSizes() {
940
+ const getItemSize = this.sizeGetter;
941
+ if (!getItemSize) return;
942
+ for (let index = 0; index < this.itemCount; index++) {
943
+ if (this.measurementCache.has(index)) continue;
944
+ const size = getItemSize(index);
945
+ if (Number.isFinite(size) && size >= 0) {
946
+ this.measurementCache.set(index, size);
947
+ this.sized.add(index);
948
+ }
949
+ }
900
950
  }
901
951
  /**
902
952
  * Get the visible range of items.
@@ -975,6 +1025,7 @@ var RangeCalculator = class {
975
1025
  * @returns Item size
976
1026
  */
977
1027
  getItemSize(index) {
1028
+ if (this.sizeGetter) this.ensureTrees();
978
1029
  return this.measurementCache.getOrEstimate(index);
979
1030
  }
980
1031
  /**
@@ -993,6 +1044,7 @@ var RangeCalculator = class {
993
1044
  * @returns Previous height, or undefined if not measured
994
1045
  */
995
1046
  setItemHeight(index, height) {
1047
+ this.sized.delete(index);
996
1048
  const previousHeight = this.measurementCache.set(index, height);
997
1049
  this.track(index, previousHeight === void 0 ? 1 : 0, height - (previousHeight ?? 0));
998
1050
  return previousHeight;
@@ -1004,6 +1056,7 @@ var RangeCalculator = class {
1004
1056
  * @returns Cached height, or undefined if not measured
1005
1057
  */
1006
1058
  getCachedHeight(index) {
1059
+ if (this.sizeGetter) this.ensureTrees();
1007
1060
  return this.measurementCache.get(index);
1008
1061
  }
1009
1062
  /**
@@ -1013,15 +1066,24 @@ var RangeCalculator = class {
1013
1066
  */
1014
1067
  invalidate(index) {
1015
1068
  const previousHeight = this.measurementCache.get(index);
1069
+ this.sized.delete(index);
1016
1070
  if (this.measurementCache.delete(index)) {
1017
1071
  this.track(index, -1, -(previousHeight ?? 0));
1018
1072
  }
1073
+ if (this.sizeGetter && Number.isInteger(index) && index >= 0 && index < this.itemCount) {
1074
+ const size = this.sizeGetter(index);
1075
+ if (Number.isFinite(size) && size >= 0) {
1076
+ this.setItemHeight(index, size);
1077
+ this.sized.add(index);
1078
+ }
1079
+ }
1019
1080
  }
1020
1081
  /**
1021
1082
  * Invalidate all measurements.
1022
1083
  */
1023
1084
  invalidateAll() {
1024
1085
  this.measurementCache.clear();
1086
+ this.sized.clear();
1025
1087
  this.treesValid = false;
1026
1088
  }
1027
1089
  /**
@@ -1037,6 +1099,9 @@ var RangeCalculator = class {
1037
1099
  for (const measured of this.measurementCache.getMeasuredIndices()) {
1038
1100
  if (measured >= count) this.measurementCache.delete(measured);
1039
1101
  }
1102
+ for (const index of this.sized) {
1103
+ if (index >= count) this.sized.delete(index);
1104
+ }
1040
1105
  }
1041
1106
  this.treesValid = false;
1042
1107
  }
@@ -1062,6 +1127,7 @@ var RangeCalculator = class {
1062
1127
  * @returns Estimated height
1063
1128
  */
1064
1129
  getEstimatedHeight() {
1130
+ if (this.sizeGetter) this.ensureTrees();
1065
1131
  return this.measurementCache.getEstimatedHeight();
1066
1132
  }
1067
1133
  /**
@@ -1123,6 +1189,7 @@ var RangeCalculator = class {
1123
1189
  ensureTrees() {
1124
1190
  if (this.treesValid) return;
1125
1191
  const size = this.itemCount;
1192
+ this.applyKnownSizes();
1126
1193
  this.countTree = new Float64Array(size + 1);
1127
1194
  this.sumTree = new Float64Array(size + 1);
1128
1195
  for (const index of this.measurementCache.getMeasuredIndices()) {
@@ -1418,10 +1485,12 @@ function createKernel(options = {}) {
1418
1485
  const rangeCalculator = new RangeCalculator({
1419
1486
  itemCount: config.itemCount,
1420
1487
  estimatedItemHeight: config.estimatedItemHeight,
1421
- overscan: config.overscan
1488
+ overscan: config.overscan,
1489
+ getItemSize: config.getItemSize
1422
1490
  });
1423
1491
  const pluginRegistry = new PluginRegistry();
1424
1492
  const startPadding = () => config.paddingStart ?? 0;
1493
+ const listStart = () => (config.scrollMargin ?? 0) + startPadding();
1425
1494
  let rangeAnnounced = false;
1426
1495
  let currentRange = {
1427
1496
  startIndex: 0,
@@ -1450,7 +1519,7 @@ function createKernel(options = {}) {
1450
1519
  const horizontal = config.direction === "horizontal";
1451
1520
  const offset = horizontal ? position.scrollLeft : position.scrollTop;
1452
1521
  const viewport = viewportManager.getViewport();
1453
- const maxScroll = rangeCalculator.getTotalSize() + startPadding() - (horizontal ? viewport.width : viewport.height);
1522
+ const maxScroll = rangeCalculator.getTotalSize() + listStart() - (horizontal ? viewport.width : viewport.height);
1454
1523
  const percentage = maxScroll > 0 ? Math.min(1, Math.max(0, offset / maxScroll)) : 0;
1455
1524
  return { ...position, offset, percentage };
1456
1525
  },
@@ -1462,7 +1531,7 @@ function createKernel(options = {}) {
1462
1531
  },
1463
1532
  scrollToIndex(index, options2 = {}) {
1464
1533
  const { align = "auto", offset: additionalOffset = 0, ...scrollOptions } = options2;
1465
- const itemOffset = rangeCalculator.getItemOffset(index) + startPadding();
1534
+ const itemOffset = rangeCalculator.getItemOffset(index) + listStart();
1466
1535
  const itemSize = rangeCalculator.getItemSize(index);
1467
1536
  const viewport = viewportManager.getViewport();
1468
1537
  const horizontal = config.direction === "horizontal";
@@ -1494,7 +1563,7 @@ function createKernel(options = {}) {
1494
1563
  break;
1495
1564
  }
1496
1565
  }
1497
- const maxScroll = Math.max(0, rangeCalculator.getTotalSize() + startPadding() - viewportSize);
1566
+ const maxScroll = Math.max(0, rangeCalculator.getTotalSize() + listStart() - viewportSize);
1498
1567
  targetOffset = Math.max(0, Math.min(targetOffset + additionalOffset, maxScroll));
1499
1568
  scrollEngine.scrollTo(targetOffset, scrollOptions);
1500
1569
  },
@@ -1544,7 +1613,7 @@ function createKernel(options = {}) {
1544
1613
  return rangeCalculator.getEstimatedHeight();
1545
1614
  },
1546
1615
  getItemOffset(index) {
1547
- return rangeCalculator.getItemOffset(index) + startPadding();
1616
+ return rangeCalculator.getItemOffset(index) + listStart();
1548
1617
  },
1549
1618
  // Events
1550
1619
  emit(event) {
@@ -1575,6 +1644,14 @@ function createKernel(options = {}) {
1575
1644
  layoutChanged = newOptions.paddingStart !== startPadding();
1576
1645
  config.paddingStart = newOptions.paddingStart;
1577
1646
  }
1647
+ if (newOptions.scrollMargin !== void 0) {
1648
+ layoutChanged = layoutChanged || newOptions.scrollMargin !== (config.scrollMargin ?? 0);
1649
+ config.scrollMargin = newOptions.scrollMargin;
1650
+ }
1651
+ if ("getItemSize" in newOptions && newOptions.getItemSize !== config.getItemSize) {
1652
+ config.getItemSize = newOptions.getItemSize;
1653
+ layoutChanged = rangeCalculator.setSizeGetter(newOptions.getItemSize) || layoutChanged;
1654
+ }
1578
1655
  if (newOptions.estimatedItemHeight !== void 0) {
1579
1656
  layoutChanged = layoutChanged || newOptions.estimatedItemHeight !== config.estimatedItemHeight;
1580
1657
  config.estimatedItemHeight = newOptions.estimatedItemHeight;
@@ -1651,7 +1728,7 @@ function createKernel(options = {}) {
1651
1728
  if (!attached) return;
1652
1729
  const viewport = viewportManager.getViewport();
1653
1730
  const scrollPosition = scrollEngine.getScrollPosition();
1654
- const scrollOffset = (config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft) - startPadding();
1731
+ const scrollOffset = (config.direction === "vertical" ? scrollPosition.scrollTop : scrollPosition.scrollLeft) - listStart();
1655
1732
  const viewportSize = config.direction === "vertical" ? viewport.height : viewport.width;
1656
1733
  const previousRange = { ...currentRange };
1657
1734
  currentRange = rangeCalculator.getVisibleRange(scrollOffset, viewportSize);
@@ -1740,6 +1817,7 @@ function useVirtualList(options) {
1740
1817
  overscan = 5,
1741
1818
  paddingStart = 0,
1742
1819
  paddingEnd = 0,
1820
+ scrollMargin = 0,
1743
1821
  initialOffset = 0,
1744
1822
  getItemKey = (index) => index,
1745
1823
  horizontal = false,
@@ -1754,7 +1832,10 @@ function useVirtualList(options) {
1754
1832
  overscan,
1755
1833
  direction: horizontal ? "horizontal" : "vertical",
1756
1834
  getItemKey,
1757
- paddingStart
1835
+ paddingStart,
1836
+ scrollMargin,
1837
+ // Known heights lay out every item up front, not only the rows attached with measureElement
1838
+ getItemSize: getItemHeight
1758
1839
  })
1759
1840
  );
1760
1841
  const [virtualItems, setVirtualItems] = useState([]);
@@ -1836,6 +1917,12 @@ function useVirtualList(options) {
1836
1917
  useEffect(() => {
1837
1918
  kernel.configure({ paddingStart });
1838
1919
  }, [paddingStart, kernel]);
1920
+ useEffect(() => {
1921
+ kernel.configure({ scrollMargin });
1922
+ }, [scrollMargin, kernel]);
1923
+ useEffect(() => {
1924
+ kernel.configure({ getItemSize: getItemHeight });
1925
+ }, [getItemHeight, kernel]);
1839
1926
  useEffect(() => {
1840
1927
  kernel.configure({ direction: horizontal ? "horizontal" : "vertical" });
1841
1928
  }, [horizontal, kernel]);
@@ -1879,7 +1966,7 @@ function useVirtualList(options) {
1879
1966
  observer?.observe(element);
1880
1967
  if (getItemHeight) {
1881
1968
  const size = getItemHeight(index);
1882
- kernel.measureItem(index, size);
1969
+ if (kernel.getCachedHeight(index) !== size) kernel.measureItem(index, size);
1883
1970
  } else {
1884
1971
  const rect = element.getBoundingClientRect();
1885
1972
  const size = horizontal ? rect.width : rect.height;