From b5c99b234bad973a08016df740ef861c9aa7824f Mon Sep 17 00:00:00 2001 From: christriants <30908909+christriants@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:07:25 -0500 Subject: [PATCH] fix: update scrollToIndex center-alignment (#864) --- packages/virtual-core/src/index.ts | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index c66c8c92..c35101b1 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -845,21 +845,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 @@ -897,12 +889,29 @@ export class Virtualizer< } } - const toOffset = - align === 'end' - ? item.end + this.options.scrollPaddingEnd - : item.start - this.options.scrollPaddingStart - - return [this.getOffsetForAlignment(toOffset, align), align] as const + const centerOffset = + item.start - this.options.scrollPaddingStart + (item.size - size) / 2 + + 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