Skip to content

Commit 6360f79

Browse files
authored
docs(vue-virtual): use onUpdated to prevent scroll jumping (#1125)
1 parent c2f1c39 commit 6360f79

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

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

Lines changed: 11 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 { computed, onMounted, onUpdated, ref, shallowRef } from 'vue'
3939
import { useVirtualizer } from '@tanstack/vue-virtual'
4040
import { generateSentences } from './utils'
4141
@@ -54,13 +54,15 @@ 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+
columnVirtualizer.value.measureElement(null)
61+
virtualItemEls.value.forEach((el) => {
62+
if (el) columnVirtualizer.value.measureElement(el)
63+
})
6564
}
65+
66+
onMounted(measureAll)
67+
onUpdated(measureAll)
6668
</script>

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

Lines changed: 13 additions & 11 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,9 +47,9 @@
4747
</template>
4848

4949
<script setup lang="ts">
50-
import { ref, computed, onMounted, type VNodeRef } from 'vue'
51-
import { useWindowVirtualizer, useVirtualizer } from '@tanstack/vue-virtual'
52-
import { generateData, generateColumns } from './utils'
50+
import { computed, onMounted, onUpdated, ref, shallowRef } from 'vue'
51+
import { useVirtualizer, useWindowVirtualizer } from '@tanstack/vue-virtual'
52+
import { generateColumns, generateData } from './utils'
5353
5454
const columns = generateColumns(30)
5555
const data = generateData(columns)
@@ -103,13 +103,15 @@ 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+
rowVirtualizer.value.measureElement(null)
110+
virtualItemEls.value.forEach((el) => {
111+
if (el) rowVirtualizer.value.measureElement(el)
112+
})
114113
}
114+
115+
onMounted(measureAll)
116+
onUpdated(measureAll)
115117
</script>

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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,15 @@ 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+
rowVirtualizer.value.measureElement(null)
75+
virtualItemEls.value.forEach((el) => {
76+
if (el) rowVirtualizer.value.measureElement(el)
77+
})
7978
}
79+
80+
onMounted(measureAll)
81+
onUpdated(measureAll)
8082
</script>

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

Lines changed: 11 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 { computed, onMounted, onUpdated, ref, shallowRef } from 'vue'
4040
import { useWindowVirtualizer } from '@tanstack/vue-virtual'
4141
import { generateSentences } from './utils'
4242
@@ -64,13 +64,15 @@ 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+
rowVirtualizer.value.measureElement(null)
71+
virtualItemEls.value.forEach((el) => {
72+
if (el) rowVirtualizer.value.measureElement(el)
73+
})
7574
}
75+
76+
onMounted(measureAll)
77+
onUpdated(measureAll)
7678
</script>

0 commit comments

Comments
 (0)