# svelte-dynamic-scroll

> [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![CI](https://github.com/redfe/svelte-dynamic-scroll/actions/workflows/ci.yml/badge.svg)](https://github.com/redfe/svelte-dynamic-scroll/actions/w

Latest version **0.0.14** (published 2023-12-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install svelte-dynamic-scroll
pnpm add svelte-dynamic-scroll
yarn add svelte-dynamic-scroll
bun add svelte-dynamic-scroll
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2023-12-24 |
| First published | 2023-07-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | redfe |
| Maintainers | redfedev |

## Links

- npm: https://www.npmjs.com/package/svelte-dynamic-scroll
- Repository: https://github.com/redfe/svelte-dynamic-scroll
- Issues: https://github.com/redfe/svelte-dynamic-scroll/issues
- npm.io page: https://npm.io/package/svelte-dynamic-scroll

## Recent versions

- 0.0.14 (latest) — 2023-12-24
- 0.0.13 — 2023-11-19
- 0.0.12 — 2023-10-07
- 0.0.11 — 2023-10-07
- 0.0.10 — 2023-08-13
- 0.0.9 — 2023-08-13
- 0.0.8 — 2023-08-03
- 0.0.7 — 2023-07-27
- 0.0.6 — 2023-07-26
- 0.0.5 — 2023-07-23
- 0.0.4 — 2023-07-16
- 0.0.2 — 2023-07-16
- 0.0.1 — 2023-07-16

## README

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![CI](https://github.com/redfe/svelte-dynamic-scroll/actions/workflows/ci.yml/badge.svg)](https://github.com/redfe/svelte-dynamic-scroll/actions/workflows/ci.yml)
[![npm version](https://badge.fury.io/js/svelte-dynamic-scroll.svg)](https://badge.fury.io/js/svelte-dynamic-scroll)

# 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

```svelte
<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

| Property               | Type                                      | Default   | Description                                                                                                                                         |
| ---------------------- | ----------------------------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| previousChunk          | (lastValue:T=) => T[] &#124; Promise<T[]> | undefined | Function to be called when the scroll reaches the top of the container.                                                                             |
| nextChunk              | (lastValue:T=) => T[] &#124; Promise<T[]> | undefined | Function to be called when the scroll reaches the bottom of the container.                                                                          |
| bufferSize             | number                                    | -1        | Number of elements to be rendered before and after the visible area.                                                                                |
| triggerRangeRatio      | number                                    | 0.1       | The ratio of the visible area to trigger the previousChunk or nextChunk function.                                                                   |
| maxRetryCountOnPreLoad | number                                    | 20        | Maximum number of retries when the previousChunk or nextChunk function returns null.                                                                |
| onScrollCallback       | (event:UIEvent) => void                   | undefined | Callback function to be invoked when the scroll event occurs.                                                                                       |
| axis                   | string                                    | 'y'       | Scroll axis. 'x' or 'y'                                                                                                                             |
| list                   | T[]                                       | []        | 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. |
| scrollPosition         | number                                    | undefined | Scroll position.                                                                                                                                    |

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

## Slots

| Name      | Description                          |
| --------- | ------------------------------------ |
| (default) | Slot for rendering elements.         |
| loading   | Slot for rendering loading elements. |

## Default slot Properties

| Property | Type   | Description                                                                                                                                         |
| -------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| index    | number | Index of the element to be rendered.                                                                                                                |
| value    | T      | Value 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:

```bash
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:

```bash
npm run package
```

To create a production version of your showcase app:

```bash
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) 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](https://opensource.org/license/mit/)).

To publish your library to [npm](https://www.npmjs.com):

```bash
npm publish
```

---
_Source: https://npm.io/package/svelte-dynamic-scroll · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
