Skip to content

Commit

Permalink
feat(VVirtualScroll): add scrollToIndex options and fractional scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Sep 2, 2024
1 parent 3bc7229 commit eb450eb
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/vuetify/src/composables/virtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ export function useVirtual <T> (props: VirtualProps, items: Ref<readonly T[]>) {

function calculateOffset (index: number) {
index = clamp(index, 0, items.value.length - 1)
return offsets[index] || 0
const whole = Math.floor(index)
const fraction = index % 1
const next = whole + 1
const wholeOffset = offsets[whole] || 0
const nextOffset = offsets[next] ?? wholeOffset
return wholeOffset + (nextOffset - wholeOffset) * fraction
}

function calculateIndex (scrollTop: number) {
Expand Down Expand Up @@ -220,12 +225,19 @@ export function useVirtual <T> (props: VirtualProps, items: Ref<readonly T[]>) {
paddingBottom.value = calculateOffset(items.value.length) - calculateOffset(last.value)
}

function scrollToIndex (index: number) {
function scrollToIndex (index: number, options?: ScrollToOptions) {
const offset = calculateOffset(index)
if (!containerRef.value || (index && !offset)) {
targetScrollIndex = index
} else {
containerRef.value.scrollTop = offset
if (options) {
containerRef.value.scrollTo({
top: offset,
...options,
})
} else {
containerRef.value.scrollTop = offset
}
}
}

Expand Down

0 comments on commit eb450eb

Please sign in to comment.