svelte-tiny-virtual-list

repository·main·Indexed 20 days ago

https://github.com/jonasgeiler/svelte-tiny-virtual-list

A lightweight (~5kb gzipped), dependency-free Svelte library for list virtualization. It enables rendering millions of items by only rendering those visible in the viewport, supporting both vertical and horizontal scrolling with fixed or variable item sizes. Version 4.0.0-rc.2 utilizes Svelte Snippets for item, header, and footer rendering.

Tokens
1.6K
Snippets
7
Records
9
Agent score
68%

What's inside svelte-tiny-virtual-list

  1. Define itemSize with different types

    main

    The itemSize prop is highly flexible and can be provided in three ways:

    1. Fixed Size: A single number if all items are identical.
    2. Array of Sizes: A number[] where each element corresponds to the size of the item at that index.
    3. Getter Function: A function (index: number) => number for dynamic or calculated sizes.
    // Fixed
    itemSize={50}
    
    // Array
    itemSize={[50, 100, 50, 75]}
    
    // Function
    itemSize={(index) => index % 2 === 0 ? 50 : 100}
  2. Use VirtualListSnippets for item and layout rendering

    main

    Instead of traditional slots, svelte-tiny-virtual-list uses Svelte Snippets for rendering content.

    item Snippet

    This is the primary snippet used to render each list item. It receives an object containing:

    • index: number - The index of the item.
    • style: string - The calculated style (e.g., position, transform) that must be applied to the item's container element.
    • header: An optional snippet rendered above the list.
    • footer: An optional snippet rendered below the list.

    Note: The children slot is deprecated in favor of the item snippet.

    <VirtualList 
      {itemCount} 
      {itemSize} 
      height={500}
    >
      {#snippet item({ index, style })}
        <div style={style}>
          Item {index}
        </div>
      {/snippet}
    </VirtualList>
  3. Handle VirtualListEvents

    main

    The component emits two main events to allow synchronization with external state or logic:

    onItemsUpdated

    Called whenever the range of visible items changes. It provides a detail object:

    • start: number - Index of the first visible item.
    • end: number - Index of the last visible item.

    onAfterScroll

    Called after the scroll event has been handled. It provides a detail object:

    • event: Event - The original scroll event.
    • offset: number - The current value of wrapper.scrollTop or wrapper.scrollLeft.
  4. Configure VirtualListProps

    main

    The VirtualList component accepts several props to control its dimensions, scrolling behavior, and item rendering. The props are split into two mutually exclusive configurations based on the scrollDirection:

    Vertical Scroll (Default)

    • scrollDirection: 'vertical'
    • height: number (Required)
    • width: number | string (Optional, defaults to '100%')

    Horizontal Scroll

    • scrollDirection: 'horizontal' (Required)
    • width: number (Required)
    • height: number | string (Optional, defaults to '100%')

    Common Props

    • itemCount: number (Required) - Total number of items.
    • itemSize: number | number[] | ((index: number) => number) (Required) - The size of items. Can be a fixed value, an array of sizes, or a getter function.
    • scrollOffset: number (Optional) - Controls the current scroll offset. Defaults to 0.
    • scrollToIndex: number (Optional) - The index to scroll to. Defaults to -1 (no scrolling).
    • scrollToAlignment: 'auto' | 'start' | 'center' | 'end' (Optional) - Alignment of the item when using scrollToIndex. Defaults to 'start'.
    • scrollToBehaviour: 'auto' | 'smooth' | 'instant' (Optional) - Scrolling behavior when using scrollToIndex. Defaults to 'instant'.
    • stickyIndices: number[] (Optional) - Array of indexes to make items sticky using CSS position: sticky.
    • overscanCount: number (Optional) - Number of extra items to render above/below visible items. Defaults to 3.
    • estimatedItemSize: number (Optional) - Used to estimate total list size before measurement. Defaults to 0.
    • getKey: ((index: number) => any) | null (Optional) - Function to return a unique key for an item. If falsy, uses the index.
  5. Set list direction using DIRECTION constants

    main

    The DIRECTION constant defines the orientation of the virtual list. This affects how scrolling and alignment are handled.

    • HORIZONTAL: For lists scrolling left-to-right.
    • VERTICAL: For lists scrolling top-to-bottom.
    import { DIRECTION } from 'svelte-tiny-virtual-list';
    
    // Example usage in a component prop
    <VirtualList direction={DIRECTION.HORIZONTAL} ... />
  6. Configure list alignment using ALIGNMENT constants

    main

    When configuring the alignment of items within the virtual list, use the ALIGNMENT constant to ensure type safety and avoid string typos. Supported values are:

    • AUTO: Automatic alignment.
    • START: Align to the start of the container.
    • CENTER: Align to the center.
    • END: Align to the end of the container.
    import { ALIGNMENT } from 'svelte-tiny-virtual-list';
    
    // Example usage in a component prop
    <VirtualList alignment={ALIGNMENT.CENTER} ... />
  7. Use VirtualList types and interfaces

    main

    The package exports several types for configuring the component and handling events. Use these for type-safe implementations in TypeScript.

    import type {
    	AfterScrollDetail,
    	Alignment,
    	Direction,
    	ItemSize,
    	ItemsUpdatedDetail,
    	ScrollBehaviour,
    	VirtualListEvents,
    	VirtualListProps,
    	VirtualListSnippets
    } from 'svelte-tiny-virtual-list';