Repository navigation
fix(vue-virtual): detach observers when the scroll element becomes null #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
piecyk
merged 1 commit into
TanStack:main
from
clean99:contrib/vue-null-scroll-element-cleanup
Nov 1, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/vue-virtual': patch | ||
| --- | ||
|
|
||
| Detach scroll observers when the scroll element becomes null. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
| import { effectScope, nextTick, shallowRef } from 'vue' | ||
| import { useVirtualizer, useWindowVirtualizer } from '../src' | ||
| import type { EffectScope } from 'vue' | ||
|
|
||
| describe('scroll element lifecycle', () => { | ||
| let scope: EffectScope | ||
|
|
||
| beforeEach(() => { | ||
| scope = effectScope() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| scope.stop() | ||
| }) | ||
|
|
||
| function setup<T extends HTMLElement | Window>(initialElement: T | null) { | ||
| const scrollElement = shallowRef(initialElement) | ||
| const cleanupRect = vi.fn() | ||
| const cleanupOffset = vi.fn() | ||
| const options = { | ||
| count: 10, | ||
| estimateSize: () => 50, | ||
| getScrollElement: () => scrollElement.value, | ||
| observeElementRect: vi.fn(() => cleanupRect), | ||
| observeElementOffset: vi.fn(() => cleanupOffset), | ||
| scrollToFn: vi.fn(), | ||
| } | ||
|
|
||
| return { scrollElement, cleanupRect, cleanupOffset, options } | ||
| } | ||
|
|
||
| test('attaches observers when an initially null element becomes available', async () => { | ||
| const { scrollElement, options } = setup<HTMLElement>(null) | ||
| const virtualizer = scope.run(() => useVirtualizer(options))! | ||
|
|
||
| expect(virtualizer.value.scrollElement).toBeNull() | ||
| expect(options.observeElementRect).not.toHaveBeenCalled() | ||
| expect(options.observeElementOffset).not.toHaveBeenCalled() | ||
|
|
||
| scrollElement.value = document.createElement('div') | ||
| await nextTick() | ||
|
|
||
| expect(virtualizer.value.scrollElement).toBe(scrollElement.value) | ||
| expect(options.observeElementRect).toHaveBeenCalledTimes(1) | ||
| expect(options.observeElementOffset).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('cleans up observers when the element becomes null and reattaches later', async () => { | ||
| const element = document.createElement('div') | ||
| const { scrollElement, cleanupRect, cleanupOffset, options } = | ||
| setup(element) | ||
| const virtualizer = scope.run(() => useVirtualizer(options))! | ||
|
|
||
| expect(virtualizer.value.scrollElement).toBe(element) | ||
|
|
||
| scrollElement.value = null | ||
| await nextTick() | ||
|
|
||
| expect(cleanupRect).toHaveBeenCalledTimes(1) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(1) | ||
| expect(virtualizer.value.scrollElement).toBeNull() | ||
| expect(virtualizer.value.targetWindow).toBeNull() | ||
|
|
||
| scrollElement.value = element | ||
| await nextTick() | ||
|
|
||
| expect(virtualizer.value.scrollElement).toBe(element) | ||
| expect(options.observeElementRect).toHaveBeenCalledTimes(2) | ||
| expect(options.observeElementOffset).toHaveBeenCalledTimes(2) | ||
|
|
||
| scope.stop() | ||
| expect(cleanupRect).toHaveBeenCalledTimes(2) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(2) | ||
| expect(virtualizer.value.scrollElement).toBeNull() | ||
| }) | ||
|
|
||
| test('cleans up observers on element replacement and scope disposal', async () => { | ||
| const { scrollElement, cleanupRect, cleanupOffset, options } = setup( | ||
| document.createElement('div'), | ||
| ) | ||
| const virtualizer = scope.run(() => useVirtualizer(options))! | ||
|
|
||
| scrollElement.value = document.createElement('div') | ||
| await nextTick() | ||
|
|
||
| expect(cleanupRect).toHaveBeenCalledTimes(1) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(1) | ||
| expect(virtualizer.value.scrollElement).toBe(scrollElement.value) | ||
| expect(options.observeElementRect).toHaveBeenCalledTimes(2) | ||
| expect(options.observeElementOffset).toHaveBeenCalledTimes(2) | ||
|
|
||
| scope.stop() | ||
| expect(cleanupRect).toHaveBeenCalledTimes(2) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(2) | ||
| expect(virtualizer.value.scrollElement).toBeNull() | ||
| }) | ||
|
|
||
| test('cleans up window observers when the window getter becomes null', async () => { | ||
| const { scrollElement, cleanupRect, cleanupOffset, options } = | ||
| setup<Window>(window) | ||
| const virtualizer = scope.run(() => useWindowVirtualizer(options))! | ||
|
|
||
| expect(virtualizer.value.scrollElement).toBe(window) | ||
|
|
||
| scrollElement.value = null | ||
| await nextTick() | ||
|
|
||
| expect(cleanupRect).toHaveBeenCalledTimes(1) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(1) | ||
| expect(virtualizer.value.scrollElement).toBeNull() | ||
|
|
||
| scope.stop() | ||
| expect(cleanupRect).toHaveBeenCalledTimes(1) | ||
| expect(cleanupOffset).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "include": ["src", "vite.config.ts"] | ||
| "include": ["src", "tests", "vite.config.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.