@tanstack/virtual-core
Advanced tools
Comparing version 3.0.0-beta.43 to 3.0.0-beta.44
@@ -111,4 +111,7 @@ export * from './utils'; | ||
getOffsetForAlignment: (toOffset: number, align: ScrollAlignment) => number; | ||
getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "center" | "end"]; | ||
private isDynamicMode; | ||
private cancelScrollToIndex; | ||
scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void; | ||
scrollToIndex: (index: number, { align, behavior }?: ScrollToIndexOptions) => void; | ||
scrollToIndex: (index: number, { align: initialAlign, behavior }?: ScrollToIndexOptions) => void; | ||
scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void; | ||
@@ -115,0 +118,0 @@ getTotalSize: () => number; |
@@ -435,2 +435,29 @@ /** | ||
}; | ||
this.getOffsetForIndex = function (index, align) { | ||
if (align === void 0) { | ||
align = 'auto'; | ||
} | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
var measurement = notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return [_this.scrollOffset, align]; | ||
} | ||
} | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return [_this.getOffsetForAlignment(toOffset, align), align]; | ||
}; | ||
this.isDynamicMode = function () { | ||
return Object.keys(_this.measureElementCache).length > 0; | ||
}; | ||
this.cancelScrollToIndex = function () { | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
} | ||
}; | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
@@ -441,13 +468,10 @@ var _ref4 = _temp === void 0 ? {} : _temp, | ||
behavior = _ref4.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var options = { | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), { | ||
adjustments: undefined, | ||
behavior: behavior, | ||
sync: false | ||
}; | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options); | ||
behavior: behavior | ||
}); | ||
}; | ||
@@ -457,35 +481,17 @@ this.scrollToIndex = function (index, _temp2) { | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
initialAlign = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
} | ||
var measurement = notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return; | ||
} | ||
} | ||
var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) { | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return _this.getOffsetForAlignment(toOffset, align); | ||
}; | ||
var toOffset = getOffsetForIndexAndAlignment(measurement); | ||
var options = { | ||
var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign), | ||
toOffset = _this$getOffsetForInd[0], | ||
align = _this$getOffsetForInd[1]; | ||
_this._scrollToOffset(toOffset, { | ||
adjustments: undefined, | ||
behavior: behavior | ||
}; | ||
_this._scrollToOffset(toOffset, options); | ||
if (isDynamic) { | ||
}); | ||
if (behavior !== 'smooth' && _this.isDynamicMode()) { | ||
_this.scrollToIndexTimeoutId = setTimeout(function () { | ||
@@ -495,3 +501,4 @@ _this.scrollToIndexTimeoutId = null; | ||
if (elementInDOM) { | ||
var _toOffset = getOffsetForIndexAndAlignment(notUndefined(_this.getMeasurements()[index])); | ||
var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align), | ||
_toOffset = _this$getOffsetForInd2[0]; | ||
if (!approxEqual(_toOffset, _this.scrollOffset)) { | ||
@@ -515,6 +522,5 @@ _this.scrollToIndex(index, { | ||
behavior = _ref6.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
@@ -521,0 +527,0 @@ _this._scrollToOffset(_this.scrollOffset + delta, { |
@@ -438,2 +438,29 @@ /** | ||
}; | ||
this.getOffsetForIndex = function (index, align) { | ||
if (align === void 0) { | ||
align = 'auto'; | ||
} | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
var measurement = utils.notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return [_this.scrollOffset, align]; | ||
} | ||
} | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return [_this.getOffsetForAlignment(toOffset, align), align]; | ||
}; | ||
this.isDynamicMode = function () { | ||
return Object.keys(_this.measureElementCache).length > 0; | ||
}; | ||
this.cancelScrollToIndex = function () { | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
} | ||
}; | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
@@ -444,13 +471,10 @@ var _ref4 = _temp === void 0 ? {} : _temp, | ||
behavior = _ref4.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var options = { | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), { | ||
adjustments: undefined, | ||
behavior: behavior, | ||
sync: false | ||
}; | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options); | ||
behavior: behavior | ||
}); | ||
}; | ||
@@ -460,35 +484,17 @@ this.scrollToIndex = function (index, _temp2) { | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
initialAlign = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
} | ||
var measurement = utils.notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return; | ||
} | ||
} | ||
var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) { | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return _this.getOffsetForAlignment(toOffset, align); | ||
}; | ||
var toOffset = getOffsetForIndexAndAlignment(measurement); | ||
var options = { | ||
var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign), | ||
toOffset = _this$getOffsetForInd[0], | ||
align = _this$getOffsetForInd[1]; | ||
_this._scrollToOffset(toOffset, { | ||
adjustments: undefined, | ||
behavior: behavior | ||
}; | ||
_this._scrollToOffset(toOffset, options); | ||
if (isDynamic) { | ||
}); | ||
if (behavior !== 'smooth' && _this.isDynamicMode()) { | ||
_this.scrollToIndexTimeoutId = setTimeout(function () { | ||
@@ -498,3 +504,4 @@ _this.scrollToIndexTimeoutId = null; | ||
if (elementInDOM) { | ||
var _toOffset = getOffsetForIndexAndAlignment(utils.notUndefined(_this.getMeasurements()[index])); | ||
var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align), | ||
_toOffset = _this$getOffsetForInd2[0]; | ||
if (!utils.approxEqual(_toOffset, _this.scrollOffset)) { | ||
@@ -518,6 +525,5 @@ _this.scrollToIndex(index, { | ||
behavior = _ref6.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
@@ -524,0 +530,0 @@ _this._scrollToOffset(_this.scrollOffset + delta, { |
@@ -498,2 +498,29 @@ /** | ||
}; | ||
this.getOffsetForIndex = function (index, align) { | ||
if (align === void 0) { | ||
align = 'auto'; | ||
} | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
var measurement = notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return [_this.scrollOffset, align]; | ||
} | ||
} | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return [_this.getOffsetForAlignment(toOffset, align), align]; | ||
}; | ||
this.isDynamicMode = function () { | ||
return Object.keys(_this.measureElementCache).length > 0; | ||
}; | ||
this.cancelScrollToIndex = function () { | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
} | ||
}; | ||
this.scrollToOffset = function (toOffset, _temp) { | ||
@@ -504,13 +531,10 @@ var _ref4 = _temp === void 0 ? {} : _temp, | ||
behavior = _ref4.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var options = { | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), { | ||
adjustments: undefined, | ||
behavior: behavior, | ||
sync: false | ||
}; | ||
_this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options); | ||
behavior: behavior | ||
}); | ||
}; | ||
@@ -520,35 +544,17 @@ this.scrollToIndex = function (index, _temp2) { | ||
_ref5$align = _ref5.align, | ||
align = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
initialAlign = _ref5$align === void 0 ? 'auto' : _ref5$align, | ||
behavior = _ref5.behavior; | ||
index = Math.max(0, Math.min(index, _this.options.count - 1)); | ||
if (_this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(_this.scrollToIndexTimeoutId); | ||
_this.scrollToIndexTimeoutId = null; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
} | ||
var measurement = notUndefined(_this.getMeasurements()[index]); | ||
if (align === 'auto') { | ||
if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) { | ||
align = 'end'; | ||
} else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) { | ||
align = 'start'; | ||
} else { | ||
return; | ||
} | ||
} | ||
var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) { | ||
var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart; | ||
return _this.getOffsetForAlignment(toOffset, align); | ||
}; | ||
var toOffset = getOffsetForIndexAndAlignment(measurement); | ||
var options = { | ||
var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign), | ||
toOffset = _this$getOffsetForInd[0], | ||
align = _this$getOffsetForInd[1]; | ||
_this._scrollToOffset(toOffset, { | ||
adjustments: undefined, | ||
behavior: behavior | ||
}; | ||
_this._scrollToOffset(toOffset, options); | ||
if (isDynamic) { | ||
}); | ||
if (behavior !== 'smooth' && _this.isDynamicMode()) { | ||
_this.scrollToIndexTimeoutId = setTimeout(function () { | ||
@@ -558,3 +564,4 @@ _this.scrollToIndexTimeoutId = null; | ||
if (elementInDOM) { | ||
var _toOffset = getOffsetForIndexAndAlignment(notUndefined(_this.getMeasurements()[index])); | ||
var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align), | ||
_toOffset = _this$getOffsetForInd2[0]; | ||
if (!approxEqual(_toOffset, _this.scrollOffset)) { | ||
@@ -578,6 +585,5 @@ _this.scrollToIndex(index, { | ||
behavior = _ref6.behavior; | ||
var isDynamic = Object.keys(_this.measureElementCache).length > 0; | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn('The `smooth` scroll behavior is not supported with dynamic size.'); | ||
return; | ||
_this.cancelScrollToIndex(); | ||
if (behavior === 'smooth' && _this.isDynamicMode()) { | ||
console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.'); | ||
} | ||
@@ -584,0 +590,0 @@ _this._scrollToOffset(_this.scrollOffset + delta, { |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VirtualCore={})}(this,(function(e){"use strict";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},t.apply(this,arguments)}function n(e,t,n){var o,i,r=null!=(o=n.initialDeps)?o:[];return function(){var o;n.key&&null!=n.debug&&n.debug()&&(o=Date.now());var s,l=e();if(!(l.length!==r.length||l.some((function(e,t){return r[t]!==e}))))return i;if(r=l,n.key&&null!=n.debug&&n.debug()&&(s=Date.now()),i=t.apply(void 0,l),n.key&&null!=n.debug&&n.debug()){var a=Math.round(100*(Date.now()-o))/100,u=Math.round(100*(Date.now()-s))/100,c=u/16,d=function(e,t){for(e=String(e);e.length<t;)e=" "+e;return e};console.info("%c⏱ "+d(u,5)+" /"+d(a,5)+" ms","\n font-size: .6rem;\n font-weight: bold;\n color: hsl("+Math.max(0,Math.min(120-120*c,120))+"deg 100% 31%);",null==n?void 0:n.key)}return null==n||null==n.onChange||n.onChange(i),i}}function o(e,t){if(void 0===e)throw new Error("Unexpected undefined"+(t?": "+t:""));return e}var i=function(e,t){return Math.abs(e-t)<1},r=function(e){return e},s=function(e){for(var t=Math.max(e.startIndex-e.overscan,0),n=Math.min(e.endIndex+e.overscan,e.count-1),o=[],i=t;i<=n;i++)o.push(i);return o},l={element:["scrollLeft","scrollTop"],window:["scrollX","scrollY"]},a=function(e){return function(t,n){if(t.scrollElement){var o=l[e][0],i=l[e][1],r=t.scrollElement[o],s=t.scrollElement[i],a=function(){var e=t.scrollElement[t.options.horizontal?o:i];n(e)};a();var u=function(e){var n=e.currentTarget,l=n[o],u=n[i];(t.options.horizontal?r-l:s-u)&&a(),r=l,s=u};return t.scrollElement.addEventListener("scroll",u,{capture:!1,passive:!0}),function(){t.scrollElement.removeEventListener("scroll",u)}}}},u=a("element"),c=a("window"),d=function(e,t){return Math.round(e.getBoundingClientRect()[t.options.horizontal?"width":"height"])};e.Virtualizer=function(e){var l,a=this;this.unsubs=[],this.scrollElement=null,this.isScrolling=!1,this.isScrollingTimeoutId=null,this.scrollToIndexTimeoutId=null,this.measurementsCache=[],this.itemSizeCache={},this.pendingMeasuredCacheIndexes=[],this.scrollDirection=null,this.scrollAdjustments=0,this.measureElementCache={},this.getResizeObserver=(l=null,function(){return l||("undefined"!=typeof ResizeObserver?l=new ResizeObserver((function(e){e.forEach((function(e){a._measureElement(e.target,!1)}))})):null)}),this.range={startIndex:0,endIndex:0},this.setOptions=function(e){Object.entries(e).forEach((function(t){var n=t[0];void 0===t[1]&&delete e[n]})),a.options=t({debug:!1,initialOffset:0,overscan:1,paddingStart:0,paddingEnd:0,scrollPaddingStart:0,scrollPaddingEnd:0,horizontal:!1,getItemKey:r,rangeExtractor:s,onChange:function(){},measureElement:d,initialRect:{width:0,height:0},scrollMargin:0,scrollingDelay:150,indexAttribute:"data-index",initialMeasurementsCache:[]},e)},this.notify=function(){null==a.options.onChange||a.options.onChange(a)},this.cleanup=function(){a.unsubs.filter(Boolean).forEach((function(e){return e()})),a.unsubs=[],a.scrollElement=null},this._didMount=function(){var e=a.getResizeObserver();return Object.values(a.measureElementCache).forEach((function(t){return null==e?void 0:e.observe(t)})),function(){null==e||e.disconnect(),a.cleanup()}},this._willUpdate=function(){var e=a.options.getScrollElement();a.scrollElement!==e&&(a.cleanup(),a.scrollElement=e,a._scrollToOffset(a.scrollOffset,{adjustments:void 0,behavior:void 0}),a.unsubs.push(a.options.observeElementRect(a,(function(e){a.scrollRect=e,a.maybeNotify()}))),a.unsubs.push(a.options.observeElementOffset(a,(function(e){a.scrollAdjustments=0,a.scrollOffset!==e&&(null!==a.isScrollingTimeoutId&&(clearTimeout(a.isScrollingTimeoutId),a.isScrollingTimeoutId=null),a.isScrolling=!0,a.scrollDirection=a.scrollOffset<e?"forward":"backward",a.scrollOffset=e,a.maybeNotify(),a.isScrollingTimeoutId=setTimeout((function(){a.isScrollingTimeoutId=null,a.isScrolling=!1,a.scrollDirection=null,a.maybeNotify()}),a.options.scrollingDelay))}))))},this.getSize=function(){return a.scrollRect[a.options.horizontal?"width":"height"]},this.getMeasurements=n((function(){return[a.options.count,a.options.paddingStart,a.options.scrollMargin,a.options.getItemKey,a.itemSizeCache]}),(function(e,t,n,o,i){var r=a.pendingMeasuredCacheIndexes.length>0?Math.min.apply(Math,a.pendingMeasuredCacheIndexes):0;a.pendingMeasuredCacheIndexes=[];for(var s=a.measurementsCache.slice(0,r),l=r;l<e;l++){var u=o(l),c=i[u],d=s[l-1]?s[l-1].end:t+n,f="number"==typeof c?c:a.options.estimateSize(l),h=d+f;s[l]={index:l,start:d,size:f,end:h,key:u}}return a.measurementsCache=s,s}),{key:!1,debug:function(){return a.options.debug}}),this.calculateRange=n((function(){return[a.getMeasurements(),a.getSize(),a.scrollOffset]}),(function(e,t,n){return a.range=function(e){var t=e.measurements,n=e.outerSize,o=e.scrollOffset,i=t.length-1,r=function(e,t,n,o){for(;e<=t;){var i=(e+t)/2|0,r=n(i);if(r<o)e=i+1;else{if(!(r>o))return i;t=i-1}}return e>0?e-1:0}(0,i,(function(e){return t[e].start}),o),s=r;for(;s<i&&t[s].end<o+n;)s++;return{startIndex:r,endIndex:s}}({measurements:e,outerSize:t,scrollOffset:n})}),{key:!1,debug:function(){return a.options.debug}}),this.maybeNotify=n((function(){return[].concat(Object.values(a.calculateRange()),[a.isScrolling])}),(function(){a.notify()}),{key:!1,debug:function(){return a.options.debug},initialDeps:[].concat(Object.values(this.range),[this.isScrolling])}),this.getIndexes=n((function(){return[a.options.rangeExtractor,a.calculateRange(),a.options.overscan,a.options.count]}),(function(e,n,o,i){return e(t({},n,{overscan:o,count:i}))}),{key:!1,debug:function(){return a.options.debug}}),this.indexFromElement=function(e){var t=a.options.indexAttribute,n=e.getAttribute(t);return n?parseInt(n,10):(console.warn("Missing attribute name '"+t+"={index}' on measured element."),-1)},this._measureElement=function(e,n){var o,i=a.indexFromElement(e),r=a.measurementsCache[i];if(r){var s=a.measureElementCache[r.key],l=a.getResizeObserver();if(!e.isConnected)return null==l||l.unobserve(e),void(e===s&&delete a.measureElementCache[r.key]);if(s!==e)s&&(null==l||l.unobserve(s)),null==l||l.observe(e),a.measureElementCache[r.key]=e;else if(!n&&!s.__virtualizerSkipFirstNotSync)return void(s.__virtualizerSkipFirstNotSync=!0);var u,c=a.options.measureElement(e,a),d=c-(null!=(o=a.itemSizeCache[r.key])?o:r.size);if(0!==d)r.start<a.scrollOffset&&a._scrollToOffset(a.scrollOffset,{adjustments:a.scrollAdjustments+=d,behavior:void 0}),a.pendingMeasuredCacheIndexes.push(i),a.itemSizeCache=t({},a.itemSizeCache,((u={})[r.key]=c,u)),a.notify()}},this.measureElement=function(e){e&&a._measureElement(e,!0)},this.getVirtualItems=n((function(){return[a.getIndexes(),a.getMeasurements()]}),(function(e,t){for(var n=[],o=0,i=e.length;o<i;o++){var r=t[e[o]];n.push(r)}return n}),{key:!1,debug:function(){return a.options.debug}}),this.getOffsetForAlignment=function(e,t){var n=a.getSize();"auto"===t&&(t=e<=a.scrollOffset?"start":e>=a.scrollOffset+n?"end":"start"),"start"===t||("end"===t?e-=n:"center"===t&&(e-=n/2));var o=a.options.horizontal?"scrollWidth":"scrollHeight",i=(a.scrollElement?"document"in a.scrollElement?a.scrollElement.document.documentElement[o]:a.scrollElement[o]:0)-a.getSize();return Math.max(Math.min(i,e),0)},this.scrollToOffset=function(e,t){var n=void 0===t?{}:t,o=n.align,i=void 0===o?"start":o,r=n.behavior;if(Object.keys(a.measureElementCache).length>0&&"smooth"===r)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var s={adjustments:void 0,behavior:r,sync:!1};a._scrollToOffset(a.getOffsetForAlignment(e,i),s)}},this.scrollToIndex=function(e,t){var n=void 0===t?{}:t,r=n.align,s=void 0===r?"auto":r,l=n.behavior;e=Math.max(0,Math.min(e,a.options.count-1)),null!==a.scrollToIndexTimeoutId&&(clearTimeout(a.scrollToIndexTimeoutId),a.scrollToIndexTimeoutId=null);var u=Object.keys(a.measureElementCache).length>0;if(u&&"smooth"===l)console.warn("The `smooth` scroll behavior is not supported with dynamic size.");else{var c=o(a.getMeasurements()[e]);if("auto"===s)if(c.end>=a.scrollOffset+a.getSize()-a.options.scrollPaddingEnd)s="end";else{if(!(c.start<=a.scrollOffset+a.options.scrollPaddingStart))return;s="start"}var d=function(e){var t="end"===s?e.end+a.options.scrollPaddingEnd:e.start-a.options.scrollPaddingStart;return a.getOffsetForAlignment(t,s)},f=d(c),h={adjustments:void 0,behavior:l};a._scrollToOffset(f,h),u&&(a.scrollToIndexTimeoutId=setTimeout((function(){if(a.scrollToIndexTimeoutId=null,!!a.measureElementCache[a.options.getItemKey(e)]){var t=d(o(a.getMeasurements()[e]));i(t,a.scrollOffset)||a.scrollToIndex(e,{align:s,behavior:l})}else a.scrollToIndex(e,{align:s,behavior:l})})))}},this.scrollBy=function(e,t){var n=(void 0===t?{}:t).behavior;Object.keys(a.measureElementCache).length>0&&"smooth"===n?console.warn("The `smooth` scroll behavior is not supported with dynamic size."):a._scrollToOffset(a.scrollOffset+e,{adjustments:void 0,behavior:n})},this.getTotalSize=function(){var e;return((null==(e=a.getMeasurements()[a.options.count-1])?void 0:e.end)||a.options.paddingStart)-a.options.scrollMargin+a.options.paddingEnd},this._scrollToOffset=function(e,t){var n=t.adjustments,o=t.behavior;a.options.scrollToFn(e,{behavior:o,adjustments:n},a)},this.measure=function(){a.itemSizeCache={},a.notify()},this.setOptions(e),this.scrollRect=this.options.initialRect,this.scrollOffset=this.options.initialOffset,this.measurementsCache=this.options.initialMeasurementsCache,this.measurementsCache.forEach((function(e){a.itemSizeCache[e.key]=e.size})),this.maybeNotify()},e.approxEqual=i,e.defaultKeyExtractor=r,e.defaultRangeExtractor=s,e.elementScroll=function(e,t,n){var o,i,r=t.adjustments,s=void 0===r?0:r,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((i={})[n.options.horizontal?"left":"top"]=a,i.behavior=l,i))},e.measureElement=d,e.memo=n,e.notUndefined=o,e.observeElementOffset=u,e.observeElementRect=function(e,t){var n=new ResizeObserver((function(e){var n=e[0];if(n){var o=n.contentRect,i=o.width,r=o.height;t({width:Math.round(i),height:Math.round(r)})}else t({width:0,height:0})}));if(e.scrollElement)return t(e.scrollElement.getBoundingClientRect()),n.observe(e.scrollElement),function(){n.unobserve(e.scrollElement)}},e.observeWindowOffset=c,e.observeWindowRect=function(e,t){var n=function(e,t){var n={height:-1,width:-1};return function(o){(e.options.horizontal?o.width!==n.width:o.height!==n.height)&&t(o),n=o}}(e,t),o=function(){return n({width:e.scrollElement.innerWidth,height:e.scrollElement.innerHeight})};if(e.scrollElement)return o(),e.scrollElement.addEventListener("resize",o,{capture:!1,passive:!0}),function(){e.scrollElement.removeEventListener("resize",o)}},e.windowScroll=function(e,t,n){var o,i,r=t.adjustments,s=void 0===r?0:r,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((i={})[n.options.horizontal?"left":"top"]=a,i.behavior=l,i))},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).VirtualCore={})}(this,(function(e){"use strict";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},t.apply(this,arguments)}function n(e,t,n){var o,i,r=null!=(o=n.initialDeps)?o:[];return function(){var o;n.key&&null!=n.debug&&n.debug()&&(o=Date.now());var s,l=e();if(!(l.length!==r.length||l.some((function(e,t){return r[t]!==e}))))return i;if(r=l,n.key&&null!=n.debug&&n.debug()&&(s=Date.now()),i=t.apply(void 0,l),n.key&&null!=n.debug&&n.debug()){var a=Math.round(100*(Date.now()-o))/100,u=Math.round(100*(Date.now()-s))/100,c=u/16,d=function(e,t){for(e=String(e);e.length<t;)e=" "+e;return e};console.info("%c⏱ "+d(u,5)+" /"+d(a,5)+" ms","\n font-size: .6rem;\n font-weight: bold;\n color: hsl("+Math.max(0,Math.min(120-120*c,120))+"deg 100% 31%);",null==n?void 0:n.key)}return null==n||null==n.onChange||n.onChange(i),i}}function o(e,t){if(void 0===e)throw new Error("Unexpected undefined"+(t?": "+t:""));return e}var i=function(e,t){return Math.abs(e-t)<1},r=function(e){return e},s=function(e){for(var t=Math.max(e.startIndex-e.overscan,0),n=Math.min(e.endIndex+e.overscan,e.count-1),o=[],i=t;i<=n;i++)o.push(i);return o},l={element:["scrollLeft","scrollTop"],window:["scrollX","scrollY"]},a=function(e){return function(t,n){if(t.scrollElement){var o=l[e][0],i=l[e][1],r=t.scrollElement[o],s=t.scrollElement[i],a=function(){var e=t.scrollElement[t.options.horizontal?o:i];n(e)};a();var u=function(e){var n=e.currentTarget,l=n[o],u=n[i];(t.options.horizontal?r-l:s-u)&&a(),r=l,s=u};return t.scrollElement.addEventListener("scroll",u,{capture:!1,passive:!0}),function(){t.scrollElement.removeEventListener("scroll",u)}}}},u=a("element"),c=a("window"),d=function(e,t){return Math.round(e.getBoundingClientRect()[t.options.horizontal?"width":"height"])};e.Virtualizer=function(e){var l,a=this;this.unsubs=[],this.scrollElement=null,this.isScrolling=!1,this.isScrollingTimeoutId=null,this.scrollToIndexTimeoutId=null,this.measurementsCache=[],this.itemSizeCache={},this.pendingMeasuredCacheIndexes=[],this.scrollDirection=null,this.scrollAdjustments=0,this.measureElementCache={},this.getResizeObserver=(l=null,function(){return l||("undefined"!=typeof ResizeObserver?l=new ResizeObserver((function(e){e.forEach((function(e){a._measureElement(e.target,!1)}))})):null)}),this.range={startIndex:0,endIndex:0},this.setOptions=function(e){Object.entries(e).forEach((function(t){var n=t[0];void 0===t[1]&&delete e[n]})),a.options=t({debug:!1,initialOffset:0,overscan:1,paddingStart:0,paddingEnd:0,scrollPaddingStart:0,scrollPaddingEnd:0,horizontal:!1,getItemKey:r,rangeExtractor:s,onChange:function(){},measureElement:d,initialRect:{width:0,height:0},scrollMargin:0,scrollingDelay:150,indexAttribute:"data-index",initialMeasurementsCache:[]},e)},this.notify=function(){null==a.options.onChange||a.options.onChange(a)},this.cleanup=function(){a.unsubs.filter(Boolean).forEach((function(e){return e()})),a.unsubs=[],a.scrollElement=null},this._didMount=function(){var e=a.getResizeObserver();return Object.values(a.measureElementCache).forEach((function(t){return null==e?void 0:e.observe(t)})),function(){null==e||e.disconnect(),a.cleanup()}},this._willUpdate=function(){var e=a.options.getScrollElement();a.scrollElement!==e&&(a.cleanup(),a.scrollElement=e,a._scrollToOffset(a.scrollOffset,{adjustments:void 0,behavior:void 0}),a.unsubs.push(a.options.observeElementRect(a,(function(e){a.scrollRect=e,a.maybeNotify()}))),a.unsubs.push(a.options.observeElementOffset(a,(function(e){a.scrollAdjustments=0,a.scrollOffset!==e&&(null!==a.isScrollingTimeoutId&&(clearTimeout(a.isScrollingTimeoutId),a.isScrollingTimeoutId=null),a.isScrolling=!0,a.scrollDirection=a.scrollOffset<e?"forward":"backward",a.scrollOffset=e,a.maybeNotify(),a.isScrollingTimeoutId=setTimeout((function(){a.isScrollingTimeoutId=null,a.isScrolling=!1,a.scrollDirection=null,a.maybeNotify()}),a.options.scrollingDelay))}))))},this.getSize=function(){return a.scrollRect[a.options.horizontal?"width":"height"]},this.getMeasurements=n((function(){return[a.options.count,a.options.paddingStart,a.options.scrollMargin,a.options.getItemKey,a.itemSizeCache]}),(function(e,t,n,o,i){var r=a.pendingMeasuredCacheIndexes.length>0?Math.min.apply(Math,a.pendingMeasuredCacheIndexes):0;a.pendingMeasuredCacheIndexes=[];for(var s=a.measurementsCache.slice(0,r),l=r;l<e;l++){var u=o(l),c=i[u],d=s[l-1]?s[l-1].end:t+n,f="number"==typeof c?c:a.options.estimateSize(l),h=d+f;s[l]={index:l,start:d,size:f,end:h,key:u}}return a.measurementsCache=s,s}),{key:!1,debug:function(){return a.options.debug}}),this.calculateRange=n((function(){return[a.getMeasurements(),a.getSize(),a.scrollOffset]}),(function(e,t,n){return a.range=function(e){var t=e.measurements,n=e.outerSize,o=e.scrollOffset,i=t.length-1,r=function(e,t,n,o){for(;e<=t;){var i=(e+t)/2|0,r=n(i);if(r<o)e=i+1;else{if(!(r>o))return i;t=i-1}}return e>0?e-1:0}(0,i,(function(e){return t[e].start}),o),s=r;for(;s<i&&t[s].end<o+n;)s++;return{startIndex:r,endIndex:s}}({measurements:e,outerSize:t,scrollOffset:n})}),{key:!1,debug:function(){return a.options.debug}}),this.maybeNotify=n((function(){return[].concat(Object.values(a.calculateRange()),[a.isScrolling])}),(function(){a.notify()}),{key:!1,debug:function(){return a.options.debug},initialDeps:[].concat(Object.values(this.range),[this.isScrolling])}),this.getIndexes=n((function(){return[a.options.rangeExtractor,a.calculateRange(),a.options.overscan,a.options.count]}),(function(e,n,o,i){return e(t({},n,{overscan:o,count:i}))}),{key:!1,debug:function(){return a.options.debug}}),this.indexFromElement=function(e){var t=a.options.indexAttribute,n=e.getAttribute(t);return n?parseInt(n,10):(console.warn("Missing attribute name '"+t+"={index}' on measured element."),-1)},this._measureElement=function(e,n){var o,i=a.indexFromElement(e),r=a.measurementsCache[i];if(r){var s=a.measureElementCache[r.key],l=a.getResizeObserver();if(!e.isConnected)return null==l||l.unobserve(e),void(e===s&&delete a.measureElementCache[r.key]);if(s!==e)s&&(null==l||l.unobserve(s)),null==l||l.observe(e),a.measureElementCache[r.key]=e;else if(!n&&!s.__virtualizerSkipFirstNotSync)return void(s.__virtualizerSkipFirstNotSync=!0);var u,c=a.options.measureElement(e,a),d=c-(null!=(o=a.itemSizeCache[r.key])?o:r.size);if(0!==d)r.start<a.scrollOffset&&a._scrollToOffset(a.scrollOffset,{adjustments:a.scrollAdjustments+=d,behavior:void 0}),a.pendingMeasuredCacheIndexes.push(i),a.itemSizeCache=t({},a.itemSizeCache,((u={})[r.key]=c,u)),a.notify()}},this.measureElement=function(e){e&&a._measureElement(e,!0)},this.getVirtualItems=n((function(){return[a.getIndexes(),a.getMeasurements()]}),(function(e,t){for(var n=[],o=0,i=e.length;o<i;o++){var r=t[e[o]];n.push(r)}return n}),{key:!1,debug:function(){return a.options.debug}}),this.getOffsetForAlignment=function(e,t){var n=a.getSize();"auto"===t&&(t=e<=a.scrollOffset?"start":e>=a.scrollOffset+n?"end":"start"),"start"===t||("end"===t?e-=n:"center"===t&&(e-=n/2));var o=a.options.horizontal?"scrollWidth":"scrollHeight",i=(a.scrollElement?"document"in a.scrollElement?a.scrollElement.document.documentElement[o]:a.scrollElement[o]:0)-a.getSize();return Math.max(Math.min(i,e),0)},this.getOffsetForIndex=function(e,t){void 0===t&&(t="auto"),e=Math.max(0,Math.min(e,a.options.count-1));var n=o(a.getMeasurements()[e]);if("auto"===t)if(n.end>=a.scrollOffset+a.getSize()-a.options.scrollPaddingEnd)t="end";else{if(!(n.start<=a.scrollOffset+a.options.scrollPaddingStart))return[a.scrollOffset,t];t="start"}var i="end"===t?n.end+a.options.scrollPaddingEnd:n.start-a.options.scrollPaddingStart;return[a.getOffsetForAlignment(i,t),t]},this.isDynamicMode=function(){return Object.keys(a.measureElementCache).length>0},this.cancelScrollToIndex=function(){null!==a.scrollToIndexTimeoutId&&(clearTimeout(a.scrollToIndexTimeoutId),a.scrollToIndexTimeoutId=null)},this.scrollToOffset=function(e,t){var n=void 0===t?{}:t,o=n.align,i=void 0===o?"start":o,r=n.behavior;a.cancelScrollToIndex(),"smooth"===r&&a.isDynamicMode()&&console.warn("The `smooth` scroll behavior is not fully supported with dynamic size."),a._scrollToOffset(a.getOffsetForAlignment(e,i),{adjustments:void 0,behavior:r})},this.scrollToIndex=function(e,t){var n=void 0===t?{}:t,o=n.align,r=void 0===o?"auto":o,s=n.behavior;e=Math.max(0,Math.min(e,a.options.count-1)),a.cancelScrollToIndex(),"smooth"===s&&a.isDynamicMode()&&console.warn("The `smooth` scroll behavior is not fully supported with dynamic size.");var l=a.getOffsetForIndex(e,r),u=l[0],c=l[1];a._scrollToOffset(u,{adjustments:void 0,behavior:s}),"smooth"!==s&&a.isDynamicMode()&&(a.scrollToIndexTimeoutId=setTimeout((function(){if(a.scrollToIndexTimeoutId=null,!!a.measureElementCache[a.options.getItemKey(e)]){var t=a.getOffsetForIndex(e,c)[0];i(t,a.scrollOffset)||a.scrollToIndex(e,{align:c,behavior:s})}else a.scrollToIndex(e,{align:c,behavior:s})})))},this.scrollBy=function(e,t){var n=(void 0===t?{}:t).behavior;a.cancelScrollToIndex(),"smooth"===n&&a.isDynamicMode()&&console.warn("The `smooth` scroll behavior is not fully supported with dynamic size."),a._scrollToOffset(a.scrollOffset+e,{adjustments:void 0,behavior:n})},this.getTotalSize=function(){var e;return((null==(e=a.getMeasurements()[a.options.count-1])?void 0:e.end)||a.options.paddingStart)-a.options.scrollMargin+a.options.paddingEnd},this._scrollToOffset=function(e,t){var n=t.adjustments,o=t.behavior;a.options.scrollToFn(e,{behavior:o,adjustments:n},a)},this.measure=function(){a.itemSizeCache={},a.notify()},this.setOptions(e),this.scrollRect=this.options.initialRect,this.scrollOffset=this.options.initialOffset,this.measurementsCache=this.options.initialMeasurementsCache,this.measurementsCache.forEach((function(e){a.itemSizeCache[e.key]=e.size})),this.maybeNotify()},e.approxEqual=i,e.defaultKeyExtractor=r,e.defaultRangeExtractor=s,e.elementScroll=function(e,t,n){var o,i,r=t.adjustments,s=void 0===r?0:r,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((i={})[n.options.horizontal?"left":"top"]=a,i.behavior=l,i))},e.measureElement=d,e.memo=n,e.notUndefined=o,e.observeElementOffset=u,e.observeElementRect=function(e,t){var n=new ResizeObserver((function(e){var n=e[0];if(n){var o=n.contentRect,i=o.width,r=o.height;t({width:Math.round(i),height:Math.round(r)})}else t({width:0,height:0})}));if(e.scrollElement)return t(e.scrollElement.getBoundingClientRect()),n.observe(e.scrollElement),function(){n.unobserve(e.scrollElement)}},e.observeWindowOffset=c,e.observeWindowRect=function(e,t){var n=function(e,t){var n={height:-1,width:-1};return function(o){(e.options.horizontal?o.width!==n.width:o.height!==n.height)&&t(o),n=o}}(e,t),o=function(){return n({width:e.scrollElement.innerWidth,height:e.scrollElement.innerHeight})};if(e.scrollElement)return o(),e.scrollElement.addEventListener("resize",o,{capture:!1,passive:!0}),function(){e.scrollElement.removeEventListener("resize",o)}},e.windowScroll=function(e,t,n){var o,i,r=t.adjustments,s=void 0===r?0:r,l=t.behavior,a=e+s;null==(o=n.scrollElement)||null==o.scrollTo||o.scrollTo(((i={})[n.options.horizontal?"left":"top"]=a,i.behavior=l,i))},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/virtual-core", | ||
"author": "Tanner Linsley", | ||
"version": "3.0.0-beta.43", | ||
"version": "3.0.0-beta.44", | ||
"description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
109
src/index.ts
@@ -666,43 +666,5 @@ import { approxEqual, memo, notUndefined } from './utils' | ||
scrollToOffset = ( | ||
toOffset: number, | ||
{ align = 'start', behavior }: ScrollToOffsetOptions = {}, | ||
) => { | ||
const isDynamic = Object.keys(this.measureElementCache).length > 0 | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn( | ||
'The `smooth` scroll behavior is not supported with dynamic size.', | ||
) | ||
return | ||
} | ||
const options = { | ||
adjustments: undefined, | ||
behavior, | ||
sync: false, | ||
} | ||
this._scrollToOffset(this.getOffsetForAlignment(toOffset, align), options) | ||
} | ||
scrollToIndex = ( | ||
index: number, | ||
{ align = 'auto', behavior }: ScrollToIndexOptions = {}, | ||
) => { | ||
getOffsetForIndex = (index: number, align: ScrollAlignment = 'auto') => { | ||
index = Math.max(0, Math.min(index, this.options.count - 1)) | ||
if (this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(this.scrollToIndexTimeoutId) | ||
this.scrollToIndexTimeoutId = null | ||
} | ||
const isDynamic = Object.keys(this.measureElementCache).length > 0 | ||
if (isDynamic && behavior === 'smooth') { | ||
console.warn( | ||
'The `smooth` scroll behavior is not supported with dynamic size.', | ||
) | ||
return | ||
} | ||
const measurement = notUndefined(this.getMeasurements()[index]) | ||
@@ -722,24 +684,60 @@ | ||
} else { | ||
return | ||
return [this.scrollOffset, align] as const | ||
} | ||
} | ||
const getOffsetForIndexAndAlignment = (measurement: VirtualItem) => { | ||
const toOffset = | ||
align === 'end' | ||
? measurement.end + this.options.scrollPaddingEnd | ||
: measurement.start - this.options.scrollPaddingStart | ||
const toOffset = | ||
align === 'end' | ||
? measurement.end + this.options.scrollPaddingEnd | ||
: measurement.start - this.options.scrollPaddingStart | ||
return this.getOffsetForAlignment(toOffset, align) | ||
return [this.getOffsetForAlignment(toOffset, align), align] as const | ||
} | ||
private isDynamicMode = () => Object.keys(this.measureElementCache).length > 0 | ||
private cancelScrollToIndex = () => { | ||
if (this.scrollToIndexTimeoutId !== null) { | ||
clearTimeout(this.scrollToIndexTimeoutId) | ||
this.scrollToIndexTimeoutId = null | ||
} | ||
} | ||
const toOffset = getOffsetForIndexAndAlignment(measurement) | ||
scrollToOffset = ( | ||
toOffset: number, | ||
{ align = 'start', behavior }: ScrollToOffsetOptions = {}, | ||
) => { | ||
this.cancelScrollToIndex() | ||
const options = { | ||
if (behavior === 'smooth' && this.isDynamicMode()) { | ||
console.warn( | ||
'The `smooth` scroll behavior is not fully supported with dynamic size.', | ||
) | ||
} | ||
this._scrollToOffset(this.getOffsetForAlignment(toOffset, align), { | ||
adjustments: undefined, | ||
behavior, | ||
}) | ||
} | ||
scrollToIndex = ( | ||
index: number, | ||
{ align: initialAlign = 'auto', behavior }: ScrollToIndexOptions = {}, | ||
) => { | ||
index = Math.max(0, Math.min(index, this.options.count - 1)) | ||
this.cancelScrollToIndex() | ||
if (behavior === 'smooth' && this.isDynamicMode()) { | ||
console.warn( | ||
'The `smooth` scroll behavior is not fully supported with dynamic size.', | ||
) | ||
} | ||
this._scrollToOffset(toOffset, options) | ||
if (isDynamic) { | ||
const [toOffset, align] = this.getOffsetForIndex(index, initialAlign) | ||
this._scrollToOffset(toOffset, { adjustments: undefined, behavior }) | ||
if (behavior !== 'smooth' && this.isDynamicMode()) { | ||
this.scrollToIndexTimeoutId = setTimeout(() => { | ||
@@ -752,5 +750,3 @@ this.scrollToIndexTimeoutId = null | ||
if (elementInDOM) { | ||
const toOffset = getOffsetForIndexAndAlignment( | ||
notUndefined(this.getMeasurements()[index]), | ||
) | ||
const [toOffset] = this.getOffsetForIndex(index, align) | ||
@@ -768,9 +764,8 @@ if (!approxEqual(toOffset, this.scrollOffset)) { | ||
scrollBy = (delta: number, { behavior }: ScrollToOffsetOptions = {}) => { | ||
const isDynamic = Object.keys(this.measureElementCache).length > 0 | ||
this.cancelScrollToIndex() | ||
if (isDynamic && behavior === 'smooth') { | ||
if (behavior === 'smooth' && this.isDynamicMode()) { | ||
console.warn( | ||
'The `smooth` scroll behavior is not supported with dynamic size.', | ||
'The `smooth` scroll behavior is not fully supported with dynamic size.', | ||
) | ||
return | ||
} | ||
@@ -777,0 +772,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
391232
3649