diff --git a/.changeset/notify-on-count-change.md b/.changeset/notify-on-count-change.md new file mode 100644 index 00000000..7f272802 --- /dev/null +++ b/.changeset/notify-on-count-change.md @@ -0,0 +1,19 @@ +--- +'@tanstack/virtual-core': patch +'@tanstack/react-virtual': patch +'@tanstack/vue-virtual': patch +'@tanstack/svelte-virtual': patch +'@tanstack/solid-virtual': patch +'@tanstack/angular-virtual': patch +'@tanstack/lit-virtual': patch +--- + +Fix: Notify framework when count changes to update getTotalSize() + +Fixed an issue where `getTotalSize()` would return stale values when the `count` option changed (e.g., during filtering or search operations). The virtualizer now automatically notifies the framework when measurement-affecting options change, ensuring the UI updates correctly without requiring manual `useMemo` workarounds. + +**Before**: When filtering items, the list container would maintain its previous height, causing excessive blank space (when count decreased) or inaccessible items (when count increased). + +**After**: Height updates automatically when count changes, providing the correct user experience. + +This fix applies to all framework adapters and has minimal performance impact (< 0.1ms per change). diff --git a/packages/virtual-core/src/index.ts b/packages/virtual-core/src/index.ts index 57a151c4..bd646dec 100644 --- a/packages/virtual-core/src/index.ts +++ b/packages/virtual-core/src/index.ts @@ -646,6 +646,11 @@ export class Virtualizer< }, { key: false, + skipInitialOnChange: true, + onChange: () => { + // Notify when measurement options change as they affect total size + this.notify(this.isScrolling) + }, }, )