• Stars
    star
    368
  • Rank 113,269 (Top 3 %)
  • Language Svelte
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny but mighty list virtualization library for Svelte, with zero dependencies ๐Ÿ’ช Supports variable heights/widths, sticky items, scrolling to index, and more!

ListLogo

svelte-tiny-virtual-list

A tiny but mighty list virtualization library, with zero dependencies ๐Ÿ’ช

NPM VERSION NPM DOWNLOADS DEPENDENCIES

About โ€ข Features โ€ข Installation โ€ข Usage โ€ข Examples โ€ข License

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!

Features

  • Tiny & dependency free โ€“ Only ~5kb gzipped
  • Render millions of items, without breaking a sweat
  • Scroll to index or set the initial scroll offset
  • Supports fixed or variable heights/widths
  • Vertical or Horizontal lists
  • svelte-infinite-loading compatibility

Installation

If you're using this component in a Sapper application, make sure to install the package to devDependencies!
More Details

With npm:

$ npm install svelte-tiny-virtual-list

With yarn:

$ yarn add svelte-tiny-virtual-list

With pnpm (recommended):

$ npm i -g pnpm
$ pnpm install svelte-tiny-virtual-list

From CDN (via unpkg):

<!-- UMD -->
<script src="https://unpkg.com/svelte-tiny-virtual-list@^1/dist/svelte-tiny-virtual-list.js"></script>

<!-- ES Module -->
<script src="https://unpkg.com/svelte-tiny-virtual-list@^1/dist/svelte-tiny-virtual-list.mjs"></script>

Usage

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

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

<VirtualList
    width="100%"
    height={600}
    itemCount={data.length}
    itemSize={50}>
  <div slot="item" let:index let:style {style}>
    Letter: {data[index]}, Row: #{index}
  </div>
</VirtualList>

Also works pretty well with svelte-infinite-loading:

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

  let data = ['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
    width="100%"
    height={600}
    itemCount={data.length}
    itemSize={50}>
  <div slot="item" let:index let:style {style}>
    Letter: {data[index]}, Row: #{index}
  </div>

  <div slot="footer">
    <InfiniteLoading on:infinite={infiniteHandler} />
  </div>
</VirtualList>

Props

Property Type Required? Description
width number | string* โœ“ Width of List. This property will determine the number of rendered items when scrollDirection is 'horizontal'.
height number | string* โœ“ Height of List. This property will determine the number of rendered items when scrollDirection is 'vertical'.
itemCount number โœ“ The number of items you want to render
itemSize number | number[] | (index: number) => number โœ“ Either a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list, or a function that returns the height of an item given its index: (index: number): number
scrollDirection string Whether the list should scroll vertically or horizontally. One of 'vertical' (default) or 'horizontal'.
scrollOffset number Can be used to control the scroll offset; Also useful for setting an initial scroll offset
scrollToIndex number Item index to scroll to (by forcefully scrolling if necessary)
scrollToAlignment string Used in combination with scrollToIndex, this prop controls the alignment of the scrolled to item. One of: 'start', 'center', 'end' or 'auto'. Use 'start' to always align items to the top of the container and 'end' to align them bottom. Use 'center' to align them in the middle of the container. 'auto' scrolls the least amount possible to ensure that the specified scrollToIndex item is fully visible.
scrollToBehaviour string Used in combination with scrollToIndex, this prop controls the behaviour of the scrolling. One of: 'auto', 'smooth' or 'instant' (default).
stickyIndices number[] An array of indexes (eg. [0, 10, 25, 30]) to make certain items in the list sticky (position: sticky)
overscanCount number Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices.
estimatedItemSize number Used 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 Function 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.

* height must be a number when scrollDirection is 'vertical'. Similarly, width must be a number if scrollDirection is 'horizontal'

Slots

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

Events

  • afterScroll - Fired after handling the scroll event
    • detail Props:
      • event: ScrollEvent - The original scroll event
      • offset: number - Either the value of wrapper.scrollTop or wrapper.scrollLeft
  • itemsUpdated - Fired when the visible items are updated
    • detail Props:
      • start: number - Index of the first visible item
      • end: number - Index of the last visible item

Methods

  • recomputeSizes(startIndex: number) - This method force recomputes the item sizes after the specified index (these are normally cached).

VirtualList has no way of knowing when its underlying data has changed, since it only receives a itemSize property. If the itemSize is a number, this isn't an issue, as it can compare before and after values and automatically call recomputeSizes internally. However, if you're passing a function to itemSize, that type of comparison is error prone. In that event, you'll need to call recomputeSizes manually to inform the VirtualList that the size of its items has changed.

Use the methods like this:

<script>
  import { onMount } from 'svelte';
  import VirtualList from 'svelte-tiny-virtual-list';

  const data = ['A', 'B', 'C', 'D', 'E', 'F', /* ... */];
  
  let virtualList;
  
  function handleClick() {
    virtualList.recomputeSizes(0);
  }
</script>

<button on:click={handleClick}>Recompute Sizes</button>

<VirtualList
        bind:this={virtualList}
        width="100%"
        height={600}
        itemCount={data.length}
        itemSize={50}>
  <div slot="item" let:index let:style {style}>
    Letter: {data[index]}, Row: #{index}
  </div>
</VirtualList>

Styling

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

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

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

<div class="list">
  <VirtualList
      width="100%"
      height={600}
      itemCount={data.length}
      itemSize={50}>
    <div slot="item" let:index let:style {style}>
      Letter: {data[index]}, Row: #{index}
    </div>
  </VirtualList>
</div>

<style>
  .list :global(.virtual-list-wrapper) {
    background-color: #0f0;
    /* ... */
  }
  
  .list :global(.virtual-list-inner) {
    background-color: #f00;
    /* ... */
  }
</style>

Examples / Demo

License

MIT License

More Repositories

1

svelte-infinite-loading

An infinite scroll component for Svelte, to help you implement an infinite scroll list more easily.
Svelte
258
star
2

Gameshell-Love2D-Template

A simple template to quickly create Lรถve2D games for the Gameshell (by clockworkpi)
Lua
28
star
3

TI-Nspire-Minesweeper

A simple Minesweeper-Clone made for the TI-Nspire with Lua ๐Ÿ•น
Lua
17
star
4

Sudoku

A beautifully-designed Sudoku game made with Svelte and TailwindCSS
Svelte
15
star
5

FlightWiki

A simple example Wiki running on the Flight PHP micro-framework
PHP
9
star
6

SherlockHomepage

A .htpasswd-Manager for your homepage!
PHP
7
star
7

clockTube

A simple YouTube Client - Browse and view Videos directly on your Gameshell!
Lua
7
star
8

Toolbox

Little website that I've created using Svelte and Sapper, providing various self-made tools that I use regularly
Svelte
7
star
9

zacktype

Test your typing skill and compete with your friends! โŒจ๏ธ
TypeScript
7
star
10

BBB-Downloader

Super simple downloader for Big Blue Button Meetings ๐ŸŽž
TypeScript
6
star
11

3d-rasterizer-lua

A simple 3D Software Engine made in Lua with Lร–VE2D
Lua
5
star
12

Love2D-Joystick-Tester

A simple Lร–VE2D application to test joystick axes, buttons and hats. Supports having multiple joysticks connected!
Lua
5
star
13

pathix

A simple Powershell script to clean up your PATH on Windows
PowerShell
4
star
14

Lagan-Bulma-Theme

A simple, beautiful theme for the Lagan Admin-Panel
HTML
4
star
15

MQTT-3D-Cube

A 3D cube controllable by MQTT - Made for my teacher
HTML
4
star
16

URL-Shortener

Source code of my own URL-Shortener Service
PHP
4
star
17

Iconmonstr-API

An unofficial API to access icons from iconmonstr.com
PHP
4
star
18

Mind-Map-Generator

Super simple mind map generator I've made for school
JavaScript
3
star
19

EightyEighty.js

A nice little Intel 8080 emulator for Node.js and Browser! ๐Ÿ’พ
TypeScript
3
star
20

Funbox

Copy of my Toolbox project, with some less useful tools - so just for fun
Svelte
2
star
21

svelte-tailwindcss-template

Simple template for building apps using Svelte and TailwindCSS
JavaScript
2
star
22

FaviconAPI

Simple API for generating favicons โšก
PHP
2
star
23

brainfuck.rs

A simple brainfuck interpreter in Rust
Rust
2
star
24

BrezlBot

A fun little Telegram bot I've written for my friends and me, which allows users to reward others by giving them pretzels ๐Ÿฅจ
TypeScript
2
star
25

gorousel

A stupidly simple wallpaper carousel program ๐Ÿ–ผ
Go
2
star
26

hiper

Automatically update Hetzner Cloud Floating IP server assignments in a Docker Swarm
Go
2
star
27

Mage

Magically creates wonderful images for you! โœจ
PHP
2
star
28

renovate-config

๐Ÿ”ง Configuration and common presets for Mend Renovate.
1
star
29

brainfuck-php

๐Ÿงช An optimizing brainfuck parser & interpreter written in PHP.
PHP
1
star
30

skar

An awful tool I've written for collecting many files into one archive file (heavily inspired by tar)
TypeScript
1
star
31

Arcade

Play Intel 8080 games in your browser! ๐Ÿ•น๏ธ
Svelte
1
star
32

Pregramer

Beautiful Social Media Share Previews for your Instagram Posts ๐Ÿ“ธ
PHP
1
star
33

Lagan-Blog-Example

A little blog example built with Lagan.
HTML
1
star
34

Brainfuck-Interpreter-Java

A simple Brainfuck Interpreter written in Java
Java
1
star
35

Skayt

A simple, MQTT-like IoT protocol hosted on a simple NodeMCU server.
Lua
1
star
36

lua-fenster

๐Ÿ“š The most minimal cross-platform GUI library - now in Lua! (WIP)
C
1
star
37

3d-raytracer-lua

๐Ÿงช A simple 3D raytracer written in Lua over a couple of weekends.
Lua
1
star