Skip to content

Commit

Permalink
fix: update scrollToIndex center-alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
christriants committed Oct 28, 2024
1 parent 0a76523 commit 9ac99ff
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ac99ff

Please sign in to comment.