Skip to content

Commit 78c8c9d

Browse files
committed
fix: virtual list error at firefox, close #6470
1 parent c690f41 commit 78c8c9d

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

components/vc-virtual-list/List.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ const List = defineComponent({
226226
offset: undefined,
227227
});
228228
}
229+
if (componentRef.value) {
230+
state.scrollTop = componentRef.value.scrollTop;
231+
}
232+
},
233+
{
234+
immediate: true,
229235
},
230-
{ immediate: true },
231236
);
232237
watch(
233238
[
@@ -277,11 +282,11 @@ const List = defineComponent({
277282
itemTop = currentItemBottom;
278283
}
279284

280-
// Fallback to normal if not match. This code should never reach
281-
/* istanbul ignore next */
285+
// When scrollTop at the end but data cut to small count will reach this
282286
if (startIndex === undefined) {
283287
startIndex = 0;
284288
startOffset = 0;
289+
endIndex = Math.ceil(height / itemHeight);
285290
}
286291
if (endIndex === undefined) {
287292
endIndex = dataLen - 1;
@@ -325,7 +330,7 @@ const List = defineComponent({
325330
// When data size reduce. It may trigger native scroll event back to fit scroll position
326331
function onFallbackScroll(e: UIEvent) {
327332
const { scrollTop: newScrollTop } = e.currentTarget as Element;
328-
if (Math.abs(newScrollTop - state.scrollTop) >= 1) {
333+
if (newScrollTop !== state.scrollTop) {
329334
syncScrollTop(newScrollTop);
330335
}
331336

components/vc-virtual-list/ScrollBar.tsx

+13-12
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,19 @@ export default defineComponent({
119119
this.onScrollbarTouchStart,
120120
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
121121
);
122-
this.thumbRef.current.removeEventListener(
123-
'touchstart',
124-
this.onMouseDown,
125-
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
126-
);
127-
this.thumbRef.current.removeEventListener(
128-
'touchmove',
129-
this.onMouseMove,
130-
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
131-
);
132-
this.thumbRef.current.removeEventListener('touchend', this.onMouseUp);
133-
122+
if (this.thumbRef.current) {
123+
this.thumbRef.current.removeEventListener(
124+
'touchstart',
125+
this.onMouseDown,
126+
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
127+
);
128+
this.thumbRef.current.removeEventListener(
129+
'touchmove',
130+
this.onMouseMove,
131+
supportsPassive ? ({ passive: false } as EventListenerOptions) : false,
132+
);
133+
this.thumbRef.current.removeEventListener('touchend', this.onMouseUp);
134+
}
134135
raf.cancel(this.moveRaf);
135136
},
136137

components/vc-virtual-list/hooks/useHeights.tsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { VNodeProps, Ref, ShallowRef } from 'vue';
2-
import { watch, ref } from 'vue';
2+
import { onUnmounted, watch, ref } from 'vue';
33
import type { GetKey } from '../interface';
4+
import wrapperRaf from '../../_util/raf';
45

56
export type CacheMap = Map<any, number>;
67

@@ -16,14 +17,14 @@ export default function useHeights<T>(
1617
watch(mergedData, () => {
1718
updatedMark.value = Symbol('update');
1819
});
19-
let heightUpdateId = 0;
20+
let collectRaf: number = undefined;
21+
22+
function cancelRaf() {
23+
wrapperRaf.cancel(collectRaf);
24+
}
2025
function collectHeight() {
21-
heightUpdateId += 1;
22-
const currentId = heightUpdateId;
23-
Promise.resolve().then(() => {
24-
// Only collect when it's latest call
25-
if (currentId !== heightUpdateId) return;
26-
// let changed = false;
26+
cancelRaf();
27+
collectRaf = wrapperRaf(() => {
2728
instance.forEach((element, key) => {
2829
if (element && element.offsetParent) {
2930
const { offsetHeight } = element;
@@ -57,6 +58,9 @@ export default function useHeights<T>(
5758
}
5859
}
5960
}
61+
onUnmounted(() => {
62+
cancelRaf();
63+
});
6064

6165
return [setInstance, collectHeight, heights, updatedMark];
6266
}

components/vc-virtual-list/hooks/useScrollTo.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function useScrollTo(
103103
collectHeight();
104104
}
105105
syncScroll(times - 1, newTargetAlign);
106-
});
106+
}, 2);
107107
};
108108

109109
syncScroll(5);

0 commit comments

Comments
 (0)