Skip to content

Commit

Permalink
fix: update scrollToIndex center-alignment (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
christriants authored Dec 16, 2024
1 parent 7e7bd9c commit b5c99b2
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b5c99b2

Please sign in to comment.