Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
svelte-tiny-virtual-list
Advanced tools
A tiny but mighty list virtualization component for svelte, with zero dependencies 💪
A tiny but mighty list virtualization library, with zero dependencies 💪
About • Features • Installation • Usage • Examples • License
Instead of rendering all your list of data in a huge list, the virtual list component just renders the bits 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!
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>
<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>
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 | ✓ | 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. | |
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. |
* height
must be a number when scrollDirection
is 'vertical'
. Similarly, width
must be a number if scrollDirection
is 'horizontal'
item
- Slot for each item
index: number
- Item indexstyle: 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 listfooter
- Slot for the elements that should appear at the bottom of the list (e.g. InfiniteLoading
component from svelte-infinite-loading
)afterScroll
- Fired after handling the scroll event
detail
Props:
event: ScrollEvent
- The original scroll eventoffset: number
- Either the value of rootNode.scrollTop
or rootNode.scrollLeft
itemsUpdated
- Fired when the visible items are updated
detail
Props:
start: number
- Index of the first visible itemend: number
- Index of the last visible itemrecomputeSizes(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.
<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>
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>
FAQs
A tiny but mighty list virtualization component for svelte, with zero dependencies 💪
The npm package svelte-tiny-virtual-list receives a total of 3,319 weekly downloads. As such, svelte-tiny-virtual-list popularity was classified as popular.
We found that svelte-tiny-virtual-list demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.