Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-3-infinite-scroll-directive-plugin

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-3-infinite-scroll-directive-plugin

Vue 3 plugin to help implementing an infinite scroll

  • 1.0.6
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Vue 3 Infinite Scroll Plugin

A Vue 3 directive for implementing infinite scroll functionality.

Test it by yourself

Test the directive by yourself in this sandbox

Demo

Installation

npm install npm i vue-3-infinite-scroll-directive-plugin
yarn add npm i vue-3-infinite-scroll-directive-plugin

Usage

Import and Register the Plugin In your main application file (e.g., main.ts or main.js), import and register the plugin:

import { createApp } from 'vue';
import App from './App.vue';
import { infiniteScrollPlugin } from 'vue-3-infinite-scroll-directive-plugin';

createApp(App).use(infiniteScrollPlugin).mount('#app');

Directive Usage

Add the v-infinite-scroll directive to the scrollable element in your template. Specify the handler function to be called when the bottom of the element is reached.

<template>
  <div v-infinite-scroll="handleInfiniteScroll" 
    data-infinite-scroll-margin="20" 
    data-infinite-scroll-delay="200" 
    :data-infinite-scroll-disabled="isLoading">
    <!-- Your content here -->
  </div>
  <div class="loader-wrapper" v-if="isLoading">
    <span class="loader"></span>
  </div>
</template>

Example Setup

<script lang="ts" setup>
  import { ref } from 'vue';

  const todos = ref([]);
  const nextPage = ref(1);
  const isLoading = ref(false);

  const handleInfiniteScroll = async () => {
    isLoading.value = true;

    const data = await (await fetch(`https://jsonplaceholder.typicode.com/todos/${nextPage.value}`)).json();
    todos.value.push(data);
    nextPage.value++;

    setTimeout(() => {
      isLoading.value = false;
    }, Math.random() * 2000);
  };
</script>
<template>
  <div v-infinite-scroll="handleInfiniteScroll" 
    data-infinite-scroll-margin="20" 
    data-infinite-scroll-delay="200" 
    :data-infinite-scroll-disabled="isLoading"
  >
    <ul>
      <li  v-for="todo in todos" :key="todo.id"  class="todo">
        <div>
          {{ todo.title }}
        </div>
      </li>
    </ul>
  </div>
  <div class="loader-wrapper" v-if="isLoading">
    <span class="loader"></span>
  </div>
</template>

Options

  • v-infinite-scroll: Pass the callback to execute when bottom is reached

  • data-infinite-scroll-margin: Set the margin (in pixels) from the bottom of the scrollable element to trigger the infinite scroll. Default is 0.

  • data-infinite-scroll-delay: Set the delay (in milliseconds) before allowing another infinite scroll trigger. Default is 2000.

  • data-infinite-scroll-disabled: Bind this attribute to a variable to enable/disable infinite scroll based on its truthiness.

Types

export type ScrollableHtmlElement = HTMLElement & { __v_destroyInfiniteScrollObserver?: Function };

Cleanup

The plugin takes care of disconnecting the observer and removing the event listener when the component is destroyed.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Keywords

FAQs

Package last updated on 09 Mar 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc