0.2.5 โข Published 5 months ago
@humanspeak/svelte-virtual-list v0.2.5
@humanspeak/svelte-virtual-list
A high-performance virtual list component for Svelte 5 applications that efficiently renders large datasets with minimal memory usage.
Features
- ๐ Dynamic item height handling - no fixed height required
- ๐ Bi-directional scrolling support (top-to-bottom and bottom-to-top)
- ๐ Automatic resize handling for dynamic content
- ๐ TypeScript support with full type safety
- ๐ SSR compatible with hydration support
- โจ Svelte 5 runes and snippets support
- ๐จ Customizable styling with class props
- ๐ Debug mode for development
- ๐ฏ Smooth scrolling with configurable buffer zones
- ๐ง Memory-optimized for 10k+ items
- ๐งช Comprehensive test coverage (vitest and playwright)
- ๐ Progressive initialization for large datasets
- ๐น๏ธ Programmatic scrolling with
scrollToIndex
scrollToIndex: Programmatic Scrolling
You can now programmatically scroll to any item in the list using the scrollToIndex method. This is useful for chat apps, jump-to-item navigation, and more. Thank you for the feature request.
Usage Example
<script lang="ts">
import SvelteVirtualList from '@humanspeak/svelte-virtual-list'
let listRef
const items = Array.from({ length: 10000 }, (_, i) => ({ id: i, text: `Item ${i}` }))
function goToItem5000() {
// Scroll to item 5000 with smooth scrolling
listRef.scrollToIndex(5000, true)
}
</script>
<button on:click={goToItem5000}> Scroll to item 5000 </button>
<SvelteVirtualList {items} bind:this={listRef}>
{#snippet renderItem(item)}
<div>{item.text}</div>
{/snippet}
</SvelteVirtualList>API
scrollToIndex(index: number, smoothScroll = true, shouldThrowOnBounds = true)index: The item index to scroll to (0-based)smoothScroll: If true, uses smooth scrolling (default: true)shouldThrowOnBounds: If true, throws if index is out of bounds (default: true)
Installation
npm install @humanspeak/svelte-virtual-listBasic Usage
<script lang="ts">
import SvelteVirtualList from '@humanspeak/svelte-virtual-list'
const items = Array.from({ length: 1000 }, (_, i) => ({
id: i,
text: `Item ${i}`
}))
</script>
<SvelteVirtualList {items}>
{#snippet renderItem(item)}
<div>{item.text}</div>
{/snippet}
</SvelteVirtualList>Advanced Features
Chat Application Example
<script lang="ts">
import SvelteVirtualList from '@humanspeak/svelte-virtual-list'
type Message = {
id: number
text: string
timestamp: Date
}
const messages: Message[] = Array.from({ length: 100 }, (_, i) => ({
id: i,
text: `Message ${i}`,
timestamp: new Date()
}))
</script>
<div style="height: 500px;">
<SvelteVirtualList items={messages} mode="bottomToTop" debug>
{#snippet renderItem(message)}
<div class="message-container">
<p>{message.text}</p>
<span class="timestamp">
{message.timestamp.toLocaleString()}
</span>
</div>
{/snippet}
</SvelteVirtualList>
</div>Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | T[] | Required | Array of items to render |
defaultItemHeight | number | 40 | Initial height for items before measurement |
mode | 'topToBottom' \| 'bottomToTop' | 'topToBottom' | Scroll direction |
bufferSize | number | 20 | Number of items to render outside viewport |
debug | boolean | false | Enable debug logging and visualizations |
containerClass | string | '' | Class for outer container |
viewportClass | string | '' | Class for scrollable viewport |
contentClass | string | '' | Class for content wrapper |
itemsClass | string | '' | Class for items container |
Performance Considerations
- The
bufferSizeprop affects memory usage and scroll smoothness - Items are measured and cached for optimal performance
- Dynamic height calculations happen automatically
- Resize observers handle container/content changes
- Virtual DOM updates are batched for efficiency
License
MIT ยฉ Humanspeak, Inc.
Credits
Made with โฅ by Humanspeak