Skip to content

KVirtualList

A simple virtual list, useful when rendering large amounts of data.

The code implementation of KVirtualList comes from svelte-virtual-scroll-list

Install

bash
pnpm add @ikun-ui/virtual-list
bash
yarn add @ikun-ui/virtual-list
bash
npm install @ikun-ui/virtual-list

Basic usage

Without further ado, please take a look at the demo

this is item 0
this is item 1
this is item 2
this is item 3
this is item 4
this is item 5
this is item 6
this is item 7
this is item 8
this is item 9
this is item 10
this is item 11
this is item 12
this is item 13
this is item 14
this is item 15
this is item 16
this is item 17
this is item 18
this is item 19
this is item 20
this is item 21
this is item 22
this is item 23
this is item 24
this is item 25
this is item 26
this is item 27
this is item 28
this is item 29

Different heights

You can set different heights for item according to your needs.

this is item 0 -- 30px
this is item 1 -- 40px
this is item 2 -- 50px
this is item 3 -- 60px
this is item 4 -- 70px
this is item 5 -- 80px
this is item 6 -- 90px
this is item 7 -- 100px
this is item 8 -- 110px
this is item 9 -- 120px
this is item 10 -- 130px
this is item 11 -- 140px
this is item 12 -- 150px
this is item 13 -- 160px
this is item 14 -- 170px
this is item 15 -- 180px
this is item 16 -- 190px
this is item 17 -- 200px
this is item 18 -- 210px
this is item 19 -- 220px
this is item 20 -- 230px
this is item 21 -- 240px
this is item 22 -- 250px
this is item 23 -- 260px
this is item 24 -- 270px
this is item 25 -- 280px
this is item 26 -- 290px
this is item 27 -- 300px
this is item 28 -- 310px
this is item 29 -- 320px

Horizontal virtual lists

isHorizontal can control the display of horizontal virtual lists.

this is item 0
this is item 1
this is item 2
this is item 3
this is item 4
this is item 5
this is item 6
this is item 7
this is item 8
this is item 9
this is item 10
this is item 11
this is item 12
this is item 13
this is item 14
this is item 15
this is item 16
this is item 17
this is item 18
this is item 19
this is item 20
this is item 21
this is item 22
this is item 23
this is item 24
this is item 25
this is item 26
this is item 27
this is item 28
this is item 29

Slots allow you to add content at the top and bottom of the list.

this is header 🥵
this is item 0
this is item 1
this is item 2
this is item 3
this is item 4
this is item 5
this is item 6
this is item 7
this is item 8
this is item 9
this is item 10
this is item 11
this is item 12
this is item 13
this is item 14
this is item 15
this is item 16
this is item 17
this is item 18
this is item 19
this is item 20
this is item 21
this is item 22
this is item 23
this is item 24
this is item 25
this is item 26
this is item 27
this is item 28
this is item 29
this is footer 🐔

Scroll to bottom

KVirtualList provides some APIs, such as scrollToBottom.

this is item 0
this is item 1
this is item 2
this is item 3
this is item 4
this is item 5
this is item 6
this is item 7
this is item 8
this is item 9
this is item 10
this is item 11
this is item 12
this is item 13
this is item 14
this is item 15
this is item 16
this is item 17
this is item 18
this is item 19
this is item 20
this is item 21
this is item 22
this is item 23
this is item 24
this is item 25
this is item 26
this is item 27
this is item 28
this is item 29

VirtualList Props

NameTypeDefaultDescription
dataArray<Record<string, any>>-Source for list.
keystring'id'Unique key for getting data from data
keepsnumber30Count of rendered items
estimateSizenumber50Estimate size of each item, needs for smooth scrollbar.
isHorizontalbooleanfalseScroll direction.
startnumber0Scroll position start index
offsetnumber0Scroll position offset
topThresholdnumber0The threshold to emit top event, attention to multiple calls.
bottomThresholdnumber0The threshold to emit bottom event, attention to multiple calls.
clsstring''Additional class for component
attrsRecord<string, string>{}Additional attributes

VirtualList Events

NameDescriptionType
scrolltrigger when scroll.(data: {event: CustomEvent, range: VRange})=> void
topFires when the scroll bar reaches the top()=> void
bottomFires when the scroll bar reaches the bottom()=> void
typescript
declare interface VRange {
	start: number;
	end: number;
	padFront: number;
	padBehind: number;
}

VirtualList Slots

NameDescription
defaultCustomize item content
footerCustomize footer item content
headerCustomize header item content

VirtualList Api

methodargumentsdescription
scrollToBottomnoneScroll list to bottom
scrollToIndexindex: numberSet scroll position to a designated index
scrollToOffsetoffset: numberSet scroll position to a designated offset
getSizeid: typeof props.keyGet the designated item size
getSizesnoneGet the total number of stored (rendered) items
getOffsetnoneGet current scroll offset
getClientSizenoneGet wrapper element client viewport size (width or height)
getScrollSizenoneGet all scroll size (scrollHeight or scrollWidth)

MIT Licensed