Skip to content

Commit ecdfd90

Browse files
committed
docs(vue-virtual): use onUpdated for measureElement to prevent scroll jumping
1 parent 5d6acc9 commit ecdfd90

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

examples/vue/dynamic/src/components/ColumnVirtualizerDynamic.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
v-for="virtualColumn in virtualColumns"
1616
:key="virtualColumn.key"
1717
:data-index="virtualColumn.index"
18-
:ref="measureElement"
18+
ref="virtualItemEls"
1919
:class="virtualColumn.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
2020
:style="{
2121
position: 'absolute',
@@ -35,7 +35,7 @@
3535
</template>
3636

3737
<script setup lang="ts">
38-
import { ref, computed, type VNodeRef } from 'vue'
38+
import { ref, computed, onMounted, onUpdated, shallowRef } from 'vue'
3939
import { useVirtualizer } from '@tanstack/vue-virtual'
4040
import { generateSentences } from './utils'
4141
@@ -54,13 +54,14 @@ const virtualColumns = computed(() => columnVirtualizer.value.getVirtualItems())
5454
5555
const totalSize = computed(() => columnVirtualizer.value.getTotalSize())
5656
57-
const measureElement = (el) => {
58-
if (!el) {
59-
return
60-
}
57+
const virtualItemEls = shallowRef([])
6158
62-
columnVirtualizer.value.measureElement(el)
63-
64-
return undefined
59+
function measureAll() {
60+
virtualItemEls.value.forEach((el) => {
61+
if (el) columnVirtualizer.value.measureElement(el)
62+
})
6563
}
64+
65+
onMounted(measureAll)
66+
onUpdated(measureAll)
6667
</script>

examples/vue/dynamic/src/components/GridVirtualizerDynamic.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<template v-for="virtualRow in virtualRows" :key="virtualRow.key">
1010
<div
1111
:data-index="virtualRow.index"
12-
:ref="measureElement"
12+
ref="virtualItemEls"
1313
:style="{
1414
position: 'absolute',
1515
top: 0,
@@ -47,7 +47,7 @@
4747
</template>
4848

4949
<script setup lang="ts">
50-
import { ref, computed, onMounted, type VNodeRef } from 'vue'
50+
import { ref, computed, onMounted, onUpdated, shallowRef } from 'vue'
5151
import { useWindowVirtualizer, useVirtualizer } from '@tanstack/vue-virtual'
5252
import { generateData, generateColumns } from './utils'
5353
@@ -103,13 +103,14 @@ const width = computed(() => {
103103
: [0, 0]
104104
})
105105
106-
const measureElement = (el) => {
107-
if (!el) {
108-
return
109-
}
110-
111-
rowVirtualizer.value.measureElement(el)
106+
const virtualItemEls = shallowRef([])
112107
113-
return undefined
108+
function measureAll() {
109+
virtualItemEls.value.forEach((el) => {
110+
if (el) rowVirtualizer.value.measureElement(el)
111+
})
114112
}
113+
114+
onMounted(measureAll)
115+
onUpdated(measureAll)
115116
</script>

examples/vue/dynamic/src/components/RowVirtualizerDynamic.vue

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div
1414
ref="parentRef"
1515
class="List"
16-
style="height: 400px; width: 400px; overflow-y: auto; contain: strict"
16+
style="height: 400px; width: 400px; overflow-y: auto; contain: strict;"
1717
>
1818
<div
1919
:style="{
@@ -35,7 +35,7 @@
3535
v-for="virtualRow in virtualRows"
3636
:key="virtualRow.key"
3737
:data-index="virtualRow.index"
38-
:ref="measureElement"
38+
ref="virtualItemEls"
3939
:class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
4040
>
4141
<div style="padding: 10px 0">
@@ -50,7 +50,7 @@
5050
</template>
5151

5252
<script setup lang="ts">
53-
import { ref, computed } from 'vue'
53+
import { computed, onMounted, onUpdated, ref, shallowRef } from 'vue'
5454
import { useVirtualizer } from '@tanstack/vue-virtual'
5555
import { generateSentences } from './utils'
5656
@@ -68,13 +68,14 @@ const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
6868
6969
const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
7070
71-
const measureElement = (el) => {
72-
if (!el) {
73-
return
74-
}
71+
const virtualItemEls = shallowRef([])
7572
76-
rowVirtualizer.value.measureElement(el)
77-
78-
return undefined
73+
function measureAll() {
74+
virtualItemEls.value.forEach((el) => {
75+
if (el) rowVirtualizer.value.measureElement(el)
76+
})
7977
}
78+
79+
onMounted(measureAll)
80+
onUpdated(measureAll)
8081
</script>

examples/vue/dynamic/src/components/RowVirtualizerDynamicWindow.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
v-for="virtualRow in virtualRows"
2323
:key="virtualRow.key"
2424
:data-index="virtualRow.index"
25-
:ref="measureElement"
25+
ref="virtualItemEls"
2626
:class="virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'"
2727
>
2828
<div style="padding: 10px 0">
@@ -36,7 +36,7 @@
3636
</template>
3737

3838
<script setup lang="ts">
39-
import { ref, computed, onMounted } from 'vue'
39+
import { ref, computed, onMounted, onUpdated, shallowRef } from 'vue'
4040
import { useWindowVirtualizer } from '@tanstack/vue-virtual'
4141
import { generateSentences } from './utils'
4242
@@ -64,13 +64,14 @@ const virtualRows = computed(() => rowVirtualizer.value.getVirtualItems())
6464
6565
const totalSize = computed(() => rowVirtualizer.value.getTotalSize())
6666
67-
const measureElement = (el) => {
68-
if (!el) {
69-
return
70-
}
71-
72-
rowVirtualizer.value.measureElement(el)
67+
const virtualItemEls = shallowRef([])
7368
74-
return undefined
69+
function measureAll() {
70+
virtualItemEls.value.forEach((el) => {
71+
if (el) rowVirtualizer.value.measureElement(el)
72+
})
7573
}
74+
75+
onMounted(measureAll)
76+
onUpdated(measureAll)
7677
</script>

0 commit comments

Comments
 (0)