Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useInfiniteScroll.md
1---2category: Sensors3---45# useInfiniteScroll67Infinite scrolling of the element.89## Usage1011```vue12<script setup lang="ts">13import { useInfiniteScroll } from '@vueuse/core'14import { ref, useTemplateRef } from 'vue'1516const el = useTemplateRef('el')17const data = ref([1, 2, 3, 4, 5, 6])1819const { reset } = useInfiniteScroll(20el,21() => {22// load more23data.value.push(...moreData)24},25{26distance: 10,27canLoadMore: () => {28// inidicate when there is no more content to load so onLoadMore stops triggering29// if (noMoreContent) return false30return true // for demo purposes31},32}33)3435function resetList() {36data.value = []37reset()38}39</script>4041<template>42<div ref="el">43<div v-for="item in data">44{{ item }}45</div>46</div>47<button @click="resetList()">48Reset49</button>50</template>51```5253## Direction5455Different scroll directions require different CSS style settings:5657| Direction | Required CSS |58| ------------------ | ----------------------------------------------------- |59| `bottom` (default) | No special settings required |60| `top` | `display: flex;`<br>`flex-direction: column-reverse;` |61| `left` | `display: flex;`<br>`flex-direction: row-reverse;` |62| `right` | `display: flex;` |6364::: warning65Make sure to indicate when there is no more content to load with `canLoadMore`, otherwise `onLoadMore` will trigger as long as there is space for more content.66:::6768## Directive Usage6970```vue71<script setup lang="ts">72import { vInfiniteScroll } from '@vueuse/components'73import { ref } from 'vue'7475const data = ref([1, 2, 3, 4, 5, 6])7677function onLoadMore() {78const length = data.value.length + 179data.value.push(...Array.from({ length: 5 }, (_, i) => length + i))80}81function canLoadMore() {82// inidicate when there is no more content to load so onLoadMore stops triggering83// if (noMoreContent) return false84return true // for demo purposes85}86</script>8788<template>89<div v-infinite-scroll="onLoadMore">90<div v-for="item in data" :key="item">91{{ item }}92</div>93</div>9495<!-- with options -->96<div v-infinite-scroll="[onLoadMore, { distance: 10, canLoadMore }]">97<div v-for="item in data" :key="item">98{{ item }}99</div>100</div>101</template>102```103104## Type Declarations105106```ts107type InfiniteScrollElement =108| HTMLElement109| SVGElement110| Window111| Document112| null113| undefined114export interface UseInfiniteScrollOptions<115T extends InfiniteScrollElement = InfiniteScrollElement,116> extends UseScrollOptions {117/**118* The minimum distance between the bottom of the element and the bottom of the viewport119*120* @default 0121*/122distance?: number123/**124* The direction in which to listen the scroll.125*126* @default 'bottom'127*/128direction?: "top" | "bottom" | "left" | "right"129/**130* The interval time between two load more (to avoid too many invokes).131*132* @default 100133*/134interval?: number135/**136* A function that determines whether more content can be loaded for a specific element.137* Should return `true` if loading more content is allowed for the given element,138* and `false` otherwise.139*/140canLoadMore?: (el: T) => boolean141}142export interface UseInfiniteScrollReturn {143isLoading: ComputedRef<boolean>144reset: () => void145}146/**147* Reactive infinite scroll.148*149* @see https://vueuse.org/useInfiniteScroll150*/151export declare function useInfiniteScroll<T extends InfiniteScrollElement>(152element: MaybeRefOrGetter<T>,153onLoadMore: (state: UnwrapNestedRefs<UseScrollReturn>) => Awaitable<void>,154options?: UseInfiniteScrollOptions<T>,155): UseInfiniteScrollReturn156```157