Socket
Socket
Sign inDemoInstall

svelte-infinite

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.3 to 0.3.0

dist/loaderState.svelte.d.ts

3

dist/index.d.ts

@@ -1,2 +0,3 @@

import InfiniteLoader, { loaderState } from "./InfiniteLoader.svelte";
import InfiniteLoader from "./InfiniteLoader.svelte";
import { loaderState } from "./loaderState.svelte";
export { InfiniteLoader, loaderState };

@@ -1,2 +0,3 @@

import InfiniteLoader, { loaderState } from "./InfiniteLoader.svelte";
import InfiniteLoader from "./InfiniteLoader.svelte";
import { loaderState } from "./loaderState.svelte";
export { InfiniteLoader, loaderState };
import { SvelteComponent } from "svelte";
export declare const loaderState: {
loaded: () => void;
complete: () => void;
reset: () => void;
error: () => void;
};
import { type Snippet } from "svelte";

@@ -9,0 +3,0 @@ declare const __propDef: {

@@ -9,3 +9,3 @@ {

},
"version": "0.2.3",
"version": "0.3.0",
"license": "MIT",

@@ -61,2 +61,3 @@ "homepage": "https://svelte-5-infinite.vercel.app",

"typescript": "^5.3.3",
"typescript-svelte-plugin": "^0.3.37",
"vite": "^5.1.4",

@@ -63,0 +64,0 @@ "vitest": "^1.3.1"

@@ -15,3 +15,3 @@ <p align="center">

> Svelte Infinite Loader designed and rebuilt specifically for use in **Svelte 5** with runes
> Svelte Infinite Loader designed and rebuilt specifically for use with **Svelte 5**

@@ -22,6 +22,9 @@ ✨ Flexible

🔎 `IntersectionObserver` based
🔥 Using Runes and Snippets
🧑‍🔧 **Demo**: [svelte-5-infinite.vercel.app](https://svelte-5-infinite.vercel.app)
Svelte 5 is still early days, but I couldn't find an infinite loader-type component that was maintained for the last few years of Svelte 4. So I had recently built this for a Svelte 5-based application I was working on and was pretty happy with it, so I decided to clean it up and share it with the world! As Svelte 5 inevitably changes over the next weeks and months, I plan to keep this package updated and working with the latest available version of Svelte 5.
Svelte 5 is still early days, but I couldn't find an infinite loader-type component that was maintained for the last few years of Svelte 4 even. So I had recently built this for a Svelte 5-based application I'm working on and was pretty happy with it, so I decided to share it with the world!
As Svelte 5 inevitably changes over the next weeks and months, I plan to keep this package updated and working with the latest available version of Svelte 5. Don't hesitate to open an issue if something has changed in the latest Svelte releases or you come across a bug!
## 🏗️ Getting Started

@@ -60,10 +63,10 @@

The component should wrap your list of items, and `loaderState` should be used in your `triggerLoad` function (and/or elsewhere) to interact with the internal state of the Loader component. You tell it whether you're out of data, ran into an error, etc.
## 🍍 Example
See the example below and [in this repository](https://github.com/ndom91/svelte-infinite/blob/main/src/routes/%2Bpage.svelte#L12-L50) for more details.
This is a more realistic example use-case which includes a paginated data endpoint that your `triggerLoad` function should hit every time it's called to load more data. It also includes the use of some of the optional snippets to render custom markup inside the loader component.
## 🍍 Example
```svelte
<script lang="ts">
// +page.svelte
import { InfiniteLoader, loaderState } from "svelte-infinite"

@@ -73,9 +76,10 @@ import UserCard from "$components/UserCard.svelte"

const LOAD_LIMIT = 20
// Assume `$page.data.items` is the `+page.server.ts` server-side loaded
// and rendered initial 20 items of the list
const allItems = $state<{ id: number, body: string }[]>($page.data.items)
let pageNumber = $state(1)
// 1. You'll have to pass the InfiniteLoader component a load function
// 1. This `loadMore` function is what we'll pass the InfiniteLoader component
// to its `triggerLoad` prop.
const loadMore = async () => {
// This is a relatively straight-forward load function with support for pagination
try {

@@ -89,3 +93,3 @@ pageNumber += 1

if (allItems.length < LOAD_LIMIT) {
loaderState.complete()
loaderState.complete() // <--- using loaderState
return

@@ -96,3 +100,3 @@ }

// Execute the API call to grab more data
// Fetch an endpoint that supports server-side pagination
const dataResponse = await fetch(`/api/data?${searchParams}`)

@@ -103,4 +107,5 @@

// available to page through
if (!dataResponse.ok) {
loaderState.error()
loaderState.error() // <--- using loaderState

@@ -114,4 +119,3 @@ // On errors, set the pageNumber back so we can retry

// If we've received data, push it to the reactive state variable
// rendering our items inside the `<InfiniteLoader />` below.
// If we've successfully received data, push it to the reactive state variable
if (data.items.length) {

@@ -124,9 +128,9 @@ allItems.push(...data.items)

if (allItems.length >= data.totalCount) {
loaderState.complete()
loaderState.complete() // <--- using loaderState
} else {
loaderState.loaded()
loaderState.loaded() // <--- using loaderState
}
} catch (error) {
console.error(error)
loaderState.error()
loaderState.error() // <--- using loaderState
pageNumber -= 1

@@ -167,16 +171,16 @@ }

### loaderState
### `loaderState` Controller
The `loaderState` import is an object with 4 methods on it:
The `loaderState` controller has 4 methods on it. You should call these at the appropriate times to control the internal state of the `InfiniteLoader`.
- `loaderState.loaded()`
- Designed to be called after a successful fetch.
- Designed to be called after a successful fetch. Will set the internal state back to `READY` so another fetch can be attempted.
- `loaderState.error()`
- Designed to be called after a failed fetch or any other error. This will cause the `InfiniteLoader` to render a "Retry" button by default, or the `error` snippet.
- `loaderState.complete()`
- Designed to be called when you've reached the end of your list and there are no more items to fetch. This will render a "No more data" string, or the `no-data` snippet.
- Designed to be called when you've reached the end of your list and there are no more items to fetch. This will render a "No more data" string, or the `noData` snippet.
- `loaderState.reset()`
- Designed to be called when you want to reset the state of the `InfiniteLoader` to its initial state, for example if there is a search input tied to your infinite list and the user enters a new query.
- Designed to be called when you want to reset the state of the `InfiniteLoader` to its initial state, for example if there is a search input tied to your data and the user enters a new query.
### Props
### `InfiniteLoader` Props

@@ -187,13 +191,13 @@ - `triggerLoad: () => Promise<void>` - **required**

- The options to pass to the `IntersectionObserver` instance. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#options) for more details. The default `rootMargin` value will cause the target to intersect 200px earlier and trigger the `loadMore` function before it actually intersects with the root element (window by default). This has the effect of beginning to load the next page of data before the user has actually reached the current bottom of the list, making the experience feel more smooth.
- It may also be required to pass in a reference to your scroll container as the `root` option, if your scroll container is not the window.
- If you are using a separate scroll container (element with `overflow-y: scroll`) other than the window / viewport, then it might be necessary for you to also pass a [custom `root` element](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root) here.
- `loopTimeout: number = 3000` - optional
- If the `loopMaxCalls` is reached within the detection timeout, a cool down period is triggered of this length (in milliseconds).
- Length of the cool down period (in milliseconds).
- `loopDetectionTimeout: number = 2000` - optional
- The time in milliseconds in which the `loopMaxCalls` count must be hit in order to trigger a cool down period of `loopTimeout` length.
- The time in milliseconds in which the `loopMaxCalls` count must be hit in order to trigger a cool down period.
- `loopMaxCalls: number = 5` - optional
- The number of calls to the `triggerLoad` function within timeout which should trigger cool down period.
- The limit of `triggerLoad` executions which will trigger a cool down period, if reached within the `loopDetectionTimeout`.
### Snippets
### `InfiniteLoader` Snippets
Snippets [replace slots](https://svelte-5-preview.vercel.app/docs/snippets#snippets-and-slots) in Svelte 5, and as such are used here to customize the content shown at the bottom of the scroller in various states. The `InfiniteLoader` component has 4 props for snippets available.
Snippets [replace slots](https://svelte-5-preview.vercel.app/docs/snippets#snippets-and-slots) in Svelte 5, and as such are used here to customize the content shown at the bottom of the scroller in various states. The `InfiniteLoader` component has 5 snippet "slots" available.

@@ -200,0 +204,0 @@ - `loading`

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc