@tanstack/virtual-core 3.17.10 → 3.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,7 +96,7 @@ const observeOffset = (instance, cb, readOffset) => {
96
96
  let offset = 0;
97
97
  const fallback = registerScrollendEvent ? null : utils.debounce(
98
98
  targetWindow,
99
- () => cb(offset, false),
99
+ () => cb(readOffset(element), false),
100
100
  instance.options.isScrollingResetDelay
101
101
  );
102
102
  const createHandler = (isScrolling) => () => {
@@ -426,7 +426,7 @@ class Virtualizer {
426
426
  };
427
427
  };
428
428
  this._willUpdate = () => {
429
- var _a;
429
+ var _a, _b;
430
430
  const scrollElement = this.options.enabled ? this.options.getScrollElement() : null;
431
431
  if (this.scrollElement !== scrollElement) {
432
432
  this.cleanup();
@@ -451,11 +451,14 @@ class Virtualizer {
451
451
  );
452
452
  this.unsubs.push(
453
453
  this.options.observeElementOffset(this, (offset, isScrolling) => {
454
+ var _a2;
454
455
  if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
455
456
  return;
456
457
  }
457
458
  if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
458
459
  offset = this._intendedScrollOffset;
460
+ } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && this.scrollState.hasReachedTarget && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
461
+ this.scrollState = null;
459
462
  }
460
463
  this._intendedScrollOffset = null;
461
464
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
@@ -528,7 +531,11 @@ class Virtualizer {
528
531
  if (anchorDelta !== 0) {
529
532
  this._iosDeferredAdjustment += anchorDelta;
530
533
  }
531
- } else {
534
+ } else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !utils.approxEqual(
535
+ this.getScrollOffset() - anchorDelta,
536
+ this.scrollState.lastTargetOffset
537
+ )) ;
538
+ else {
532
539
  this._scrollToOffset(this.getScrollOffset(), {
533
540
  adjustments: void 0,
534
541
  behavior: void 0
@@ -886,7 +893,7 @@ class Virtualizer {
886
893
  this.observer.observe(node);
887
894
  this.elementsCache.set(key, node);
888
895
  }
889
- if ((!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
896
+ if (node.isConnected && (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
890
897
  this.resizeItem(index, this.options.measureElement(node, void 0, this));
891
898
  }
892
899
  };
@@ -1045,7 +1052,9 @@ class Virtualizer {
1045
1052
  }
1046
1053
  }
1047
1054
  if (align === "end" && index === this.options.count - 1) {
1048
- return [this.getMaxScrollOffset(), align];
1055
+ const maxOffset = this.getMaxScrollOffset();
1056
+ const toOffset2 = maxOffset - this.options.paddingEnd + this.options.scrollPaddingEnd;
1057
+ return [Math.max(Math.min(toOffset2, maxOffset), 0), align];
1049
1058
  }
1050
1059
  const toOffset = align === "end" ? item.end + this.options.scrollPaddingEnd : item.start - this.options.scrollPaddingStart;
1051
1060
  return [
@@ -1063,7 +1072,8 @@ class Virtualizer {
1063
1072
  behavior,
1064
1073
  startedAt: now,
1065
1074
  lastTargetOffset: offset,
1066
- stableFrames: 0
1075
+ stableFrames: 0,
1076
+ hasReachedTarget: false
1067
1077
  };
1068
1078
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1069
1079
  this.scheduleScrollReconcile();
@@ -1086,7 +1096,8 @@ class Virtualizer {
1086
1096
  behavior,
1087
1097
  startedAt: now,
1088
1098
  lastTargetOffset: offset,
1089
- stableFrames: 0
1099
+ stableFrames: 0,
1100
+ hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
1090
1101
  };
1091
1102
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1092
1103
  this.scheduleScrollReconcile();
@@ -1100,23 +1111,41 @@ class Virtualizer {
1100
1111
  behavior,
1101
1112
  startedAt: now,
1102
1113
  lastTargetOffset: offset,
1103
- stableFrames: 0
1114
+ stableFrames: 0,
1115
+ hasReachedTarget: false
1104
1116
  };
1105
1117
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1106
1118
  this.scheduleScrollReconcile();
1107
1119
  };
1108
1120
  this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1109
1121
  if (this.options.count > 0) {
1110
- this.scrollToIndex(this.options.count - 1, {
1122
+ this._iosDeferredAdjustment = 0;
1123
+ const offset = Math.max(this.getMaxScrollOffset(), 0);
1124
+ this.scrollState = {
1125
+ index: this.options.count - 1,
1111
1126
  align: "end",
1112
- behavior
1113
- });
1127
+ behavior,
1128
+ toEnd: true,
1129
+ startedAt: this.now(),
1130
+ lastTargetOffset: offset,
1131
+ stableFrames: 0,
1132
+ hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
1133
+ };
1134
+ this._scrollToOffset(offset, { adjustments: void 0, behavior });
1135
+ this.scheduleScrollReconcile();
1114
1136
  return;
1115
1137
  }
1116
1138
  this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
1117
1139
  behavior
1118
1140
  });
1119
1141
  };
1142
+ this.cancelScroll = () => {
1143
+ if (this.rafId != null && this.targetWindow) {
1144
+ this.targetWindow.cancelAnimationFrame(this.rafId);
1145
+ this.rafId = null;
1146
+ }
1147
+ this.scrollState = null;
1148
+ };
1120
1149
  this.getTotalSize = () => {
1121
1150
  var _a, _b;
1122
1151
  const measurements = this.getMeasurements();
@@ -1222,6 +1251,10 @@ class Virtualizer {
1222
1251
  this.reconcileScroll();
1223
1252
  });
1224
1253
  }
1254
+ getScrollStateTarget(state) {
1255
+ const offsetInfo = state.toEnd ? [Math.max(this.getMaxScrollOffset(), 0)] : state.index != null ? this.getOffsetForIndex(state.index, state.align) : void 0;
1256
+ return offsetInfo ? offsetInfo[0] : state.lastTargetOffset;
1257
+ }
1225
1258
  reconcileScroll() {
1226
1259
  if (!this.scrollState) return;
1227
1260
  const el = this.scrollElement;
@@ -1231,11 +1264,11 @@ class Virtualizer {
1231
1264
  this.scrollState = null;
1232
1265
  return;
1233
1266
  }
1234
- const offsetInfo = this.scrollState.index != null ? this.getOffsetForIndex(this.scrollState.index, this.scrollState.align) : void 0;
1235
- const targetOffset = offsetInfo ? offsetInfo[0] : this.scrollState.lastTargetOffset;
1236
- const STABLE_FRAMES = 1;
1267
+ const targetOffset = this.getScrollStateTarget(this.scrollState);
1268
+ const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1237
1269
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
1238
1270
  if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
1271
+ this.scrollState.hasReachedTarget = true;
1239
1272
  this.scrollState.stableFrames++;
1240
1273
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1241
1274
  if (this.getScrollOffset() !== targetOffset) {
@@ -1254,7 +1287,9 @@ class Virtualizer {
1254
1287
  const distance = Math.abs(targetOffset - this.getScrollOffset());
1255
1288
  const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
1256
1289
  this.scrollState.lastTargetOffset = targetOffset;
1257
- if (!keepSmooth) {
1290
+ if (keepSmooth) {
1291
+ this.scrollState.hasReachedTarget = false;
1292
+ } else {
1258
1293
  this.scrollState.behavior = "auto";
1259
1294
  }
1260
1295
  this._scrollToOffset(targetOffset, {