From 9ac99ffabb47d1cf58f29746c52ee1d0ea7c2502 Mon Sep 17 00:00:00 2001 From: Chris Triantafilis Date: Mon, 28 Oct 2024 19:28:07 -0400 Subject: [PATCH] fix: update scrollToIndex center-alignment --- packages/virtual-core/src/index.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 2b3d4ff1..1c65c59b 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -840,21 +840,13 @@ export class Virtualizer< const scrollOffset = this.getScrollOffset() if (align === 'auto') { - if (toOffset <= scrollOffset) { - align = 'start' - } else if (toOffset >= scrollOffset + size) { + if (toOffset >= scrollOffset + size) { align = 'end' - } else { - align = 'start' } } - if (align === 'start') { - toOffset = toOffset - } else if (align === 'end') { - toOffset = toOffset - size - } else if (align === 'center') { - toOffset = toOffset - size / 2 + if (align === 'end') { + toOffset -= size } const scrollSizeProp = this.options.horizontal @@ -892,12 +884,16 @@ export class Virtualizer< } } - const toOffset = - align === 'end' - ? item.end + this.options.scrollPaddingEnd - : item.start - this.options.scrollPaddingStart + const centerOffset = item.start - this.options.scrollPaddingStart + (item.size - size) / 2 - return [this.getOffsetForAlignment(toOffset, align), align] as const + switch (align) { + case 'center': + return [this.getOffsetForAlignment(centerOffset, align), align] as const + case 'end': + return [this.getOffsetForAlignment(item.end + this.options.scrollPaddingEnd, align), align] as const + default: + return [this.getOffsetForAlignment(item.start - this.options.scrollPaddingStart, align), align] as const + } } private isDynamicMode = () => this.elementsCache.size > 0