Socket
Book a DemoInstallSign in
Socket

@tutorlatin/svelte-tiny-virtual-list

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tutorlatin/svelte-tiny-virtual-list

A tiny but mighty list virtualization component for svelte, with zero dependencies 💪

3.0.12
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

Logo

@tutorlatin/svelte-tiny-virtual-list

A tiny but mighty list virtualization library, with zero dependencies 💪

NPM VERSION NPM DOWNLOADS DEPENDENCIES

AboutFeaturesInstallationUsageExamplesLicense

About

Instead of rendering all your data in a huge list, the virtual list component just renders the items that are visible, keeping your page nice and light.
This is heavily inspired by react-tiny-virtual-list and uses most of its code and functionality!

This is a maintained fork of svelte-tiny-virtual-list.

Features

Installation

With npm:

$ npm install @tutorlatin/svelte-tiny-virtual-list

With yarn:

$ yarn add @tutorlatin/svelte-tiny-virtual-list

With pnpm:

$ pnpm install @tutorlatin/svelte-tiny-virtual-list

Usage

<script>
	import VirtualList from '@tutorlatin/svelte-tiny-virtual-list';

	const data = ['A', 'B', 'C', 'D', 'E', 'F' /* ... */];
</script>

<VirtualList height={600} itemCount={data.length} itemSize={50}>
	{#snippet item({ style, index })}
		<div {style}>
			Letter: {data[index]}, Row: #{index}
		</div>
	{/snippet}
</VirtualList>

Also works pretty well with svelte-infinite-loading:

<script>
	import VirtualList from '@tutorlatin/svelte-tiny-virtual-list';
	import InfiniteLoading from 'svelte-infinite-loading';

	let data = $state(['A', 'B', 'C', 'D', 'E', 'F' /* ... */]);

	function infiniteHandler({ detail: { complete, error } }) {
		try {
			// Normally you'd make an http request here...

			const newData = ['G', 'H', 'I', 'J', 'K', 'L' /* ... */];

			data = [...data, ...newData];
			complete();
		} catch (e) {
			error();
		}
	}
</script>

<VirtualList height={600} itemCount={data.length} itemSize={50}>
	{#snippet item({ style, index })}
		<div {style}>
			Letter: {data[index]}, Row: #{index}
		</div>
	{/snippet}

	{#snippet footer()}
		<div>
			<InfiniteLoading on:infinite={infiniteHandler} />
		</div>
	{/snippet}
</VirtualList>

Props

PropertyTypeDefaultDescription
itemCountnumberREQUIREDThe number of items you want to render.
itemSizenumber | number[] | (index: number) => numberREQUIREDEither a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list (⚠ if passing an array, do not mutate in place: replace the array reference to trigger updates), or a function that returns the height of an item given its index: (index: number) => number.
widthnumber | string'100%'Width of the list view box. When scrollDirection is 'horizontal', this property determines the number of rendered items.
heightnumber | string'100%'Height of the list view box. When scrollDirection is 'vertical', this property determines the number of rendered items.
scrollDirection'vertical' | 'horizontal''vertical'Whether the list should scroll vertically or horizontally.
scrollOffsetnumber0Used to control the scroll offset, but also useful for setting an initial scroll offset.
scrollToIndexnumber-1Item index to scroll to (by forcefully scrolling if necessary).
scrollToAlignment'start' | 'center' | 'end' | 'auto''start'Used in combination with scrollToIndex, this prop controls the alignment of the scrolled to item. Use 'auto' to scroll the least amount required to ensure that the specified scrollToIndex item is fully visible.
scrollToBehaviour'smooth' | 'instant' | 'auto''instant'Used in combination with scrollToIndex, this prop controls the behaviour of the scrolling. See: [Element: scroll() method - Web APIs
stickyIndicesnumber[][]An array of indexes (eg. [0, 10, 25, 30]) to make certain items in the list sticky (position: sticky)
overscanCountnumber3Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices.
estimatedItemSizenumber0Used to estimate the total size of the list before all of its items have actually been measured. The estimated total height is progressively adjusted as items are rendered.
getKey((index: number) => any) | nullundefinedFunction that returns the key of an item in the list, which is used to uniquely identify an item. This is useful for dynamic data coming from a database or similar. By default, it's using the item's index.
onAfterScroll({ event: ScrollEvent, offset: number }) => voidundefinedFunction that fires after handling the scroll event. Props: event: ScrollEvent - The original scroll event, offset: number - Either the value of wrapper.scrollTop or wrapper.scrollLeft
onListItemsUpdate({ start: number, end: number }) => voidundefinedFunction that fires when the visible items are updated. Props: start: number - Index of the first visible item, end: number - Index of the last visible item.

Snippets

  • item - Snippet for each item
    • Prop: { index, style }
      • index: number - Item index
      • style: string - Item style, must be applied to the snippet (look above for example)
  • header - Snippet for the elements that should appear at the top of the list
  • footer - Snippet for the elements that should appear at the bottom of the list (e.g. InfiniteLoading component from svelte-infinite-loading)

Styling

You can style the elements of the virtual list like this:

<script>
	import VirtualList from '@tutorlatin/svelte-tiny-virtual-list';

	const data = ['A', 'B', 'C', 'D', 'E', 'F' /* ... */];
</script>

<div class="list">
	<VirtualList height={600} itemCount={data.length} itemSize={50}>
		{#snippet item({ style, index })}
			<div {style}>
				Letter: {data[index]}, Row: #{index}
			</div>
		{/snippet}
	</VirtualList>
</div>

<style>
	.list :global(.virtual-list-wrapper) {
		background-color: yellow;
		/* ... */
	}

	.list :global(.virtual-list-inner) {
		background-color: red;
		/* ... */
	}

	.list {
		--virtual-list-sticky-bg: red;
	}
</style>

Examples / Demo

License

MIT License

Keywords

svelte

FAQs

Package last updated on 16 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.