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/useVirtualList.md
1---2category: Component3---45# useVirtualList67::: warning8Consider using [`@tanstack/vue-virtual`](https://tanstack.com/virtual/v3/docs/framework/vue/vue-virtual) instead, if you are looking for more features.9:::1011Create virtual lists with ease. Virtual lists (sometimes called [_virtual scrollers_](https://vue-virtual-scroller-demo.netlify.app/)) allow you to render a large number of items performantly. They only render the minimum number of DOM nodes necessary to show the items within the `container` element by using the `wrapper` element to emulate the container element's full height.1213## Usage1415### Simple list1617```ts18import { useVirtualList } from '@vueuse/core'1920const { list, containerProps, wrapperProps } = useVirtualList(21Array.from(Array.from({ length: 99999 }).keys()),22{23// Keep `itemHeight` in sync with the item's row.24itemHeight: 22,25},26)27```2829### Config3031| State | Type | Description |32| ---------- | -------- | ----------------------------------------------------------------------------------------------- |33| itemHeight | `number` | ensure that the total height of the `wrapper` element is calculated correctly.\* |34| itemWidth | `number` | ensure that the total width of the `wrapper` element is calculated correctly.\* |35| overscan | `number` | number of pre-rendered DOM nodes. Prevents whitespace between items if you scroll very quickly. |3637\* The `itemHeight` or `itemWidth` must be kept in sync with the height of each row rendered. If you are seeing extra whitespace or jitter when scrolling to the bottom of the list, ensure the `itemHeight` or `itemWidth` is the same height as the row.3839### Reactive list4041```ts42import { useToggle, useVirtualList } from '@vueuse/core'43import { computed } from 'vue'4445const [isEven, toggle] = useToggle()46const allItems = Array.from(Array.from({ length: 99999 }).keys())47const filteredList = computed(() => allItems.filter(i => isEven.value ? i % 2 === 0 : i % 2 === 1))4849const { list, containerProps, wrapperProps } = useVirtualList(50filteredList,51{52itemHeight: 22,53},54)55```5657```vue58<template>59<p>Showing {{ isEven ? 'even' : 'odd' }} items</p>60<button @click="toggle">61Toggle Even/Odd62</button>63<div v-bind="containerProps" style="height: 300px">64<div v-bind="wrapperProps">65<div v-for="item in list" :key="item.index" style="height: 22px">66Row: {{ item.data }}67</div>68</div>69</div>70</template>71```7273### Horizontal list7475```ts76import { useVirtualList } from '@vueuse/core'7778const allItems = Array.from(Array.from({ length: 99999 }).keys())7980const { list, containerProps, wrapperProps } = useVirtualList(81allItems,82{83itemWidth: 200,84},85)86```8788```vue89<template>90<div v-bind="containerProps" style="height: 300px">91<div v-bind="wrapperProps">92<div v-for="item in list" :key="item.index" style="width: 200px">93Row: {{ item.data }}94</div>95</div>96</div>97</template>98```99100## Component Usage101102```vue103<template>104<UseVirtualList :list="list" :options="options" height="300px">105<template #default="props">106<!-- you can get current item of list here -->107<div style="height: 22px">108Row {{ props.index }} {{ props.data }}109</div>110</template>111</UseVirtualList>112</template>113```114115To scroll to a specific element, the component exposes `scrollTo(index: number) => void`.116117## Type Declarations118119```ts120type UseVirtualListItemSize = number | ((index: number) => number)121export interface UseHorizontalVirtualListOptions extends UseVirtualListOptionsBase {122/**123* item width, accept a pixel value or a function that returns the width124*125* @default 0126*/127itemWidth: UseVirtualListItemSize128}129export interface UseVerticalVirtualListOptions extends UseVirtualListOptionsBase {130/**131* item height, accept a pixel value or a function that returns the height132*133* @default 0134*/135itemHeight: UseVirtualListItemSize136}137export interface UseVirtualListOptionsBase {138/**139* the extra buffer items outside of the view area140*141* @default 5142*/143overscan?: number144}145export type UseVirtualListOptions =146| UseHorizontalVirtualListOptions147| UseVerticalVirtualListOptions148export interface UseVirtualListItem<T> {149data: T150index: number151}152export interface UseVirtualListReturn<T> {153list: Ref<UseVirtualListItem<T>[]>154scrollTo: (index: number) => void155containerProps: {156ref: Ref<HTMLElement | null>157onScroll: () => void158style: StyleValue159}160wrapperProps: ComputedRef<{161style:162| {163width: string164height: string165marginTop: string166}167| {168width: string169height: string170marginLeft: string171display: string172}173}>174}175/**176* Please consider using [`vue-virtual-scroller`](https://github.com/Akryum/vue-virtual-scroller) if you are looking for more features.177*/178export declare function useVirtualList<T = any>(179list: MaybeRef<readonly T[]>,180options: UseVirtualListOptions,181): UseVirtualListReturn<T>182```183