forked from TanStack/virtual
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9368a80
commit ec5bef4
Showing
5 changed files
with
153 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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,8 @@ | ||
import { PartialKeys, Virtualizer, VirtualizerOptions } from '@tanstack/virtual-core'; | ||
export * from '@tanstack/virtual-core'; | ||
import { Readable } from 'svelte/store'; | ||
export type SvelteVirtualizer<TScrollElement extends Element | Window, TItemElement extends Element> = Omit<Virtualizer<TScrollElement, TItemElement>, 'setOptions'> & { | ||
setOptions: (options: Partial<VirtualizerOptions<TScrollElement, TItemElement>>) => void; | ||
}; | ||
export declare function createVirtualizer<TScrollElement extends Element, TItemElement extends Element>(options: PartialKeys<VirtualizerOptions<TScrollElement, TItemElement>, 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): Readable<SvelteVirtualizer<TScrollElement, TItemElement>>; | ||
export declare function createWindowVirtualizer<TItemElement extends Element>(options: PartialKeys<VirtualizerOptions<Window, TItemElement>, 'getScrollElement' | 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'>): Readable<SvelteVirtualizer<Window, TItemElement>>; |
This file contains 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,46 @@ | ||
import { elementScroll, observeElementOffset, observeElementRect, observeWindowOffset, observeWindowRect, Virtualizer, windowScroll, } from '@tanstack/virtual-core'; | ||
export * from '@tanstack/virtual-core'; | ||
import { derived, writable } from 'svelte/store'; | ||
function createVirtualizerBase(initialOptions) { | ||
const virtualizer = new Virtualizer(initialOptions); | ||
const originalSetOptions = virtualizer.setOptions; | ||
let virtualizerWritable; | ||
const setOptions = (options) => { | ||
const resolvedOptions = { | ||
...virtualizer.options, | ||
...options, | ||
onChange: options.onChange, | ||
}; | ||
originalSetOptions({ | ||
...resolvedOptions, | ||
onChange: (instance, sync) => { | ||
virtualizerWritable.set(instance); | ||
resolvedOptions.onChange?.(instance, sync); | ||
}, | ||
}); | ||
virtualizer._willUpdate(); | ||
}; | ||
virtualizerWritable = writable(virtualizer, () => { | ||
setOptions(initialOptions); | ||
return virtualizer._didMount(); | ||
}); | ||
return derived(virtualizerWritable, (instance) => Object.assign(instance, { setOptions })); | ||
} | ||
export function createVirtualizer(options) { | ||
return createVirtualizerBase({ | ||
observeElementRect: observeElementRect, | ||
observeElementOffset: observeElementOffset, | ||
scrollToFn: elementScroll, | ||
...options, | ||
}); | ||
} | ||
export function createWindowVirtualizer(options) { | ||
return createVirtualizerBase({ | ||
getScrollElement: () => (typeof document !== 'undefined' ? window : null), | ||
observeElementRect: observeWindowRect, | ||
observeElementOffset: observeWindowOffset, | ||
scrollToFn: windowScroll, | ||
initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined, | ||
...options, | ||
}); | ||
} |
This file contains 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 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,6 @@ | ||
import { svelte } from '@sveltejs/vite-plugin-svelte' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
plugins: [svelte()], | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.