0.0.14 • Published 5 months ago

svelte-dynamic-scroll v0.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

License: MIT CI npm version

Svelte Dynamic Scroll

A simple Dynamic Scroll Component for Svelte.

Installation

$ npm i -D svelte-dynamic-scroll

or

$ yarn add -D svelte-dynamic-scroll

Usage

<script>
	import { DynamicScroll } from 'svelte-dynamic-scroll';

	/**
	 * @param {number} lastValue
	 */
	function nextChunk(lastValue) {
		const _last = lastValue ?? -1;
		let array = [];
		for (let i = 0; i < 10; i++) {
			array.push(_last + (i + 1));
		}
		return array;
	}
</script>

<div class="app">
	<h2>Infinite Scroll</h2>
	<div class="container">
		<DynamicScroll {nextChunk} let:index let:value>
			<div>{value}:({index})</div>
			<div slot="loading">loading...</div>
		</DynamicScroll>
	</div>
</div>

<style>
	:global(body) {
		margin: 0;
	}
	.app {
		height: 100vh;
	}
	h2 {
		height: 50px;
		margin: 0;
		padding: 10px;
		box-sizing: border-box;
	}
	.container {
		/* It is important to specify height */
		height: calc(100% - 50px);
	}
</style>

Demo

https://redfe.github.io/svelte-dynamic-scroll

Properties

PropertyTypeDefaultDescription
previousChunk(lastValue:T=) => T[] | Promise<T[]>undefinedFunction to be called when the scroll reaches the top of the container.
nextChunk(lastValue:T=) => T[] | Promise<T[]>undefinedFunction to be called when the scroll reaches the bottom of the container.
bufferSizenumber-1Number of elements to be rendered before and after the visible area.
triggerRangeRationumber0.1The ratio of the visible area to trigger the previousChunk or nextChunk function.
maxRetryCountOnPreLoadnumber20Maximum number of retries when the previousChunk or nextChunk function returns null.
onScrollCallback(event:UIEvent) => voidundefinedCallback function to be invoked when the scroll event occurs.
axisstring'y'Scroll axis. 'x' or 'y'
listT[][]the list of elements to be rendered. It should have the same type as the return value of the previousChunk function and the nextChunk function.
scrollPositionnumberundefinedScroll position.

T is the type of the return value of the previousChunk function and the nextChunk function.

Slots

NameDescription
(default)Slot for rendering elements.
loadingSlot for rendering loading elements.

Default slot Properties

PropertyTypeDescription
indexnumberIndex of the element to be rendered.
valueTValue of the element to be rendered. It should have the same type as the return value of the previousChunk function and the nextChunk function.

T is the type of the return value of the previousChunk function and the nextChunk function.

Developing

Once you've created a project and installed dependencies with npm install (or pnpm install or yarn), start a development server:

npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open

Everything inside src/lib is part of your library, everything inside src/routes can be used as a showcase or preview app.

Building

To build your library:

npm run package

To create a production version of your showcase app:

npm run build

You can preview the production build with npm run preview.

To deploy your app, you may need to install an adapter for your target environment.

Publishing

Go into the package.json and give your package the desired name through the "name" option. Also consider adding a "license" field and point it to a LICENSE file which you can create from a template (one popular option is the MIT license).

To publish your library to npm:

npm publish
0.0.14

5 months ago

0.0.13

6 months ago

0.0.11

7 months ago

0.0.12

7 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago