0.2.5 โ€ข Published 5 months ago

@humanspeak/svelte-virtual-list v0.2.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@humanspeak/svelte-virtual-list

NPM version Build Status Coverage Status License Downloads CodeQL Install size Code Style: Trunk TypeScript Types Maintenance

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-list

Basic 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

PropTypeDefaultDescription
itemsT[]RequiredArray of items to render
defaultItemHeightnumber40Initial height for items before measurement
mode'topToBottom' \| 'bottomToTop''topToBottom'Scroll direction
bufferSizenumber20Number of items to render outside viewport
debugbooleanfalseEnable debug logging and visualizations
containerClassstring''Class for outer container
viewportClassstring''Class for scrollable viewport
contentClassstring''Class for content wrapper
itemsClassstring''Class for items container

Performance Considerations

  • The bufferSize prop 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

0.2.5

5 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.1

10 months ago

0.0.0

10 months ago