• Stars
    star
    456
  • Rank 95,985 (Top 2 %)
  • Language Svelte
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 27 days 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
301
star
2

Gameshell-Love2D-Template

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

lua-fenster

๐Ÿ“š The most minimal cross-platform GUI library - now in Lua!
C
25
star
4

TI-Nspire-Minesweeper

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

sudoku

๐ŸŽฎ A beautiful little Sudoku game built with Svelte and TailwindCSS.
Svelte
15
star
6

FlightWiki

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

zacktype

๐ŸŽฎ A very minimal typing speed measurement game built with Svelte.
TypeScript
8
star
8

SherlockHomepage

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

clockTube

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

pathix

A simple Powershell script to clean up your PATH on Windows
PowerShell
7
star
11

Toolbox

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

BBB-Downloader

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

3d-soft-engine-lua

๐Ÿงช A simple 3D engine written in Lua using using lua-fenster.
Lua
6
star
14

love2d-joystick-tester

๐ŸŽฎ A simple Lร–VE application to test joysticks and gamepads.
Lua
6
star
15

Lagan-Bulma-Theme

A simple, beautiful theme for the Lagan Admin-Panel
HTML
5
star
16

Iconmonstr-API

An unofficial API to access icons from iconmonstr.com
PHP
5
star
17

EightyEighty.js

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

MQTT-3D-Cube

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

URL-Shortener

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

Mind-Map-Generator

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

3d-raytracer-lua

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

Funbox

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

svelte-tailwindcss-template

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

gorousel

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

FaviconAPI

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

brainfuck.rs

A simple brainfuck interpreter in Rust
Rust
2
star
27

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
28

hiper

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

Mage

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

todolist-angularjs

๐ŸŒ A simple todolist webapp made with AngularJS.
HTML
1
star
31

renovate-config

๐Ÿ”ง My personal configuration preset for Mend Renovate.
1
star
32

brainfuck-php

๐Ÿงช An optimizing Brainfuck parser & interpreter written in PHP.
PHP
1
star
33

skar

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

Arcade

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

Pregramer

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

Lagan-Blog-Example

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

Brainfuck-Interpreter-Java

A simple Brainfuck Interpreter written in Java
Java
1
star
38

Skayt

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