Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 20 additions & 28 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,17 +873,23 @@ export class Virtualizer<
)
}

getOffsetForAlignment = (toOffset: number, align: ScrollAlignment) => {
getOffsetForAlignment = (
toOffset: number,
align: ScrollAlignment,
itemSize = 0,
) => {
const size = this.getSize()
const scrollOffset = this.getScrollOffset()

if (align === 'auto') {
if (toOffset >= scrollOffset + size) {
align = 'end'
}
align = toOffset >= scrollOffset + size ? 'end' : 'start'
}

if (align === 'end') {
if (align === 'center') {
// When aligning to a particular item (e.g. with scrollToIndex),
// adjust offset by the size of the item to center on the item
toOffset += (itemSize - size) / 2
} else if (align === 'end') {
toOffset -= size
}

Expand Down Expand Up @@ -922,29 +928,15 @@ export class Virtualizer<
}
}

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
}
const toOffset =
align === 'end'
? item.end + this.options.scrollPaddingEnd
: item.start - this.options.scrollPaddingStart

return [
this.getOffsetForAlignment(toOffset, align, item.size),
align,
] as const
}

private isDynamicMode = () => this.elementsCache.size > 0
Expand Down