Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion examples/react/dynamic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ function RowVirtualizerDynamic() {

return (
<div>
<button
onClick={() => {
virtualizer.scrollToIndex(0)
}}
>
scroll to the top
</button>
<span style={{ padding: '0 4px' }} />
<button
onClick={() => {
virtualizer.scrollToIndex(count / 2)
Expand All @@ -40,7 +48,7 @@ function RowVirtualizerDynamic() {
virtualizer.scrollToIndex(count - 1)
}}
>
scroll to the last
scroll to the end
</button>
<hr />
<div
Expand Down
167 changes: 122 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions packages/react-virtual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"src"
],
"devDependencies": {
"@testing-library/react": "^12.1.2",
"@types/react": "17.0.45",
"@types/react-dom": "17.0.17",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"@testing-library/react": "^13.4.0",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"resize-observer-polyfill": "^1.5.1"
},
"dependencies": {
Expand Down
38 changes: 20 additions & 18 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ export class Virtualizer<
return rangeExtractor({
...range,
overscan,
count: count,
count,
})
},
{
Expand Down Expand Up @@ -626,27 +626,38 @@ export class Virtualizer<
)

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

if (align === 'auto') {
if (toOffset <= offset) {
if (toOffset <= this.scrollOffset) {
align = 'start'
} else if (toOffset >= offset + size) {
} else if (toOffset >= this.scrollOffset + size) {
align = 'end'
} else {
align = 'start'
}
}

if (align === 'start') {
return toOffset
toOffset = toOffset
} else if (align === 'end') {
return toOffset - size
toOffset = toOffset - size
} else if (align === 'center') {
return toOffset - size / 2
toOffset = toOffset - size / 2
}
return toOffset

const scrollSizeProp = this.options.horizontal
? 'scrollWidth'
: 'scrollHeight'
const scrollSize = this.scrollElement
? 'document' in this.scrollElement
? this.scrollElement.document.documentElement[scrollSizeProp]
: this.scrollElement[scrollSizeProp]
: 0

const maxOffset = scrollSize - this.getSize()

return Math.max(Math.min(maxOffset, toOffset), 0)
}

scrollToOffset = (
Expand Down Expand Up @@ -724,16 +735,7 @@ export class Virtualizer<
? measurement.end + this.options.scrollPaddingEnd
: measurement.start - this.options.scrollPaddingStart

const sizeProp = this.options.horizontal ? 'scrollWidth' : 'scrollHeight'
const scrollSize = this.scrollElement
? 'document' in this.scrollElement
? this.scrollElement.document.documentElement[sizeProp]
: this.scrollElement[sizeProp]
: 0

const maxOffset = scrollSize - this.getSize()

return Math.min(maxOffset, this.getOffsetForAlignment(toOffset, align))
return this.getOffsetForAlignment(toOffset, align)
}

const toOffset = getOffsetForIndexAndAlignment(measurement)
Expand Down