-
-
Notifications
You must be signed in to change notification settings - Fork 339
Svelte 5 support #866
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
Comments
Here's how I made it work in svelte 5, mimicking the existing @tanstack/virtual-solid package: import {
Virtualizer,
elementScroll,
observeElementOffset,
observeElementRect,
observeWindowOffset,
observeWindowRect,
windowScroll,
type PartialKeys,
type VirtualizerOptions
} from "@tanstack/virtual-core"
export * from "@tanstack/virtual-core"
function createVirtualizerBase<
TScrollElement extends Element | Window,
TItemElement extends Element,
>(
options: VirtualizerOptions<TScrollElement, TItemElement>,
): Virtualizer<TScrollElement, TItemElement> {
const resolvedOptions = { ...options }
const instance = new Virtualizer(resolvedOptions)
let virtualItems = $state(instance.getVirtualItems())
let totalSize = $state(instance.getTotalSize())
const handler = {
get(
target: Virtualizer<TScrollElement, TItemElement>,
prop: keyof Virtualizer<TScrollElement, TItemElement>
) {
if (prop === "getVirtualItems")
return () => virtualItems
if (prop === "getTotalSize")
return () => totalSize
return Reflect.get(target, prop)
}
}
const virtualizer = new Proxy(instance, handler)
virtualizer.setOptions(resolvedOptions)
$effect(() => {
const cleanup = virtualizer._didMount()
virtualizer._willUpdate()
return cleanup
})
$effect(() => {
virtualizer.setOptions({
...resolvedOptions,
...options,
onChange: (instance, sync) => {
instance._willUpdate()
virtualItems = instance.getVirtualItems()
totalSize = instance.getTotalSize()
options.onChange?.(instance, sync)
}
})
virtualizer.measure()
})
return virtualizer
}
export function createVirtualizer<
TScrollElement extends Element,
TItemElement extends Element,
>(
options: PartialKeys<
VirtualizerOptions<TScrollElement, TItemElement>,
"observeElementRect" | "observeElementOffset" | "scrollToFn"
>,
): Virtualizer<TScrollElement, TItemElement> {
return createVirtualizerBase<TScrollElement, TItemElement>({
observeElementRect: observeElementRect,
observeElementOffset: observeElementOffset,
scrollToFn: elementScroll,
...options
});
}
export function createWindowVirtualizer<TItemElement extends Element>(
options: PartialKeys<
VirtualizerOptions<Window, TItemElement>,
| "getScrollElement"
| "observeElementRect"
| "observeElementOffset"
| "scrollToFn"
>,
): Virtualizer<Window, TItemElement> {
return createVirtualizerBase<Window, TItemElement>({
getScrollElement: () => (typeof document !== "undefined" ? window : null),
observeElementRect: observeWindowRect,
observeElementOffset: observeWindowOffset,
scrollToFn: windowScroll,
initialOffset: () => (typeof document !== "undefined" ? window.scrollY : 0),
...options
})
} It's literally just a clone of the |
Hello @tannerlinsley! Svelte 5 was released some time ago, do you think we can get an update to support it? Do you have any internal discussions in the team regarding this? Would be highly appreciated! |
@tannerlinsley looking forward to seeing if there is any upgrade plans to Svelte 5 |
Anyone knows how to make this sandbox work on svelte 5? that uses @tanstack/svelte-virtual |
@jithujoshyjy tried your method, the list items seem very jumpy when you click on them. Also the scrollbar keeps going to 0 every time you click show more Code Sandbox Link showing the problem
|
Yea, tanstack virtual seems very unusable on svelte 5. https://tanstack.com/virtual/v3/docs/framework/svelte/examples/dynamic Add |
Hi @jithujoshyjy , would you be interested in submitting a PR with your work? |
@slidenerd I have the same issue, in a another situation. I've an action bound to a row, and the response returns the updated item. When updating the underlying array, it jumps to the top of the table. Did you find a workaround to avoid jumpy table? |
@phhoef seems that it has something to do with global state I abandoned this virtual list since i was not getting anywhere with it and started using this Warning though, scroll to index doesnt work as reliably as i expected it to but rest is super solid |
@slidenerd thanks for your feedback. Do you use the virtual list with tanstack-table or just a normal list? Would you mind sharing a code example how you use it with tanstack table? |
@phhoef i dont need a table in my application at all so I just need a virtual list. Given the state of this library, I am not using tanstack anything at the moment. As mentioned above, I independently found a virtual list whose scroll to index with smooth behavior is broken. The problem with virtua is that it does not persist scroll positions between routes. Keep in mind with virtua that this is a MAJOR ISSUE |
Describe the bug
It's working fine in Svelte 4. I'm currently trying out the library in Svelte 5 and found my use case doesn't work.
I got an empty table with the same code. I suspect it cannot keep track of the initial element binding of the scroll element, because it works if I manually make a
mounted
state and call$virtualizer._willUpdate()
.Your minimal, reproducible example
https://www.sveltelab.dev/github.com/rayrw/svelte5-tanstack-virtual
Steps to reproduce
I've made a minimal reproduction repo.
Please note that we have to manually run
npm i --force && npm run dev
before #863 is merged.With the hack from L19-L27, it seems to work. However, when I commented out it, I got an empty table.
I suspect it cannot keep track of the initial element binding of the scroll element?
Expected behavior
I hope we can get rid of the manual mounting/element binding check and call of
$virtualizer._willUpdate()
.How often does this bug happen?
None
Screenshots or Videos
No response
Platform
macOS, Arc browser Version 1.65.0 (54911)
Chromium Engine Version 130.0.6723.59
tanstack-virtual version
3.10.8
TypeScript version
No response
Additional context
No response
Terms & Code of Conduct
The text was updated successfully, but these errors were encountered: