Svelte Virtual List logo

Events & Callbacks

Callbacks available for responding to virtual list events.

onLoadMore

Triggered when the user scrolls near the end of the list. Used for implementing infinite scroll / pagination.

Signature

onLoadMore?: () => void | Promise<void>
onLoadMore?: () => void | Promise<void>

Example

<script lang="ts">
    import VirtualList from '@humanspeak/svelte-virtual-list'

    let items = $state([...initialItems])
    let hasMore = $state(true)
    let isLoading = $state(false)

    async function loadMore() {
        if (isLoading) return
        isLoading = true

        try {
            const newItems = await fetchMoreItems()
            items = [...items, ...newItems]

            if (newItems.length === 0) {
                hasMore = false
            }
        } finally {
            isLoading = false
        }
    }
</script>

<VirtualList
    {items}
    onLoadMore={loadMore}
    {hasMore}
    loadMoreThreshold={10}
>
    {#snippet renderItem(item)}
        <div>{item.text}</div>
    {/snippet}
</VirtualList>
<script lang="ts">
    import VirtualList from '@humanspeak/svelte-virtual-list'

    let items = $state([...initialItems])
    let hasMore = $state(true)
    let isLoading = $state(false)

    async function loadMore() {
        if (isLoading) return
        isLoading = true

        try {
            const newItems = await fetchMoreItems()
            items = [...items, ...newItems]

            if (newItems.length === 0) {
                hasMore = false
            }
        } finally {
            isLoading = false
        }
    }
</script>

<VirtualList
    {items}
    onLoadMore={loadMore}
    {hasMore}
    loadMoreThreshold={10}
>
    {#snippet renderItem(item)}
        <div>{item.text}</div>
    {/snippet}
</VirtualList>

Related Props

PropDescription
hasMoreSet to false to stop triggering onLoadMore
loadMoreThresholdItems from end to trigger (default: 20)

debugFunction

Receive real-time debug information about the list state.

Signature

debugFunction?: (info: SvelteVirtualListDebugInfo) => void
debugFunction?: (info: SvelteVirtualListDebugInfo) => void

Example

<script lang="ts">
    import VirtualList from '@humanspeak/svelte-virtual-list'
    import type { SvelteVirtualListDebugInfo } from '@humanspeak/svelte-virtual-list'

    function onDebug(info: SvelteVirtualListDebugInfo) {
        // Track scroll position
        if (info.atBottom) {
            console.log('User reached the bottom')
        }

        // Monitor performance
        console.log(`Rendering ${info.visibleItemsCount} of ${info.totalItems} items`)
    }
</script>

<VirtualList
    {items}
    debug={true}
    debugFunction={onDebug}
>
    {#snippet renderItem(item)}
        <div>{item.text}</div>
    {/snippet}
</VirtualList>
<script lang="ts">
    import VirtualList from '@humanspeak/svelte-virtual-list'
    import type { SvelteVirtualListDebugInfo } from '@humanspeak/svelte-virtual-list'

    function onDebug(info: SvelteVirtualListDebugInfo) {
        // Track scroll position
        if (info.atBottom) {
            console.log('User reached the bottom')
        }

        // Monitor performance
        console.log(`Rendering ${info.visibleItemsCount} of ${info.totalItems} items`)
    }
</script>

<VirtualList
    {items}
    debug={true}
    debugFunction={onDebug}
>
    {#snippet renderItem(item)}
        <div>{item.text}</div>
    {/snippet}
</VirtualList>

Debug Info Properties

PropertyTypeDescription
startIndexnumberFirst rendered item index
endIndexnumberLast rendered item index
totalItemsnumberTotal items in list
visibleItemsCountnumberCurrently visible items
processedItemsnumberItems with measured heights
averageItemHeightnumberCalculated average height
atTopbooleanScrolled to top
atBottombooleanScrolled to bottom
totalHeightnumberTotal content height